use of org.apache.camel.Handler 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);
}
}
}
use of org.apache.camel.Handler in project nhin-d by DirectProject.
the class DSNMailSender method sendMail.
/**
* Processor handler method. This method sends the message to SMTP gateway specified in the gateway URL.
* The SMTP to and from headers and taken from the actual message.
* @param exchange The exchange that holds the message.
* @throws Exception
*/
@Handler
public void sendMail(Exchange exchange) throws Exception {
if (gatewayHost == null || gatewayHost.isEmpty())
throw new IllegalStateException("Gateway URL is null or empty");
// simple SMTP converation
if (clientFactory == null)
throw new IllegalStateException("SMTP client cannot be null");
if (exchange.getIn() == null || exchange.getIn().getBody() == null)
return;
final MimeMessage dsnMessage = (MimeMessage) exchange.getIn().getBody();
final String recpList = dsnMessage.getHeader(MailStandard.Headers.To, ",");
final String sender = dsnMessage.getHeader(MailStandard.Headers.From, ",");
SMTPClient client = clientFactory.createInstance();
client.connect(gatewayHost, gatewayPort);
client.helo(localhost);
if (!client.setSender(sender))
throw new IOException("Failed to set sender.");
final String[] recips = recpList.split(",");
for (String recip : recips) {
if (!client.addRecipient(recip))
throw new IOException("Failed to set recipient " + recip);
}
final Writer writer = client.sendMessageData();
if (writer == null)
throw new IOException("Failed to get data body writer.");
final ByteArrayOutputStream writerStream = new ByteArrayOutputStream();
try {
dsnMessage.writeTo(writerStream);
IOUtils.write(writerStream.toByteArray(), writer);
writer.close();
client.completePendingCommand();
if (client.getReplyCode() != SMTP_OK)
throw new IOException("Failed complete data command with error code " + client.getReplyCode());
} finally {
IOUtils.closeQuietly(writerStream);
IOUtils.closeQuietly(writer);
client.quit();
client.disconnect();
}
}
use of org.apache.camel.Handler in project nhin-d by DirectProject.
the class DefaultBundleCacheUpdateProcessorImpl method updateBundleCache.
/**
* {@inheritDoc}
*/
@Handler
public void updateBundleCache() {
Collection<TrustBundle> bundles;
try {
bundles = dao.getTrustBundles();
for (TrustBundle bundle : bundles) {
boolean refresh = false;
// if the refresh interval is 0 or less, then we won't ever auto refresh the bundle
if (bundle.getRefreshInterval() <= 0)
continue;
// see if this bundle needs to be checked for updating
final Calendar lastAttempt = bundle.getLastSuccessfulRefresh();
if (lastAttempt == null)
// never been attempted successfully... better go get it
refresh = true;
else {
// check the the last attempt date against now and see if we need to refresh
long now = System.currentTimeMillis();
Calendar lastAttemptCheck = (Calendar) lastAttempt.clone();
lastAttemptCheck.add(Calendar.SECOND, bundle.getRefreshInterval());
if (lastAttemptCheck.getTimeInMillis() <= now)
refresh = true;
}
if (refresh) {
// refresh the bundle
try {
refreshProcessor.refreshBundle(bundle);
} catch (Exception e) {
log.warn("Failed to check the status of bundle " + bundle.getBundleName(), e);
}
}
}
} catch (Exception e) {
log.warn("Failed to check the status of trust bundles ", e);
}
}
use of org.apache.camel.Handler in project nhin-d by DirectProject.
the class DefaultBundleRefreshProcessorImpl method refreshBundle.
/**
* {@inheritDoc}
*/
@Handler
public void refreshBundle(TrustBundle bundle) {
// track when the process started
final Calendar processAttempStart = Calendar.getInstance(Locale.getDefault());
// get the bundle from the URL
final byte[] rawBundle = downloadBundleToByteArray(bundle, processAttempStart);
if (rawBundle == null)
return;
// check to see if there is a difference in the anchor sets
// use a checksum
boolean update = false;
String checkSum = "";
if (bundle.getCheckSum() == null)
// never got a check sum...
update = true;
else {
try {
checkSum = BundleThumbprint.toThumbprint(rawBundle).toString();
update = !bundle.getCheckSum().equals(BundleThumbprint.toThumbprint(rawBundle).toString());
}///CLOVER:OFF
catch (NoSuchAlgorithmException ex) {
dao.updateLastUpdateError(bundle.getId(), processAttempStart, BundleRefreshError.INVALID_BUNDLE_FORMAT);
log.error("Failed to generate downloaded bundle thumbprint ", ex);
}
///CLOVER:ON
}
if (!update) {
dao.updateLastUpdateError(bundle.getId(), processAttempStart, BundleRefreshError.SUCCESS);
return;
}
final Collection<X509Certificate> bundleCerts = convertRawBundleToAnchorCollection(rawBundle, bundle, processAttempStart);
if (bundleCerts == null)
return;
final HashSet<X509Certificate> downloadedSet = new HashSet<X509Certificate>((Collection<X509Certificate>) bundleCerts);
try {
final Collection<TrustBundleAnchor> newAnchors = new ArrayList<TrustBundleAnchor>();
for (X509Certificate downloadedAnchor : downloadedSet) {
try {
final TrustBundleAnchor anchorToAdd = new TrustBundleAnchor();
anchorToAdd.setData(downloadedAnchor.getEncoded());
anchorToAdd.setTrustBundle(bundle);
newAnchors.add(anchorToAdd);
}///CLOVER:OFF
catch (Exception e) {
log.warn("Failed to convert downloaded anchor to byte array. ", e);
}
///CLOVER:ON
}
bundle.setTrustBundleAnchors(newAnchors);
dao.updateTrustBundleAnchors(bundle.getId(), processAttempStart, newAnchors, checkSum);
dao.updateLastUpdateError(bundle.getId(), processAttempStart, BundleRefreshError.SUCCESS);
} catch (ConfigurationStoreException e) {
dao.updateLastUpdateError(bundle.getId(), processAttempStart, BundleRefreshError.INVALID_BUNDLE_FORMAT);
log.error("Failed to write updated bundle anchors to data store ", e);
}
}
Aggregations