Search in sources :

Example 1 with AS2HttpHeaderSetter

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

the class AS2SenderModule method _sendViaHTTP.

private void _sendViaHTTP(@Nonnull final AS2Message aMsg, @Nonnull final MimeBodyPart aSecuredMimePart, @Nullable final MIC aMIC, @Nullable final EContentTransferEncoding eCTE, @Nullable final IHTTPOutgoingDumper aOutgoingDumper, @Nullable final IHTTPIncomingDumper aIncomingDumper, @Nonnull final AS2ResourceHelper aResHelper) throws AS2Exception, IOException, MessagingException {
    final Partnership aPartnership = aMsg.partnership();
    // Create the HTTP connection
    final String sUrl = aPartnership.getAS2URL();
    final EHttpMethod eRequestMethod = EHttpMethod.POST;
    // decide on the connection type to use according to the MimeBodyPart:
    // If it contains the data, (and no DataHandler), then use HttpUrlClient,
    // otherwise, use HttpClient
    final AS2HttpClient aConn = getHttpClient(sUrl, eRequestMethod, getSession().getHttpProxy());
    try {
        if (aOutgoingDumper != null)
            aOutgoingDumper.start(sUrl, aMsg);
        if (LOGGER.isInfoEnabled())
            LOGGER.info("Connecting to " + sUrl + aMsg.getLoggingText());
        final boolean bQuoteHeaderValues = isQuoteHeaderValues();
        updateHttpHeaders(new AS2HttpHeaderSetter(aConn, aOutgoingDumper, bQuoteHeaderValues), aMsg);
        if (aOutgoingDumper != null)
            aOutgoingDumper.finishedHeaders();
        aMsg.attrs().putIn(CNetAttribute.MA_DESTINATION_IP, aConn.getURL().getHost());
        aMsg.attrs().putIn(CNetAttribute.MA_DESTINATION_PORT, aConn.getURL().getPort());
        final InputStream aMsgIS = aSecuredMimePart.getInputStream();
        // Transfer the data
        final StopWatch aSW = StopWatch.createdStarted();
        final long nBytes = aConn.send(aMsgIS, eCTE, aOutgoingDumper, aResHelper);
        aSW.stop();
        if (LOGGER.isInfoEnabled())
            LOGGER.info("AS2 Message transferred " + AS2IOHelper.getTransferRate(nBytes, aSW) + aMsg.getLoggingText());
        if (aOutgoingDumper != null)
            aOutgoingDumper.finishedPayload();
        final int nHttpResponseCode = aConn.getResponseCode();
        if (getOutgoingHttpCallback() != null)
            getOutgoingHttpCallback().onOutgoingHttpMessage(true, aMsg.getAS2From(), aMsg.getAS2To(), aMsg.getMessageID(), aMIC == null ? null : aMIC.getClone(), eCTE, sUrl, nHttpResponseCode);
        // Check the HTTP Response code
        if (AS2HttpClient.isErrorResponseCode(nHttpResponseCode)) {
            if (LOGGER.isErrorEnabled())
                LOGGER.error("Error URL '" + sUrl + "' - HTTP " + nHttpResponseCode + " " + aConn.getResponseMessage() + " " + aMsg.getLoggingText());
            throw new AS2HttpResponseException(sUrl, nHttpResponseCode, aConn.getResponseMessage());
        }
        // Receive an MDN
        try {
            // Receive an MDN
            if (aMsg.isRequestingMDN()) {
                // Check if the AsyncMDN is required
                if (aPartnership.getAS2ReceiptDeliveryOption() == null) {
                    // Note: If an MDN is requested, a MIC is present
                    assert aMIC != null;
                    receiveSyncMDN(aMsg, aConn, aMIC, aIncomingDumper, aResHelper);
                    if (LOGGER.isInfoEnabled())
                        LOGGER.info("message sent" + aMsg.getLoggingText());
                }
            }
        } catch (final AS2DispositionException ex) {
            // was not successful
            throw ex;
        } catch (final AS2Exception ex) {
            // Don't re-send or fail, just log an error if one occurs while
            // receiving the MDN
            onReceivedMDNError(aMsg, ex);
        }
    } finally {
        // Closes all resources
        aConn.disconnect();
    }
}
Also used : InputStream(java.io.InputStream) StopWatch(com.helger.commons.timing.StopWatch) AS2DispositionException(com.helger.as2lib.disposition.AS2DispositionException) AS2HttpHeaderSetter(com.helger.as2lib.util.http.AS2HttpHeaderSetter) Partnership(com.helger.as2lib.partner.Partnership) EHttpMethod(com.helger.commons.http.EHttpMethod) AS2Exception(com.helger.as2lib.exception.AS2Exception) WrappedAS2Exception(com.helger.as2lib.exception.WrappedAS2Exception) AS2HttpClient(com.helger.as2lib.util.http.AS2HttpClient)

Example 2 with AS2HttpHeaderSetter

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

the class AsynchMDNSenderModule method _sendViaHTTP.

