-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 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
28 changes: 28 additions & 0 deletions
28
fj-doc-val-p7m/src/main/java/org/fugerit/java/doc/val/p7m/P7MUtils.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,28 @@ | ||
package org.fugerit.java.doc.val.p7m; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
import org.bouncycastle.cms.CMSException; | ||
import org.bouncycastle.cms.CMSProcessable; | ||
import org.bouncycastle.cms.CMSSignedData; | ||
import org.fugerit.java.core.io.StreamIO; | ||
|
||
public class P7MUtils { | ||
|
||
private P7MUtils() {} | ||
|
||
public static void extractContent( InputStream p7mContent, OutputStream contentStream ) throws CMSException, IOException { | ||
CMSSignedData csd = new CMSSignedData( p7mContent ); | ||
CMSProcessable cmsContent = csd.getSignedContent(); | ||
if ( cmsContent != null ) { | ||
byte[] content = (byte[])cmsContent.getContent(); | ||
try ( ByteArrayInputStream is = new ByteArrayInputStream( content ) ) { | ||
StreamIO.pipeStream(is, contentStream, StreamIO.MODE_CLOSE_BOTH); | ||
} | ||
} | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
fj-doc-val-p7m/src/test/java/test/org/fugerit/java/doc/val/p7m/TestExtractContentP7M.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,50 @@ | ||
package test.org.fugerit.java.doc.val.p7m; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
|
||
import org.bouncycastle.cms.CMSException; | ||
import org.fugerit.java.core.function.SafeFunction; | ||
import org.fugerit.java.core.io.FileIO; | ||
import org.fugerit.java.doc.val.p7m.P7MUtils; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class TestExtractContentP7M { | ||
|
||
@Test | ||
public void testP7MKo() { | ||
Assert.assertThrows( CMSException.class , () -> { | ||
String path = "src/test/resources/sample/png_as_p7m.p7m"; | ||
File testP7M = new File( path ); | ||
log.info( "test extract ko : {}", testP7M.getCanonicalPath() ); | ||
try ( FileInputStream is = new FileInputStream( testP7M ); | ||
ByteArrayOutputStream os = new ByteArrayOutputStream() ) { | ||
P7MUtils.extractContent(is, os); | ||
} | ||
}) ; | ||
} | ||
|
||
@Test | ||
public void testP7MOk() { | ||
SafeFunction.apply( () -> { | ||
String path = "src/test/resources/sample/pdf_as_pdf.p7m"; | ||
File testP7M = new File( path ); | ||
log.info( "test extract : {}", testP7M.getCanonicalPath() ); | ||
File outputBase = new File( "target" ); | ||
File outputContent = new File( outputBase, "content.pdf" ); | ||
outputContent.delete(); | ||
try ( FileInputStream is = new FileInputStream( testP7M ); | ||
ByteArrayOutputStream os = new ByteArrayOutputStream() ) { | ||
P7MUtils.extractContent(is, os); | ||
FileIO.writeBytes( os.toByteArray() , outputContent ); | ||
} | ||
Assert.assertTrue( outputContent.exists() ); | ||
} ); | ||
} | ||
|
||
} |