Search in sources :

Example 11 with HttpMimeMessageEntity

use of com.helger.phase4.http.HttpMimeMessageEntity in project phase4 by phax.

the class AS4eSENSCEFTwoWayFuncTest method testEsens_TA16_PushPush.

/**
 * Prerequisite:<br>
 * SMSH and RMSH are configured to exchange AS4 messages according to the
 * e-SENS profile (Two-Way/Push-and-Push MEP). SMSH sends an AS4 User Message
 * to the RMSH.<br>
 * <br>
 * Predicate: <br>
 * The RMSH returns a non-repudiation receipt within a HTTP response with
 * status code 2XX.
 *
 * @throws Exception
 *         In case of error
 */
@Test
public void testEsens_TA16_PushPush() throws Exception {
    final ICommonsList<WSS4JAttachment> aAttachments = new CommonsArrayList<>();
    aAttachments.add(WSS4JAttachment.createOutgoingFileAttachment(Phase4OutgoingAttachment.builder().data(ClassPathResource.getAsFile(AS4TestConstants.ATTACHMENT_SHORTXML_XML)).mimeTypeXML().compressionGZIP().build(), s_aResMgr));
    final AS4MimeMessage aMsg = MimeMessageCreator.generateMimeMessage(m_eSoapVersion, testSignedUserMessage(m_eSoapVersion, m_aPayload, aAttachments, s_aResMgr), aAttachments);
    final String sResponse = sendMimeMessage(new HttpMimeMessageEntity(aMsg), true, null);
    assertTrue(sResponse.contains(AS4TestConstants.RECEIPT_ASSERTCHECK));
    assertTrue(sResponse.contains(AS4TestConstants.NON_REPUDIATION_INFORMATION));
    // Wait for async response to come in
    // Otherwise indeterministic errors
    ThreadHelper.sleepSeconds(2);
}
Also used : AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) HttpMimeMessageEntity(com.helger.phase4.http.HttpMimeMessageEntity) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment) Test(org.junit.Test)

Example 12 with HttpMimeMessageEntity

use of com.helger.phase4.http.HttpMimeMessageEntity in project phase4 by phax.

the class AS4CEFOneWayFuncTest method testAS4_TA17.

/**
 * Prerequisite:<br>
 * eSENS_TA13.<br>
 * The SMSH is simulated to send an AS4 User message with a compressed then
 * signed payload to the RMSH.<br>
 * <br>
 * Predicate: <br>
 * The RMSH sends back an AS4 non-repudiation receipt.
 *
 * @throws Exception
 *         In case of error
 */
@Test
public void testAS4_TA17() throws Exception {
    final ICommonsList<WSS4JAttachment> aAttachments = new CommonsArrayList<>();
    aAttachments.add(WSS4JAttachment.createOutgoingFileAttachment(Phase4OutgoingAttachment.builder().data(ClassPathResource.getAsFile(AS4TestConstants.ATTACHMENT_SHORTXML_XML)).mimeTypeXML().compressionGZIP().build(), s_aResMgr));
    final Document aDoc = createTestSignedUserMessage(m_eSoapVersion, m_aPayload, aAttachments, s_aResMgr);
    final AS4MimeMessage aMsg = MimeMessageCreator.generateMimeMessage(m_eSoapVersion, aDoc, aAttachments);
    final String sResponse = sendMimeMessage(new HttpMimeMessageEntity(aMsg), true, null);
    assertTrue(sResponse.contains(AS4TestConstants.NON_REPUDIATION_INFORMATION));
}
Also used : AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) Document(org.w3c.dom.Document) HttpMimeMessageEntity(com.helger.phase4.http.HttpMimeMessageEntity) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment) Test(org.junit.Test)

Example 13 with HttpMimeMessageEntity

use of com.helger.phase4.http.HttpMimeMessageEntity in project phase4 by phax.

the class MainOldAS4Client method main.

/**
 * Starting point for the SAAJ - SOAP Client Testing
 *
 * @param args
 *        ignored
 */
