This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 7139b515f21 (chores) camel-as2: break large and complex methods
7139b515f21 is described below

commit 7139b515f214c5c3ee888510379ebb6ea63e003b
Author: Otavio Rodolfo Piske <angusyo...@gmail.com>
AuthorDate: Thu Apr 25 13:41:08 2024 +0200

    (chores) camel-as2: break large and complex methods
---
 .../camel/component/as2/api/AS2ClientManager.java  | 253 +++++++++++++--------
 .../component/as2/api/entity/EntityParser.java     | 102 +++++----
 2 files changed, 209 insertions(+), 146 deletions(-)

diff --git 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2ClientManager.java
 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2ClientManager.java
index e407c301284..099d40ac2fa 100644
--- 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2ClientManager.java
+++ 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2ClientManager.java
@@ -262,132 +262,47 @@ public class AS2ClientManager {
         }
         switch (as2MessageStructure) {
             case PLAIN: {
-                // Add EDI Entity to main body of request.
-                applicationEntity.setMainBody(true);
-                EntityUtils.setMessageEntity(request, applicationEntity);
+                plain(applicationEntity, request);
                 break;
             }
             case SIGNED: {
-                // Create Multipart Signed Entity containing EDI Entity
-                AS2SignedDataGenerator signingGenerator = 
createSigningGenerator(httpContext);
-                MultipartSignedEntity multipartSignedEntity = new 
MultipartSignedEntity(
-                        applicationEntity, signingGenerator,
-                        StandardCharsets.US_ASCII.name(), 
AS2TransferEncoding.BASE64, true, null);
-
-                // Add Multipart Signed Entity to main body of request.
-                EntityUtils.setMessageEntity(request, multipartSignedEntity);
+                signed(httpContext, applicationEntity, request);
                 break;
             }
             case ENCRYPTED: {
-                // Create Enveloped Entity containing EDI Entity
-                CMSEnvelopedDataGenerator envelopedDataGenerator = 
createEncryptingGenerator(httpContext);
-                OutputEncryptor encryptor = createEncryptor(httpContext);
-                ApplicationPkcs7MimeEnvelopedDataEntity 
pkcs7MimeEnvelopedDataEntity
-                        = new ApplicationPkcs7MimeEnvelopedDataEntity(
-                                applicationEntity, envelopedDataGenerator, 
encryptor, AS2TransferEncoding.BASE64, true);
-
-                // Add Multipart Enveloped Entity to main body of request.
-                EntityUtils.setMessageEntity(request, 
pkcs7MimeEnvelopedDataEntity);
+                encrypted(httpContext, applicationEntity, request);
                 break;
             }
             case SIGNED_ENCRYPTED: {
-                // Create Multipart Signed Entity containing EDI Entity
-                AS2SignedDataGenerator signingGenrator = 
createSigningGenerator(httpContext);
-                MultipartSignedEntity multipartSignedEntity = new 
MultipartSignedEntity(
-                        applicationEntity,
-                        signingGenrator, StandardCharsets.US_ASCII.name(), 
AS2TransferEncoding.BASE64, false, null);
-
-                // Create Enveloped Entity containing Multipart Signed Entity
-                CMSEnvelopedDataGenerator envelopedDataGenerator = 
createEncryptingGenerator(httpContext);
-                OutputEncryptor encryptor = createEncryptor(httpContext);
-                ApplicationPkcs7MimeEnvelopedDataEntity 
pkcs7MimeEnvelopedDataEntity
-                        = new ApplicationPkcs7MimeEnvelopedDataEntity(
-                                multipartSignedEntity, envelopedDataGenerator, 
encryptor, AS2TransferEncoding.BASE64, true);
-
-                // Add Multipart Enveloped Entity to main body of request.
-                EntityUtils.setMessageEntity(request, 
pkcs7MimeEnvelopedDataEntity);
+                signedEncrypted(httpContext, applicationEntity, request);
                 break;
             }
             case PLAIN_COMPRESSED: {
-                // Create Compressed Entity containing EDI Entity
-                CMSCompressedDataGenerator compressedDataGenerator = 
createCompressorGenerator();
-                OutputCompressor compressor = createCompressor(httpContext);
-                ApplicationPkcs7MimeCompressedDataEntity 
pkcs7MimeCompressedDataEntity
-                        = new ApplicationPkcs7MimeCompressedDataEntity(
-                                applicationEntity, compressedDataGenerator, 
compressor, AS2TransferEncoding.BASE64, true);
-
-                // Add Compressed Entity to main body of request.
-                EntityUtils.setMessageEntity(request, 
pkcs7MimeCompressedDataEntity);
+                plainCompressed(httpContext, applicationEntity, request);
                 break;
             }
             case SIGNED_COMPRESSED: {
-                // Create Multipart Signed Entity containing EDI Entity
-                AS2SignedDataGenerator signingGenrator = 
createSigningGenerator(httpContext);
-                MultipartSignedEntity multipartSignedEntity = new 
MultipartSignedEntity(
-                        applicationEntity,
-                        signingGenrator, StandardCharsets.US_ASCII.name(), 
AS2TransferEncoding.BASE64, false, null);
-
-                // Create Compressed Entity containing Multipart Signed Entity
-                CMSCompressedDataGenerator compressedDataGenerator = 
createCompressorGenerator();
-                OutputCompressor compressor = createCompressor(httpContext);
-                ApplicationPkcs7MimeCompressedDataEntity 
pkcs7MimeCompressedDataEntity
-                        = new ApplicationPkcs7MimeCompressedDataEntity(
-                                multipartSignedEntity, 
compressedDataGenerator, compressor, AS2TransferEncoding.BASE64, true);
-
-                // Add Compressed Entity to main body of request.
-                EntityUtils.setMessageEntity(request, 
pkcs7MimeCompressedDataEntity);
+                signedCompressed(httpContext, applicationEntity, request);
                 break;
             }
             case ENCRYPTED_COMPRESSED: {
-                // Create Compressed Entity containing EDI Entity
-                CMSCompressedDataGenerator compressedDataGenerator = 
createCompressorGenerator();
-                OutputCompressor compressor = createCompressor(httpContext);
-                ApplicationPkcs7MimeCompressedDataEntity 
pkcs7MimeCompressedDataEntity
-                        = new ApplicationPkcs7MimeCompressedDataEntity(
-                                applicationEntity, compressedDataGenerator, 
compressor, AS2TransferEncoding.BASE64, false);
-
-                // Create Enveloped Entity containing Compressed Entity
-                CMSEnvelopedDataGenerator envelopedDataGenerator = 
createEncryptingGenerator(httpContext);
-                OutputEncryptor encryptor = createEncryptor(httpContext);
-                ApplicationPkcs7MimeEnvelopedDataEntity 
pkcs7MimeEnvelopedDataEntity
-                        = new ApplicationPkcs7MimeEnvelopedDataEntity(
-                                pkcs7MimeCompressedDataEntity, 
envelopedDataGenerator, encryptor, AS2TransferEncoding.BASE64,
-                                true);
-
-                // Add Enveloped Entity to main body of request
-                EntityUtils.setMessageEntity(request, 
pkcs7MimeEnvelopedDataEntity);
+                encryptedCompressed(httpContext, applicationEntity, request);
                 break;
             }
             case ENCRYPTED_COMPRESSED_SIGNED: {
-                // Create Multipart Signed Entity containing EDI Entity
-                AS2SignedDataGenerator signingGenrator = 
createSigningGenerator(httpContext);
-                MultipartSignedEntity multipartSignedEntity = new 
MultipartSignedEntity(
-                        applicationEntity, signingGenrator,
-                        StandardCharsets.US_ASCII.name(), 
AS2TransferEncoding.BASE64, false, null);
-
-                // Create Compressed Entity containing Multipart Signed Entity
-                CMSCompressedDataGenerator compressedDataGenerator = 
createCompressorGenerator();
-                OutputCompressor compressor = createCompressor(httpContext);
-                ApplicationPkcs7MimeCompressedDataEntity 
pkcs7MimeCompressedDataEntity
-                        = new ApplicationPkcs7MimeCompressedDataEntity(
-                                multipartSignedEntity, 
compressedDataGenerator, compressor, AS2TransferEncoding.BASE64, false);
-
-                // Create Enveloped Entity containing Compressed Entity
-                CMSEnvelopedDataGenerator envelopedDataGenerator = 
createEncryptingGenerator(httpContext);
-                OutputEncryptor encryptor = createEncryptor(httpContext);
-                ApplicationPkcs7MimeEnvelopedDataEntity 
pkcs7MimeEnvelopedDataEntity
-                        = new ApplicationPkcs7MimeEnvelopedDataEntity(
-                                pkcs7MimeCompressedDataEntity, 
envelopedDataGenerator, encryptor, AS2TransferEncoding.BASE64,
-                                true);
-
-                // Add Enveloped Entity to main body of request
-                EntityUtils.setMessageEntity(request, 
pkcs7MimeEnvelopedDataEntity);
+                encryptedCompressedSigned(httpContext, applicationEntity, 
request);
                 break;
             }
             default:
                 throw new HttpException("Unknown AS2 Message Structure");
         }
 
