[ 
https://issues.apache.org/jira/browse/CAMEL-12605?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16700672#comment-16700672
 ] 

ASF GitHub Bot commented on CAMEL-12605:
----------------------------------------

oscerd closed pull request #2639: [CAMEL-12605] Added support for compression
URL: https://github.com/apache/camel/pull/2639
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7MimeCompressedDataEntity.java
 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7MimeCompressedDataEntity.java
new file mode 100644
index 00000000000..7a88761f3a3
--- /dev/null
+++ 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7MimeCompressedDataEntity.java
@@ -0,0 +1,120 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.as2.api.entity;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.camel.component.as2.api.AS2Charset;
+import org.apache.camel.component.as2.api.AS2Header;
+import org.apache.camel.component.as2.api.CanonicalOutputStream;
+import org.apache.camel.component.as2.api.util.EntityUtils;
+import org.apache.http.Header;
+import org.apache.http.HeaderIterator;
+import org.apache.http.HttpException;
+import org.apache.http.entity.ContentType;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.Args;
+import org.bouncycastle.cms.CMSCompressedData;
+import org.bouncycastle.cms.CMSCompressedDataGenerator;
+import org.bouncycastle.cms.CMSProcessableByteArray;
+import org.bouncycastle.cms.CMSTypedData;
+import org.bouncycastle.operator.InputExpanderProvider;
+import org.bouncycastle.operator.OutputCompressor;
+
+public class ApplicationPkcs7MimeCompressedDataEntity extends MimeEntity {
+    
+    private static final String CONTENT_DISPOSITION = "attachment; 
filename=\"smime.p7z\"";
+    
+    private byte[] compressedData;
+    
+    public ApplicationPkcs7MimeCompressedDataEntity(MimeEntity entity2Encrypt,
+                                                    CMSCompressedDataGenerator 
dataGenerator,
+                                                    OutputCompressor 
compressor,
+                                                    String 
compressedContentTransferEncoding,
+                                                    boolean isMainBody)
+            throws HttpException {
+        setContentType(ContentType.create("application/pkcs7-mime", new 
BasicNameValuePair("smime-type", "compressed-data"),
+                new BasicNameValuePair("name", "smime.p7z")));
+        setContentTransferEncoding(compressedContentTransferEncoding);
+        addHeader(AS2Header.CONTENT_DISPOSITION, CONTENT_DISPOSITION);
+        setMainBody(isMainBody);
+        try {
+            this.compressedData = createCompressedData(entity2Encrypt, 
dataGenerator, compressor);
+        } catch (Exception e) {
+            throw new HttpException("Failed to create encrypted data");
+        }
+    }
+    
+    public ApplicationPkcs7MimeCompressedDataEntity(byte[] compressedData, 
String compressedContentTransferEncoding, boolean isMainBody) {
+        this.compressedData = Args.notNull(compressedData, "encryptedData");
+        
+        setContentType(ContentType.create("application/pkcs7-mime", new 
BasicNameValuePair("smime-type", "compressed-datat"),
+                new BasicNameValuePair("name", "smime.p7z")));
+        setContentTransferEncoding(compressedContentTransferEncoding);
+        addHeader(AS2Header.CONTENT_DISPOSITION, CONTENT_DISPOSITION);
+        setMainBody(isMainBody);
+    }
+    
+    @Override
+    public void writeTo(OutputStream outstream) throws IOException {
+        NoCloseOutputStream ncos = new NoCloseOutputStream(outstream);
+
+        // Write out mime part headers if this is not the main body of message.
+        if (!isMainBody()) {
+            try (CanonicalOutputStream canonicalOutstream = new 
CanonicalOutputStream(ncos, AS2Charset.US_ASCII)) {
+
+                HeaderIterator it = headerIterator();
+                while (it.hasNext()) {
+                    Header header = it.nextHeader();
+                    canonicalOutstream.writeln(header.toString());
+                }
+                canonicalOutstream.writeln(); // ensure empty line between
+                                              // headers and body; RFC2046 -
+                                              // 5.1.1
+            }
+        }
+        
+        // Write out signed data.
+        String transferEncoding = getContentTransferEncoding() == null ? null 
: getContentTransferEncoding().getValue();
+        try (OutputStream transferEncodedStream = EntityUtils.encode(ncos, 
transferEncoding)) {
+
+            transferEncodedStream.write(compressedData);
+        } catch (Exception e) {
+            throw new IOException("Failed to write to output stream", e);
+        }
+    }
+    
+    public MimeEntity getCompressedEntity(InputExpanderProvider 
expanderProvider) throws HttpException {
+        return EntityParser.parseCompressedEntity(compressedData, 
expanderProvider);
+    }
+    
+    private byte[] createCompressedData(MimeEntity entity2Encrypt, 
CMSCompressedDataGenerator compressedDataGenerator, OutputCompressor 
compressor) throws Exception {
+        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
+            entity2Encrypt.writeTo(bos);
+            bos.flush();
+
+            CMSTypedData contentData = new 
CMSProcessableByteArray(bos.toByteArray());
+            CMSCompressedData  compressedData = 
compressedDataGenerator.generate(contentData, compressor);
+            return compressedData.getEncoded();
+        } catch (Exception e) {
+            throw new Exception("", e);
+        }
+    }
+
+}
diff --git 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7MimeEntity.java
 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7MimeEntity.java