public static void main(final String[] args) {
    try (final AS4ResourceHelper aResHelper = new AS4ResourceHelper()) {
        String sURL = "http://127.0.0.1:8080/as4";
        if (false)
            sURL = "http://msh.holodeck-b2b.org:8080/msh";
        // Deactivate if not sending to local holodeck
        if (false)
            sURL = "http://localhost:8080/msh/";
        final HttpClientSettings aHCS = new HttpClientSettings();
        if (sURL.startsWith("https"))
            aHCS.setSSLContextTrustAll();
        if (false) {
            aHCS.setProxyHost(new HttpHost("172.30.9.6", 8080));
            aHCS.addNonProxyHostsFromPipeString("localhost|127.0.0.1");
        }
        final CloseableHttpClient aClient = new HttpClientFactory(aHCS).createHttpClient();
        LOGGER.info("Sending to " + sURL);
        final HttpPost aPost = new HttpPost(sURL);
        final ICommonsList<WSS4JAttachment> aAttachments = new CommonsArrayList<>();
        final Node aPayload = DOMReader.readXMLDOM(new ClassPathResource("SOAPBodyPayload.xml"));
        final ESoapVersion eSoapVersion = ESoapVersion.SOAP_12;
        final IAS4CryptoFactory aCryptoFactory = AS4CryptoFactoryProperties.getDefaultInstance();
        if (true) {
            // 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) {
            // BodyPayload SIGNED
            final Document aDoc = MockClientMessages.createUserMessageSigned(eSoapVersion, aPayload, aAttachments, aResHelper);
            aPost.setEntity(new HttpXMLEntity(aDoc, eSoapVersion.getMimeType()));
        } 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(Phase4OutgoingAttachment.builder().data(ClassPathResource.getAsFile("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(new HttpMimeMessageEntity(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 :)");
        // re-instantiate if you want to see the request that is getting sent
        LOGGER.info(EntityUtils.toString(aPost.getEntity()));
        final CloseableHttpResponse aHttpResponse = aClient.execute(aPost);
        LOGGER.info("GET Response Status:: " + aHttpResponse.getStatusLine().getStatusCode());
        // print result
        LOGGER.info(EntityUtils.toString(aHttpResponse.getEntity()));
    } catch (final Exception e) {
        LOGGER.error("Error occurred while sending SOAP Request to Server", e);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) IAS4CryptoFactory(com.helger.phase4.crypto.IAS4CryptoFactory) Node(org.w3c.dom.Node) HttpXMLEntity(com.helger.phase4.http.HttpXMLEntity) Document(org.w3c.dom.Document) AS4UserMessage(com.helger.phase4.messaging.domain.AS4UserMessage) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) HttpHost(org.apache.http.HttpHost) ESoapVersion(com.helger.phase4.soap.ESoapVersion) AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientSettings(com.helger.httpclient.HttpClientSettings) HttpMimeMessageEntity(com.helger.phase4.http.HttpMimeMessageEntity) AS4ResourceHelper(com.helger.phase4.util.AS4ResourceHelper) HttpClientFactory(com.helger.httpclient.HttpClientFactory) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment)

Example 14 with HttpMimeMessageEntity

use of com.helger.phase4.http.HttpMimeMessageEntity in project phase4 by phax.

the class AS4ClientUserMessage method buildMessage.

@Override
@Nonnull
public AS4ClientBuiltMessage buildMessage(@Nonnull @Nonempty final String sMessageID, @Nullable final IAS4ClientBuildMessageCallback aCallback) throws WSSecurityException, MessagingException {
    final String sAgreementRefPMode = m_aPModeIDFactory.apply(this);
    // check mandatory attributes
    _checkMandatoryAttributes();
    final boolean bSign = signingParams().isSigningEnabled();
    final boolean bEncrypt = cryptParams().isCryptEnabled(LOGGER::warn);
    final boolean bAttachmentsPresent = m_aAttachments.isNotEmpty();
    final Ebms3MessageInfo aEbms3MessageInfo = MessageHelperMethods.createEbms3MessageInfo(sMessageID, getRefToMessageID(), getSendingDateTimeOrNow());
    final Ebms3PayloadInfo aEbms3PayloadInfo = MessageHelperMethods.createEbms3PayloadInfo(m_aPayload != null, m_aAttachments);
    final Ebms3CollaborationInfo aEbms3CollaborationInfo = MessageHelperMethods.createEbms3CollaborationInfo(sAgreementRefPMode, m_sAgreementRefValue, m_sServiceType, m_sServiceValue, m_sAction, m_sConversationID);
    final Ebms3PartyInfo aEbms3PartyInfo = MessageHelperMethods.createEbms3PartyInfo(m_sFromRole, m_sFromPartyIDType, m_sFromPartyIDValue, m_sToRole, m_sToPartyIDType, m_sToPartyIDValue);
    final Ebms3MessageProperties aEbms3MessageProperties = MessageHelperMethods.createEbms3MessageProperties(m_aEbms3Properties);
    final AS4UserMessage aUserMsg = AS4UserMessage.create(aEbms3MessageInfo, aEbms3PayloadInfo, aEbms3CollaborationInfo, aEbms3PartyInfo, aEbms3MessageProperties, getSoapVersion()).setMustUnderstand(true);
    if (aCallback != null)
        aCallback.onAS4Message(aUserMsg);
    final Document aPureDoc = aUserMsg.getAsSoapDocument(m_aPayload);
    if (aCallback != null)
        aCallback.onSoapDocument(aPureDoc);
    // 1. compress
    // Is done when the attachments are added
    // 2. sign and/or encrpyt
    Document aDoc = aPureDoc;
    AS4MimeMessage aMimeMsg = null;
    if (bSign || bEncrypt) {
        AS4HttpDebug.debug(() -> "Unsigned/unencrypted UserMessage:\n" + XMLWriter.getNodeAsString(aPureDoc, AS4HttpDebug.getDebugXMLWriterSettings()));
        final IAS4CryptoFactory aCryptoFactory = internalCreateCryptoFactory();
        // 2a. sign
        if (bSign) {
            final boolean bMustUnderstand = true;
            final Document aSignedDoc = AS4Signer.createSignedMessage(aCryptoFactory, aDoc, getSoapVersion(), aUserMsg.getMessagingID(), m_aAttachments, getAS4ResourceHelper(), bMustUnderstand, signingParams().getClone());
            aDoc = aSignedDoc;
            if (aCallback != null)
                aCallback.onSignedSoapDocument(aSignedDoc);
            AS4HttpDebug.debug(() -> "Signed UserMessage:\n" + XMLWriter.getNodeAsString(aSignedDoc, AS4HttpDebug.getDebugXMLWriterSettings()));
        }
        // 2b. encrypt
        if (bEncrypt) {
            // MustUnderstand always set to true
            final boolean bMustUnderstand = true;
            if (bAttachmentsPresent) {
                aMimeMsg = AS4Encryptor.encryptMimeMessage(getSoapVersion(), aDoc, m_aAttachments, aCryptoFactory, bMustUnderstand, getAS4ResourceHelper(), cryptParams().getClone());
                if (aCallback != null)
                    aCallback.onEncryptedMimeMessage(aMimeMsg);
            } else {
                final Document aEncryptedDoc = AS4Encryptor.encryptSoapBodyPayload(aCryptoFactory, getSoapVersion(), aDoc, bMustUnderstand, cryptParams().getClone());
                if (aCallback != null)
                    aCallback.onEncryptedSoapDocument(aDoc);
                aDoc = aEncryptedDoc;
            }
        }
    }
    if (bAttachmentsPresent && aMimeMsg == null) {
        // * not encrypted, not signed
        // * not encrypted, signed
        aMimeMsg = MimeMessageCreator.generateMimeMessage(getSoapVersion(), aDoc, m_aAttachments);
    }
    if (aMimeMsg != null) {
        // Wrap MIME message
        return new AS4ClientBuiltMessage(sMessageID, new HttpMimeMessageEntity(aMimeMsg));
    }
    // Wrap SOAP XML
    return new AS4ClientBuiltMessage(sMessageID, new HttpXMLEntity(aDoc, getSoapVersion().getMimeType()));
}
Also used : Ebms3MessageProperties(com.helger.phase4.ebms3header.Ebms3MessageProperties) IAS4CryptoFactory(com.helger.phase4.crypto.IAS4CryptoFactory) HttpXMLEntity(com.helger.phase4.http.HttpXMLEntity) Document(org.w3c.dom.Document) AS4UserMessage(com.helger.phase4.messaging.domain.AS4UserMessage) Ebms3PartyInfo(com.helger.phase4.ebms3header.Ebms3PartyInfo) Ebms3PayloadInfo(com.helger.phase4.ebms3header.Ebms3PayloadInfo) Ebms3CollaborationInfo(com.helger.phase4.ebms3header.Ebms3CollaborationInfo) AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) HttpMimeMessageEntity(com.helger.phase4.http.HttpMimeMessageEntity) Ebms3MessageInfo(com.helger.phase4.ebms3header.Ebms3MessageInfo) Nonnull(javax.annotation.Nonnull)