+        final HttpResponse response = sendRequest(httpContext, request);
+        httpContext.setAttribute(HTTP_RESPONSE, response);
+        return httpContext;
+    }
+
+    private HttpResponse sendRequest(HttpCoreContext httpContext, 
BasicClassicHttpRequest request) throws HttpException {
         HttpResponse response;
         try {
             httpContext.setAttribute(AS2_CONNECTION, as2ClientConnection);
@@ -403,8 +318,144 @@ public class AS2ClientManager {
         } catch (ExecutionException ex) {
             throw new HttpException("Retrieving connection from Pool failed or 
timed out", ex);
         }
-        httpContext.setAttribute(HTTP_RESPONSE, response);
-        return httpContext;
+        return response;
+    }
+
+    private void encryptedCompressedSigned(
+            HttpCoreContext httpContext, ApplicationEntity applicationEntity, 
BasicClassicHttpRequest request)
+            throws HttpException {
+        // Create Multipart Signed Entity containing EDI Entity
+        AS2SignedDataGenerator signingGenrator = 
createSigningGenerator(httpContext);
+        MultipartSignedEntity multipartSignedEntity = new 
MultipartSignedEntity(
+                applicationEntity, signingGenrator,
+                StandardCharsets.US_ASCII.name(), AS2TransferEncoding.BASE64, 
false, null);
+
+        // Create Compressed Entity containing Multipart Signed Entity
+        CMSCompressedDataGenerator compressedDataGenerator = 
createCompressorGenerator();
+        OutputCompressor compressor = createCompressor(httpContext);
+        ApplicationPkcs7MimeCompressedDataEntity pkcs7MimeCompressedDataEntity
+                = new ApplicationPkcs7MimeCompressedDataEntity(
+                        multipartSignedEntity, compressedDataGenerator, 
compressor, AS2TransferEncoding.BASE64, false);
+
+        // Create Enveloped Entity containing Compressed Entity
+        CMSEnvelopedDataGenerator envelopedDataGenerator = 
createEncryptingGenerator(httpContext);
+        OutputEncryptor encryptor = createEncryptor(httpContext);
+        ApplicationPkcs7MimeEnvelopedDataEntity pkcs7MimeEnvelopedDataEntity
+                = new ApplicationPkcs7MimeEnvelopedDataEntity(
+                        pkcs7MimeCompressedDataEntity, envelopedDataGenerator, 
encryptor, AS2TransferEncoding.BASE64,
+                        true);
+
+        // Add Enveloped Entity to main body of request
+        EntityUtils.setMessageEntity(request, pkcs7MimeEnvelopedDataEntity);
+    }
+
+    private void encryptedCompressed(
+            HttpCoreContext httpContext, ApplicationEntity applicationEntity, 
BasicClassicHttpRequest request)
+            throws HttpException {
+        // Create Compressed Entity containing EDI Entity
+        CMSCompressedDataGenerator compressedDataGenerator = 
createCompressorGenerator();
+        OutputCompressor compressor = createCompressor(httpContext);
+        ApplicationPkcs7MimeCompressedDataEntity pkcs7MimeCompressedDataEntity
+                = new ApplicationPkcs7MimeCompressedDataEntity(
+                        applicationEntity, compressedDataGenerator, 
compressor, AS2TransferEncoding.BASE64, false);
+
+        // Create Enveloped Entity containing Compressed Entity
+        CMSEnvelopedDataGenerator envelopedDataGenerator = 
createEncryptingGenerator(httpContext);
+        OutputEncryptor encryptor = createEncryptor(httpContext);
+        ApplicationPkcs7MimeEnvelopedDataEntity pkcs7MimeEnvelopedDataEntity
+                = new ApplicationPkcs7MimeEnvelopedDataEntity(
+                        pkcs7MimeCompressedDataEntity, envelopedDataGenerator, 
encryptor, AS2TransferEncoding.BASE64,
+                        true);
+
+        // Add Enveloped Entity to main body of request
+        EntityUtils.setMessageEntity(request, pkcs7MimeEnvelopedDataEntity);
+    }
+
+    private void signedCompressed(
+            HttpCoreContext httpContext, ApplicationEntity applicationEntity, 
BasicClassicHttpRequest request)
+            throws HttpException {
+        // Create Multipart Signed Entity containing EDI Entity
+        AS2SignedDataGenerator signingGenrator = 
createSigningGenerator(httpContext);
+        MultipartSignedEntity multipartSignedEntity = new 
MultipartSignedEntity(
+                applicationEntity,
+                signingGenrator, StandardCharsets.US_ASCII.name(), 
AS2TransferEncoding.BASE64, false, null);
+
+        // Create Compressed Entity containing Multipart Signed Entity
+        CMSCompressedDataGenerator compressedDataGenerator = 
createCompressorGenerator();
+        OutputCompressor compressor = createCompressor(httpContext);
+        ApplicationPkcs7MimeCompressedDataEntity pkcs7MimeCompressedDataEntity
+                = new ApplicationPkcs7MimeCompressedDataEntity(
+                        multipartSignedEntity, compressedDataGenerator, 
compressor, AS2TransferEncoding.BASE64, true);
+
+        // Add Compressed Entity to main body of request.
+        EntityUtils.setMessageEntity(request, pkcs7MimeCompressedDataEntity);
+    }
+
+    private void plainCompressed(
+            HttpCoreContext httpContext, ApplicationEntity applicationEntity, 
BasicClassicHttpRequest request)
+            throws HttpException {
+        // Create Compressed Entity containing EDI Entity
+        CMSCompressedDataGenerator compressedDataGenerator = 
createCompressorGenerator();
+        OutputCompressor compressor = createCompressor(httpContext);
+        ApplicationPkcs7MimeCompressedDataEntity pkcs7MimeCompressedDataEntity
+                = new ApplicationPkcs7MimeCompressedDataEntity(
+                        applicationEntity, compressedDataGenerator, 
compressor, AS2TransferEncoding.BASE64, true);
+
+        // Add Compressed Entity to main body of request.
+        EntityUtils.setMessageEntity(request, pkcs7MimeCompressedDataEntity);
+    }
+
+    // Create Multipart Signed Entity containing EDI Entity
+    private void signedEncrypted(
+            HttpCoreContext httpContext, ApplicationEntity applicationEntity, 
BasicClassicHttpRequest request)
+            throws HttpException {
+        AS2SignedDataGenerator signingGenrator = 
createSigningGenerator(httpContext);
+        MultipartSignedEntity multipartSignedEntity = new 
MultipartSignedEntity(
+                applicationEntity,
+                signingGenrator, StandardCharsets.US_ASCII.name(), 
AS2TransferEncoding.BASE64, false, null);
+
+        // Create Enveloped Entity containing Multipart Signed Entity
+        CMSEnvelopedDataGenerator envelopedDataGenerator = 
createEncryptingGenerator(httpContext);
+        OutputEncryptor encryptor = createEncryptor(httpContext);
+        ApplicationPkcs7MimeEnvelopedDataEntity pkcs7MimeEnvelopedDataEntity
+                = new ApplicationPkcs7MimeEnvelopedDataEntity(
+                        multipartSignedEntity, envelopedDataGenerator, 
encryptor, AS2TransferEncoding.BASE64, true);
+
+        // Add Multipart Enveloped Entity to main body of request.
+        EntityUtils.setMessageEntity(request, pkcs7MimeEnvelopedDataEntity);
+    }
+
+    /**
+     * Create Enveloped Entity containing EDI Entity
+     */
+    private void encrypted(HttpCoreContext httpContext, ApplicationEntity 
applicationEntity, BasicClassicHttpRequest request)
+            throws HttpException {
+        CMSEnvelopedDataGenerator envelopedDataGenerator = 
createEncryptingGenerator(httpContext);
+        OutputEncryptor encryptor = createEncryptor(httpContext);
+        ApplicationPkcs7MimeEnvelopedDataEntity pkcs7MimeEnvelopedDataEntity
+                = new ApplicationPkcs7MimeEnvelopedDataEntity(
+                        applicationEntity, envelopedDataGenerator, encryptor, 
AS2TransferEncoding.BASE64, true);
+
+        // Add Multipart Enveloped Entity to main body of request.
+        EntityUtils.setMessageEntity(request, pkcs7MimeEnvelopedDataEntity);
+    }
+
+    // Create Multipart Signed Entity containing EDI Entity
+    private void signed(HttpCoreContext httpContext, ApplicationEntity 
applicationEntity, BasicClassicHttpRequest request)
+            throws HttpException {
+        AS2SignedDataGenerator signingGenerator = 
createSigningGenerator(httpContext);
+        MultipartSignedEntity multipartSignedEntity = new 
MultipartSignedEntity(
+                applicationEntity, signingGenerator,
+                StandardCharsets.US_ASCII.name(), AS2TransferEncoding.BASE64, 
true, null);
+
+        // Add Multipart Signed Entity to main body of request.
+        EntityUtils.setMessageEntity(request, multipartSignedEntity);
+    }
+
+    // Add EDI Entity to main body of request.
+    private static void plain(ApplicationEntity applicationEntity, 
BasicClassicHttpRequest request) {
+        applicationEntity.setMainBody(true);
+        EntityUtils.setMessageEntity(request, applicationEntity);
     }
 
     public AS2SignedDataGenerator createSigningGenerator(HttpCoreContext 
httpContext) throws HttpException {
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 e9c882e8bcf..8c5728144a7 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
@@ -455,63 +455,75 @@ public final class EntityParser {
                     // contentTypeStr can be null when 
dispositionNotificationTo isn't set
                     return;
                 }
-                ContentType contentType = ContentType.parse(contentTypeStr);
+                doParseAS2MessageEntity(message, contentTypeStr, entity);
+            } catch (HttpException e) {
+                throw e;
+            } catch (Exception e) {
+                throw new HttpException("Failed to parse entity content", e);
+            }
+        }
 
-                // Determine Charset
-                String charsetName = StandardCharsets.US_ASCII.name();
-                Charset charset = contentType.getCharset();
-                if (charset != null) {
-                    charsetName = charset.name();
-                }
+    }
 
