use of com.helger.as2lib.cert.AS2CertificateNotFoundException in project as2-lib by phax.
the class AS2Helper method createMDNData.
/**
* Create and fill the MDN parameter
*
* @param aSession
* Session to retrieve the certificate factory for signing
* @param aMdn
* The MDN object to be filled
* @param bSignMDN
* <code>true</code> to sign the MDN
* @param bIncludeCertificateInSignedContent
* <code>true</code> if the passed certificate should be part of the
* signed content, <code>false</code> if the certificate should not be
* put in the content. E.g. for PEPPOL this must be <code>true</code>.
* @param eMICAlg
* The MIC algorithm to be used. Must be present if bSignMDN is
* <code>true</code>.
* @param bUseOldRFC3851MicAlgs
* <code>true</code> to use the old RFC 3851 MIC algorithm names (e.g.
* <code>sha1</code>), <code>false</code> to use the new RFC 5751 MIC
* algorithm names (e.g. <code>sha-1</code>).
* @param bRemoveCmsAlgorithmProtect
* if <code>true</code>, the CMS attribute "AlgorithmProtect" will be
* removed. This is needed in compatibility with e.g. IBM Sterling.
* Default value should be <code>false</code>. Since 4.10.1. See Issue
* #137.
* @throws Exception
* In case something internally goes wrong
*/
public static void createMDNData(@Nonnull final IAS2Session aSession, @Nonnull final IMessageMDN aMdn, final boolean bSignMDN, final boolean bIncludeCertificateInSignedContent, @Nullable final ECryptoAlgorithmSign eMICAlg, final boolean bUseOldRFC3851MicAlgs, final boolean bRemoveCmsAlgorithmProtect) throws Exception {
ValueEnforcer.notNull(aSession, "AS2Session");
ValueEnforcer.notNull(aMdn, "MDN");
if (bSignMDN)
ValueEnforcer.notNull(eMICAlg, "MICAlg");
// Create the report and sub-body parts
final MimeMultipart aReportParts = new MimeMultipart();
// Create the text part
final MimeBodyPart aTextPart = new MimeBodyPart();
final String sText = aMdn.getText() + CHttp.EOL;
aTextPart.setContent(sText, CMimeType.TEXT_PLAIN.getAsString());
aTextPart.setHeader(CHttpHeader.CONTENT_TYPE, CMimeType.TEXT_PLAIN.getAsString());
aReportParts.addBodyPart(aTextPart);
// Create the report part
final MimeBodyPart aReportPart = new MimeBodyPart();
{
final InternetHeaders aReportValues = new InternetHeaders();
aReportValues.setHeader(HEADER_REPORTING_UA, aMdn.attrs().getAsString(AS2MessageMDN.MDNA_REPORTING_UA));
aReportValues.setHeader(HEADER_ORIGINAL_RECIPIENT, aMdn.attrs().getAsString(AS2MessageMDN.MDNA_ORIG_RECIPIENT));
aReportValues.setHeader(HEADER_FINAL_RECIPIENT, aMdn.attrs().getAsString(AS2MessageMDN.MDNA_FINAL_RECIPIENT));
aReportValues.setHeader(HEADER_ORIGINAL_MESSAGE_ID, aMdn.attrs().getAsString(AS2MessageMDN.MDNA_ORIG_MESSAGEID));
aReportValues.setHeader(HEADER_DISPOSITION, aMdn.attrs().getAsString(AS2MessageMDN.MDNA_DISPOSITION));
aReportValues.setHeader(HEADER_RECEIVED_CONTENT_MIC, aMdn.attrs().getAsString(AS2MessageMDN.MDNA_MIC));
final StringBuilder aReportData = new StringBuilder();
final Enumeration<?> aReportEn = aReportValues.getAllHeaderLines();
while (aReportEn.hasMoreElements()) aReportData.append((String) aReportEn.nextElement()).append(CHttp.EOL);
aReportData.append(CHttp.EOL);
aReportPart.setContent(aReportData.toString(), "message/disposition-notification");
}
aReportPart.setHeader(CHttpHeader.CONTENT_TYPE, "message/disposition-notification");
aReportParts.addBodyPart(aReportPart);
// Convert report parts to MimeBodyPart
final MimeBodyPart aReport = new MimeBodyPart();
aReportParts.setSubType("report; report-type=disposition-notification");
aReport.setContent(aReportParts);
aReport.setHeader(CHttpHeader.CONTENT_TYPE, aReportParts.getContentType());
// Sign the MDN data if needed
if (bSignMDN) {
final ICertificateFactory aCertFactory = aSession.getCertificateFactory();
try {
final X509Certificate aSenderCert = aCertFactory.getCertificate(aMdn, ECertificatePartnershipType.SENDER);
final PrivateKey aSenderKey = aCertFactory.getPrivateKey(aSenderCert);
final MimeBodyPart aSignedReport = getCryptoHelper().sign(aReport, aSenderCert, aSenderKey, eMICAlg, bIncludeCertificateInSignedContent, bUseOldRFC3851MicAlgs, bRemoveCmsAlgorithmProtect, EContentTransferEncoding.BASE64);
aMdn.setData(aSignedReport);
if (LOGGER.isInfoEnabled())
LOGGER.info("Successfully signed outgoing MDN message" + aMdn.getLoggingText());
} catch (final AS2CertificateNotFoundException | AS2KeyNotFoundException ex) {
ex.terminate();
LOGGER.warn("Failed to sign MDN - using an unsigned MDN instead");
aMdn.setData(aReport);
}
} else {
// No signing needed
aMdn.setData(aReport);
}
// Update the MDN headers with content information
final MimeBodyPart aData = aMdn.getData();
aMdn.headers().setContentType(aData.getContentType());
// final int size = getSize (aData);
// aMdn.setHeader (CAS2Header.HEADER_CONTENT_LENGTH, Integer.toString
// (size));
}
Aggregations