Search in sources :

Example 11 with NonBlockingByteArrayOutputStream

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

the class BCCryptoHelperTest method testSign_QuotedPrintable.

@Test
public void testSign_QuotedPrintable() throws Exception {
    final MimeBodyPart aPart = new MimeBodyPart();
    aPart.setText("Hello world", StandardCharsets.ISO_8859_1.name());
    final MimeBodyPart aSigned = AS2Helper.getCryptoHelper().sign(aPart, (X509Certificate) PKE.getCertificate(), PKE.getPrivateKey(), ECryptoAlgorithmSign.DIGEST_SHA_256, false, false, false, EContentTransferEncoding.QUOTED_PRINTABLE);
    assertNotNull(aSigned);
    final String sBoundary = AS2HttpHelper.parseContentType(aSigned.getContentType()).getParameter("boundary");
    assertNotNull(sBoundary);
    final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream();
    aSigned.writeTo(aBAOS);
    final String sExpectedStart = "Content-Type: multipart/signed; protocol=\"application/pkcs7-signature\"; micalg=sha-256; \r\n" + "\tboundary=\"" + sBoundary + "\"\r\n" + "\r\n" + "--" + sBoundary + "\r\n" + "Content-Type: text/plain; charset=ISO-8859-1\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "Hello world\r\n" + "--" + sBoundary + "\r\n" + "Content-Type: application/pkcs7-signature; name=smime.p7s; smime-type=signed-data\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n" + "Content-Disposition: attachment; filename=\"smime.p7s\"\r\n" + "Content-Description: S/MIME Cryptographic Signature\r\n" + "\r\n" + "0=80=06=09*=86H=86=F7\r\n" + "=01=07=02=A0=800=80=02=01=011\r\n" + "0=0B=06=09`=86H=01e=03=04=02=010=80=06=09*=86H=86=F7\r\n" + "=01=07=01=00=001=82=02=A40=82=02=A0=02=01=010=81=C30=81=BA1#0!=06=09*=86H=\r\n" + "=86=F7\r\n" + "=01=09=01=16=14service@mendelson.de1=0B0=09=06=03U=04=06=13=02DE1=0F0\r\n" + "=06=03U=04=08=0C=06Berlin1=0F0\r\n" + "=06=03U=04=07=0C=06Berlin1\"0 =06=03U=04\r\n" + "=0C=19mendelson-e-commerce GmbH1!0=1F=06=03U=04=0B=0C=18Do not use in produ=\r\n" + "ction1=1D0=1B=06=03U=04=03=0C=14mendelson test key 3=02=04Z*[=1C0=0B=06=09`=\r\n" + "=86H=01e=03=04=02=01=A0=81=B40=18=06=09*=86H=86=F7\r\n" + "=01=09=031=0B=06=09*=86H=86=F7\r\n" + "=01=07=010=1C=06=09*=86H=86=F7\r\n" + "=01=09=051=0F=17\r\n";
    final String sExpectedEnd = "\r\n" + "--" + sBoundary + "--\r\n";
    final String sReal = aBAOS.getAsString(StandardCharsets.ISO_8859_1);
    // assertEquals (sReal, sExpectedStart);
    assertTrue(sReal.startsWith(sExpectedStart));
    assertTrue(sReal.endsWith(sExpectedEnd));
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 12 with NonBlockingByteArrayOutputStream

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

the class BCCryptoHelperTest method testSign_Base64.

@Test
public void testSign_Base64() throws Exception {
    final MimeBodyPart aPart = new MimeBodyPart();
    aPart.setText("Hello world", StandardCharsets.ISO_8859_1.name());
    final MimeBodyPart aSigned = AS2Helper.getCryptoHelper().sign(aPart, (X509Certificate) PKE.getCertificate(), PKE.getPrivateKey(), ECryptoAlgorithmSign.DIGEST_SHA_256, false, false, false, EContentTransferEncoding.BASE64);
    assertNotNull(aSigned);
    final String sBoundary = AS2HttpHelper.parseContentType(aSigned.getContentType()).getParameter("boundary");
    assertNotNull(sBoundary);
    final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream();
    aSigned.writeTo(aBAOS);
    final String sExpectedStart = "Content-Type: multipart/signed; protocol=\"application/pkcs7-signature\"; micalg=sha-256; \r\n" + "\tboundary=\"" + sBoundary + "\"\r\n" + "\r\n" + "--" + sBoundary + "\r\n" + "Content-Type: text/plain; charset=ISO-8859-1\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "Hello world\r\n" + "--" + sBoundary + "\r\n" + "Content-Type: application/pkcs7-signature; name=smime.p7s; smime-type=signed-data\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-Disposition: attachment; filename=\"smime.p7s\"\r\n" + "Content-Description: S/MIME Cryptographic Signature\r\n" + "\r\n" + "MIAGCSqGSIb3DQEHAqCAMIACAQExDTALBglghkgBZQMEAgEwgAYJKoZIhvcNAQcBAAAxggKkMIIC\r\n" + "oAIBATCBwzCBujEjMCEGCSqGSIb3DQEJARYUc2VydmljZUBtZW5kZWxzb24uZGUxCzAJBgNVBAYT\r\n" + "AkRFMQ8wDQYDVQQIDAZCZXJsaW4xDzANBgNVBAcMBkJlcmxpbjEiMCAGA1UECgwZbWVuZGVsc29u\r\n" + "LWUtY29tbWVyY2UgR21iSDEhMB8GA1UECwwYRG8gbm90IHVzZSBpbiBwcm9kdWN0aW9uMR0wGwYD\r\n" + "VQQDDBRtZW5kZWxzb24gdGVzdCBrZXkgMwIEWipbHDALBglghkgBZQMEAgGggbQwGAYJKoZIhvcN\r\n";
    final String sExpectedEnd = "\r\n" + "--" + sBoundary + "--\r\n";
    final String sReal = aBAOS.getAsString(StandardCharsets.ISO_8859_1);
    assertTrue(sReal.startsWith(sExpectedStart));
    assertTrue(sReal.endsWith(sExpectedEnd));
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 13 with NonBlockingByteArrayOutputStream

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

the class BCCryptoHelperTest method testEncryptCTE_QuotedPrintable.

@Test
public void testEncryptCTE_QuotedPrintable() throws Exception {
    final MimeBodyPart aPart = new MimeBodyPart();
    aPart.setText("Hello world");
    final MimeBodyPart aEncrypted = AS2Helper.getCryptoHelper().encrypt(aPart, CERT_ENCRYPT, ECryptoAlgorithmCrypt.CRYPT_3DES, EContentTransferEncoding.QUOTED_PRINTABLE);
    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: quoted-printable\r\n" + "Content-Disposition: attachment; filename=\"smime.p7m\"\r\n" + "Content-Description: S/MIME Encrypted Message\r\n" + "\r\n" + "0=80=06=09*=86H=86=F7\r\n" + "=01=07=03=A0=800=80=02=01=001=82=01=E00=82=01=DC=02=01=000=81=C30=81=BA1#0!=\r\n" + "=06=09*=86H=86=F7\r\n" + "=01=09=01=16=14service@mendelson.de1=0B0=09=06=03U=04=06=13=02DE1=0F0\r\n" + "=06=03U=04=08=0C=06Berlin1=0F0\r\n" + "=06=03U=04=07=0C=06Berlin1\"0 =06=03U=04\r\n" + "=0C=19mendelson-e-commerce GmbH1!0=1F=06=03U=04=0B=0C=18Do not use in produ=\r\n" + "ction1=1D0=1B=06=03U=04=03=0C=14mendelson test key 4=02=04Z*[=C80\r\n" + "=06=09*=86H=86=F7\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)

Example 14 with NonBlockingByteArrayOutputStream

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

the class BCCryptoHelperTest method testEncryptCTE_Binary.

@Test
public void testEncryptCTE_Binary() throws Exception {
    final MimeBodyPart aPart = new MimeBodyPart();
    aPart.setText("Hello world");
    final MimeBodyPart aEncrypted = AS2Helper.getCryptoHelper().encrypt(aPart, CERT_ENCRYPT, ECryptoAlgorithmCrypt.CRYPT_3DES, EContentTransferEncoding.BINARY);
    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: binary\r\n" + "Content-Disposition: attachment; filename=\"smime.p7m\"\r\n" + "Content-Description: S/MIME Encrypted Message\r\n" + "\r\n";
    final String sReal = aBAOS.getAsString(StandardCharsets.ISO_8859_1);
    assertTrue(sReal.startsWith(sExpectedStart));
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 15 with NonBlockingByteArrayOutputStream

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

the class HTTPHelper method sendSimpleHTTPResponse.

/**
 * Send a simple HTTP response that only contains the HTTP status code and the
 * respective descriptive text. An empty header map us used.
 *
 * @param aResponseHandler
 *        The response handler to be used.
 * @param nResponseCode
 *        The HTTP response code to use.
 * @throws IOException
 *         In case sending fails for whatever reason
 */
public static void sendSimpleHTTPResponse(@Nonnull final IAS2HttpResponseHandler aResponseHandler, @Nonnegative final int nResponseCode) throws IOException {
    try (final NonBlockingByteArrayOutputStream aData = new NonBlockingByteArrayOutputStream()) {
        final String sHTTPLine = Integer.toString(nResponseCode) + " " + CHttp.getHttpResponseMessage(nResponseCode) + CHttp.EOL;
        aData.write(sHTTPLine.getBytes(CHttp.HTTP_CHARSET));
        aResponseHandler.sendHttpResponse(nResponseCode, new HttpHeaderMap(), aData);
    }
}
Also used : HttpHeaderMap(com.helger.commons.http.HttpHeaderMap) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)

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