Example 15 with HttpMimeMessageEntity

use of com.helger.phase4.http.HttpMimeMessageEntity in project phase4 by phax.

the class UserMessageCompressionTest method testUserMessageCompressedEncrpytedSuccessful.

@Test
public void testUserMessageCompressedEncrpytedSuccessful() throws Exception {
    final ICommonsList<WSS4JAttachment> aAttachments = new CommonsArrayList<>();
    aAttachments.add(WSS4JAttachment.createOutgoingFileAttachment(Phase4OutgoingAttachment.builder().data(ClassPathResource.getAsFile(AS4TestConstants.TEST_SOAP_BODY_PAYLOAD_XML)).mimeTypeXML().compressionGZIP().build(), s_aResMgr));
    final Document aDoc = MockMessages.createUserMessageNotSigned(m_eSOAPVersion, null, aAttachments).getAsSoapDocument();
    final AS4MimeMessage aMsg = AS4Encryptor.encryptMimeMessage(m_eSOAPVersion, aDoc, aAttachments, m_aCryptoFactory, false, s_aResMgr, m_aCryptParams);
    sendMimeMessage(new HttpMimeMessageEntity(aMsg), true, null);
}
Also used : AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) Document(org.w3c.dom.Document) HttpMimeMessageEntity(com.helger.phase4.http.HttpMimeMessageEntity) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment) Test(org.junit.Test)

