Search in sources :

Example 1 with AS4ClientUserMessage

use of com.helger.phase4.client.AS4ClientUserMessage in project phase4 by phax.

the class AbstractAS4UserMessageBuilder method applyToUserMessage.

/**
 * This method applies all builder parameters onto the user message, except
 * the attachments.
 *
 * @param aUserMsg
 *        The user message the parameters should be applied to. May not be
 *        <code>null</code>.
 */
@OverridingMethodsMustInvokeSuper
protected void applyToUserMessage(@Nonnull final AS4ClientUserMessage aUserMsg) {
    if (m_aHttpRetrySettings != null)
        aUserMsg.httpRetrySettings().assignFrom(m_aHttpRetrySettings);
    aUserMsg.getHttpPoster().setHttpClientFactory(m_aHttpClientFactory);
    // Otherwise Oxalis dies
    aUserMsg.getHttpPoster().setQuoteHttpHeaders(false);
    aUserMsg.setSoapVersion(m_eSoapVersion);
    aUserMsg.setSendingDateTimeOrNow(m_aSendingDateTime);
    // Set the keystore/truststore parameters
    aUserMsg.setAS4CryptoFactory(m_aCryptoFactory);
    aUserMsg.setPMode(m_aPMode, true);
    // Set after PMode
    if (m_aReceiverCertificate != null)
        aUserMsg.cryptParams().setCertificate(m_aReceiverCertificate);
    aUserMsg.setAgreementRefValue(m_sAgreementRef);
    if (StringHelper.hasText(m_sPModeID))
        aUserMsg.setPModeID(m_sPModeID);
    else
        aUserMsg.setPModeIDFactory(x -> null);
    aUserMsg.setServiceType(m_sServiceType);
    aUserMsg.setServiceValue(m_sService);
    aUserMsg.setAction(m_sAction);
    if (StringHelper.hasText(m_sMessageID))
        aUserMsg.setMessageID(m_sMessageID);
    if (StringHelper.hasText(m_sRefToMessageID))
        aUserMsg.setRefToMessageID(m_sRefToMessageID);
    // Empty conversation ID is okay
    aUserMsg.setConversationID(m_sConversationID != null ? m_sConversationID : MessageHelperMethods.createRandomConversationID());
    aUserMsg.setFromPartyIDType(m_sFromPartyIDType);
    aUserMsg.setFromPartyID(m_sFromPartyID);
    aUserMsg.setFromRole(m_sFromRole);
    aUserMsg.setToPartyIDType(m_sToPartyIDType);
    aUserMsg.setToPartyID(m_sToPartyID);
    aUserMsg.setToRole(m_sToRole);
    for (final MessageProperty aItem : m_aMessageProperties) aUserMsg.ebms3Properties().add(aItem.getAsEbms3Property());
}
Also used : 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) MessageProperty(com.helger.phase4.model.MessageProperty) OverridingMethodsMustInvokeSuper(javax.annotation.OverridingMethodsMustInvokeSuper)

Example 2 with AS4ClientUserMessage

use of com.helger.phase4.client.AS4ClientUserMessage in project phase4 by phax.

the class AbstractAS4UserMessageBuilderMIMEPayload method mainSendMessage.

@Override
protected final void mainSendMessage() throws Phase4Exception {
    // Temporary file manager
    try (final AS4ResourceHelper aResHelper = new AS4ResourceHelper()) {
        // Start building AS4 User Message
        final AS4ClientUserMessage aUserMsg = new AS4ClientUserMessage(aResHelper);
        applyToUserMessage(aUserMsg);
        // No payload - only one attachment
        aUserMsg.setPayload(null);
        // Add main attachment
        aUserMsg.addAttachment(WSS4JAttachment.createOutgoingFileAttachment(m_aPayload, aResHelper));
        // Add other attachments
        for (final Phase4OutgoingAttachment aAttachment : m_aAttachments) aUserMsg.addAttachment(WSS4JAttachment.createOutgoingFileAttachment(aAttachment, aResHelper));
        // Main sending
        AS4BidirectionalClientHelper.sendAS4UserMessageAndReceiveAS4SignalMessage(m_aCryptoFactory, pmodeResolver(), incomingAttachmentFactory(), incomingProfileSelector(), aUserMsg, m_aLocale, m_sEndpointURL, m_aBuildMessageCallback, m_aOutgoingDumper, m_aIncomingDumper, m_aRetryCallback, m_aResponseConsumer, m_aSignalMsgConsumer);
    } catch (final Phase4Exception ex) {
        // Re-throw
        throw ex;
    } catch (final Exception ex) {
        // wrap
        throw new Phase4Exception("Wrapped Phase4Exception", ex);
    }
}
Also used : Phase4Exception(com.helger.phase4.util.Phase4Exception) Phase4OutgoingAttachment(com.helger.phase4.attachment.Phase4OutgoingAttachment) AS4ClientUserMessage(com.helger.phase4.client.AS4ClientUserMessage) AS4ResourceHelper(com.helger.phase4.util.AS4ResourceHelper) Phase4Exception(com.helger.phase4.util.Phase4Exception)

