Search in sources :

Example 6 with AS2ResourceHelper

use of com.helger.as2lib.util.AS2ResourceHelper in project as2-lib by phax.

the class ReadMDNFuncTest method testReadMDN02.

@Test
public void testReadMDN02() throws Exception {
    String sPrefix = "mdn/4af6f84c-d882-4466-8e0c-305a7fbe37b3";
    sPrefix = "mdn/20190925-david";
    final IReadableResource aHeaderRes = new ClassPathResource(sPrefix + ".header");
    assertTrue(aHeaderRes.exists());
    final IReadableResource aPayloadRes = new ClassPathResource(sPrefix + ".payload");
    assertTrue(aPayloadRes.exists());
    final IReadableResource aCertRes = new ClassPathResource(sPrefix + ".pem");
    assertTrue(aCertRes.exists());
    final HttpHeaderMap aHeaders = new HttpHeaderMap();
    try (NonBlockingBufferedReader aBR = new NonBlockingBufferedReader(aHeaderRes.getReader(StandardCharsets.ISO_8859_1))) {
        String s;
        while ((s = aBR.readLine()) != null) {
            final int i = s.indexOf(':');
            final String sName = s.substring(0, i).trim();
            final String sValue = s.substring(i + 1).trim();
            aHeaders.addHeader(sName, sValue);
        }
    }
    if (false)
        assertEquals("<MOKOsi42435716cf621589dnode1POP000046@sfgt1.unix.fina.hr>", aHeaders.getFirstHeaderValue("Message-ID"));
    final X509Certificate aCert = CertificateHelper.convertStringToCertficateOrNull(StreamHelper.getAllBytesAsString(aCertRes, StandardCharsets.ISO_8859_1));
    assertNotNull(aCert);
    final AS2Message aMsg = new AS2Message();
    // Create a MessageMDN and copy HTTP headers
    final IMessageMDN aMDN = new AS2MessageMDN(aMsg);
    aMDN.headers().addAllHeaders(aHeaders);
    final MimeBodyPart aPart = new MimeBodyPart(AS2HttpHelper.getAsInternetHeaders(aMDN.headers()), StreamHelper.getAllBytes(aPayloadRes));
    assertNotNull(aPart);
    aMsg.getMDN().setData(aPart);
    final ICryptoHelper aCryptoHelper = AS2Helper.getCryptoHelper();
    assertTrue(aCryptoHelper.isSigned(aPart));
    assertFalse(aCryptoHelper.isEncrypted(aPart));
    assertFalse(aCryptoHelper.isCompressed(aPart.getContentType()));
    final Consumer<X509Certificate> aCertHolder = null;
    try (final AS2ResourceHelper aResHelper = new AS2ResourceHelper()) {
        AS2Helper.parseMDN(aMsg, aCert, true, aCertHolder, aResHelper);
        fail();
    } catch (final CertificateExpiredException ex) {
        // expected to fail
        if (false)
            ex.printStackTrace();
    }
}
Also used : AS2MessageMDN(com.helger.as2lib.message.AS2MessageMDN) CertificateExpiredException(java.security.cert.CertificateExpiredException) IReadableResource(com.helger.commons.io.resource.IReadableResource) ICryptoHelper(com.helger.as2lib.crypto.ICryptoHelper) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) X509Certificate(java.security.cert.X509Certificate) AS2ResourceHelper(com.helger.as2lib.util.AS2ResourceHelper) HttpHeaderMap(com.helger.commons.http.HttpHeaderMap) AS2Message(com.helger.as2lib.message.AS2Message) IMessageMDN(com.helger.as2lib.message.IMessageMDN) MimeBodyPart(javax.mail.internet.MimeBodyPart) NonBlockingBufferedReader(com.helger.commons.io.stream.NonBlockingBufferedReader) Test(org.junit.Test)

Aggregations

AS2ResourceHelper (com.helger.as2lib.util.AS2ResourceHelper)6 MimeBodyPart (javax.mail.internet.MimeBodyPart)5 AS2Exception (com.helger.as2lib.exception.AS2Exception)4 WrappedAS2Exception (com.helger.as2lib.exception.WrappedAS2Exception)4 AS2Message (com.helger.as2lib.message.AS2Message)4 AS2NoModuleException (com.helger.as2lib.processor.AS2NoModuleException)4 AS2ComponentNotFoundException (com.helger.as2lib.session.AS2ComponentNotFoundException)4 IOException (java.io.IOException)4 MessagingException (javax.mail.MessagingException)4 ICryptoHelper (com.helger.as2lib.crypto.ICryptoHelper)3 AS2DispositionException (com.helger.as2lib.disposition.AS2DispositionException)2 AS2MessageMDN (com.helger.as2lib.message.AS2MessageMDN)2 IMessageMDN (com.helger.as2lib.message.IMessageMDN)2 IHTTPOutgoingDumper (com.helger.as2lib.util.dump.IHTTPOutgoingDumper)2 HttpHeaderMap (com.helger.commons.http.HttpHeaderMap)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)2 IReadableResource (com.helger.commons.io.resource.IReadableResource)2 NonBlockingBufferedReader (com.helger.commons.io.stream.NonBlockingBufferedReader)2 X509Certificate (java.security.cert.X509Certificate)2 SMIMEException (org.bouncycastle.mail.smime.SMIMEException)2