Search in sources :

Example 1 with SoapMimeMultipart

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

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

the class UserMessageFailureForgeryTest method testUserMessageEncryptedMimeAttachmentForged.

// Encryption
// Cannot be tested easily, since the error gets only thrown if the SPI tries
// to read the stream of the attachment
// @Ignore
@Test
public void testUserMessageEncryptedMimeAttachmentForged() throws Exception {
    final ICommonsList<WSS4JAttachment> aAttachments = new CommonsArrayList<>();
    aAttachments.add(WSS4JAttachment.createOutgoingFileAttachment(Phase4OutgoingAttachment.builder().data(ClassPathResource.getAsFile(AS4TestConstants.ATTACHMENT_SHORTXML_XML)).mimeTypeXML().build(), s_aResMgr));
    final AS4MimeMessage aMimeMsg = AS4Encryptor.encryptMimeMessage(m_eSoapVersion, MockMessages.createUserMessageNotSigned(m_eSoapVersion, null, aAttachments).getAsSoapDocument(), aAttachments, m_aCryptoFactory, true, s_aResMgr, m_aCryptParams);
    final SoapMimeMultipart aMultipart = (SoapMimeMultipart) aMimeMsg.getContent();
    // Since we want to change the attachment
    final MimeBodyPart aMimeBodyPart = (MimeBodyPart) aMultipart.getBodyPart(1);
    aMimeBodyPart.setContent("Crappy text".getBytes(StandardCharsets.ISO_8859_1), CMimeType.APPLICATION_OCTET_STREAM.getAsString());
    aMimeMsg.saveChanges();
    sendMimeMessage(new HttpMimeMessageEntity(aMimeMsg), false, EEbmsError.EBMS_FAILED_DECRYPTION.getErrorCode());
}
Also used : AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) SoapMimeMultipart(com.helger.phase4.messaging.mime.SoapMimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) HttpMimeMessageEntity(com.helger.phase4.http.HttpMimeMessageEntity) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment) Test(org.junit.Test)

Example 3 with SoapMimeMultipart

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

the class UserMessageFailureForgeryTest method testUserMessageWithAttachmentPartInfoOnly.

@Test
public void testUserMessageWithAttachmentPartInfoOnly() throws Exception {
    final ICommonsList<WSS4JAttachment> aAttachments = new CommonsArrayList<>();
    aAttachments.add(WSS4JAttachment.createOutgoingFileAttachment(Phase4OutgoingAttachment.builder().data(ClassPathResource.getAsFile(AS4TestConstants.ATTACHMENT_TEST_XML_GZ)).mimeTypeXML().build(), s_aResMgr));
    final AS4MimeMessage aMimeMsg = MimeMessageCreator.generateMimeMessage(m_eSoapVersion, MockMessages.createUserMessageNotSigned(m_eSoapVersion, null, aAttachments).getAsSoapDocument(), aAttachments);
    final SoapMimeMultipart aMultipart = (SoapMimeMultipart) aMimeMsg.getContent();
    // Since we want to remove the attachment
    aMultipart.removeBodyPart(1);
    aMimeMsg.saveChanges();
    sendMimeMessage(new HttpMimeMessageEntity(aMimeMsg), false, EEbmsError.EBMS_EXTERNAL_PAYLOAD_ERROR.getErrorCode());
}
Also used : AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) SoapMimeMultipart(com.helger.phase4.messaging.mime.SoapMimeMultipart) HttpMimeMessageEntity(com.helger.phase4.http.HttpMimeMessageEntity) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment) Test(org.junit.Test)

Aggregations

WSS4JAttachment (com.helger.phase4.attachment.WSS4JAttachment)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)2 HttpMimeMessageEntity (com.helger.phase4.http.HttpMimeMessageEntity)2 AS4MimeMessage (com.helger.phase4.messaging.mime.AS4MimeMessage)2 SoapMimeMultipart (com.helger.phase4.messaging.mime.SoapMimeMultipart)2 MimeBodyPart (javax.mail.internet.MimeBodyPart)2 Test (org.junit.Test)2 EContentTransferEncoding (com.helger.mail.cte.EContentTransferEncoding)1 Charset (java.nio.charset.Charset)1 DataHandler (javax.activation.DataHandler)1 Nonnull (javax.annotation.Nonnull)1 DOMSource (javax.xml.transform.dom.DOMSource)1