Search in sources :

Example 1 with ByteArrayDataSource

use of com.helger.mail.datasource.ByteArrayDataSource in project as2-lib by phax.

the class AS2MDNReceiverHandler method handleIncomingMessage.

public void handleIncomingMessage(@Nonnull @Nonempty final String sClientInfo, @Nonnull final DataSource aMsgData, @Nonnull final AS2Message aMsg, @Nonnull final IAS2HttpResponseHandler aResponseHandler) {
    // Read in the message request, headers, and data
    try (final AS2ResourceHelper aResHelper = new AS2ResourceHelper()) {
        final byte[] aData = StreamHelper.getAllBytes(aMsgData.getInputStream());
        // Asynch MDN 2007-03-12
        // check if the requested URL is defined in attribute "as2_receipt_option"
        // in one of partnerships, if yes, then process incoming AsyncMDN
        final String sReceivedContentType = AS2HttpHelper.getCleanContentType(aMsg.getHeader(CHttpHeader.CONTENT_TYPE));
        final MimeBodyPart aReceivedPart = new MimeBodyPart(AS2HttpHelper.getAsInternetHeaders(aMsg.headers()), aData);
        aMsg.setData(aReceivedPart);
        // MimeBodyPart receivedPart = new MimeBodyPart();
        aReceivedPart.setDataHandler(new ByteArrayDataSource(aData, sReceivedContentType, null).getAsDataHandler());
        // Must be set AFTER the DataHandler!
        aReceivedPart.setHeader(CHttpHeader.CONTENT_TYPE, sReceivedContentType);
        aMsg.setData(aReceivedPart);
        receiveMDN(aMsg, aData, aResponseHandler, aResHelper);
        if (LOGGER.isInfoEnabled())
            LOGGER.info("Received async MDN " + sClientInfo + aMsg.getLoggingText());
    } catch (final Exception ex) {
        m_aReceiverModule.handleError(aMsg, WrappedAS2Exception.wrap(ex));
    }
}
Also used : AS2ResourceHelper(com.helger.as2lib.util.AS2ResourceHelper) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(com.helger.mail.datasource.ByteArrayDataSource) MessagingException(javax.mail.MessagingException) AS2NoModuleException(com.helger.as2lib.processor.AS2NoModuleException) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) IOException(java.io.IOException) AS2ComponentNotFoundException(com.helger.as2lib.session.AS2ComponentNotFoundException)

Example 2 with ByteArrayDataSource

use of com.helger.mail.datasource.ByteArrayDataSource in project as2-lib by phax.

the class AS2MDNReceiverHandler method handle.

public void handle(@Nonnull final AbstractActiveNetModule aOwner, @Nonnull final Socket aSocket) {
    final String sClientInfo = getClientInfo(aSocket);
    if (LOGGER.isInfoEnabled())
        LOGGER.info("incoming connection for receiving AsyncMDN [" + sClientInfo + "]");
    final AS2Message aMsg = new AS2Message();
    final boolean bQuoteHeaderValues = m_aReceiverModule.isQuoteHeaderValues();
    final IAS2HttpResponseHandler aResponseHandler = new AS2HttpResponseHandlerSocket(aSocket, bQuoteHeaderValues);
    // Time the transmission
    final StopWatch aSW = StopWatch.createdStarted();
    DataSource aMdnDataSource = null;
    try {
        // Read in the message request, headers, and data
        final IHTTPIncomingDumper aIncomingDumper = getEffectiveHttpIncomingDumper();
        aMdnDataSource = HTTPHelper.readAndDecodeHttpRequest(new AS2HttpRequestDataProviderInputStream(aSocket.getInputStream()), aResponseHandler, aMsg, aIncomingDumper);
    } catch (final Exception ex) {
        new AS2NetException(aSocket.getInetAddress(), aSocket.getPort(), ex).terminate();
    }
    aSW.stop();
    if (aMdnDataSource == null) {
        LOGGER.error("Not having a data source to operate on");
    } else {
        if (aMdnDataSource instanceof ByteArrayDataSource) {
            if (LOGGER.isInfoEnabled())
                LOGGER.info("received " + AS2IOHelper.getTransferRate(((ByteArrayDataSource) aMdnDataSource).directGetBytes().length, aSW) + " from " + sClientInfo + aMsg.getLoggingText());
        } else {
            LOGGER.info("received message from " + sClientInfo + aMsg.getLoggingText() + " in " + aSW.getMillis() + " ms");
        }
        handleIncomingMessage(sClientInfo, aMdnDataSource, aMsg, aResponseHandler);
    }
}
Also used : AS2HttpResponseHandlerSocket(com.helger.as2lib.util.http.AS2HttpResponseHandlerSocket) AS2Message(com.helger.as2lib.message.AS2Message) IAS2HttpResponseHandler(com.helger.as2lib.util.http.IAS2HttpResponseHandler) AS2HttpRequestDataProviderInputStream(com.helger.as2lib.util.http.AS2HttpRequestDataProviderInputStream) ByteArrayDataSource(com.helger.mail.datasource.ByteArrayDataSource) MessagingException(javax.mail.MessagingException) AS2NoModuleException(com.helger.as2lib.processor.AS2NoModuleException) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) IOException(java.io.IOException) AS2ComponentNotFoundException(com.helger.as2lib.session.AS2ComponentNotFoundException) StopWatch(com.helger.commons.timing.StopWatch) ByteArrayDataSource(com.helger.mail.datasource.ByteArrayDataSource) DataSource(javax.activation.DataSource) IHTTPIncomingDumper(com.helger.as2lib.util.dump.IHTTPIncomingDumper)

