Search in sources :

Example 1 with IProcessorResenderModule

use of com.helger.as2lib.processor.resender.IProcessorResenderModule 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;
}
Also used : AS2SenderModule(com.helger.as2lib.processor.sender.AS2SenderModule) IMessage(com.helger.as2lib.message.IMessage) MessagingException(javax.mail.MessagingException) AS2Exception(com.helger.as2lib.exception.AS2Exception) AS2CertificateExistsException(com.helger.as2lib.cert.AS2CertificateExistsException) X509Certificate(java.security.cert.X509Certificate) StopWatch(com.helger.commons.timing.StopWatch) AS2Session(com.helger.as2lib.session.AS2Session) ImmediateResenderModule(com.helger.as2lib.processor.resender.ImmediateResenderModule) Partnership(com.helger.as2lib.partner.Partnership) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) IProcessorResenderModule(com.helger.as2lib.processor.resender.IProcessorResenderModule) Nonnull(javax.annotation.Nonnull)

Aggregations

AS2CertificateExistsException (com.helger.as2lib.cert.AS2CertificateExistsException)1 AS2Exception (com.helger.as2lib.exception.AS2Exception)1 IMessage (com.helger.as2lib.message.IMessage)1 Partnership (com.helger.as2lib.partner.Partnership)1 IProcessorResenderModule (com.helger.as2lib.processor.resender.IProcessorResenderModule)1 ImmediateResenderModule (com.helger.as2lib.processor.resender.ImmediateResenderModule)1 AS2SenderModule (com.helger.as2lib.processor.sender.AS2SenderModule)1 AS2Session (com.helger.as2lib.session.AS2Session)1 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)1 StopWatch (com.helger.commons.timing.StopWatch)1 X509Certificate (java.security.cert.X509Certificate)1 Nonnull (javax.annotation.Nonnull)1 MessagingException (javax.mail.MessagingException)1