Search in sources :

Example 16 with AS4MimeMessage

use of com.helger.phase4.messaging.mime.AS4MimeMessage 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 17 with AS4MimeMessage

use of com.helger.phase4.messaging.mime.AS4MimeMessage in project phase4 by phax.

the class MimeMessageCreator method generateMimeMessage.

@Nonnull
public static AS4MimeMessage generateMimeMessage(@Nonnull final ESoapVersion eSoapVersion, @Nonnull final Document aSoapEnvelope, @Nullable final ICommonsList<WSS4JAttachment> aEncryptedAttachments) throws MessagingException {
    ValueEnforcer.notNull(eSoapVersion, "SoapVersion");
    ValueEnforcer.notNull(aSoapEnvelope, "SoapEnvelope");
    final Charset aCharset = AS4XMLHelper.XWS.getCharset();
    final SoapMimeMultipart aMimeMultipart = new SoapMimeMultipart(eSoapVersion, aCharset);
    final EContentTransferEncoding eCTE = EContentTransferEncoding.BINARY;
    final String sContentType = eSoapVersion.getMimeType(aCharset).getAsString();
    {
        // Message Itself (repeatable)
        final MimeBodyPart aMessagePart = new MimeBodyPart();
        aMessagePart.setDataHandler(new DataHandler(new DOMSource(aSoapEnvelope), sContentType));
        aMessagePart.setHeader(CHttpHeader.CONTENT_TRANSFER_ENCODING, eCTE.getID());
        aMimeMultipart.addBodyPart(aMessagePart);
    }
    boolean bIsRepeatable = true;
    if (aEncryptedAttachments != null)
        for (final WSS4JAttachment aEncryptedAttachment : aEncryptedAttachments) {
            aEncryptedAttachment.addToMimeMultipart(aMimeMultipart);
            if (!aEncryptedAttachment.isRepeatable())
                bIsRepeatable = false;
        }
    // Build main message
    final AS4MimeMessage aMsg = new AS4MimeMessage((Session) null, bIsRepeatable);
    aMsg.setContent(aMimeMultipart);
    aMsg.saveChanges();
    return aMsg;
}
Also used : EContentTransferEncoding(com.helger.mail.cte.EContentTransferEncoding) DOMSource(javax.xml.transform.dom.DOMSource) Charset(java.nio.charset.Charset) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment) Nonnull(javax.annotation.Nonnull)

Example 18 with AS4MimeMessage

use of com.helger.phase4.messaging.mime.AS4MimeMessage in project phase4 by phax.

the class AS4RequestHandler method _createResponseUserMessage.

/**
 * With this method it is possible to send a usermessage back, the method will
 * check if signing is needed and if the message needs to be a mime message.
 *
 * @param aState
 *        The state of the incoming message. Never <code>null</code>.
 * @param eSoapVersion
 *        the SOAP version to use. May not be <code>null</code>
 * @param aResponseUserMsg
 *        the response user message that should be sent
 * @param sMessagingID
 *        ID of the "Messaging" element
 * @param aResponseAttachments
 *        attachments if any that should be added
 * @param aSigningParams
 *        Signing parameters
 * @param aCryptParams
 *        Encryption parameters
 * @throws WSSecurityException
 *         on error
 * @throws MessagingException
 *         on error
 */
@Nonnull
private IAS4ResponseFactory _createResponseUserMessage(@Nonnull final IAS4MessageState aState, @Nonnull final ESoapVersion eSoapVersion, @Nonnull final AS4UserMessage aResponseUserMsg, @Nonnull final ICommonsList<WSS4JAttachment> aResponseAttachments, @Nonnull final AS4SigningParams aSigningParams, @Nonnull final AS4CryptParams aCryptParams) throws WSSecurityException, MessagingException {
    final String sResponseMessageID = aResponseUserMsg.getEbms3UserMessage().getMessageInfo().getMessageId();
    final Document aSignedDoc = _signResponseIfNeeded(aResponseAttachments, aSigningParams, aResponseUserMsg.getAsSoapDocument(), eSoapVersion, aResponseUserMsg.getMessagingID());
    final IAS4ResponseFactory ret;
    if (aResponseAttachments.isEmpty()) {
        // FIXME encryption of SOAP body is missing here
        ret = new AS4ResponseFactoryXML(m_aMessageMetadata, aState, sResponseMessageID, aSignedDoc, eSoapVersion.getMimeType());
    } else {
        // Create (maybe encrypted) MIME message
        final AS4MimeMessage aMimeMsg = _createMimeMessageForResponse(aSignedDoc, aResponseAttachments, eSoapVersion, aCryptParams);
        ret = new AS4ResponseFactoryMIME(m_aMessageMetadata, aState, sResponseMessageID, aMimeMsg);
    }
    return ret;
}
Also used : AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) Document(org.w3c.dom.Document) Nonnull(javax.annotation.Nonnull)

Example 19 with AS4MimeMessage

use of com.helger.phase4.messaging.mime.AS4MimeMessage in project phase4 by phax.

the class AS4RequestHandler method _createMimeMessageForResponse.

/**
 * Returns the MimeMessage with encrypted attachment or without depending on
 * what is configured in the PMode within Leg2.
 *
 * @param aResponseDoc
 *        the document that contains the user message
 * @param aResponseAttachments
 *        The Attachments that should be encrypted
 * @param aLeg
 *        Leg to get necessary information, EncryptionAlgorithm, SOAPVersion
 * @param sEncryptToAlias
 *        The alias into the keystore that should be used for encryption
 * @return a MimeMessage to be sent
 * @throws MessagingException
 * @throws WSSecurityException
 */
@Nonnull
private AS4MimeMessage _createMimeMessageForResponse(@Nonnull final Document aResponseDoc, @Nonnull final ICommonsList<WSS4JAttachment> aResponseAttachments, @Nonnull final ESoapVersion eSoapVersion, @Nonnull final AS4CryptParams aCryptParms) throws WSSecurityException, MessagingException {
    final AS4MimeMessage aMimeMsg;
    if (aCryptParms.isCryptEnabled(LOGGER::warn)) {
        final boolean bMustUnderstand = true;
        aMimeMsg = AS4Encryptor.encryptMimeMessage(eSoapVersion, aResponseDoc, aResponseAttachments, m_aCryptoFactory, bMustUnderstand, m_aResHelper, aCryptParms);
    } else {
        aMimeMsg = MimeMessageCreator.generateMimeMessage(eSoapVersion, aResponseDoc, aResponseAttachments);
    }
    if (aMimeMsg == null)
        throw new IllegalStateException("Failed to create MimeMessage!");
    return aMimeMsg;
}
Also used : AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) Nonnull(javax.annotation.Nonnull)

Example 20 with AS4MimeMessage

use of com.helger.phase4.messaging.mime.AS4MimeMessage 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

AS4MimeMessage (com.helger.phase4.messaging.mime.AS4MimeMessage)46 HttpMimeMessageEntity (com.helger.phase4.http.HttpMimeMessageEntity)43 Test (org.junit.Test)42 WSS4JAttachment (com.helger.phase4.attachment.WSS4JAttachment)39 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)37 Document (org.w3c.dom.Document)22 AS4UserMessage (com.helger.phase4.messaging.domain.AS4UserMessage)16 Nonnull (javax.annotation.Nonnull)5 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