Search in sources :

Example 6 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project as2-lib by phax.

the class AbstractMessage method writeObject.

private void writeObject(@Nonnull final ObjectOutputStream aOOS) throws IOException {
    // Write to BAOS first to avoid serializing an incomplete object
    try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream()) {
        try {
            if (m_aData != null) {
                aBAOS.write(1);
                m_aData.writeTo(aBAOS);
            } else {
                aBAOS.write(0);
            }
        } catch (final MessagingException ex) {
            throw new IOException("Messaging exception: " + ex.getMessage());
        }
        aOOS.write(aBAOS.toByteArray());
    }
    // write the message's MDN
    aOOS.writeObject(m_aMDN);
}
Also used : MessagingException(javax.mail.MessagingException) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) IOException(java.io.IOException)

Example 7 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project as2-lib by phax.

the class AbstractMessageMDN method writeObject.

private void writeObject(@Nonnull final ObjectOutputStream aOOS) throws IOException {
    // write text
    aOOS.writeObject(m_sText);
    // write the mime body
    try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream()) {
        if (m_aData != null) {
            aBAOS.write(1);
            m_aData.writeTo(aBAOS);
        } else {
            aBAOS.write(0);
        }
        aBAOS.writeTo(aOOS);
    } catch (final MessagingException ex) {
        throw new IOException("Messaging exception", ex);
    }
}
Also used : MessagingException(javax.mail.MessagingException) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) IOException(java.io.IOException)

Example 8 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project as2-lib by phax.

the class MimeBodyPartFuncTest method testWriteContentTransferEncodingBase64.

@Test
public void testWriteContentTransferEncodingBase64() throws MessagingException, IOException {
    final String sTestMsg = "Test message\nLine 2\n\nLine 4\nEOF";
    final String sEncodedMsg = Base64.safeEncode(sTestMsg, StandardCharsets.ISO_8859_1);
    // Build message content
    final MimeBodyPart aPart = new MimeBodyPart();
    aPart.setText(sTestMsg, StandardCharsets.ISO_8859_1.name());
    aPart.addHeader("x-custom", "junit");
    aPart.addHeader(CHttpHeader.CONTENT_TYPE, "text/plain");
    aPart.addHeader(CHttpHeader.CONTENT_TRANSFER_ENCODING, EContentTransferEncoding.BASE64.getID());
    aPart.addHeader(CHttpHeader.CONTENT_LENGTH, Integer.toString(sEncodedMsg.length()));
    final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream();
    aPart.writeTo(aBAOS);
    StreamHelper.close(aBAOS);
    final String sMsgPart = aBAOS.getAsString(StandardCharsets.ISO_8859_1);
    if (false)
        LOGGER.info(sMsgPart);
    assertTrue(sMsgPart, sMsgPart.contains("Content-Type: text/plain"));
    assertTrue(sMsgPart, sMsgPart.contains("Content-Transfer-Encoding: base64"));
    assertTrue(sMsgPart, sMsgPart.contains("x-custom: junit"));
    assertTrue(sMsgPart, sMsgPart.contains(sEncodedMsg));
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 9 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project as2-lib by phax.

the class AS2MDNReceiverHandler method reparse.

public void reparse(@Nonnull final AS2Message aMsg, @Nonnull final AS2HttpClient aHttpClient, @Nullable final IHTTPIncomingDumper aIncomingDumper) throws AS2Exception {
    // Create a MessageMDN and copy HTTP headers
    final IMessageMDN aMDN = new AS2MessageMDN(aMsg);
    // Bug in ph-commons 9.1.3 in addAllHeaders!
    aMDN.headers().addAllHeaders(aHttpClient.getResponseHeaderFields());
    // Receive the MDN data
    byte[] aMDNBytes = null;
    try (final NonBlockingByteArrayOutputStream aMDNStream = new NonBlockingByteArrayOutputStream()) {
        final InputStream aIS = aHttpClient.getInputStream();
        final CopyByteStreamBuilder aBuilder = StreamHelper.copyByteStream().from(aIS).closeFrom(true).to(aMDNStream).closeTo(false);
        // Retrieve the message content
        final long nContentLength = StringParser.parseLong(aMDN.getHeader(CHttpHeader.CONTENT_LENGTH), -1);
        if (nContentLength >= 0)
            aBuilder.limit(nContentLength);
        aBuilder.build();
        aMDNBytes = aMDNStream.toByteArray();
    } catch (final IOException ex) {
        LOGGER.error("Error reparsing", ex);
    }
    if (aIncomingDumper != null)
        aIncomingDumper.dumpIncomingRequest(aMDN.headers().getAllHeaderLines(true), aMDNBytes != null ? aMDNBytes : ArrayHelper.EMPTY_BYTE_ARRAY, aMDN);
    MimeBodyPart aPart = null;
    if (aMDNBytes != null)
        try {
            aPart = new MimeBodyPart(AS2HttpHelper.getAsInternetHeaders(aMDN.headers()), aMDNBytes);
        } catch (final MessagingException ex) {
            LOGGER.error("Error creating MimeBodyPart for received MDN", ex);
        }
    aMDN.setData(aPart);
    // get the MDN partnership info
    aMDN.partnership().setSenderAS2ID(aMDN.getHeader(CHttpHeader.AS2_FROM));
    aMDN.partnership().setReceiverAS2ID(aMDN.getHeader(CHttpHeader.AS2_TO));
}
Also used : AS2MessageMDN(com.helger.as2lib.message.AS2MessageMDN) MessagingException(javax.mail.MessagingException) AS2HttpRequestDataProviderInputStream(com.helger.as2lib.util.http.AS2HttpRequestDataProviderInputStream) InputStream(java.io.InputStream) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) IMessageMDN(com.helger.as2lib.message.IMessageMDN) IOException(java.io.IOException) CopyByteStreamBuilder(com.helger.commons.io.stream.StreamHelper.CopyByteStreamBuilder) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 10 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project as2-lib by phax.