index c1d6952f413..548705503f0 100644
--- 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7MimeEntity.java
+++ 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationPkcs7MimeEntity.java
@@ -100,7 +100,7 @@ public void writeTo(OutputStream outstream) throws 
IOException {
         }
     }
     
-    public MimeEntity getEncryptedEntity(PrivateKey privateKey) {
+    public MimeEntity getEncryptedEntity(PrivateKey privateKey) throws 
HttpException {
         
         return EntityParser.parseEnvelopedEntity(encryptedData, privateKey);
         
diff --git 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/EntityParser.java
 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/EntityParser.java
index aaf12e86c8a..4d8a2b20913 100644
--- 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/EntityParser.java
+++ 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/EntityParser.java
@@ -51,11 +51,14 @@
 import org.apache.http.message.ParserCursor;
 import org.apache.http.util.Args;
 import org.apache.http.util.CharArrayBuffer;
+import org.bouncycastle.cms.CMSCompressedData;
 import org.bouncycastle.cms.CMSEnvelopedData;
+import org.bouncycastle.cms.CMSException;
 import org.bouncycastle.cms.Recipient;
 import org.bouncycastle.cms.RecipientInformation;
 import org.bouncycastle.cms.RecipientInformationStore;
 import org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient;
+import org.bouncycastle.operator.InputExpanderProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -177,12 +180,26 @@ public static void skipToBoundary(AS2SessionInputBuffer 
inbuffer, String boundar
 
     }
     
-    public static MimeEntity parseEnvelopedEntity(byte[] envelopedContent, 
PrivateKey privateKey) {
+    public static MimeEntity parseCompressedEntity(byte[] compressedData, 
InputExpanderProvider expanderProvider)
+            throws HttpException {
+
+        byte[] uncompressedContent = uncompressData(compressedData, 
expanderProvider);
+
+        return parseEntity(uncompressedContent);
+    }
+
+    public static MimeEntity parseEnvelopedEntity(byte[] envelopedContent, 
PrivateKey privateKey) throws HttpException {
+
+        byte[] decryptedContent = decryptData(envelopedContent, privateKey);
+
+        return parseEntity(decryptedContent);
+    }
+
+    public static MimeEntity parseEntity(byte[] content) throws HttpException {
         
         try {
-            byte[] decryptedContent = decryptData(envelopedContent, 
privateKey);
             
-            InputStream is = new ByteArrayInputStream(decryptedContent);
+            InputStream is = new ByteArrayInputStream(content);
             AS2SessionInputBuffer inbuffer = new AS2SessionInputBuffer(new 
HttpTransportMetricsImpl(), DEFAULT_BUFFER_SIZE);
             inbuffer.bind(is);
 
@@ -191,54 +208,68 @@ public static MimeEntity parseEnvelopedEntity(byte[] 
envelopedContent, PrivateKe
                     new ArrayList<CharArrayBuffer>());
 
             // Get Content-Type and Content-Transfer-Encoding
-            ContentType envelopedEntityContentType = null;
-            String envelopedEntityContentTransferEncoding = null;
+            ContentType entityContentType = null;
+            String entityContentTransferEncoding = null;
             for (Header header : headers) {
                 switch (header.getName()) {
                 case AS2Header.CONTENT_TYPE:
-                    envelopedEntityContentType = 
ContentType.parse(header.getValue());
+                    entityContentType = ContentType.parse(header.getValue());
                     break;
                 case AS2Header.CONTENT_TRANSFER_ENCODING:
-                    envelopedEntityContentTransferEncoding = header.getValue();
+                    entityContentTransferEncoding = header.getValue();
                     break;
                 default:
                     continue;
                 }
             }
-            if (envelopedEntityContentType == null) {
+            if (entityContentType == null) {
                 throw new HttpException("Failed to find Content-Type header in 
enveloped entity");
             }
 
-            MimeEntity entity = parseEntityBody(inbuffer, null, 
envelopedEntityContentType, envelopedEntityContentTransferEncoding, headers);
+            MimeEntity entity = parseEntityBody(inbuffer, null, 
entityContentType, entityContentTransferEncoding, headers);
             entity.removeAllHeaders();
             entity.setHeaders(headers);
             
             return entity;
         } catch (Exception e) {
-            return null;
+            throw new HttpException("Failed to parse entity", e);
         }
     }
 
-    public static byte[] decryptData(byte[] encryptedData, PrivateKey 
privateKey) throws Exception {
-        // Create enveloped data from encrypted data
-        CMSEnvelopedData cmsEnvelopedData = new 
CMSEnvelopedData(encryptedData);
-        
-        // Extract recipient information form enveloped data.
-        RecipientInformationStore recipientsInformationStore = 
cmsEnvelopedData.getRecipientInfos();
-        Collection<RecipientInformation> recipients = 
recipientsInformationStore.getRecipients();
-        Iterator<RecipientInformation> it = recipients.iterator();
-        
-        // Decrypt if enveloped data contains recipient information
-        if (it.hasNext()) {
-            // Create recipient from private key.
-            Recipient recipient = new 
JceKeyTransEnvelopedRecipient(privateKey);
-
-            // Extract decrypted data from recipient information
-            RecipientInformation recipientInfo = it.next();
-            return recipientInfo.getContent(recipient);
+    public static byte[] uncompressData(byte[] compressedData, 
InputExpanderProvider expanderProvider)
+            throws HttpException {
+        try {
+            CMSCompressedData cmsCompressedData = new 
CMSCompressedData(compressedData);
+            return cmsCompressedData.getContent(expanderProvider);
+        } catch (CMSException e) {
+            throw new HttpException("Failed to decompress data", e);
+        }
+    }
+
+    public static byte[] decryptData(byte[] encryptedData, PrivateKey 
privateKey) throws HttpException {
+        try {
+            // Create enveloped data from encrypted data
+            CMSEnvelopedData cmsEnvelopedData = new 
CMSEnvelopedData(encryptedData);
+            
+            // Extract recipient information form enveloped data.
+            RecipientInformationStore recipientsInformationStore = 
cmsEnvelopedData.getRecipientInfos();
+            Collection<RecipientInformation> recipients = 
recipientsInformationStore.getRecipients();
+            Iterator<RecipientInformation> it = recipients.iterator();
+            
+            // Decrypt if enveloped data contains recipient information
+            if (it.hasNext()) {
+                // Create recipient from private key.
+                Recipient recipient = new 
JceKeyTransEnvelopedRecipient(privateKey);
+
+                // Extract decrypted data from recipient information
+                RecipientInformation recipientInfo = it.next();
+                return recipientInfo.getContent(recipient);
+            }
+        } catch (CMSException e) {
+            throw new HttpException("Failed to decrypt data", e);
         }
         
-        return null;
+        throw new HttpException("Failed to decrypt data: bno recipeint 
information");
     }
     
     public static void parseMultipartSignedEntity(HttpMessage message)
diff --git 
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/CompressedEntityTest.java
 
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/CompressedEntityTest.java
new file mode 100644
index 00000000000..dc549e9bc8d
--- /dev/null
+++ 
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/CompressedEntityTest.java
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.as2.api.entity;
+
+import org.bouncycastle.cms.CMSCompressedDataGenerator;
+import org.bouncycastle.cms.jcajce.ZlibCompressor;
+import org.bouncycastle.cms.jcajce.ZlibExpanderProvider;
+import org.bouncycastle.operator.OutputCompressor;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class CompressedEntityTest {
+    
+    public static final String TEXT_PLAIN_CONTENT =
+            "MDN for -\r\n"
+            + " Message ID: <200207310834482A70BF63@\\\"~~foo~~\\\">\r\n"
+            + "  From: \"\\\"  as2Name  \\\"\"\r\n"
+            + "  To: \"0123456780000\""
+            + "  Received on: 2002-07-31 at 09:34:14 (EDT)\r\n"
+            + " Status: processed\r\n"
+            + " Comment: This is not a guarantee that the message has\r\n"
+            + "  been completely processed or &understood by the receiving\r\n"
+            + "  translator\r\n"
+            + "\r\n";
+
+    public static final String TEXT_PLAIN_CONTENT_CHARSET_NAME = "US-ASCII";
+
+    public static final String TEXT_PLAIN_CONTENT_TRANSFER_ENCODING = "7bit";
+
+    public static final String EXPECTED_TEXT_PLAIN_CONTENT =
+            "MDN for -\r\n"
+            + " Message ID: <200207310834482A70BF63@\\\"~~foo~~\\\">\r\n"
+            + "  From: \"\\\"  as2Name  \\\"\"\r\n"
+            + "  To: \"0123456780000\""
+            + "  Received on: 2002-07-31 at 09:34:14 (EDT)\r\n"
+            + " Status: processed\r\n"
+            + " Comment: This is not a guarantee that the message has\r\n"
+            + "  been completely processed or &understood by the receiving\r\n"
+            + "  translator\r\n"
+            + "\r\n";
+
+    
+    public static final String 
APPLICATION_PKCS7_MIME_COMPRESSED_TRANSFER_ENCODING = "base64";
+
+    @Before
+    public void setUp() throws Exception {
+    }
+
+    @After
+    public void tearDown() throws Exception {
+    }
+    
+    @Test
+    public void createCompressedEntityTest() throws Exception {
+        TextPlainEntity textPlainEntity = new 
TextPlainEntity(TEXT_PLAIN_CONTENT, TEXT_PLAIN_CONTENT_CHARSET_NAME,
+                TEXT_PLAIN_CONTENT_TRANSFER_ENCODING, false);
+
+        CMSCompressedDataGenerator cGen = new CMSCompressedDataGenerator();
+
+        OutputCompressor compressor = new ZlibCompressor();
+
+        ApplicationPkcs7MimeCompressedDataEntity compressedEntity = new 
ApplicationPkcs7MimeCompressedDataEntity(
+                textPlainEntity, cGen, compressor, 
APPLICATION_PKCS7_MIME_COMPRESSED_TRANSFER_ENCODING, false);
+
+        MimeEntity decompressedEntity = 
compressedEntity.getCompressedEntity(new ZlibExpanderProvider());
+        assertTrue("", decompressedEntity instanceof TextPlainEntity);
+        TextPlainEntity decompressedTextPlainEntity = (TextPlainEntity) 
decompressedEntity;
+        assertEquals("", EXPECTED_TEXT_PLAIN_CONTENT, 
decompressedTextPlainEntity.getText());
+    }
+
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Enhance the AS2 Component to send and receive encrypted AS2 messages
> --------------------------------------------------------------------
>
>                 Key: CAMEL-12605
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12605
>             Project: Camel
>          Issue Type: Improvement
>    Affects Versions: 2.23.0
>            Reporter: William Collins
>            Assignee: William Collins
>            Priority: Major
>
> Enhance the AS2 Component to support encrypted AS2 messages per RFC4130



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to