Search in sources :

Example 1 with AS4ResourceHelper

use of com.helger.phase4.util.AS4ResourceHelper in project phase4 by phax.

the class AbstractCEFTestSetUp method createTestSignedUserMessage.

@Nonnull
protected Document createTestSignedUserMessage(@Nonnull final ESoapVersion eSOAPVersion, @Nullable final Node aPayload, @Nullable final ICommonsList<WSS4JAttachment> aAttachments, @Nonnull final AS4ResourceHelper aResMgr) throws WSSecurityException {
    final AS4UserMessage aMsg = createTestUserMessageSoapNotSigned(aPayload, aAttachments);
    final Document aSignedDoc = AS4Signer.createSignedMessage(m_aCryptoFactory, aMsg.getAsSoapDocument(aPayload), eSOAPVersion, aMsg.getMessagingID(), aAttachments, aResMgr, false, AS4SigningParams.createDefault());
    return aSignedDoc;
}
Also used : Document(org.w3c.dom.Document) AS4UserMessage(com.helger.phase4.messaging.domain.AS4UserMessage) Nonnull(javax.annotation.Nonnull)

Example 2 with AS4ResourceHelper

use of com.helger.phase4.util.AS4ResourceHelper 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 3 with AS4ResourceHelper

use of com.helger.phase4.util.AS4ResourceHelper in project phase4 by phax.

the class MockClientMessages method createReceiptMessageSigned.

@Nonnull
public static Document createReceiptMessageSigned(@Nonnull final ESoapVersion eSoapVersion, @Nullable final Node aPayload, @Nullable final ICommonsList<WSS4JAttachment> aAttachments, @Nonnull @WillNotClose final AS4ResourceHelper aResHelper) throws WSSecurityException, DOMException {
    final Document aUserMessage = createUserMessageSigned(eSoapVersion, aPayload, aAttachments, aResHelper);
    final AS4ReceiptMessage aReceiptMsg = AS4ReceiptMessage.create(eSoapVersion, MessageHelperMethods.createRandomMessageID(), null, aUserMessage, true).setMustUnderstand(true);
    final Document aDoc = aReceiptMsg.getAsSoapDocument();
    return AS4Signer.createSignedMessage(AS4CryptoFactoryProperties.getDefaultInstance(), aDoc, eSoapVersion, aReceiptMsg.getMessagingID(), aAttachments, aResHelper, false, AS4SigningParams.createDefault());
}
Also used : AS4ReceiptMessage(com.helger.phase4.messaging.domain.AS4ReceiptMessage) Document(org.w3c.dom.Document) Nonnull(javax.annotation.Nonnull)

Example 4 with AS4ResourceHelper

use of com.helger.phase4.util.AS4ResourceHelper in project phase4 by phax.

the class MockClientMessages method createErrorMessageSigned.

@Nonnull
public static Document createErrorMessageSigned(@Nonnull final ESoapVersion eSoapVersion, @Nullable final ICommonsList<WSS4JAttachment> aAttachments, @Nonnull @WillNotClose final AS4ResourceHelper aResHelper) throws WSSecurityException {
    final ICommonsList<Ebms3Error> aEbms3ErrorList = new CommonsArrayList<>(EEbmsError.EBMS_INVALID_HEADER.getAsEbms3Error(Locale.US, null));
    final AS4ErrorMessage aErrorMsg = AS4ErrorMessage.create(eSoapVersion, "srcmsgid", aEbms3ErrorList).setMustUnderstand(true);
    final Document aSignedDoc = AS4Signer.createSignedMessage(AS4CryptoFactoryProperties.getDefaultInstance(), aErrorMsg.getAsSoapDocument(), eSoapVersion, aErrorMsg.getMessagingID(), aAttachments, aResHelper, false, AS4SigningParams.createDefault());
    return aSignedDoc;
}
Also used : Ebms3Error(com.helger.phase4.ebms3header.Ebms3Error) Document(org.w3c.dom.Document) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) AS4ErrorMessage(com.helger.phase4.messaging.domain.AS4ErrorMessage) Nonnull(javax.annotation.Nonnull)

Example 5 with AS4ResourceHelper

use of com.helger.phase4.util.AS4ResourceHelper in project phase4 by phax.

the class AS4Encryptor method _encryptMimeMessage.

