-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fj-doc-val-core] DocValidatorTypeCheck facade to check file type #260
[fj-doc-val-p7m] check the inner type on P7MContentValidator
- Loading branch information
Showing
6 changed files
with
201 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
fj-doc-val-core/src/main/java/org/fugerit/java/doc/val/core/DocValidatorTypeCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.fugerit.java.doc.val.core; | ||
|
||
import lombok.Getter; | ||
import org.fugerit.java.core.function.SafeFunction; | ||
import org.fugerit.java.core.io.StreamIO; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* This facade tries to retrieve a document's type. | ||
* | ||
*/ | ||
public class DocValidatorTypeCheck { | ||
|
||
@Getter | ||
private DocValidatorFacade facade; | ||
|
||
private DocValidatorTypeCheck( DocValidatorFacade facade ) { | ||
this.facade = facade; | ||
} | ||
|
||
/** | ||
* Type check of a byte stream against the configured DocTypeValidator facade. | ||
* | ||
* It will return <code>null</code> if the stream is not valid for all the validators, otherwise DocTypeValidator.getMimeType() of the first validator matching the type. | ||
* | ||
* NOTE: This method always buffer the input, so it can be memory consuming. | ||
* | ||
* @param is the byte stream to check | ||
* @return <code>null</code> if the stream is not valid for all the validators, otherwise DocTypeValidator.getMimeType() of the first validator matching the type | ||
*/ | ||
public String checkType(InputStream is) { | ||
return SafeFunction.get( () -> this.checkType( StreamIO.readBytes( is ) ) ); | ||
} | ||
|
||
/** | ||
* Type check for an input document against the configured DocTypeValidator facade. | ||
* | ||
* It will return <code>null</code> if the stream is not valid for all the validators, otherwise DocTypeValidator.getMimeType() of the first validator matching the type. | ||
* | ||
* NOTE: This method always buffer the input, so it can be memory consuming. | ||
* | ||
* @param buffer the document to check | ||
* @return <code>null</code> if the input is not valid for all the validators, otherwise DocTypeValidator.getMimeType() of the first validator matching the type | ||
*/ | ||
public String checkType(byte[] buffer) { | ||
return SafeFunction.get( () -> { | ||
for ( DocTypeValidator validator : this.facade.validators() ) { | ||
try (ByteArrayInputStream input = new ByteArrayInputStream( buffer )) { | ||
if ( validator.check( input ) ) { | ||
return validator.getMimeType(); | ||
} | ||
} | ||
} | ||
// default case, type not found | ||
return null; | ||
} ); | ||
} | ||
|
||
public static DocValidatorTypeCheck newInstance( DocTypeValidator... validator ) { | ||
return newInstance( DocValidatorFacade.newFacadeStrict( validator ) ); | ||
} | ||
|
||
public static DocValidatorTypeCheck newInstance( DocValidatorFacade facade ) { | ||
return new DocValidatorTypeCheck( facade ); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...-val-core/src/test/java/test/org/fugerit/java/doc/core/val/TestDocValidatorTypeCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package test.org.fugerit.java.doc.core.val; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.fugerit.java.core.function.SafeFunction; | ||
import org.fugerit.java.core.lang.helpers.ClassHelper; | ||
import org.fugerit.java.doc.val.core.DocValidatorTypeCheck; | ||
import org.fugerit.java.doc.val.core.basic.ImageValidator; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.awt.*; | ||
import java.io.InputStream; | ||
|
||
@Slf4j | ||
public class TestDocValidatorTypeCheck { | ||
|
||
private static final DocValidatorTypeCheck TYPE_CHECK = DocValidatorTypeCheck.newInstance( | ||
ImageValidator.JPG_VALIDATOR, ImageValidator.PNG_VALIDATOR ); | ||
|
||
private static final String BASE_PATH = "sample"; | ||
|
||
private String worker( String fileName ) { | ||
return SafeFunction.get( () -> { | ||
String path = BASE_PATH+"/"+fileName; | ||
log.info( "test path {}", path ); | ||
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( path ) ) { | ||
return TYPE_CHECK.checkType( is ); | ||
} | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testJpg() { | ||
Assert.assertEquals(ImageValidator.JPG_VALIDATOR.getMimeType(), this.worker( "jpg_as_jpg.jpg" ) ); | ||
} | ||
|
||
@Test | ||
public void testPng() { | ||
Assert.assertEquals(ImageValidator.PNG_VALIDATOR.getMimeType(), this.worker( "png_as_png.png" ) ); | ||
} | ||
|
||
@Test | ||
public void testNull() { | ||
Assert.assertNull( this.worker( "pdf_as_jpg.jpg" ) ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters