Search in sources :

Example 6 with HttpHeaderMap

use of com.helger.commons.http.HttpHeaderMap in project as2-lib by phax.

the class AS2HttpClient method getResponseHeaderFields.

@Nonnull
@ReturnsMutableCopy
public HttpHeaderMap getResponseHeaderFields() throws AS2Exception {
    // message was not sent yet, not response
    if (m_aCloseableHttpResponse == null)
        throw new AS2Exception("No response as message was yet sent");
    final Header[] aHeaders = m_aCloseableHttpResponse.getAllHeaders();
    final HttpHeaderMap ret = new HttpHeaderMap();
    for (final Header aHeader : aHeaders) ret.addHeader(aHeader.getName(), aHeader.getValue());
    return ret;
}
Also used : HttpHeaderMap(com.helger.commons.http.HttpHeaderMap) Header(org.apache.http.Header) AS2Exception(com.helger.as2lib.exception.AS2Exception) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Example 7 with HttpHeaderMap

use of com.helger.commons.http.HttpHeaderMap in project as2-lib by phax.

the class HTTPHelper method sendSimpleHTTPResponse.

/**
 * Send a simple HTTP response that only contains the HTTP status code and the
 * respective descriptive text. An empty header map us used.
 *
 * @param aResponseHandler
 *        The response handler to be used.
 * @param nResponseCode
 *        The HTTP response code to use.
 * @throws IOException
 *         In case sending fails for whatever reason
 */
public static void sendSimpleHTTPResponse(@Nonnull final IAS2HttpResponseHandler aResponseHandler, @Nonnegative final int nResponseCode) throws IOException {
    try (final NonBlockingByteArrayOutputStream aData = new NonBlockingByteArrayOutputStream()) {
        final String sHTTPLine = Integer.toString(nResponseCode) + " " + CHttp.getHttpResponseMessage(nResponseCode) + CHttp.EOL;
        aData.write(sHTTPLine.getBytes(CHttp.HTTP_CHARSET));
        aResponseHandler.sendHttpResponse(nResponseCode, new HttpHeaderMap(), aData);
    }
}
Also used : HttpHeaderMap(com.helger.commons.http.HttpHeaderMap) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)

Example 8 with HttpHeaderMap

use of com.helger.commons.http.HttpHeaderMap in project phase4 by phax.

the class MessageHelperMethods method getAndRemoveAllHeaders.

@Nonnull
@ReturnsMutableCopy
public static HttpHeaderMap getAndRemoveAllHeaders(@Nonnull final MimeMessage aMimeMsg) throws MessagingException {
    final HttpHeaderMap ret = new HttpHeaderMap();
    // Unification happens on the result header map
    forEachHeaderAndRemoveAfterwards(aMimeMsg, ret::addHeader, false);
    return ret;
}
Also used : HttpHeaderMap(com.helger.commons.http.HttpHeaderMap) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Example 9 with HttpHeaderMap

use of com.helger.commons.http.HttpHeaderMap in project phase4 by phax.

the class AbstractAS4Client method sendMessageWithRetries.

/**
 * Send the AS4 client message created by
 * {@link #buildMessage(String, IAS4ClientBuildMessageCallback)} to the
 * provided URL. This methods does take retries into account. It synchronously
 * handles the retries and only returns after the last retry.
 *
 * @param <T>
 *        The response data type
 * @param sURL
 *        The URL to send the HTTP POST to
 * @param aResponseHandler
 *        The response handler that converts the HTTP response to a domain
 *        object. May not be <code>null</code>.
 * @param aCallback
 *        An optional callback for the different stages of building the
 *        document. May be <code>null</code>.
 * @param aOutgoingDumper
 *        An outgoing dumper to be used. Maybe <code>null</code>. If
 *        <code>null</code> the global outgoing dumper from
 *        {@link AS4DumpManager} is used.
 * @param aRetryCallback
 *        An optional callback to be invoked if a retry happens on HTTP level.
 *        May be <code>null</code>.
 * @return The sent message that contains
 * @throws IOException
 *         in case of error when building or sending the message
 * @throws WSSecurityException
 *         In case there is an issue with signing/encryption
 * @throws MessagingException
 *         in case something happens in MIME wrapping
 * @since 0.9.14
 */