Example 3 with ByteArrayDataSource

use of com.helger.mail.datasource.ByteArrayDataSource in project as2-lib by phax.

the class AbstractAS2ReceiveBaseXServletHandler method onRequest.

public final void onRequest(@Nonnull final HttpServletRequest aHttpRequest, @Nonnull final HttpServletResponse aHttpResponse, @Nonnull final EHttpVersion eHttpVersion, @Nonnull final EHttpMethod eHttpMethod, @Nonnull final IRequestWebScope aRequestScope) throws ServletException, IOException {
    // Handle the incoming message, and return the MDN if necessary
    final String sClientInfo = aHttpRequest.getRemoteAddr() + ":" + aHttpRequest.getRemotePort();
    LOGGER.info("Starting to handle incoming AS2 request - " + sClientInfo);
    // Create empty message
    final AS2Message aMsg = new AS2Message();
    aMsg.attrs().putIn(CNetAttribute.MA_SOURCE_IP, aHttpRequest.getRemoteAddr());
    aMsg.attrs().putIn(CNetAttribute.MA_SOURCE_PORT, aHttpRequest.getRemotePort());
    aMsg.attrs().putIn(CNetAttribute.MA_DESTINATION_IP, aHttpRequest.getLocalAddr());
    aMsg.attrs().putIn(CNetAttribute.MA_DESTINATION_PORT, aHttpRequest.getLocalPort());
    // Request type (e.g. "POST")
    aMsg.attrs().putIn(HTTPHelper.MA_HTTP_REQ_TYPE, aHttpRequest.getMethod());
    // Request URL (e.g. "/as2")
    aMsg.attrs().putIn(HTTPHelper.MA_HTTP_REQ_URL, ServletHelper.getRequestRequestURI(aHttpRequest));
    // Add all request headers to the AS2 message
    aMsg.headers().setAllHeaders(aRequestScope.headers());
    // Build the handler that performs the response handling
    final boolean bQuoteHeaderValues = isQuoteHeaderValues();
    final AS2OutputStreamCreatorHttpServletResponse aResponseHandler = new AS2OutputStreamCreatorHttpServletResponse(aHttpResponse, bQuoteHeaderValues);
    // Read the S/MIME content in a byte array - memory!
    // Chunked encoding was already handled, so read "as-is"
    final long nContentLength = aHttpRequest.getContentLengthLong();
    if (nContentLength > Integer.MAX_VALUE)
        throw new IllegalStateException("Currently only payload with up to 2GB can be handled! This request has " + nContentLength + " bytes.");
    // Open it once, and close it at the end
    try (final ServletInputStream aRequestIS = aHttpRequest.getInputStream()) {
        // Time the transmission
        final StopWatch aSW = StopWatch.createdStarted();
        DataSource aMsgDataSource = null;
        try {
            // Read in the message request, headers, and data
            final IHTTPIncomingDumper aIncomingDumper = getEffectiveHttpIncomingDumper();
            aMsgDataSource = HTTPHelper.readAndDecodeHttpRequest(new AS2HttpRequestDataProviderServletRequest(aRequestScope, aRequestIS), aResponseHandler, aMsg, aIncomingDumper);
        } catch (final Exception ex) {
            AS2Exception.log(ex.getClass(), true, "Failed to read Servlet Request: " + ex.getMessage(), null, null, ex.getCause());
        }
        aSW.stop();
        if (aMsgDataSource == null) {
            LOGGER.error("Not having a data source to operate on");
        } else {
            if (aMsgDataSource instanceof ByteArrayDataSource) {
                if (LOGGER.isInfoEnabled())
                    LOGGER.info("received " + AS2IOHelper.getTransferRate(((ByteArrayDataSource) aMsgDataSource).directGetBytes().length, aSW) + " from " + sClientInfo + aMsg.getLoggingText());
            } else {
                LOGGER.info("received message from " + sClientInfo + aMsg.getLoggingText() + " in " + aSW.getMillis() + " ms");
            }
            handleIncomingMessage(sClientInfo, aMsgDataSource, aMsg, aResponseHandler);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) AS2Exception(com.helger.as2lib.exception.AS2Exception) IOException(java.io.IOException) StopWatch(com.helger.commons.timing.StopWatch) ByteArrayDataSource(com.helger.mail.datasource.ByteArrayDataSource) DataSource(javax.activation.DataSource) IHTTPIncomingDumper(com.helger.as2lib.util.dump.IHTTPIncomingDumper) AS2OutputStreamCreatorHttpServletResponse(com.helger.as2servlet.util.AS2OutputStreamCreatorHttpServletResponse) AS2Message(com.helger.as2lib.message.AS2Message) ServletInputStream(javax.servlet.ServletInputStream) ByteArrayDataSource(com.helger.mail.datasource.ByteArrayDataSource)

Example 4 with ByteArrayDataSource

use of com.helger.mail.datasource.ByteArrayDataSource in project as2-lib by phax.

the class AS2ReceiverHandler method handle.

public void handle(@Nonnull final AbstractActiveNetModule aOwner, @Nonnull final Socket aSocket) {
    final String sClientInfo = getClientInfo(aSocket);
    if (LOGGER.isInfoEnabled())
        LOGGER.info("Incoming connection " + sClientInfo);
    final AS2Message aMsg = createMessage(aSocket);
    final boolean bQuoteHeaderValues = m_aReceiverModule.isQuoteHeaderValues();
    final IAS2HttpResponseHandler aResponseHandler = new AS2HttpResponseHandlerSocket(aSocket, bQuoteHeaderValues);
    // Time the transmission
    final StopWatch aSW = StopWatch.createdStarted();
    DataSource aMsgDataSource = null;
    try {
        // Read in the message request, headers, and data
        final IHTTPIncomingDumper aIncomingDumper = getEffectiveHttpIncomingDumper();
        aMsgDataSource = HTTPHelper.readAndDecodeHttpRequest(new AS2HttpRequestDataProviderInputStream(aSocket.getInputStream()), aResponseHandler, aMsg, aIncomingDumper);
    } catch (final Exception ex) {
        new AS2NetException(aSocket.getInetAddress(), aSocket.getPort(), ex).terminate();
    }
    aSW.stop();
    if (aMsgDataSource == null) {
        LOGGER.error("Not having a data source to operate on");
    } else {
        if (aMsgDataSource instanceof ByteArrayDataSource) {
            if (LOGGER.isInfoEnabled())
                LOGGER.info("received " + AS2IOHelper.getTransferRate(((ByteArrayDataSource) aMsgDataSource).directGetBytes().length, aSW) + " from " + sClientInfo + aMsg.getLoggingText());
        } else {
            LOGGER.info("received message from " + sClientInfo + aMsg.getLoggingText() + " in " + aSW.getMillis() + " ms");
        }
        handleIncomingMessage(sClientInfo, aMsgDataSource, aMsg, aResponseHandler);
    }
}
Also used : AS2HttpResponseHandlerSocket(com.helger.as2lib.util.http.AS2HttpResponseHandlerSocket) AS2Message(com.helger.as2lib.message.AS2Message) IAS2HttpResponseHandler(com.helger.as2lib.util.http.IAS2HttpResponseHandler) AS2HttpRequestDataProviderInputStream(com.helger.as2lib.util.http.AS2HttpRequestDataProviderInputStream) ByteArrayDataSource(com.helger.mail.datasource.ByteArrayDataSource) 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) StopWatch(com.helger.commons.timing.StopWatch) ByteArrayDataSource(com.helger.mail.datasource.ByteArrayDataSource) DataSource(javax.activation.DataSource) IHTTPIncomingDumper(com.helger.as2lib.util.dump.IHTTPIncomingDumper)

Example 5 with ByteArrayDataSource

use of com.helger.mail.datasource.ByteArrayDataSource in project as2-lib by phax.

the class AbstractDirectoryPollingModule method updateMessage.

public void updateMessage(@Nonnull final IMessage aMsg, @Nonnull final File aFile) throws AS2Exception {
    final MessageParameters aParams = new MessageParameters(aMsg);
    final String sDefaults = attrs().getAsString(ATTR_DEFAULTS);
    if (sDefaults != null)
        aParams.setParameters(sDefaults);
    final String sFilename = aFile.getName();
    final String sFormat = attrs().getAsString(ATTR_FORMAT);
    if (sFormat != null) {
        final String sDelimiters = attrs().getAsString(ATTR_DELIMITERS, ".-");
        aParams.setParameters(sFormat, sDelimiters, sFilename);
    }
    try {
        final byte[] aData = SimpleFileIO.getAllFileBytes(aFile);
        String sContentType = attrs().getAsString(ATTR_MIMETYPE);
        if (sContentType == null) {
            // Default to application/octet-stream
            sContentType = CMimeType.APPLICATION_OCTET_STREAM.getAsString();
        } else {
            try {
                sContentType = aParams.format(sContentType);
            } catch (final AS2InvalidParameterException ex) {
                LOGGER.error("Bad content-type '" + sContentType + "'" + aMsg.getLoggingText());
                // Default to application/octet-stream
                sContentType = CMimeType.APPLICATION_OCTET_STREAM.getAsString();
            }
        }
        final ByteArrayDataSource aByteSource = new ByteArrayDataSource(aData, sContentType, null);
        final MimeBodyPart aBody = new MimeBodyPart();
        aBody.setDataHandler(aByteSource.getAsDataHandler());
        // Headers must be set AFTER the DataHandler
        final String sCTE = aMsg.partnership().getContentTransferEncodingSend(EContentTransferEncoding.AS2_DEFAULT.getID());
        aBody.setHeader(CHttpHeader.CONTENT_TRANSFER_ENCODING, sCTE);
        // below statement is not filename related, just want to make it
        // consist with the parameter "mimetype="application/EDI-X12""
        // defined in config.xml 2007-06-01
        aBody.setHeader(CHttpHeader.CONTENT_TYPE, sContentType);
        // add below statement will tell the receiver to save the filename
        // as the one sent by sender. 2007-06-01
        final String sSendFilename = attrs().getAsString(ATTR_SENDFILENAME);
        if ("true".equals(sSendFilename)) {
            final String sMAFilename = aMsg.attrs().getAsString(CFileAttribute.MA_FILENAME);
            final String sContentDisposition = "Attachment; filename=\"" + sMAFilename + "\"";
            aBody.setHeader(CHttpHeader.CONTENT_DISPOSITION, sContentDisposition);
            aMsg.setContentDisposition(sContentDisposition);
        }
        aMsg.setData(aBody);
    } catch (final MessagingException ex) {
        throw WrappedAS2Exception.wrap(ex);
    }
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Updating partnership for AS2 message" + aMsg.getLoggingText());
    // update the message's partnership with any stored information
    getSession().getPartnershipFactory().updatePartnership(aMsg, true);
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Finished updating partnership for AS2 message");
    aMsg.updateMessageID();
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Updated message ID to " + aMsg.getMessageID());
}
Also used : AS2InvalidParameterException(com.helger.as2lib.params.AS2InvalidParameterException) MessagingException(javax.mail.MessagingException) MessageParameters(com.helger.as2lib.params.MessageParameters) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(com.helger.mail.datasource.ByteArrayDataSource)

Aggregations

ByteArrayDataSource (com.helger.mail.datasource.ByteArrayDataSource)8 IOException (java.io.IOException)5 AS2Exception (com.helger.as2lib.exception.AS2Exception)4 DataSource (javax.activation.DataSource)4 MessagingException (javax.mail.MessagingException)4 WrappedAS2Exception (com.helger.as2lib.exception.WrappedAS2Exception)3 AS2Message (com.helger.as2lib.message.AS2Message)3 AS2NoModuleException (com.helger.as2lib.processor.AS2NoModuleException)3 AS2ComponentNotFoundException (com.helger.as2lib.session.AS2ComponentNotFoundException)3 IHTTPIncomingDumper (com.helger.as2lib.util.dump.IHTTPIncomingDumper)3 StopWatch (com.helger.commons.timing.StopWatch)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 AS2HttpRequestDataProviderInputStream (com.helger.as2lib.util.http.AS2HttpRequestDataProviderInputStream)2 AS2HttpResponseHandlerSocket (com.helger.as2lib.util.http.AS2HttpResponseHandlerSocket)2 IAS2HttpResponseHandler (com.helger.as2lib.util.http.IAS2HttpResponseHandler)2 IExtendedDataSource (com.helger.mail.datasource.IExtendedDataSource)2 InputStreamDataSource (com.helger.mail.datasource.InputStreamDataSource)2 InputStream (java.io.InputStream)2 Nonnull (javax.annotation.Nonnull)2 AS2DispositionException (com.helger.as2lib.disposition.AS2DispositionException)1