use of com.helger.phase4.client.IAS4SignalMessageConsumer in project phase4 by phax.
the class AbstractAS4UserMessageBuilder method sendMessageAndCheckForReceipt.
/**
* This is a sanity method that encapsulates all the sending checks that are
* necessary to determine overall sending success or error.<br>
* Note: this method is not thread-safe, because it changes the signal message
* consumer internally.
*
* @param aExceptionConsumer
* An optional Consumer that takes an eventually thrown
* {@link Phase4Exception}. May be <code>null</code>.
* @return {@link ESimpleUserMessageSendResult#SUCCESS} only if all parameters
* are correct, HTTP transmission was successful and if a positive AS4
* Receipt was returned. Never <code>null</code>.
* @since 1.0.0-rc1
*/
@Nonnull
public final ESimpleUserMessageSendResult sendMessageAndCheckForReceipt(@Nullable final Consumer<? super Phase4Exception> aExceptionConsumer) {
final IAS4SignalMessageConsumer aOld = m_aSignalMsgConsumer;
try {
// Store the received signal message
final Wrapper<Ebms3SignalMessage> aSignalMsgKeeper = new Wrapper<>();
m_aSignalMsgConsumer = aOld == null ? aSignalMsgKeeper::set : x -> {
aSignalMsgKeeper.set(x);
aOld.handleSignalMessage(x);
};
// Main sending
if (sendMessage().isFailure()) {
// Parameters are missing/incorrect
return ESimpleUserMessageSendResult.INVALID_PARAMETERS;
}
final Ebms3SignalMessage aSignalMsg = aSignalMsgKeeper.get();
if (aSignalMsg == null) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Failed to get a SignalMessage as the response");
// Unexpected response - invalid XML or at least no Ebms3 signal message
return ESimpleUserMessageSendResult.NO_SIGNAL_MESSAGE_RECEIVED;
}
if (aSignalMsg.hasErrorEntries()) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("The received SignalMessage contains at lease one error");
// Errors have precedence over receipts
return ESimpleUserMessageSendResult.AS4_ERROR_MESSAGE_RECEIVED;
}
if (aSignalMsg.getReceipt() != null) {
// A receipt was returned - this is deemed success
return ESimpleUserMessageSendResult.SUCCESS;
}
if (LOGGER.isDebugEnabled())
LOGGER.debug("The SignalMessage contains neither Errors nor a Receipt - unexpected SignalMessage layout.");
// Neither an error nor a receipt was returned - this is weird
return ESimpleUserMessageSendResult.INVALID_SIGNAL_MESSAGE_RECEIVED;
} catch (final Phase4Exception ex) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("An exception occurred sending out the AS4 message", ex);
if (aExceptionConsumer != null)
aExceptionConsumer.accept(ex);
// Something went wrong - see the logs
return ESimpleUserMessageSendResult.TRANSPORT_ERROR;
} finally {
// Restore the original value
m_aSignalMsgConsumer = aOld;
}
}
use of com.helger.phase4.client.IAS4SignalMessageConsumer in project phase4 by phax.
the class AS4BidirectionalClientHelper method sendAS4UserMessageAndReceiveAS4SignalMessage.
public static void sendAS4UserMessageAndReceiveAS4SignalMessage(@Nonnull final IAS4CryptoFactory aCryptoFactory, @Nonnull final IPModeResolver aPModeResolver, @Nonnull final IAS4IncomingAttachmentFactory aIAF, @Nonnull final IAS4IncomingProfileSelector aIncomingProfileSelector, @Nonnull final AS4ClientUserMessage aClientUserMsg, @Nonnull final Locale aLocale, @Nonnull final String sURL, @Nullable final IAS4ClientBuildMessageCallback aBuildMessageCallback, @Nullable final IAS4OutgoingDumper aOutgoingDumper, @Nullable final IAS4IncomingDumper aIncomingDumper, @Nullable final IAS4RetryCallback aRetryCallback, @Nullable final IAS4RawResponseConsumer aResponseConsumer, @Nullable final IAS4SignalMessageConsumer aSignalMsgConsumer) throws IOException, Phase4Exception, WSSecurityException, MessagingException {
if (LOGGER.isInfoEnabled())
LOGGER.info("Sending AS4 UserMessage to '" + sURL + "' with max. " + aClientUserMsg.httpRetrySettings().getMaxRetries() + " retries");
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(" ServiceType = '" + aClientUserMsg.getServiceType() + "'");
LOGGER.debug(" Service = '" + aClientUserMsg.getServiceValue() + "'");
LOGGER.debug(" Action = '" + aClientUserMsg.getAction() + "'");
LOGGER.debug(" ConversationId = '" + aClientUserMsg.getConversationID() + "'");
LOGGER.debug(" MessageProperties:");
for (final Ebms3Property p : aClientUserMsg.ebms3Properties()) LOGGER.debug(" [" + p.getName() + "] = [" + p.getValue() + "]");
LOGGER.debug(" Attachments (" + aClientUserMsg.attachments().size() + "):");
for (final WSS4JAttachment a : aClientUserMsg.attachments()) {
LOGGER.debug(" [" + a.getId() + "] with [" + a.getMimeType() + "] and [" + a.getCharsetOrDefault(null) + "] and [" + a.getCompressionMode() + "] and [" + a.getContentTransferEncoding() + "]");
}
}
final Wrapper<HttpResponse> aWrappedResponse = new Wrapper<>();
final ResponseHandler<byte[]> aResponseHdl = aHttpResponse -> {
// throws an ExtendedHttpResponseException on exception
final HttpEntity aEntity = ResponseHandlerHttpEntity.INSTANCE.handleResponse(aHttpResponse);
if (aEntity == null)
return null;
aWrappedResponse.set(aHttpResponse);
return EntityUtils.toByteArray(aEntity);
};
final AS4ClientSentMessage<byte[]> aResponseEntity = aClientUserMsg.sendMessageWithRetries(sURL, aResponseHdl, aBuildMessageCallback, aOutgoingDumper, aRetryCallback);
if (LOGGER.isInfoEnabled())
LOGGER.info("Successfully transmitted AS4 UserMessage with message ID '" + aResponseEntity.getMessageID() + "' to '" + sURL + "'");
if (aResponseConsumer != null)
aResponseConsumer.handleResponse(aResponseEntity);
// Try interpret result as SignalMessage
if (aResponseEntity.hasResponse() && aResponseEntity.getResponse().length > 0) {
final IAS4IncomingMessageMetadata aMessageMetadata = new AS4IncomingMessageMetadata(EAS4MessageMode.RESPONSE).setRemoteAddr(sURL);
// Read response as EBMS3 Signal Message
// Read it in any case to ensure signature validation etc. happens
final Ebms3SignalMessage aSignalMessage = AS4IncomingHandler.parseSignalMessage(aCryptoFactory, aPModeResolver, aIAF, aIncomingProfileSelector, aClientUserMsg.getAS4ResourceHelper(), aClientUserMsg.getPMode(), aLocale, aMessageMetadata, aWrappedResponse.get(), aResponseEntity.getResponse(), aIncomingDumper);
if (aSignalMessage != null && aSignalMsgConsumer != null)
aSignalMsgConsumer.handleSignalMessage(aSignalMessage);
} else
LOGGER.info("AS4 ResponseEntity is empty");
}
Aggregations