Search in sources :

Example 1 with SMIMECompressedParser

use of org.bouncycastle.mail.smime.SMIMECompressedParser in project as2-lib by phax.

the class AS2ReceiverHandler method decompress.

protected void decompress(@Nonnull final IMessage aMsg) throws AS2DispositionException {
    try {
        if (aMsg.partnership().isDisableDecompress()) {
            if (LOGGER.isInfoEnabled())
                LOGGER.info("Message claims to be compressed but decompression is disabled" + aMsg.getLoggingText());
        } else {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Decompressing a compressed AS2 message");
            MimeBodyPart aDecompressedPart;
            final ZlibExpanderProvider aExpander = new ZlibExpanderProvider();
            // Compress using stream
            if (LOGGER.isDebugEnabled()) {
                final StringBuilder aSB = new StringBuilder();
                aSB.append("Headers before uncompress\n");
                final MimeBodyPart part = aMsg.getData();
                final Enumeration<String> aHeaderLines = part.getAllHeaderLines();
                while (aHeaderLines.hasMoreElements()) {
                    aSB.append(aHeaderLines.nextElement()).append('\n');
                }
                aSB.append("done");
                LOGGER.debug(aSB.toString());
            }
            // The default buffer size in BufferedInputStream is 8192
            final SMIMECompressedParser aCompressedParser = new SMIMECompressedParser(aMsg.getData());
            // TODO: get buffer from configuration
            aDecompressedPart = SMIMEUtil.toMimeBodyPart(aCompressedParser.getContent(aExpander));
            // Update the message object
            aMsg.setData(aDecompressedPart);
            // Remember that message was decompressed
            aMsg.attrs().putIn(AS2Message.ATTRIBUTE_RECEIVED_COMPRESSED, true);
            if (LOGGER.isInfoEnabled())
                LOGGER.info("Successfully decompressed incoming AS2 message" + aMsg.getLoggingText());
        }
    } catch (final SMIMEException | CMSException | MessagingException ex) {
        if (LOGGER.isErrorEnabled())
            LOGGER.error("Error decompressing received message", ex);
        throw new AS2DispositionException(DispositionType.createError("unexpected-processing-error"), AbstractActiveNetModule.DISP_DECOMPRESSION_ERROR, ex);
    }
}
Also used : AS2DispositionException(com.helger.as2lib.disposition.AS2DispositionException) ZlibExpanderProvider(org.bouncycastle.cms.jcajce.ZlibExpanderProvider) MessagingException(javax.mail.MessagingException) SMIMEException(org.bouncycastle.mail.smime.SMIMEException) SMIMECompressedParser(org.bouncycastle.mail.smime.SMIMECompressedParser) MimeBodyPart(javax.mail.internet.MimeBodyPart) CMSException(org.bouncycastle.cms.CMSException)

Aggregations

AS2DispositionException (com.helger.as2lib.disposition.AS2DispositionException)1 MessagingException (javax.mail.MessagingException)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 CMSException (org.bouncycastle.cms.CMSException)1 ZlibExpanderProvider (org.bouncycastle.cms.jcajce.ZlibExpanderProvider)1 SMIMECompressedParser (org.bouncycastle.mail.smime.SMIMECompressedParser)1 SMIMEException (org.bouncycastle.mail.smime.SMIMEException)1