Github user JPercivall commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/483#discussion_r70635447
  
    --- Diff: 
nifi-nar-bundles/nifi-email-bundle/nifi-email-processors/src/main/java/org/apache/nifi/processors/email/smtp/handler/SMTPMessageHandlerFactory.java
 ---
    @@ -0,0 +1,159 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.nifi.processors.email.smtp.handler;
    +
    +import java.io.IOException;
    +import java.io.InputStream;
    +import java.security.cert.X509Certificate;
    +import java.util.concurrent.CountDownLatch;
    +import java.util.concurrent.LinkedBlockingQueue;
    +import java.util.concurrent.TimeUnit;
    +
    +import org.apache.nifi.logging.ComponentLog;
    +import org.apache.nifi.stream.io.ByteArrayOutputStream;
    +import org.apache.nifi.util.StopWatch;
    +import org.subethamail.smtp.DropConnectionException;
    +import org.subethamail.smtp.MessageContext;
    +import org.subethamail.smtp.MessageHandler;
    +import org.subethamail.smtp.MessageHandlerFactory;
    +import org.subethamail.smtp.RejectException;
    +import org.subethamail.smtp.TooMuchDataException;
    +import org.subethamail.smtp.server.SMTPServer;
    +
    +import org.apache.nifi.processors.email.smtp.event.SmtpEvent;
    +
    +
    +public class SMTPMessageHandlerFactory implements MessageHandlerFactory {
    +    final LinkedBlockingQueue<SmtpEvent> incomingMessages;
    +    final ComponentLog logger;
    +
    +
    +    public SMTPMessageHandlerFactory(LinkedBlockingQueue<SmtpEvent> 
incomingMessages, ComponentLog logger) {
    +        this.incomingMessages = incomingMessages;
    +        this.logger = logger;
    +
    +    }
    +
    +    @Override
    +    public MessageHandler create(MessageContext messageContext) {
    +        return new Handler(messageContext, incomingMessages, logger);
    +    }
    +
    +    class Handler implements MessageHandler {
    +        final MessageContext messageContext;
    +        String from;
    +        String recipient;
    +        ByteArrayOutputStream messageData;
    +
    +        private CountDownLatch latch;
    +
    +        public Handler(MessageContext messageContext, 
LinkedBlockingQueue<SmtpEvent> incomingMessages, ComponentLog logger){
    +            this.messageContext = messageContext;
    +            this.latch =  new CountDownLatch(1);
    +        }
    +
    +        @Override
    +        public void from(String from) throws RejectException {
    +            // TODO: possibly whitelist senders?
    +            this.from = from;
    +        }
    +
    +        @Override
    +        public void recipient(String recipient) throws RejectException {
    +            // TODO: possibly whitelist receivers?
    +            this.recipient = recipient;
    +        }
    +
    +        @Override
    +        public void data(InputStream inputStream) throws RejectException, 
TooMuchDataException {
    +            // Start counting the timer...
    +
    +            StopWatch watch = new StopWatch(true);
    +
    +            SMTPServer server = messageContext.getSMTPServer();
    +
    +            final long serverTimeout = 
TimeUnit.MILLISECONDS.convert(messageContext.getSMTPServer().getConnectionTimeout(),
 TimeUnit.MILLISECONDS);
    +
    +            ByteArrayOutputStream baos = new ByteArrayOutputStream();
    +
    +            byte [] buffer = new byte[1024];
    +
    +            int rd;
    +
    +            try {
    +                while ((rd = inputStream.read(buffer, 0, buffer.length)) 
!= -1 ) {
    +                    baos.write(buffer, 0, rd);
    +                    if (baos.getBufferLength() > 
server.getMaxMessageSize() ) {
    +                        throw new TooMuchDataException("Data exceeds the 
amount allowed.");
    +                    }
    +                }
    +                baos.flush();
    +            } catch (IOException e) {
    +                throw new DropConnectionException(450, "Unexpected error 
processing your message. ");
    +            }
    +
    +            this.messageData = baos;
    +
    +            X509Certificate[] certificates = new X509Certificate[]{};
    +
    +            String remoteIP = messageContext.getRemoteAddress().toString();
    +            String helo = messageContext.getHelo();
    +            if (messageContext.getTlsPeerCertificates() != null ){
    +                certificates = (X509Certificate[]) 
messageContext.getTlsPeerCertificates().clone();
    +            }
    +
    +            SmtpEvent message = new SmtpEvent(remoteIP, helo, from, 
recipient, certificates, messageData, latch);
    +            // Try to queue the message back to the NiFi session
    +            try {
    +                incomingMessages.offer(message, serverTimeout, 
TimeUnit.MILLISECONDS);
    +            } catch (InterruptedException e) {
    +                logger.info("The SMTP processor has just dropped a message 
due to the queue " +
    +                        "being too full, considering increasing the queue 
size");
    +                throw new DropConnectionException(421, "Could not queue 
the message. Try again ");
    +            }
    +
    +            // Once message has been sent to the queue, it should be 
processed by NiFi onTrigger,
    +            // a flowfile created and its processed status updated before 
an acknowledgment is
    +            // given back to the SMTP client
    +            long elapsed = watch.getElapsed(TimeUnit.MILLISECONDS);
    +            try {
    +                latch.await(serverTimeout - elapsed, 
TimeUnit.MILLISECONDS);
    +            } catch (InterruptedException e) {
    +                if (message.getProcessed() == true) {
    +                    logger.info("Latch opened unexpectedly but processor 
indicates data processed, proceeding normally.");
    +                } else {
    +                    logger.info("Latch opened unexpectedly and processor 
indicates data wasn't processed. Returned error to SMTP client as precautionary 
measure");
    +                    throw new DropConnectionException(423, "Unexpected 
Error. Please try again or contact the administrator in case it persists");
    --- End diff --
    
    If this is thrown, the message will still be in the queue


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to