@Nonnull
public final <T> AS4ClientSentMessage<T> sendMessageWithRetries(@Nonnull final String sURL, @Nonnull final ResponseHandler<? extends T> aResponseHandler, @Nullable final IAS4ClientBuildMessageCallback aCallback, @Nullable final IAS4OutgoingDumper aOutgoingDumper, @Nullable final IAS4RetryCallback aRetryCallback) throws IOException, WSSecurityException, MessagingException {
    // Create a new message ID for each build!
    final String sMessageID = createMessageID();
    final AS4ClientBuiltMessage aBuiltMsg = buildMessage(sMessageID, aCallback);
    HttpEntity aBuiltEntity = aBuiltMsg.getHttpEntity();
    final HttpHeaderMap aBuiltHttpHeaders = aBuiltMsg.getCustomHeaders();
    if (m_aHttpRetrySettings.isRetryEnabled() || aOutgoingDumper != null || AS4DumpManager.getOutgoingDumper() != null) {
        // Ensure a repeatable entity is provided
        aBuiltEntity = m_aResHelper.createRepeatableHttpEntity(aBuiltEntity);
    }
    // Keep the HTTP response status line for external evaluation
    final Wrapper<StatusLine> aStatusLineKeeper = new Wrapper<>();
    // Keep the HTTP response headers for external evaluation
    final HttpHeaderMap aResponseHeaders = new HttpHeaderMap();
    final ResponseHandler<T> aRealResponseHandler = x -> {
        // Remember the HTTP response data
        aStatusLineKeeper.set(x.getStatusLine());
        final Header[] aHeaders = x.getAllHeaders();
        if (aHeaders != null)
            for (final Header aHeader : aHeaders) aResponseHeaders.addHeader(aHeader.getName(), aHeader.getValue());
        // Call the original handler
        return aResponseHandler.handleResponse(x);
    };
    final T aResponseContent = m_aHttpPoster.sendGenericMessageWithRetries(sURL, aBuiltHttpHeaders, aBuiltEntity, sMessageID, m_aHttpRetrySettings, aRealResponseHandler, aOutgoingDumper, aRetryCallback);
    return new AS4ClientSentMessage<>(aBuiltMsg, aStatusLineKeeper.get(), aResponseHeaders, aResponseContent);
}
Also used : StatusLine(org.apache.http.StatusLine) BasicHttpPoster(com.helger.phase4.http.BasicHttpPoster) IGenericImplTrait(com.helger.commons.traits.IGenericImplTrait) MessageHelperMethods(com.helger.phase4.messaging.domain.MessageHelperMethods) PModeReceptionAwareness(com.helger.phase4.model.pmode.PModeReceptionAwareness) ESoapVersion(com.helger.phase4.soap.ESoapVersion) HttpHeaderMap(com.helger.commons.http.HttpHeaderMap) AS4HttpDebug(com.helger.phase4.http.AS4HttpDebug) MessagingException(javax.mail.MessagingException) MetaAS4Manager(com.helger.phase4.mgr.MetaAS4Manager) IMicroDocument(com.helger.xml.microdom.IMicroDocument) IAS4CryptoFactory(com.helger.phase4.crypto.IAS4CryptoFactory) Supplier(java.util.function.Supplier) Header(org.apache.http.Header) StatusLine(org.apache.http.StatusLine) AS4CryptParams(com.helger.phase4.crypto.AS4CryptParams) EAS4MessageType(com.helger.phase4.messaging.domain.EAS4MessageType) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) Nonempty(com.helger.commons.annotation.Nonempty) IHttpPoster(com.helger.phase4.http.IHttpPoster) Duration(java.time.Duration) AS4ResourceHelper(com.helger.phase4.util.AS4ResourceHelper) PModeLeg(com.helger.phase4.model.pmode.leg.PModeLeg) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) IAS4OutgoingDumper(com.helger.phase4.dump.IAS4OutgoingDumper) WillNotClose(javax.annotation.WillNotClose) AS4SigningParams(com.helger.phase4.crypto.AS4SigningParams) IPMode(com.helger.phase4.model.pmode.IPMode) StringHelper(com.helger.commons.string.StringHelper) HttpEntity(org.apache.http.HttpEntity) IOException(java.io.IOException) ResponseHandlerMicroDom(com.helger.httpclient.response.ResponseHandlerMicroDom) ValueEnforcer(com.helger.commons.ValueEnforcer) AS4DumpManager(com.helger.phase4.dump.AS4DumpManager) HttpRetrySettings(com.helger.phase4.http.HttpRetrySettings) MicroWriter(com.helger.xml.microdom.serialize.MicroWriter) OffsetDateTime(java.time.OffsetDateTime) Wrapper(com.helger.commons.wrapper.Wrapper) ReturnsMutableObject(com.helger.commons.annotation.ReturnsMutableObject) ResponseHandler(org.apache.http.client.ResponseHandler) HttpHeaderMap(com.helger.commons.http.HttpHeaderMap) Wrapper(com.helger.commons.wrapper.Wrapper) HttpEntity(org.apache.http.HttpEntity) Header(org.apache.http.Header) Nonnull(javax.annotation.Nonnull)

