From 8c3be6e80c81ef3e8c339965eb4874439e27e768 Mon Sep 17 00:00:00 2001 From: Philip Helger Date: Thu, 14 Sep 2023 20:45:33 +0200 Subject: [PATCH] Improved code readability --- .../phase4/client/MainOldAS4Client.java | 154 +++++++++--------- phase4-server-webapp/pom.xml | 1 + 2 files changed, 81 insertions(+), 74 deletions(-) diff --git a/phase4-lib/src/test/java/com/helger/phase4/client/MainOldAS4Client.java b/phase4-lib/src/test/java/com/helger/phase4/client/MainOldAS4Client.java index 2d5237522..f1ffcfba8 100644 --- a/phase4-lib/src/test/java/com/helger/phase4/client/MainOldAS4Client.java +++ b/phase4-lib/src/test/java/com/helger/phase4/client/MainOldAS4Client.java @@ -92,10 +92,12 @@ public static void main (final String [] args) try (final AS4ResourceHelper aResHelper = new AS4ResourceHelper ()) { String sURL = "http://127.0.0.1:8080/as4"; + + // External holodeck if (false) sURL = "http://msh.holodeck-b2b.org:8080/msh"; - // Deactivate if not sending to local holodeck + // Local holodeck if (false) sURL = "http://localhost:8080/msh/"; @@ -104,6 +106,7 @@ public static void main (final String [] args) aHCS.setSSLContextTrustAll (); if (false) { + // BRZ internal aHCS.setProxyHost (new HttpHost ("172.30.9.6", 8080)); aHCS.addNonProxyHostsFromPipeString ("localhost|127.0.0.1"); } @@ -117,22 +120,19 @@ public static void main (final String [] args) final ESoapVersion eSoapVersion = ESoapVersion.SOAP_12; final IAS4CryptoFactory aCryptoFactory = AS4CryptoFactoryProperties.getDefaultInstance (); - if (true) + switch (4) { - // No Mime Message Not signed or encrypted, just SOAP + Payload in - // SOAP - // - - // Body - // final Document aDoc = TestMessages.testSignedUserMessage - // (ESOAPVersion.SOAP_11, aPayload, aAttachments); - final AS4UserMessage aMsg = MockClientMessages.createUserMessageNotSigned (eSoapVersion, - aPayload, - aAttachments); - final Document aDoc = aMsg.getAsSoapDocument (aPayload); - aPost.setEntity (new HttpXMLEntity (aDoc, eSoapVersion.getMimeType ())); - } - else - if (false) + case 1: + { + // BodyPayload NOT SIGNED and NOT ENCRYPTED + final AS4UserMessage aMsg = MockClientMessages.createUserMessageNotSigned (eSoapVersion, + aPayload, + aAttachments); + final Document aDoc = aMsg.getAsSoapDocument (aPayload); + aPost.setEntity (new HttpXMLEntity (aDoc, eSoapVersion.getMimeType ())); + break; + } + case 2: { // BodyPayload SIGNED final Document aDoc = MockClientMessages.createUserMessageSigned (eSoapVersion, @@ -140,65 +140,71 @@ public static void main (final String [] args) aAttachments, aResHelper); aPost.setEntity (new HttpXMLEntity (aDoc, eSoapVersion.getMimeType ())); + break; } - else - if (false) - { - // BodyPayload ENCRYPTED - final AS4UserMessage aMsg = MockClientMessages.createUserMessageNotSigned (eSoapVersion, - aPayload, - aAttachments); - Document aDoc = aMsg.getAsSoapDocument (aPayload); - aDoc = AS4Encryptor.encryptSoapBodyPayload (aCryptoFactory, - eSoapVersion, - aDoc, - false, - AS4CryptParams.createDefault ().setAlias ("dummy")); - - aPost.setEntity (new HttpXMLEntity (aDoc, eSoapVersion.getMimeType ())); - } - else - if (true) - { - aAttachments.add (WSS4JAttachment.createOutgoingFileAttachment (AS4OutgoingAttachment.builder () - .data (ClassPathResource.getAsFile ("external/attachment/test.xml.gz")) - .mimeType (CMimeType.APPLICATION_GZIP) - .build (), - aResHelper)); - final AS4UserMessage aMsg = MockClientMessages.createUserMessageNotSigned (eSoapVersion, - null, - aAttachments); - final AS4MimeMessage aMimeMsg = MimeMessageCreator.generateMimeMessage (eSoapVersion, - AS4Signer.createSignedMessage (aCryptoFactory, - aMsg.getAsSoapDocument (null), - eSoapVersion, - aMsg.getMessagingID (), - aAttachments, - aResHelper, - false, - AS4SigningParams.createDefault ()), - aAttachments); - - // Move all global mime headers to the POST request - MessageHelperMethods.forEachHeaderAndRemoveAfterwards (aMimeMsg, aPost::addHeader, true); - aPost.setEntity (HttpMimeMessageEntity.create (aMimeMsg)); - } - else - if (false) - { - Document aDoc = MockClientMessages.createUserMessageSigned (eSoapVersion, - aPayload, - aAttachments, - aResHelper); - aDoc = AS4Encryptor.encryptSoapBodyPayload (aCryptoFactory, - eSoapVersion, - aDoc, - false, - AS4CryptParams.createDefault ().setAlias ("dummy")); - aPost.setEntity (new HttpXMLEntity (aDoc, eSoapVersion.getMimeType ())); - } - else - throw new IllegalStateException ("Some test message should be selected :)"); + case 3: + { + // BodyPayload ENCRYPTED + final AS4UserMessage aMsg = MockClientMessages.createUserMessageNotSigned (eSoapVersion, + aPayload, + aAttachments); + Document aDoc = aMsg.getAsSoapDocument (aPayload); + aDoc = AS4Encryptor.encryptSoapBodyPayload (aCryptoFactory, + eSoapVersion, + aDoc, + false, + AS4CryptParams.createDefault () + .setAlias (aCryptoFactory.getKeyAlias ())); + + aPost.setEntity (new HttpXMLEntity (aDoc, eSoapVersion.getMimeType ())); + break; + } + case 4: + { + // BodyPayload SIGNED and ENCRYPTED + Document aDoc = MockClientMessages.createUserMessageSigned (eSoapVersion, + aPayload, + aAttachments, + aResHelper); + aDoc = AS4Encryptor.encryptSoapBodyPayload (aCryptoFactory, + eSoapVersion, + aDoc, + false, + AS4CryptParams.createDefault () + .setAlias (aCryptoFactory.getKeyAlias ())); + aPost.setEntity (new HttpXMLEntity (aDoc, eSoapVersion.getMimeType ())); + break; + } + case 5: + { + // MIME Message SIGNED + aAttachments.add (WSS4JAttachment.createOutgoingFileAttachment (AS4OutgoingAttachment.builder () + .data (ClassPathResource.getAsFile ("external/attachment/test.xml.gz")) + .mimeType (CMimeType.APPLICATION_GZIP) + .build (), + aResHelper)); + final AS4UserMessage aMsg = MockClientMessages.createUserMessageNotSigned (eSoapVersion, + null, + aAttachments); + final AS4MimeMessage aMimeMsg = MimeMessageCreator.generateMimeMessage (eSoapVersion, + AS4Signer.createSignedMessage (aCryptoFactory, + aMsg.getAsSoapDocument (null), + eSoapVersion, + aMsg.getMessagingID (), + aAttachments, + aResHelper, + false, + AS4SigningParams.createDefault ()), + aAttachments); + + // Move all global mime headers to the POST request + MessageHelperMethods.forEachHeaderAndRemoveAfterwards (aMimeMsg, aPost::addHeader, true); + aPost.setEntity (HttpMimeMessageEntity.create (aMimeMsg)); + break; + } + default: + throw new IllegalStateException ("Some test message should be selected :)"); + } // re-instantiate if you want to see the request that is getting sent LOGGER.info (EntityUtils.toString (aPost.getEntity ())); diff --git a/phase4-server-webapp/pom.xml b/phase4-server-webapp/pom.xml index 32b0640c1..78ff8fd67 100644 --- a/phase4-server-webapp/pom.xml +++ b/phase4-server-webapp/pom.xml @@ -82,6 +82,7 @@ com.helger.peppol peppol-smp-client + org.apache.logging.log4j log4j-core