Search in sources :

Example 1 with AS2OutputStreamCreatorHttpServletResponse

use of com.helger.as2servlet.util.AS2OutputStreamCreatorHttpServletResponse 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)

Aggregations

AS2Exception (com.helger.as2lib.exception.AS2Exception)1 AS2Message (com.helger.as2lib.message.AS2Message)1 IHTTPIncomingDumper (com.helger.as2lib.util.dump.IHTTPIncomingDumper)1 AS2OutputStreamCreatorHttpServletResponse (com.helger.as2servlet.util.AS2OutputStreamCreatorHttpServletResponse)1 StopWatch (com.helger.commons.timing.StopWatch)1 ByteArrayDataSource (com.helger.mail.datasource.ByteArrayDataSource)1 IOException (java.io.IOException)1 DataSource (javax.activation.DataSource)1 ServletException (javax.servlet.ServletException)1 ServletInputStream (javax.servlet.ServletInputStream)1