NIFI-2519 polishing remote address formatting to avoid NPE and other potential exceptions.
This closes #871 Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/a0fab15e Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/a0fab15e Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/a0fab15e Branch: refs/heads/master Commit: a0fab15eb747426dd4be8cae5378715dd80f6d55 Parents: e7288af Author: Oleg Zhurakousky <o...@suitcase.io> Authored: Wed Aug 17 14:57:42 2016 -0400 Committer: Oleg Zhurakousky <o...@suitcase.io> Committed: Wed Aug 17 15:13:05 2016 -0400 ---------------------------------------------------------------------- .../nifi/processors/email/smtp/SmtpConsumer.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/a0fab15e/nifi-nar-bundles/nifi-email-bundle/nifi-email-processors/src/main/java/org/apache/nifi/processors/email/smtp/SmtpConsumer.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-email-bundle/nifi-email-processors/src/main/java/org/apache/nifi/processors/email/smtp/SmtpConsumer.java b/nifi-nar-bundles/nifi-email-bundle/nifi-email-processors/src/main/java/org/apache/nifi/processors/email/smtp/SmtpConsumer.java index a6d7b33..10eea88 100644 --- a/nifi-nar-bundles/nifi-email-bundle/nifi-email-processors/src/main/java/org/apache/nifi/processors/email/smtp/SmtpConsumer.java +++ b/nifi-nar-bundles/nifi-email-bundle/nifi-email-processors/src/main/java/org/apache/nifi/processors/email/smtp/SmtpConsumer.java @@ -19,6 +19,8 @@ package org.apache.nifi.processors.email.smtp; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.SocketAddress; import java.security.cert.Certificate; import java.security.cert.X509Certificate; import java.util.ArrayList; @@ -28,6 +30,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; + import org.apache.commons.io.IOUtils; import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.flowfile.attributes.CoreAttributes; @@ -38,7 +41,6 @@ import org.apache.nifi.processor.exception.FlowFileAccessException; import org.apache.nifi.processors.email.ListenSMTP; import org.apache.nifi.stream.io.LimitingInputStream; import org.apache.nifi.util.StopWatch; - import org.subethamail.smtp.MessageContext; import org.subethamail.smtp.MessageHandler; import org.subethamail.smtp.RejectException; @@ -148,8 +150,16 @@ public class SmtpConsumer implements MessageHandler { } } + SocketAddress address = context.getRemoteAddress(); + if (address != null) { + // will extract and format source address if available + String strAddress = address instanceof InetSocketAddress + ? ((InetSocketAddress) address).getHostString() + ":" + ((InetSocketAddress) address).getPort() + : context.getRemoteAddress().toString(); + attributes.put("smtp.src", strAddress); + } + attributes.put("smtp.helo", context.getHelo()); - attributes.put("smtp.src", context.getRemoteAddress().toString().substring(1)); attributes.put("smtp.from", from); for (int i = 0; i < recipientList.size(); i++) { attributes.put("smtp.recipient." + i, recipientList.get(i));