Search in sources :

Example 11 with ESoapVersion

use of com.helger.phase4.soap.ESoapVersion 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)

Example 12 with ESoapVersion

use of com.helger.phase4.soap.ESoapVersion in project phase4 by phax.

the class AS4ErrorMessage method create.

@Nonnull
public static AS4ErrorMessage create(@Nonnull final ESoapVersion eSoapVersion, @Nonnull final Ebms3MessageInfo aEbms3MessageInfo, @Nonnull final ICommonsList<Ebms3Error> aErrorMessages) {
    final Ebms3SignalMessage aSignalMessage = new Ebms3SignalMessage();
    // Message Info
    aSignalMessage.setMessageInfo(aEbms3MessageInfo);
    // Error Message
    aSignalMessage.setError(aErrorMessages);
    return new AS4ErrorMessage(eSoapVersion, aSignalMessage);
}
Also used : Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) Nonnull(javax.annotation.Nonnull)

Example 13 with ESoapVersion

use of com.helger.phase4.soap.ESoapVersion in project phase4 by phax.

the class AS4PullRequestMessage method create.

@Nonnull
public static AS4PullRequestMessage create(@Nonnull final ESoapVersion eSoapVersion, @Nonnull final Ebms3MessageInfo aEbms3MessageInfo, @Nullable final String sMPC, @Nullable final List<Object> aAny) {
    final Ebms3SignalMessage aSignalMessage = new Ebms3SignalMessage();
    // Message Info
    aSignalMessage.setMessageInfo(aEbms3MessageInfo);
    // PullRequest
    if (StringHelper.hasText(sMPC)) {
        final Ebms3PullRequest aEbms3PullRequest = new Ebms3PullRequest();
        aEbms3PullRequest.setMpc(sMPC);
        aSignalMessage.setPullRequest(aEbms3PullRequest);
    }
    aSignalMessage.setAny(aAny);
    return new AS4PullRequestMessage(eSoapVersion, aSignalMessage);
}
Also used : Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) Ebms3PullRequest(com.helger.phase4.ebms3header.Ebms3PullRequest) Nonnull(javax.annotation.Nonnull)

Example 14 with ESoapVersion

use of com.helger.phase4.soap.ESoapVersion in project phase4 by phax.

the class AS4UserMessage method create.

@Nonnull
public static AS4UserMessage create(@Nonnull final Ebms3MessageInfo aEbms3MessageInfo, @Nullable final Ebms3PayloadInfo aEbms3PayloadInfo, @Nonnull final Ebms3CollaborationInfo aEbms3CollaborationInfo, @Nonnull final Ebms3PartyInfo aEbms3PartyInfo, @Nullable final Ebms3MessageProperties aEbms3MessageProperties, @Nonnull final ESoapVersion eSoapVersion) {
    final Ebms3UserMessage aUserMessage = new Ebms3UserMessage();
    // Party Information
    aUserMessage.setPartyInfo(aEbms3PartyInfo);
    // Collaboration Information
    aUserMessage.setCollaborationInfo(aEbms3CollaborationInfo);
    // Properties
    aUserMessage.setMessageProperties(aEbms3MessageProperties);
    // Payload Information
    aUserMessage.setPayloadInfo(aEbms3PayloadInfo);
    // Message Info
    aUserMessage.setMessageInfo(aEbms3MessageInfo);
    return new AS4UserMessage(eSoapVersion, aUserMessage);
}
Also used : Ebms3UserMessage(com.helger.phase4.ebms3header.Ebms3UserMessage) Nonnull(javax.annotation.Nonnull)

Example 15 with ESoapVersion

use of com.helger.phase4.soap.ESoapVersion 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)

Aggregations

Nonnull (javax.annotation.Nonnull)29 Document (org.w3c.dom.Document)13 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)12 ESoapVersion (com.helger.phase4.soap.ESoapVersion)12 Ebms3MessageInfo (com.helger.phase4.ebms3header.Ebms3MessageInfo)9 Ebms3Property (com.helger.phase4.ebms3header.Ebms3Property)9 AS4UserMessage (com.helger.phase4.messaging.domain.AS4UserMessage)9 WSS4JAttachment (com.helger.phase4.attachment.WSS4JAttachment)8 Ebms3CollaborationInfo (com.helger.phase4.ebms3header.Ebms3CollaborationInfo)8 Ebms3MessageProperties (com.helger.phase4.ebms3header.Ebms3MessageProperties)8 Ebms3PartyInfo (com.helger.phase4.ebms3header.Ebms3PartyInfo)8 Ebms3PayloadInfo (com.helger.phase4.ebms3header.Ebms3PayloadInfo)8 Node (org.w3c.dom.Node)8 Ebms3Error (com.helger.phase4.ebms3header.Ebms3Error)6 Ebms3SignalMessage (com.helger.phase4.ebms3header.Ebms3SignalMessage)6 IOException (java.io.IOException)6 MessagingException (javax.mail.MessagingException)6 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)6 AS4DecompressException (com.helger.phase4.attachment.AS4DecompressException)5 Ebms3UserMessage (com.helger.phase4.ebms3header.Ebms3UserMessage)5