Search in sources :

Example 1 with AS2PartnershipNotFoundException

use of com.helger.as2lib.partner.AS2PartnershipNotFoundException in project as2-lib by phax.

the class AS2Helper method createMDN.

/**
 * Create a new MDN
 *
 * @param aSession
 *        AS2 session to be used. May not be <code>null</code>.
 * @param aMsg
 *        The source AS2 message for which the MDN is to be created. May not
 *        be <code>null</code>.
 * @param aDisposition
 *        The disposition - either success or error. May not be
 *        <code>null</code>.
 * @param sText
 *        The text to be send. May not be <code>null</code>.
 * @return The created MDN object which is already attached to the passed
 *         source AS2 message.
 * @throws Exception
 *         In case of an error
 */
@Nonnull
public static IMessageMDN createMDN(@Nonnull final IAS2Session aSession, @Nonnull final AS2Message aMsg, @Nonnull final DispositionType aDisposition, @Nonnull final String sText) throws Exception {
    ValueEnforcer.notNull(aSession, "AS2Session");
    ValueEnforcer.notNull(aMsg, "AS2Message");
    ValueEnforcer.notNull(aDisposition, "Disposition");
    ValueEnforcer.notNull(sText, "Text");
    final Partnership aPartnership = aMsg.partnership();
    final AS2MessageMDN aMDN = new AS2MessageMDN(aMsg);
    aMDN.headers().setHeader(CHttpHeader.AS2_VERSION, aSession.getAS2VersionID());
    aMDN.headers().setHeader(CHttpHeader.DATE, AS2DateHelper.getFormattedDateNow(CAS2Header.DEFAULT_DATE_FORMAT));
    aMDN.headers().setHeader(CHttpHeader.SERVER, CAS2Info.NAME_VERSION);
    aMDN.headers().setHeader(CHttpHeader.MIME_VERSION, CAS2Header.DEFAULT_MIME_VERSION);
    aMDN.headers().setHeader(CHttpHeader.AS2_FROM, aPartnership.getReceiverAS2ID());
    aMDN.headers().setHeader(CHttpHeader.AS2_TO, aPartnership.getSenderAS2ID());
    // get the MDN partnership info
    aMDN.partnership().setSenderAS2ID(aMDN.getHeader(CHttpHeader.AS2_FROM));
    aMDN.partnership().setReceiverAS2ID(aMDN.getHeader(CHttpHeader.AS2_TO));
    // Set the appropriate key store aliases
    aMDN.partnership().setSenderX509Alias(aPartnership.getReceiverX509Alias());
    aMDN.partnership().setReceiverX509Alias(aPartnership.getSenderX509Alias());
    // Update the partnership
    try {
        aSession.getPartnershipFactory().updatePartnership(aMDN, true);
    } catch (final AS2PartnershipNotFoundException ex) {
    // This would block sending an MDN in case a PartnershipNotFoundException
    // was the reason for sending the MDN :)
    }
    aMDN.headers().setHeader(CHttpHeader.FROM, aPartnership.getReceiverEmail());
    final String sSubject = aMDN.partnership().getMDNSubject();
    if (sSubject != null) {
        aMDN.headers().setHeader(CHttpHeader.SUBJECT, new MessageParameters(aMsg).format(sSubject));
    } else {
        aMDN.headers().setHeader(CHttpHeader.SUBJECT, "Your Requested MDN Response");
    }
    // Content-Transfer-Encoding for outgoing MDNs
    final String sCTE = aPartnership.getContentTransferEncodingSend(EContentTransferEncoding.AS2_DEFAULT.getID());
    aMDN.headers().addHeader(CHttpHeader.CONTENT_TRANSFER_ENCODING, sCTE);
    aMDN.setText(new MessageParameters(aMsg).format(sText));
    aMDN.attrs().putIn(AS2MessageMDN.MDNA_REPORTING_UA, CAS2Info.NAME_VERSION + "@" + aMsg.attrs().getAsString(CNetAttribute.MA_DESTINATION_IP) + ":" + aMsg.attrs().getAsString(CNetAttribute.MA_DESTINATION_PORT));
    aMDN.attrs().putIn(AS2MessageMDN.MDNA_ORIG_RECIPIENT, "rfc822; " + aMsg.getHeader(CHttpHeader.AS2_TO));
    aMDN.attrs().putIn(AS2MessageMDN.MDNA_FINAL_RECIPIENT, "rfc822; " + aPartnership.getReceiverAS2ID());
    aMDN.attrs().putIn(AS2MessageMDN.MDNA_ORIG_MESSAGEID, aMsg.getHeader(CHttpHeader.MESSAGE_ID));
    aMDN.attrs().putIn(AS2MessageMDN.MDNA_DISPOSITION, aDisposition.getAsString());
    final String sDispositionOptions = aMsg.getHeader(CHttpHeader.DISPOSITION_NOTIFICATION_OPTIONS);
    final DispositionOptions aDispositionOptions = DispositionOptions.createFromString(sDispositionOptions);
    ECryptoAlgorithmSign eSigningAlgorithm = aDispositionOptions.getFirstMICAlg();
    if (eSigningAlgorithm == null) {
        // Try from partnership (#93)
        final String sSigningAlgorithm = aPartnership.getSigningAlgorithm();
        eSigningAlgorithm = ECryptoAlgorithmSign.getFromIDOrNull(sSigningAlgorithm);
        if (eSigningAlgorithm == null) {
            if (LOGGER.isWarnEnabled())
                LOGGER.warn("The partnership signing algorithm name '" + sSigningAlgorithm + "' is unknown.");
        }
    }
    MIC aMIC = null;
    if (eSigningAlgorithm != null) {
        // If the source message was signed or encrypted, include the headers -
        // see message sending for details
        final boolean bIncludeHeadersInMIC = aPartnership.getSigningAlgorithm() != null || aPartnership.getEncryptAlgorithm() != null || aPartnership.getCompressionType() != null;
        aMIC = getCryptoHelper().calculateMIC(aMsg.getData(), eSigningAlgorithm, bIncludeHeadersInMIC);
    }
    if (aMIC != null)
        aMDN.attrs().putIn(AS2MessageMDN.MDNA_MIC, aMIC.getAsAS2String());
    boolean bSignMDN = false;
    boolean bIncludeCertificateInSignedContent = false;
    if (aDispositionOptions.getProtocol() != null) {
        if (aDispositionOptions.isProtocolRequired() || aDispositionOptions.hasMICAlg()) {
            // Sign if required or if optional and a MIC algorithm is present
            bSignMDN = true;
            // Include certificate in signed content?
            final ETriState eIncludeCertificateInSignedContent = aPartnership.getIncludeCertificateInSignedContent();
            if (eIncludeCertificateInSignedContent.isDefined()) {
                // Use per partnership
                bIncludeCertificateInSignedContent = eIncludeCertificateInSignedContent.getAsBooleanValue();
            } else {
                // Use global value
                bIncludeCertificateInSignedContent = aSession.isCryptoSignIncludeCertificateInBodyPart();
            }
        }
    }
    final boolean bUseOldRFC3851MicAlgs = aPartnership.isRFC3851MICAlgs();
    final boolean bRemoveCmsAlgorithmProtect = aPartnership.isRemoveCmsAlgorithmProtect();
    createMDNData(aSession, aMDN, bSignMDN, bIncludeCertificateInSignedContent, aDispositionOptions.getFirstMICAlg(), bUseOldRFC3851MicAlgs, bRemoveCmsAlgorithmProtect);
    aMDN.updateMessageID();
    // store MDN into msg in case AsynchMDN is sent fails, needs to be resent by
    // send module
    aMsg.setMDN(aMDN);
    return aMDN;
}
Also used : AS2MessageMDN(com.helger.as2lib.message.AS2MessageMDN) Partnership(com.helger.as2lib.partner.Partnership) ETriState(com.helger.commons.state.ETriState) DispositionOptions(com.helger.as2lib.disposition.DispositionOptions) MIC(com.helger.as2lib.crypto.MIC) MessageParameters(com.helger.as2lib.params.MessageParameters) AS2PartnershipNotFoundException(com.helger.as2lib.partner.AS2PartnershipNotFoundException) ECryptoAlgorithmSign(com.helger.as2lib.crypto.ECryptoAlgorithmSign) Nonnull(javax.annotation.Nonnull)

Aggregations

ECryptoAlgorithmSign (com.helger.as2lib.crypto.ECryptoAlgorithmSign)1 MIC (com.helger.as2lib.crypto.MIC)1 DispositionOptions (com.helger.as2lib.disposition.DispositionOptions)1 AS2MessageMDN (com.helger.as2lib.message.AS2MessageMDN)1 MessageParameters (com.helger.as2lib.params.MessageParameters)1 AS2PartnershipNotFoundException (com.helger.as2lib.partner.AS2PartnershipNotFoundException)1 Partnership (com.helger.as2lib.partner.Partnership)1 ETriState (com.helger.commons.state.ETriState)1 Nonnull (javax.annotation.Nonnull)1