use of javax.mail.internet.MimeBodyPart in project nhin-d by DirectProject.
the class EntitySerializer method serialize.
/**
* Serializes a collection of MimeBodyPart to an output stream with a given boundary.
* @param entity The entities to serialize.
* @param boundary The boundary string that will separate each entity.
* @param stream The output stream that the entities will be serialized to.
*/
public void serialize(Collection<MimeBodyPart> parts, String boundary, OutputStream stream) {
if (parts == null || parts.size() == 0) {
throw new IllegalArgumentException();
}
try {
MimeMultipart mm = new MimeMultipart();
for (MimeBodyPart part : parts) {
mm.addBodyPart(part);
}
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
mm.writeTo(oStream);
oStream.flush();
} catch (Exception e) {
throw new MimeException(MimeError.Unexpected, e);
}
}
use of javax.mail.internet.MimeBodyPart in project nhin-d by DirectProject.
the class SplitProviderDirectSignedDataGenerator_generateTest method testGenerate_safeNetHSMSignatureProvider_assertGenerated.
public void testGenerate_safeNetHSMSignatureProvider_assertGenerated() throws Exception {
/**
* This test is only run if a specific SafeNet eToken Pro HSM is connected to the testing
* system. This can be modified for another specific machine and/or token.
*/
pkcs11ProvName = TestUtils.setupSafeNetToken();
if (!StringUtils.isEmpty(pkcs11ProvName)) {
// get a certificate from the key store
final KeyStore ks = KeyStore.getInstance("PKCS11");
ks.load(null, "1Kingpuff".toCharArray());
final Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
final String alias = aliases.nextElement();
final KeyStore.Entry entry = ks.getEntry(alias, null);
if (entry instanceof KeyStore.PrivateKeyEntry) {
KeyStore.PrivateKeyEntry ent = (KeyStore.PrivateKeyEntry) entry;
signerCert = X509CertificateEx.fromX509Certificate((X509Certificate) ent.getCertificate(), ent.getPrivateKey());
break;
}
}
final SplitProviderDirectSignedDataGenerator gen = new SplitProviderDirectSignedDataGenerator(pkcs11ProvName, "BC");
setupSigningInfo(gen);
// create the content
final MimeBodyPart signedContent = new MimeBodyPart();
signedContent.addHeader("To:", "me@you.com");
signedContent.addHeader("From", "test.test.com");
signedContent.setText("Some Text To Sign");
final CMSProcessableBodyPart content = new CMSProcessableBodyPart(signedContent);
final CMSSignedData signedData = gen.generate(content);
validateSignature(signedData);
}
}
use of javax.mail.internet.MimeBodyPart in project nhin-d by DirectProject.
the class SplitProviderDirectSignedDataGenerator_generateTest method testGenerate_differentDefaultSigAndDigestProvider_assertGenerated.
public void testGenerate_differentDefaultSigAndDigestProvider_assertGenerated() throws Exception {
final SplitProviderDirectSignedDataGenerator gen = new SplitProviderDirectSignedDataGenerator("SunRsaSign", "BC");
setupSigningInfo(gen);
// create the content
final MimeBodyPart signedContent = new MimeBodyPart();
signedContent.addHeader("To:", "me@you.com");
signedContent.addHeader("From", "test.test.com");
signedContent.setText("Some Text To Sign");
final CMSProcessableBodyPart content = new CMSProcessableBodyPart(signedContent);
final CMSSignedData signedData = gen.generate(content);
validateSignature(signedData);
}
use of javax.mail.internet.MimeBodyPart in project nhin-d by DirectProject.
the class SplitProviderDirectSignedDataGenerator_generateTest method testGenerate_sameDefaultSigAndDigestProvider_assertGenerated.
public void testGenerate_sameDefaultSigAndDigestProvider_assertGenerated() throws Exception {
final SplitProviderDirectSignedDataGenerator gen = new SplitProviderDirectSignedDataGenerator("", "");
setupSigningInfo(gen);
// create the content
final MimeBodyPart signedContent = new MimeBodyPart();
signedContent.addHeader("To:", "me@you.com");
signedContent.addHeader("From", "test.test.com");
signedContent.setText("Some Text To Sign");
final CMSProcessableBodyPart content = new CMSProcessableBodyPart(signedContent);
final CMSSignedData signedData = gen.generate(content);
validateSignature(signedData);
}
use of javax.mail.internet.MimeBodyPart in project nhin-d by DirectProject.
the class DSNMessageGenerator method generateDSNFailureMessage.
/**
* Generates the DSN message a replacing the existing exchange in body with the DSN message as a MimeMessage object.
* @param txs Collection of correlated Tx objects.
* @param ex The message exchange.
* @throws Exception
*/
@Handler
public void generateDSNFailureMessage(Collection<Tx> txs, Exchange ex) throws Exception {
// change the inbound message body to null
ex.getIn().setBody(null);
// get the message that is being tracked so we can generate an error message for it
Tx messageToTrack = AbstractCompletionCondition.getMessageToTrack(txs);
if (messageToTrack != null) {
// make sure we have incomplete recipients
final Collection<String> incompleteRecips = conditionChecker.getIncompleteRecipients(txs);
if (incompleteRecips != null && !incompleteRecips.isEmpty()) {
InternetAddress originalSender = null;
String originalSubject = "";
InternetAddress postmaster = null;
String originalMessageId = "";
Enumeration<Header> fullMessageHeaders = null;
final List<DSNRecipientHeaders> recipientDSNHeaders = new ArrayList<DSNRecipientHeaders>();
final List<Address> failedRecipAddresses = new ArrayList<Address>();
final TxDetail sender = messageToTrack.getDetail(TxDetailType.FROM);
if (sender != null) {
originalSender = new InternetAddress(sender.getDetailValue());
postmaster = new InternetAddress(postmasterMailbox + "@" + getAddressDomain(originalSender));
}
final TxDetail subject = messageToTrack.getDetail(TxDetailType.SUBJECT);
if (subject != null)
originalSubject = subject.getDetailValue();
for (String incompleteRecip : incompleteRecips) {
final Address failedRecipAddress = new InternetAddress(incompleteRecip);
DSNRecipientHeaders dsnRecipHeaders = new DSNRecipientHeaders(DSNAction.FAILED, DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.UNDEFINED_STATUS), failedRecipAddress);
recipientDSNHeaders.add(dsnRecipHeaders);
failedRecipAddresses.add(failedRecipAddress);
}
///CLOVER:OFF
final TxDetail origMessId = messageToTrack.getDetail(TxDetailType.MSG_ID);
if (origMessId != null)
originalMessageId = origMessId.getDetailValue();
///CLOVER:ON
final DSNMessageHeaders messageDSNHeaders = new DSNMessageHeaders(reportingMta, originalMessageId, MtaNameType.DNS);
final TxDetail fullHeaders = messageToTrack.getDetail(TxDetailType.MSG_FULL_HEADERS);
if (fullHeaders != null)
fullMessageHeaders = this.convertStringToHeaders(fullHeaders.getDetailValue());
final MimeBodyPart textBodyPart = textGenerator.generate(originalSender, failedRecipAddresses, fullMessageHeaders);
final MimeMessage dnsMessage = generator.createDSNMessage(originalSender, originalSubject, postmaster, recipientDSNHeaders, messageDSNHeaders, textBodyPart);
ex.getIn().setBody(dnsMessage);
}
}
}
Aggregations