Skip to content

Commit 1e26f21

Browse files
committed
Extract content in p7m
1 parent 6d072ec commit 1e26f21

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- [val-p7m] utility to extract p7m content
1213
- [val-core] XmlValidator for simple xml doc type validation
1314
- [val] P7MValidator in full validator facade
1415
- [playground-quarkus] P7MValidator in validator feature
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.fugerit.java.doc.val.p7m;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.OutputStream;
7+
8+
import org.bouncycastle.cms.CMSException;
9+
import org.bouncycastle.cms.CMSProcessable;
10+
import org.bouncycastle.cms.CMSSignedData;
11+
import org.fugerit.java.core.io.StreamIO;
12+
13+
public class P7MUtils {
14+
15+
private P7MUtils() {}
16+
17+
public static void extractContent( InputStream p7mContent, OutputStream contentStream ) throws CMSException, IOException {
18+
CMSSignedData csd = new CMSSignedData( p7mContent );
19+
CMSProcessable cmsContent = csd.getSignedContent();
20+
if ( cmsContent != null ) {
21+
byte[] content = (byte[])cmsContent.getContent();
22+
try ( ByteArrayInputStream is = new ByteArrayInputStream( content ) ) {
23+
StreamIO.pipeStream(is, contentStream, StreamIO.MODE_CLOSE_BOTH);
24+
}
25+
}
26+
}
27+
28+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package test.org.fugerit.java.doc.val.p7m;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
7+
import org.bouncycastle.cms.CMSException;
8+
import org.fugerit.java.core.function.SafeFunction;
9+
import org.fugerit.java.core.io.FileIO;
10+
import org.fugerit.java.doc.val.p7m.P7MUtils;
11+
import org.junit.Assert;
12+
import org.junit.Test;
13+
14+
import lombok.extern.slf4j.Slf4j;
15+
16+
@Slf4j
17+
public class TestExtractContentP7M {
18+
19+
@Test
20+
public void testP7MKo() {
21+
Assert.assertThrows( CMSException.class , () -> {
22+
String path = "src/test/resources/sample/png_as_p7m.p7m";
23+
File testP7M = new File( path );
24+
log.info( "test extract ko : {}", testP7M.getCanonicalPath() );
25+
try ( FileInputStream is = new FileInputStream( testP7M );
26+
ByteArrayOutputStream os = new ByteArrayOutputStream() ) {
27+
P7MUtils.extractContent(is, os);
28+
}
29+
}) ;
30+
}
31+
32+
@Test
33+
public void testP7MOk() {
34+
SafeFunction.apply( () -> {
35+
String path = "src/test/resources/sample/pdf_as_pdf.p7m";
36+
File testP7M = new File( path );
37+
log.info( "test extract : {}", testP7M.getCanonicalPath() );
38+
File outputBase = new File( "target" );
39+
File outputContent = new File( outputBase, "content.pdf" );
40+
outputContent.delete();
41+
try ( FileInputStream is = new FileInputStream( testP7M );
42+
ByteArrayOutputStream os = new ByteArrayOutputStream() ) {
43+
P7MUtils.extractContent(is, os);
44+
FileIO.writeBytes( os.toByteArray() , outputContent );
45+
}
46+
Assert.assertTrue( outputContent.exists() );
47+
} );
48+
}
49+
50+
}

0 commit comments

Comments
 (0)