-                // Get any Boundary Value
-                String boundary = HttpMessageUtils.getParameterValue(message, 
AS2Header.CONTENT_TYPE, "boundary");
+    private static void doParseAS2MessageEntity(HttpMessage message, String 
contentTypeStr, HttpEntity entity)
+            throws HttpException, IOException {
+        ContentType contentType = ContentType.parse(contentTypeStr);
 
-                // Determine content transfer encoding
-                String contentTransferEncoding
-                        = HttpMessageUtils.getHeaderValue(message, 
AS2Header.CONTENT_TRANSFER_ENCODING);
+        // Determine Charset
+        String charsetName = StandardCharsets.US_ASCII.name();
+        Charset charset = contentType.getCharset();
+        if (charset != null) {
+            charsetName = charset.name();
+        }
 
-                AS2SessionInputBuffer inBuffer = new AS2SessionInputBuffer(
-                        new BasicHttpTransportMetrics(), 8 * 1024);
+        // Get any Boundary Value
+        String boundary = HttpMessageUtils.getParameterValue(message, 
AS2Header.CONTENT_TYPE, "boundary");
 
-                switch (contentType.getMimeType().toLowerCase()) {
-                    case AS2MimeType.APPLICATION_EDIFACT:
-                    case AS2MimeType.APPLICATION_EDI_X12:
-                    case AS2MimeType.APPLICATION_EDI_CONSENT:
-                        parseApplicationEDIEntity(message, 
entity.getContent(), inBuffer, contentType, contentTransferEncoding);
-                        break;
-                    case AS2MimeType.MULTIPART_SIGNED:
-                        parseMultipartSignedEntity(message, 
entity.getContent(), inBuffer, boundary, charsetName,
+        // Determine content transfer encoding
+        String contentTransferEncoding
+                = HttpMessageUtils.getHeaderValue(message, 
AS2Header.CONTENT_TRANSFER_ENCODING);
+
+        AS2SessionInputBuffer inBuffer = new AS2SessionInputBuffer(
+                new BasicHttpTransportMetrics(), 8 * 1024);
+
+        parseByMimeType(message, contentType, entity, inBuffer, 
contentTransferEncoding, boundary, charsetName);
+    }
+
+    private static void parseByMimeType(
+            HttpMessage message, ContentType contentType, HttpEntity entity, 
AS2SessionInputBuffer inBuffer,
+            String contentTransferEncoding, String boundary, String 
charsetName)
+            throws HttpException, IOException {
+        switch (contentType.getMimeType().toLowerCase()) {
+            case AS2MimeType.APPLICATION_EDIFACT:
+            case AS2MimeType.APPLICATION_EDI_X12:
+            case AS2MimeType.APPLICATION_EDI_CONSENT:
+                parseApplicationEDIEntity(message, entity.getContent(), 
inBuffer, contentType, contentTransferEncoding);
+                break;
+            case AS2MimeType.MULTIPART_SIGNED:
+                parseMultipartSignedEntity(message, entity.getContent(), 
inBuffer, boundary, charsetName,
+                        contentTransferEncoding);
+                break;
+            case AS2MimeType.APPLICATION_PKCS7_MIME:
+                switch (contentType.getParameter("smime-type")) {
+                    case "compressed-data":
+                        parseApplicationPkcs7MimeCompressedEntity(message, 
entity.getContent(), inBuffer, contentType,
                                 contentTransferEncoding);
                         break;
-                    case AS2MimeType.APPLICATION_PKCS7_MIME:
-                        switch (contentType.getParameter("smime-type")) {
-                            case "compressed-data":
-                                
parseApplicationPkcs7MimeCompressedEntity(message, entity.getContent(), 
inBuffer, contentType,
-                                        contentTransferEncoding);
-                                break;
-                            case "enveloped-data":
-                                
parseApplicationPkcs7MimeEnvelopedEntity(message, entity.getContent(), 
inBuffer, contentType,
-                                        contentTransferEncoding);
-                                break;
-                            default:
-                        }
-                        break;
-                    case AS2MimeType.MULTIPART_REPORT:
-                        
parseMessageDispositionNotificationReportEntity(message, entity.getContent(), 
inBuffer, boundary,
-                                charsetName,
+                    case "enveloped-data":
+                        parseApplicationPkcs7MimeEnvelopedEntity(message, 
entity.getContent(), inBuffer, contentType,
                                 contentTransferEncoding);
                         break;
                     default:
-                        break;
                 }
-            } catch (HttpException e) {
-                throw e;
-            } catch (Exception e) {
-                throw new HttpException("Failed to parse entity content", e);
-            }
+                break;
+            case AS2MimeType.MULTIPART_REPORT:
+                parseMessageDispositionNotificationReportEntity(message, 
entity.getContent(), inBuffer, boundary,
+                        charsetName,
+                        contentTransferEncoding);
+                break;
+            default:
+                break;
         }
-
     }
 
     public static MultipartSignedEntity parseMultipartSignedEntityBody(

Reply via email to