Aggregations

HttpMimeMessageEntity (com.helger.phase4.http.HttpMimeMessageEntity)43 AS4MimeMessage (com.helger.phase4.messaging.mime.AS4MimeMessage)43 Test (org.junit.Test)41 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)36 WSS4JAttachment (com.helger.phase4.attachment.WSS4JAttachment)36 Document (org.w3c.dom.Document)20 AS4UserMessage (com.helger.phase4.messaging.domain.AS4UserMessage)16 IStringMap (com.helger.commons.collection.attr.IStringMap)3 StringMap (com.helger.commons.collection.attr.StringMap)3 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)3 ScopedConfig (com.helger.phase4.ScopedConfig)3 Ebms3CollaborationInfo (com.helger.phase4.ebms3header.Ebms3CollaborationInfo)3 Ebms3MessageInfo (com.helger.phase4.ebms3header.Ebms3MessageInfo)3 Ebms3MessageProperties (com.helger.phase4.ebms3header.Ebms3MessageProperties)3 Ebms3PartyInfo (com.helger.phase4.ebms3header.Ebms3PartyInfo)3 Ebms3PayloadInfo (com.helger.phase4.ebms3header.Ebms3PayloadInfo)3 HttpProxyServer (org.littleshoot.proxy.HttpProxyServer)3 DefaultHttpProxyServer (org.littleshoot.proxy.impl.DefaultHttpProxyServer)3 Node (org.w3c.dom.Node)3 IAS4CryptoFactory (com.helger.phase4.crypto.IAS4CryptoFactory)2