use of com.helger.as2lib.processor.resender.ImmediateResenderModule in project as2-lib by phax.
the class AS2Client method sendSynchronous.
/**
* Send the AS2 message synchronously
*
* @param aSettings
* The settings to be used. May not be <code>null</code>.
* @param aRequest
* The request data to be send. May not be <code>null</code>.
* @return The response object. Never <code>null</code>.
*/
@Nonnull
public AS2ClientResponse sendSynchronous(@Nonnull final AS2ClientSettings aSettings, @Nonnull final AS2ClientRequest aRequest) {
ValueEnforcer.notNull(aSettings, "ClientSettings");
ValueEnforcer.notNull(aRequest, "ClientRequest");
final AS2ClientResponse aResponse = createResponse();
IMessage aMsg = null;
final StopWatch aSW = StopWatch.createdStarted();
try {
final Partnership aPartnership = buildPartnership(aSettings);
aMsg = createMessage(aPartnership, aRequest);
aResponse.setOriginalMessageID(aMsg.getMessageID());
if (LOGGER.isDebugEnabled())
LOGGER.debug("MessageID to send: " + aMsg.getMessageID());
final boolean bHasRetries = aSettings.getRetryCount() > 0;
// Start a new session
final AS2Session aSession = createSession();
initCertificateFactory(aSettings, aSession);
initPartnershipFactory(aSession);
initMessageProcessor(aSession);
if (bHasRetries) {
// Use synchronous no-delay resender
final IProcessorResenderModule aResender = new ImmediateResenderModule();
aResender.initDynamicComponent(aSession, null);
aSession.getMessageProcessor().addModule(aResender);
}
aSession.getMessageProcessor().startActiveModules();
try {
// Invoke callback
beforeSend(aSettings, aSession, aMsg);
// Build options map for "handle"
final ICommonsMap<String, Object> aHandleOptions = new CommonsHashMap<>();
if (bHasRetries)
aHandleOptions.put(IProcessorResenderModule.OPTION_RETRIES, Integer.toString(aSettings.getRetryCount()));
// Content-Transfer-Encoding is a partnership property
aPartnership.setContentTransferEncodingSend(aRequest.getContentTransferEncoding());
aPartnership.setContentTransferEncodingReceive(aRequest.getContentTransferEncoding());
// And create a sender module that directly sends the message
// The message processor registration is required for the resending
// feature
final AS2SenderModule aSender = m_aAS2SenderModuleFactory.get();
aSender.initDynamicComponent(aSession, null);
// Set connect and read timeout
aSender.setConnectionTimeoutMilliseconds(aSettings.getConnectTimeoutMS());
aSender.setReadTimeoutMilliseconds(aSettings.getReadTimeoutMS());
aSender.setQuoteHeaderValues(aSettings.isQuoteHeaderValues());
aSender.setHttpOutgoingDumperFactory(aSettings.getHttpOutgoingDumperFactory());
aSender.setHttpIncomingDumper(aSettings.getHttpIncomingDumper());
if (aSettings.getMICMatchingHandler() != null)
aSender.setMICMatchingHandler(aSettings.getMICMatchingHandler());
aSender.setVerificationCertificateConsumer(aSettings.getVerificationCertificateConsumer());
// Add all custom headers
aMsg.headers().setAllHeaders(aSettings.customHeaders());
// Added sender as processor
aSession.getMessageProcessor().addModule(aSender);
// Main sending
aSender.handle(IProcessorSenderModule.DO_SEND, aMsg, aHandleOptions);
} finally {
aSession.getMessageProcessor().stopActiveModules();
}
} catch (final Exception ex) {
LOGGER.error("Error sending AS2 message", ex);
aResponse.setException(ex);
} finally {
if (aMsg != null && aMsg.getMDN() != null) {
// May be present, even in case of an exception
aResponse.setMDN(aMsg.getMDN());
// Remember the certificate that was used to verify the MDN
final String sReceivedCert = aMsg.attrs().getAsString(AS2Message.ATTRIBUTE_RECEIVED_SIGNATURE_CERTIFICATE);
if (sReceivedCert != null) {
final X509Certificate aReceivedCert = CertificateHelper.convertStringToCertficateOrNull(sReceivedCert);
aResponse.setMDNVerificationCertificate(aReceivedCert);
}
}
}
if (LOGGER.isDebugEnabled())
LOGGER.debug("Response retrieved: " + aResponse.getAsString());
aResponse.setExecutionDuration(aSW.stopAndGetDuration());
return aResponse;
}
Aggregations