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;
}
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);
}
}
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;
}
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);
}
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;
}
Aggregations