private void _sendViaHTTP(@Nonnull final AS2Message aMsg, @Nonnull final DispositionType aDisposition, @Nullable final IHTTPOutgoingDumper aOutgoingDumper, @Nonnull final AS2ResourceHelper aResHelper) throws AS2Exception, IOException, MessagingException {
    final IMessageMDN aMdn = aMsg.getMDN();
    // Create a HTTP connection
    final String sUrl = aMsg.getAsyncMDNurl();
    final EHttpMethod eRequestMethod = EHttpMethod.POST;
    // MDN is a small message. We will always use CHttp
    final AS2HttpClient aConn = getHttpClient(sUrl, eRequestMethod, getSession().getHttpProxy());
    try {
        if (aOutgoingDumper != null)
            aOutgoingDumper.start(sUrl, aMsg);
        if (LOGGER.isInfoEnabled())
            LOGGER.info("Connecting to " + sUrl + aMsg.getLoggingText());
        // Set all custom headers first (so that they are overridden with the
        // mandatory ones in here)
        // Use HttpHeaderMap and not String to ensure name casing is identical!
        final HttpHeaderMap aHeaderMap = aMdn.headers().getClone();
        aHeaderMap.setHeader(CHttpHeader.CONNECTION, CAS2Header.DEFAULT_CONNECTION);
        aHeaderMap.setHeader(CHttpHeader.USER_AGENT, CAS2Header.DEFAULT_USER_AGENT);
        final boolean bQuoteHeaderValues = isQuoteHeaderValues();
        final AS2HttpHeaderSetter aHeaderSetter = new AS2HttpHeaderSetter(aConn, aOutgoingDumper, bQuoteHeaderValues);
        // Copy all the header from mdn to the RequestProperties of conn
        // Unification needed, because original MDN headers may contain newlines
        aHeaderMap.forEachSingleHeader(aHeaderSetter::setHttpHeader, true, false);
        if (aOutgoingDumper != null)
            aOutgoingDumper.finishedHeaders();
        aMsg.attrs().putIn(CNetAttribute.MA_DESTINATION_IP, aConn.getURL().getHost());
        aMsg.attrs().putIn(CNetAttribute.MA_DESTINATION_PORT, aConn.getURL().getPort());
        final InputStream aMsgIS = aMdn.getData().getInputStream();
        // Transfer the data
        final StopWatch aSW = StopWatch.createdStarted();
        final long nBytes = aConn.send(aMsgIS, (EContentTransferEncoding) null, aOutgoingDumper, aResHelper);
        aSW.stop();
        if (LOGGER.isInfoEnabled())
            LOGGER.info("AS2 MDN transferred " + AS2IOHelper.getTransferRate(nBytes, aSW) + aMsg.getLoggingText());
        if (aOutgoingDumper != null)
            aOutgoingDumper.finishedPayload();
        final int nHttpResponseCode = aConn.getResponseCode();
        if (getOutgoingHttpCallback() != null)
            getOutgoingHttpCallback().onOutgoingHttpMessage(false, aMsg.getAS2From(), aMsg.getAS2To(), aMsg.getMessageID(), (MIC) null, (EContentTransferEncoding) null, sUrl, nHttpResponseCode);
        // Check the HTTP Response code
        if (AS2HttpClient.isErrorResponseCode(nHttpResponseCode)) {
            if (LOGGER.isErrorEnabled())
                LOGGER.error("sent AsyncMDN [" + aDisposition.getAsString() + "] Fail(" + nHttpResponseCode + ") " + aMsg.getLoggingText());
            throw new AS2HttpResponseException(sUrl, nHttpResponseCode, aConn.getResponseMessage());
        }
        if (LOGGER.isInfoEnabled())
            LOGGER.info("sent AsyncMDN [" + aDisposition.getAsString() + "] OK(" + nHttpResponseCode + ") " + aMsg.getLoggingText());
        // log & store mdn into backup folder.
        try {
            getSession().getMessageProcessor().handle(IProcessorStorageModule.DO_STOREMDN, aMsg, null);
        } catch (final AS2ComponentNotFoundException | AS2NoModuleException ex) {
        // No message processor found
        // Or no module found in message processor
        }
    } finally {
        aConn.disconnect();
    }
}
Also used : EContentTransferEncoding(com.helger.mail.cte.EContentTransferEncoding) InputStream(java.io.InputStream) MIC(com.helger.as2lib.crypto.MIC) StopWatch(com.helger.commons.timing.StopWatch) HttpHeaderMap(com.helger.commons.http.HttpHeaderMap) AS2NoModuleException(com.helger.as2lib.processor.AS2NoModuleException) AS2HttpHeaderSetter(com.helger.as2lib.util.http.AS2HttpHeaderSetter) EHttpMethod(com.helger.commons.http.EHttpMethod) AS2ComponentNotFoundException(com.helger.as2lib.session.AS2ComponentNotFoundException) IMessageMDN(com.helger.as2lib.message.IMessageMDN) AS2HttpClient(com.helger.as2lib.util.http.AS2HttpClient)

Aggregations

AS2HttpClient (com.helger.as2lib.util.http.AS2HttpClient)2 AS2HttpHeaderSetter (com.helger.as2lib.util.http.AS2HttpHeaderSetter)2 EHttpMethod (com.helger.commons.http.EHttpMethod)2 StopWatch (com.helger.commons.timing.StopWatch)2 InputStream (java.io.InputStream)2 MIC (com.helger.as2lib.crypto.MIC)1 AS2DispositionException (com.helger.as2lib.disposition.AS2DispositionException)1 AS2Exception (com.helger.as2lib.exception.AS2Exception)1 WrappedAS2Exception (com.helger.as2lib.exception.WrappedAS2Exception)1 IMessageMDN (com.helger.as2lib.message.IMessageMDN)1 Partnership (com.helger.as2lib.partner.Partnership)1 AS2NoModuleException (com.helger.as2lib.processor.AS2NoModuleException)1 AS2ComponentNotFoundException (com.helger.as2lib.session.AS2ComponentNotFoundException)1 HttpHeaderMap (com.helger.commons.http.HttpHeaderMap)1 EContentTransferEncoding (com.helger.mail.cte.EContentTransferEncoding)1