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