the class BCCryptoHelperTest method testEncryptCTE_Base64.

@Test
public void testEncryptCTE_Base64() throws Exception {
    final MimeBodyPart aPart = new MimeBodyPart();
    aPart.setText("Hello world");
    final MimeBodyPart aEncrypted = AS2Helper.getCryptoHelper().encrypt(aPart, CERT_ENCRYPT, ECryptoAlgorithmCrypt.CRYPT_3DES, EContentTransferEncoding.BASE64);
    final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream();
    aEncrypted.writeTo(aBAOS);
    final String sExpectedStart = "Content-Type: application/pkcs7-mime; name=\"smime.p7m\"; smime-type=enveloped-data\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-Disposition: attachment; filename=\"smime.p7m\"\r\n" + "Content-Description: S/MIME Encrypted Message\r\n" + "\r\n" + "MIAGCSqGSIb3DQEHA6CAMIACAQAxggHgMIIB3AIBADCBwzCBujEjMCEGCSqGSIb3DQEJARYUc2Vy\r\n" + "dmljZUBtZW5kZWxzb24uZGUxCzAJBgNVBAYTAkRFMQ8wDQYDVQQIDAZCZXJsaW4xDzANBgNVBAcM\r\n" + "BkJlcmxpbjEiMCAGA1UECgwZbWVuZGVsc29uLWUtY29tbWVyY2UgR21iSDEhMB8GA1UECwwYRG8g\r\n" + "bm90IHVzZSBpbiBwcm9kdWN0aW9uMR0wGwYDVQQDDBRtZW5kZWxzb24gdGVzdCBrZXkgNAIEWipb\r\n";
    final String sReal = aBAOS.getAsString(StandardCharsets.ISO_8859_1);
    // assertEquals (sReal, sExpectedStart);
    assertTrue(sReal.startsWith(sExpectedStart));
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Aggregations

NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)55 Test (org.junit.Test)30 IOException (java.io.IOException)12 MimeBodyPart (javax.mail.internet.MimeBodyPart)12 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)8 InputStream (java.io.InputStream)7 Nonnull (javax.annotation.Nonnull)7 MessagingException (javax.mail.MessagingException)7 IMicroDocument (com.helger.xml.microdom.IMicroDocument)4 AS2Exception (com.helger.as2lib.exception.AS2Exception)3 IMessageMDN (com.helger.as2lib.message.IMessageMDN)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)3 ByteArrayOutputStreamProvider (com.helger.commons.io.streamprovider.ByteArrayOutputStreamProvider)3 MockMarshallerExternal (com.helger.jaxb.mock.MockMarshallerExternal)3 MockJAXBArchive (com.helger.jaxb.mock.external.MockJAXBArchive)3 XMLWriterSettings (com.helger.xml.serialize.write.XMLWriterSettings)3 Nullable (javax.annotation.Nullable)3 SMIMEException (org.bouncycastle.mail.smime.SMIMEException)3 AS2DispositionException (com.helger.as2lib.disposition.AS2DispositionException)2 WrappedAS2Exception (com.helger.as2lib.exception.WrappedAS2Exception)2