/****************************************************************
 * 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.james.protocols.smtp.core.esmtp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.james.protocols.api.CommandHandler;
import org.apache.james.protocols.api.Request;
import org.apache.james.protocols.api.Response;
import org.apache.james.protocols.smtp.SMTPResponse;
import org.apache.james.protocols.smtp.SMTPRetCode;
import org.apache.james.protocols.smtp.SMTPSession;
import org.apache.james.protocols.smtp.dsn.DSNStatus;

/**
 * Handles STARTTLS command
 */
public class StartTlsCmdHandler implements CommandHandler<SMTPSession>, EhloExtension {
    /**
     * The name of the command handled by the command handler
     */
    private final static String COMMAND_NAME = "STARTTLS";

    /**
     * @see org.apache.james.smtpserver.protocol.CommandHandler#getImplCommands()
     */
    public Collection<String> getImplCommands() {
        Collection<String> commands = new ArrayList<String>();
        commands.add(COMMAND_NAME);
        return commands;
    }

    /**
     * Handler method called upon receipt of a STARTTLS command. Resets
     * message-specific, but not authenticated user, state.
     * 
     */
    public Response onCommand(SMTPSession session, Request request) {
        SMTPResponse response = null;
        String command = request.getCommand();
        String parameters = request.getArgument();
        if (session.isStartTLSSupported()) {
            if (session.isTLSStarted()) {
                response = new SMTPResponse("500", DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.DELIVERY_INVALID_CMD) + " TLS already active RFC2487 5.2");
            } else {
                if ((parameters == null) || (parameters.length() == 0)) {
                    response = new SMTPResponse("220", DSNStatus.getStatus(DSNStatus.SUCCESS, DSNStatus.UNDEFINED_STATUS) + " Ready to start TLS");
                } else {
                    response = new SMTPResponse("501 " + DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.DELIVERY_INVALID_ARG) + " Syntax error (no parameters allowed) with STARTTLS command");
                }
                session.writeResponse(response);
                try {
                    if (!session.isTLSStarted()) {
                        session.startTLS();
                        // force reset
                        session.resetState();
                    }
                } catch (IOException e) {
                    return new SMTPResponse(SMTPRetCode.LOCAL_ERROR, "TLS not available due to temporary reason");
                }
            }

        } else {
            StringBuilder result = new StringBuilder();
            result.append(DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.DELIVERY_INVALID_CMD)).append(" Command ").append(command).append(" unrecognized.");
            response = new SMTPResponse(SMTPRetCode.SYNTAX_ERROR_COMMAND_UNRECOGNIZED, result);
        }
        return null;
    }

    /**
     * @see org.apache.james.protocols.smtp.core.esmtp.EhloExtension#getImplementedEsmtpFeatures(org.apache.james.protocols.smtp.SMTPSession)
     */
    public List<String> getImplementedEsmtpFeatures(SMTPSession session) {
        List<String> esmtpextensions = new ArrayList<String>();
        // SMTP STARTTLS
        if (!session.isTLSStarted() && session.isStartTLSSupported()) {
            esmtpextensions.add("STARTTLS");
        }
        return esmtpextensions;

    }

}

@@ -2477,46 +2477,8 @@
 ) %7B%0A
-        SMTPResponse response = null;%0A
     
@@ -2666,32 +2666,45 @@
                 
+SMTPResponse 
 response = new S
@@ -2841,32 +2841,104 @@
             
-%7D else %7B
+    return response;%0A            %7D else %7B%0A                SMTPResponse response;
 %0A           
@@ -3652,32 +3652,53 @@
               %7D%0A
+                    %0A
                 
@@ -4110,32 +4110,45 @@
 %22);%0A            
+SMTPResponse 
 response = new S
@@ -4215,16 +4215,45 @@
 esult);%0A
+            return response;%0A
         

Make sure we return the SMTPResponse back to client if needed. Thanks to Felix for spot this!