Search in sources :

Example 1 with IAS4SignalMessageConsumer

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;
    }
}
Also used : Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) X509Certificate(java.security.cert.X509Certificate) Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) MessageHelperMethods(com.helger.phase4.messaging.domain.MessageHelperMethods) IAS4SignalMessageConsumer(com.helger.phase4.client.IAS4SignalMessageConsumer) LoggerFactory(org.slf4j.LoggerFactory) Nonempty(com.helger.commons.annotation.Nonempty) IHasID(com.helger.commons.id.IHasID) EnumHelper(com.helger.commons.lang.EnumHelper) Nonnull(javax.annotation.Nonnull) Phase4Exception(com.helger.phase4.util.Phase4Exception) Nullable(javax.annotation.Nullable) Ebms3Property(com.helger.phase4.ebms3header.Ebms3Property) OverridingMethodsMustInvokeSuper(javax.annotation.OverridingMethodsMustInvokeSuper) Logger(org.slf4j.Logger) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) IPMode(com.helger.phase4.model.pmode.IPMode) StringHelper(com.helger.commons.string.StringHelper) ISuccessIndicator(com.helger.commons.state.ISuccessIndicator) Consumer(java.util.function.Consumer) MessageProperty(com.helger.phase4.model.MessageProperty) ICommonsList(com.helger.commons.collection.impl.ICommonsList) Wrapper(com.helger.commons.wrapper.Wrapper) Phase4OutgoingAttachment(com.helger.phase4.attachment.Phase4OutgoingAttachment) AS4ClientUserMessage(com.helger.phase4.client.AS4ClientUserMessage) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) Wrapper(com.helger.commons.wrapper.Wrapper) Phase4Exception(com.helger.phase4.util.Phase4Exception) IAS4SignalMessageConsumer(com.helger.phase4.client.IAS4SignalMessageConsumer) Nonnull(javax.annotation.Nonnull)

Example 2 with IAS4SignalMessageConsumer

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");
}
Also used : Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) IAS4SignalMessageConsumer(com.helger.phase4.client.IAS4SignalMessageConsumer) IAS4RawResponseConsumer(com.helger.phase4.client.IAS4RawResponseConsumer) LoggerFactory(org.slf4j.LoggerFactory) MessagingException(javax.mail.MessagingException) IAS4CryptoFactory(com.helger.phase4.crypto.IAS4CryptoFactory) ResponseHandlerHttpEntity(com.helger.httpclient.response.ResponseHandlerHttpEntity) EntityUtils(org.apache.http.util.EntityUtils) IAS4IncomingProfileSelector(com.helger.phase4.servlet.IAS4IncomingProfileSelector) EAS4MessageMode(com.helger.phase4.messaging.EAS4MessageMode) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) Locale(java.util.Locale) IAS4IncomingDumper(com.helger.phase4.dump.IAS4IncomingDumper) IAS4IncomingMessageMetadata(com.helger.phase4.messaging.IAS4IncomingMessageMetadata) IAS4RetryCallback(com.helger.phase4.client.IAS4RetryCallback) Nonnull(javax.annotation.Nonnull) IAS4IncomingAttachmentFactory(com.helger.phase4.attachment.IAS4IncomingAttachmentFactory) Phase4Exception(com.helger.phase4.util.Phase4Exception) Nullable(javax.annotation.Nullable) IAS4OutgoingDumper(com.helger.phase4.dump.IAS4OutgoingDumper) Ebms3Property(com.helger.phase4.ebms3header.Ebms3Property) AS4ClientPullRequestMessage(com.helger.phase4.client.AS4ClientPullRequestMessage) Ebms3UserMessage(com.helger.phase4.ebms3header.Ebms3UserMessage) Logger(org.slf4j.Logger) AS4IncomingMessageMetadata(com.helger.phase4.servlet.AS4IncomingMessageMetadata) IPModeResolver(com.helger.phase4.model.pmode.resolve.IPModeResolver) AS4ClientSentMessage(com.helger.phase4.client.AS4ClientSentMessage) HttpEntity(org.apache.http.HttpEntity) IOException(java.io.IOException) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment) IAS4ClientBuildMessageCallback(com.helger.phase4.client.IAS4ClientBuildMessageCallback) AS4IncomingHandler(com.helger.phase4.servlet.AS4IncomingHandler) Wrapper(com.helger.commons.wrapper.Wrapper) AS4ClientUserMessage(com.helger.phase4.client.AS4ClientUserMessage) HttpResponse(org.apache.http.HttpResponse) IAS4UserMessageConsumer(com.helger.phase4.client.IAS4UserMessageConsumer) ResponseHandler(org.apache.http.client.ResponseHandler) IAS4IncomingMessageMetadata(com.helger.phase4.messaging.IAS4IncomingMessageMetadata) Ebms3SignalMessage(com.helger.phase4.ebms3header.Ebms3SignalMessage) Wrapper(com.helger.commons.wrapper.Wrapper) ResponseHandlerHttpEntity(com.helger.httpclient.response.ResponseHandlerHttpEntity) HttpEntity(org.apache.http.HttpEntity) IAS4IncomingMessageMetadata(com.helger.phase4.messaging.IAS4IncomingMessageMetadata) AS4IncomingMessageMetadata(com.helger.phase4.servlet.AS4IncomingMessageMetadata) HttpResponse(org.apache.http.HttpResponse) Ebms3Property(com.helger.phase4.ebms3header.Ebms3Property) WSS4JAttachment(com.helger.phase4.attachment.WSS4JAttachment)

Aggregations

Wrapper (com.helger.commons.wrapper.Wrapper)2 AS4ClientUserMessage (com.helger.phase4.client.AS4ClientUserMessage)2 IAS4SignalMessageConsumer (com.helger.phase4.client.IAS4SignalMessageConsumer)2 Ebms3Property (com.helger.phase4.ebms3header.Ebms3Property)2 Ebms3SignalMessage (com.helger.phase4.ebms3header.Ebms3SignalMessage)2 Phase4Exception (com.helger.phase4.util.Phase4Exception)2 Nonnull (javax.annotation.Nonnull)2 Nullable (javax.annotation.Nullable)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Nonempty (com.helger.commons.annotation.Nonempty)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 ICommonsList (com.helger.commons.collection.impl.ICommonsList)1 IHasID (com.helger.commons.id.IHasID)1 EnumHelper (com.helger.commons.lang.EnumHelper)1 ISuccessIndicator (com.helger.commons.state.ISuccessIndicator)1 StringHelper (com.helger.commons.string.StringHelper)1 ResponseHandlerHttpEntity (com.helger.httpclient.response.ResponseHandlerHttpEntity)1 IAS4IncomingAttachmentFactory (com.helger.phase4.attachment.IAS4IncomingAttachmentFactory)1 Phase4OutgoingAttachment (com.helger.phase4.attachment.Phase4OutgoingAttachment)1