Search in sources :

Example 1 with TempSharedFileInputStream

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

the class AS2ReceiverHandler method handleIncomingMessage.

/**
 * This method can be used to handle an incoming HTTP message AFTER the
 * headers where extracted.
 *
 * @param sClientInfo
 *        Client connection info
 * @param aMsgData
 *        The message body
 * @param aMsg
 *        The AS2 message that will be filled by this method
 * @param aResponseHandler
 *        The response handler which handles HTTP error messages as well as
 *        synchronous MDN.
 */
public void handleIncomingMessage(@Nonnull final String sClientInfo, @Nonnull final DataSource aMsgData, @Nonnull final AS2Message aMsg, @Nonnull final IAS2HttpResponseHandler aResponseHandler) {
    // handler, to avoid the decrypted content gets removed (see issue #135)
    try (final AS2ResourceHelper aResHelper = new AS2ResourceHelper()) {
        try {
            final IAS2Session aSession = m_aReceiverModule.getSession();
            try {
                // Put received data in a MIME body part
                final String sReceivedContentType = AS2HttpHelper.getCleanContentType(aMsg.getHeader(CHttpHeader.CONTENT_TYPE));
                final MimeBodyPart aReceivedPart = new MimeBodyPart();
                aReceivedPart.setDataHandler(new DataHandler(aMsgData));
                // Header must be set AFTER the DataHandler!
                aReceivedPart.setHeader(CHttpHeader.CONTENT_TYPE, sReceivedContentType);
                aMsg.setData(aReceivedPart);
            } catch (final Exception ex) {
                throw new AS2DispositionException(DispositionType.createError("unexpected-processing-error"), AbstractActiveNetModule.DISP_PARSING_MIME_FAILED, ex);
            }
            // update the message
            try {
                final String sAS2From = aMsg.getAS2From();
                aMsg.partnership().setSenderAS2ID(sAS2From);
                final String sAS2To = aMsg.getAS2To();
                aMsg.partnership().setReceiverAS2ID(sAS2To);
                // Fill all partnership attributes etc.
                aSession.getPartnershipFactory().updatePartnership(aMsg, false);
            } catch (final AS2Exception ex) {
                throw AS2DispositionException.wrap(ex, () -> DispositionType.createError("authentication-failed"), () -> AbstractActiveNetModule.DISP_PARTNERSHIP_NOT_FOUND);
            }
            // Per RFC5402 compression is always before encryption but can be before
            // or after signing of message but only in one place
            final ICryptoHelper aCryptoHelper = AS2Helper.getCryptoHelper();
            boolean bIsDecompressed = false;
            // Decrypt and verify signature of the data, and attach data to the
            // message
            decrypt(aMsg, aResHelper);
            if (aCryptoHelper.isCompressed(aMsg.getContentType())) {
                if (LOGGER.isTraceEnabled())
                    LOGGER.trace("Decompressing received message before checking signature...");
                decompress(aMsg);
                bIsDecompressed = true;
            }
            verify(aMsg, aResHelper);
            if (aCryptoHelper.isCompressed(aMsg.getContentType())) {
                // or after signing of message but only in one place
                if (bIsDecompressed) {
                    throw new AS2DispositionException(DispositionType.createError("decompression-failed"), AbstractActiveNetModule.DISP_DECOMPRESSION_ERROR, new Exception("Message has already been decompressed. Per RFC5402 it cannot occur twice."));
                }
                if (LOGGER.isTraceEnabled())
                    if (aMsg.attrs().containsKey(AS2Message.ATTRIBUTE_RECEIVED_SIGNED))
                        LOGGER.trace("Decompressing received message after verifying signature...");
                    else
                        LOGGER.trace("Decompressing received message after decryption...");
                decompress(aMsg);
                bIsDecompressed = true;
            }
            if (LOGGER.isTraceEnabled())
                try {
                    LOGGER.trace("SMIME Decrypted Content-Disposition: " + aMsg.getContentDisposition() + "\n      Content-Type received: " + aMsg.getContentType() + "\n      HEADERS after decryption: " + aMsg.getData().getAllHeaders() + "\n      Content-Disposition in MSG detData() MIMEPART after decryption: " + aMsg.getData().getContentType());
                } catch (final MessagingException ex) {
                    if (LOGGER.isErrorEnabled())
                        LOGGER.error("Failed to trace message: " + aMsg, ex);
                }
            // Validate the received message before storing
            try {
                aSession.getMessageProcessor().handle(IProcessorStorageModule.DO_VALIDATE_BEFORE_STORE, aMsg, null);
            } catch (final AS2NoModuleException ex) {
            // No module installed - ignore
            } catch (final AS2Exception ex) {
                // Issue 90 - use CRLF as separator
                throw AS2DispositionException.wrap(ex, () -> DispositionType.createError("unexpected-processing-error"), () -> StringHelper.getConcatenatedOnDemand(AbstractActiveNetModule.DISP_VALIDATION_FAILED, CHttp.EOL, _getDispositionText(ex)));
            }
            // Store the received message
            try {
                aSession.getMessageProcessor().handle(IProcessorStorageModule.DO_STORE, aMsg, null);
            } catch (final AS2NoModuleException ex) {
            // No module installed - ignore
            } catch (final AS2Exception ex) {
                // Issue 90 - use CRLF as separator
                throw AS2DispositionException.wrap(ex, () -> DispositionType.createError("unexpected-processing-error"), () -> StringHelper.getConcatenatedOnDemand(AbstractActiveNetModule.DISP_STORAGE_FAILED, CHttp.EOL, _getDispositionText(ex)));
            }
            // Validate the received message after storing
            try {
                aSession.getMessageProcessor().handle(IProcessorStorageModule.DO_VALIDATE_AFTER_STORE, aMsg, null);
            } catch (final AS2NoModuleException ex) {
            // No module installed - ignore
            } catch (final AS2Exception ex) {
                // Issue 90 - use CRLF as separator
                throw AS2DispositionException.wrap(ex, () -> DispositionType.createError("unexpected-processing-error"), () -> StringHelper.getConcatenatedOnDemand(AbstractActiveNetModule.DISP_VALIDATION_FAILED, CHttp.EOL, _getDispositionText(ex)));
            }
            try {
                if (aMsg.isRequestingMDN()) {
                    if (LOGGER.isTraceEnabled())
                        LOGGER.trace("AS2 message is requesting an MDN");
                    // Transmit a success MDN if requested
                    sendMDN(sClientInfo, aResponseHandler, aMsg, DispositionType.createSuccess(), AbstractActiveNetModule.DISP_SUCCESS, ESuccess.SUCCESS);
                } else {
                    if (LOGGER.isTraceEnabled())
                        LOGGER.trace("AS2 message is not requesting an MDN - just sending HTTP 200 (OK)");
                    // Just send a HTTP OK
                    HTTPHelper.sendSimpleHTTPResponse(aResponseHandler, CHttp.HTTP_OK);
                    if (LOGGER.isInfoEnabled())
                        LOGGER.info("sent HTTP OK " + sClientInfo + aMsg.getLoggingText());
                }
            } catch (final Exception ex) {
                throw new AS2Exception("Error creating and returning MDN, message was stilled processed", ex);
            }
        } catch (final AS2DispositionException ex) {
            sendMDN(sClientInfo, aResponseHandler, aMsg, ex.getDisposition(), ex.getText(), ESuccess.FAILURE);
            m_aReceiverModule.handleError(aMsg, ex);
        } catch (final AS2Exception ex) {
            m_aReceiverModule.handleError(aMsg, ex);
        } finally {
            // close the temporary shared stream if it exists
            final TempSharedFileInputStream sis = aMsg.getTempSharedFileInputStream();
            if (null != sis) {
                try {
                    sis.closeAll();
                } catch (final IOException e) {
                    LOGGER.error("Exception while closing TempSharedFileInputStream", e);
                }
            }
        }
    }
}
Also used : MessagingException(javax.mail.MessagingException) TempSharedFileInputStream(com.helger.as2lib.util.http.TempSharedFileInputStream) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ICryptoHelper(com.helger.as2lib.crypto.ICryptoHelper) MessagingException(javax.mail.MessagingException) AS2NoModuleException(com.helger.as2lib.processor.AS2NoModuleException) AS2DispositionException(com.helger.as2lib.disposition.AS2DispositionException) AS2ProcessorException(com.helger.as2lib.processor.AS2ProcessorException) CMSException(org.bouncycastle.cms.CMSException) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) IOException(java.io.IOException) SMIMEException(org.bouncycastle.mail.smime.SMIMEException) AS2ComponentNotFoundException(com.helger.as2lib.session.AS2ComponentNotFoundException) IAS2Session(com.helger.as2lib.session.IAS2Session) AS2ResourceHelper(com.helger.as2lib.util.AS2ResourceHelper) AS2DispositionException(com.helger.as2lib.disposition.AS2DispositionException) AS2NoModuleException(com.helger.as2lib.processor.AS2NoModuleException) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

ICryptoHelper (com.helger.as2lib.crypto.ICryptoHelper)1 AS2DispositionException (com.helger.as2lib.disposition.AS2DispositionException)1 AS2Exception (com.helger.as2lib.exception.AS2Exception)1 WrappedAS2Exception (com.helger.as2lib.exception.WrappedAS2Exception)1 AS2NoModuleException (com.helger.as2lib.processor.AS2NoModuleException)1 AS2ProcessorException (com.helger.as2lib.processor.AS2ProcessorException)1 AS2ComponentNotFoundException (com.helger.as2lib.session.AS2ComponentNotFoundException)1 IAS2Session (com.helger.as2lib.session.IAS2Session)1 AS2ResourceHelper (com.helger.as2lib.util.AS2ResourceHelper)1 TempSharedFileInputStream (com.helger.as2lib.util.http.TempSharedFileInputStream)1 IOException (java.io.IOException)1 DataHandler (javax.activation.DataHandler)1 MessagingException (javax.mail.MessagingException)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 CMSException (org.bouncycastle.cms.CMSException)1 SMIMEException (org.bouncycastle.mail.smime.SMIMEException)1