@Nonnull
private static AS4MimeMessage _encryptMimeMessage(@Nonnull final ESoapVersion eSoapVersion, @Nonnull final Document aDoc, @Nullable final ICommonsList<WSS4JAttachment> aAttachments, @Nonnull final IAS4CryptoFactory aCryptoFactory, final boolean bMustUnderstand, @Nonnull @WillNotClose final AS4ResourceHelper aResHelper, @Nonnull final AS4CryptParams aCryptParams) throws WSSecurityException {
    if (LOGGER.isInfoEnabled())
        LOGGER.info("Now encrypting AS4 MIME message");
    final WSSecHeader aSecHeader = new WSSecHeader(aDoc);
    aSecHeader.insertSecurityHeader();
    final WSSecEncrypt aBuilder = _createEncrypt(aSecHeader, aCryptParams);
    // "cid:Attachments" is a predefined ID
    aBuilder.getParts().add(new WSEncryptionPart(MessageHelperMethods.PREFIX_CID + "Attachments", "Content"));
    WSS4JAttachmentCallbackHandler aAttachmentCallbackHandler = null;
    if (CollectionHelper.isNotEmpty(aAttachments)) {
        aAttachmentCallbackHandler = new WSS4JAttachmentCallbackHandler(aAttachments, aResHelper);
        aBuilder.setAttachmentCallbackHandler(aAttachmentCallbackHandler);
    }
    // Ensure mustUnderstand value
    final Attr aMustUnderstand = aSecHeader.getSecurityHeaderElement().getAttributeNodeNS(eSoapVersion.getNamespaceURI(), "mustUnderstand");
    if (aMustUnderstand != null)
        aMustUnderstand.setValue(eSoapVersion.getMustUnderstandValue(bMustUnderstand));
    // Generate a session key
    final KeyGenerator aKeyGen = KeyUtils.getKeyGenerator(WSS4JConstants.AES_128);
    final SecretKey aSymmetricKey = aKeyGen.generateKey();
    // Main sign and/or encrypt
    final Document aEncryptedDoc = aBuilder.build(aCryptoFactory.getCrypto(), aSymmetricKey);
    // The attachment callback handler contains the encrypted attachments
    // Important: read the attachment stream only once!
    ICommonsList<WSS4JAttachment> aEncryptedAttachments = null;
    if (aAttachmentCallbackHandler != null) {
        aEncryptedAttachments = aAttachmentCallbackHandler.getAllResponseAttachments();
        // MIME Type and CTE must be set for encrypted attachments!
        for (final WSS4JAttachment aAttachment : aEncryptedAttachments) {
            aAttachment.overwriteMimeType(CMimeType.APPLICATION_OCTET_STREAM.getAsString());
            aAttachment.setContentTransferEncoding(EContentTransferEncoding.BINARY);
        }
    }
    // Use the encrypted attachments!
    try {
        return MimeMessageCreator.generateMimeMessage(eSoapVersion, aEncryptedDoc, aEncryptedAttachments);
    } catch (final MessagingException ex) {
        throw new WSSecurityException(ErrorCode.FAILURE, ex, "Failed to generate MIME message");
    }
}
Also used : WSEncryptionPart(org.apache.wss4j.common.WSEncryptionPart) SecretKey(javax.crypto.SecretKey) WSSecHeader(org.apache.wss4j.dom.message.WSSecHeader) WSSecEncrypt(org.apache.wss4j.dom.message.WSSecEncrypt) MessagingException(javax.mail.MessagingException) WSS4JAttachmentCallbackHandler(com.helger.phase4.attachment.WSS4JAttachmentCallbackHandler) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) Document(org.w3c.dom.Document) KeyGenerator(javax.crypto.KeyGenerator) Attr(org.w3c.dom.Attr) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)9 Document (org.w3c.dom.Document)9 AS4ResourceHelper (com.helger.phase4.util.AS4ResourceHelper)7 WSS4JAttachment (com.helger.phase4.attachment.WSS4JAttachment)6 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)5 Phase4Exception (com.helger.phase4.util.Phase4Exception)5 IOException (java.io.IOException)5 Ebms3Error (com.helger.phase4.ebms3header.Ebms3Error)4 File (java.io.File)4 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)4 ErrorList (com.helger.commons.error.list.ErrorList)3 WSS4JAttachmentCallbackHandler (com.helger.phase4.attachment.WSS4JAttachmentCallbackHandler)3 Ebms3UserMessage (com.helger.phase4.ebms3header.Ebms3UserMessage)3 Nullable (javax.annotation.Nullable)3 IHasInputStream (com.helger.commons.io.IHasInputStream)2 HasInputStream (com.helger.commons.io.stream.HasInputStream)2 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)2 IMimeType (com.helger.commons.mime.IMimeType)2 AS4ClientUserMessage (com.helger.phase4.client.AS4ClientUserMessage)2 Ebms3PullRequest (com.helger.phase4.ebms3header.Ebms3PullRequest)2