Example 3 with AS4ClientUserMessage

use of com.helger.phase4.client.AS4ClientUserMessage 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)

Example 4 with AS4ClientUserMessage

use of com.helger.phase4.client.AS4ClientUserMessage in project phase4 by phax.

the class AS4ClientUserMessageTest method _setKeyStoreTestData.

/**
 * Sets the keystore attributes, it uses the dummy keystore
 * keys/dummy-pw-test.jks
 *
 * @param aClient
 *        the client on which these attributes should be set
 * @return the client to continue working with it
 */
@Nonnull
private static AS4ClientUserMessage _setKeyStoreTestData(@Nonnull final AS4ClientUserMessage aClient) {
    final AS4CryptoProperties aCP = new AS4CryptoProperties().setKeyStoreType(EKeyStoreType.JKS).setKeyStorePath("keys/dummy-pw-test.jks").setKeyStorePassword("test").setKeyAlias("ph-as4").setKeyPassword("test");
    aClient.setAS4CryptoFactory(new AS4CryptoFactoryProperties(aCP));
    aClient.cryptParams().setAlias(aCP.getKeyAlias());
    return aClient;
}
Also used : AS4CryptoProperties(com.helger.phase4.crypto.AS4CryptoProperties) AS4CryptoFactoryProperties(com.helger.phase4.crypto.AS4CryptoFactoryProperties) Nonnull(javax.annotation.Nonnull)

Example 5 with AS4ClientUserMessage

use of com.helger.phase4.client.AS4ClientUserMessage in project phase4 by phax.

the class AS4ClientUserMessageTest method testBuildMessageKeyStoreCheckFailure.

@Test
public void testBuildMessageKeyStoreCheckFailure() throws Exception {
    final AS4ClientUserMessage aClient = _createMandatoryAttributesSuccessMessage();
    // Set sign attributes, to get to the check, the check only gets called if
    // sign or encrypt needs to be done for the usermessage
    aClient.signingParams().setAlgorithmSign(ECryptoAlgorithmSign.RSA_SHA_256).setAlgorithmSignDigest(ECryptoAlgorithmSignDigest.DIGEST_SHA_256);
    // No Keystore attributes set
    _ensureInvalidState(aClient);
    final AS4CryptoProperties aCP = new AS4CryptoProperties().setKeyStorePath("keys/dummy-pw-test.jks");
    final AS4CryptoFactoryProperties aCF = new AS4CryptoFactoryProperties(aCP);
    aClient.setAS4CryptoFactory(aCF);
    _ensureInvalidState(aClient);
    aCF.cryptoProperties().setKeyStorePassword("test");
    _ensureInvalidState(aClient);
    aCF.cryptoProperties().setKeyStoreType(EKeyStoreType.JKS);
    _ensureInvalidState(aClient);
    aCF.cryptoProperties().setKeyAlias("ph-as4");
    _ensureInvalidState(aClient);
    aCF.cryptoProperties().setKeyPassword("test");
    _ensureValidState(aClient);
}
Also used : AS4CryptoProperties(com.helger.phase4.crypto.AS4CryptoProperties) AS4CryptoFactoryProperties(com.helger.phase4.crypto.AS4CryptoFactoryProperties) Test(org.junit.Test)

Aggregations

AS4ClientUserMessage (com.helger.phase4.client.AS4ClientUserMessage)4 Phase4Exception (com.helger.phase4.util.Phase4Exception)3 Nonnull (javax.annotation.Nonnull)3 Wrapper (com.helger.commons.wrapper.Wrapper)2 Phase4OutgoingAttachment (com.helger.phase4.attachment.Phase4OutgoingAttachment)2 IAS4ClientBuildMessageCallback (com.helger.phase4.client.IAS4ClientBuildMessageCallback)2 IAS4RetryCallback (com.helger.phase4.client.IAS4RetryCallback)2 IAS4SignalMessageConsumer (com.helger.phase4.client.IAS4SignalMessageConsumer)2 AS4CryptoFactoryProperties (com.helger.phase4.crypto.AS4CryptoFactoryProperties)2 AS4CryptoProperties (com.helger.phase4.crypto.AS4CryptoProperties)2 IAS4OutgoingDumper (com.helger.phase4.dump.IAS4OutgoingDumper)2 Ebms3Property (com.helger.phase4.ebms3header.Ebms3Property)2 Ebms3SignalMessage (com.helger.phase4.ebms3header.Ebms3SignalMessage)2 AS4ResourceHelper (com.helger.phase4.util.AS4ResourceHelper)2 IOException (java.io.IOException)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