Example 10 with HttpHeaderMap

use of com.helger.commons.http.HttpHeaderMap in project phase4 by phax.

the class AbstractAS4OutgoingDumperWithHeaders method onBeginRequest.

@Nullable
public OutputStream onBeginRequest(@Nonnull final EAS4MessageMode eMsgMode, @Nullable final IAS4IncomingMessageMetadata aMessageMetadata, @Nullable final IAS4MessageState aState, @Nonnull @Nonempty final String sMessageID, @Nullable final HttpHeaderMap aCustomHeaders, @Nonnegative final int nTry) throws IOException {
    final OutputStream ret = openOutputStream(eMsgMode, aMessageMetadata, aState, sMessageID, aCustomHeaders, nTry);
    if (ret != null && aCustomHeaders != null && aCustomHeaders.isNotEmpty()) {
        // At least one custom header is present
        for (final Map.Entry<String, ICommonsList<String>> aEntry : aCustomHeaders) {
            final String sHeader = aEntry.getKey();
            for (final String sValue : aEntry.getValue()) {
                // By default quoting is disabled
                final boolean bQuoteIfNecessary = false;
                final String sUnifiedValue = HttpHeaderMap.getUnifiedValue(sValue, bQuoteIfNecessary);
                ret.write((sHeader + HttpHeaderMap.SEPARATOR_KEY_VALUE + sUnifiedValue + CHttp.EOL).getBytes(CHttp.HTTP_CHARSET));
            }
        }
        ret.write(CHttp.EOL.getBytes(CHttp.HTTP_CHARSET));
    }
    return ret;
}
Also used : ICommonsList(com.helger.commons.collection.impl.ICommonsList) OutputStream(java.io.OutputStream) HttpHeaderMap(com.helger.commons.http.HttpHeaderMap) Map(java.util.Map) Nullable(javax.annotation.Nullable)

Aggregations

HttpHeaderMap (com.helger.commons.http.HttpHeaderMap)22 Nonnull (javax.annotation.Nonnull)7 ICommonsList (com.helger.commons.collection.impl.ICommonsList)5 IOException (java.io.IOException)5 Nullable (javax.annotation.Nullable)5 MessagingException (javax.mail.MessagingException)5 IMessageMDN (com.helger.as2lib.message.IMessageMDN)4 Ebms3SignalMessage (com.helger.phase4.ebms3header.Ebms3SignalMessage)4 Ebms3UserMessage (com.helger.phase4.ebms3header.Ebms3UserMessage)4 IPMode (com.helger.phase4.model.pmode.IPMode)4 Phase4Exception (com.helger.phase4.util.Phase4Exception)4 MimeBodyPart (javax.mail.internet.MimeBodyPart)4 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)4 Test (org.junit.Test)4 Node (org.w3c.dom.Node)4 ValueEnforcer (com.helger.commons.ValueEnforcer)3 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)3 IHasInputStream (com.helger.commons.io.IHasInputStream)3 NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)3