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);
}
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);
}
}
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));
}
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));
}
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));
}
Aggregations