001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    
020    package org.apache.james.protocols.smtp.core;
021    
022    import java.util.ArrayList;
023    import java.util.Collection;
024    
025    import org.apache.james.protocols.smtp.SMTPResponse;
026    import org.apache.james.protocols.smtp.SMTPRetCode;
027    import org.apache.james.protocols.smtp.SMTPSession;
028    import org.apache.james.protocols.smtp.dsn.DSNStatus;
029    import org.apache.james.protocols.smtp.hook.HookResult;
030    import org.apache.james.protocols.smtp.hook.QuitHook;
031    
032    /**
033     * Handles QUIT command
034     */
035    public class QuitCmdHandler extends AbstractHookableCmdHandler<QuitHook> {
036    
037        /**
038         * The name of the command handled by the command handler
039         */
040        private final static String COMMAND_NAME = "QUIT";
041    
042        /**
043         * Handler method called upon receipt of a QUIT command. This method informs
044         * the client that the connection is closing.
045         * 
046         * @param session
047         *            SMTP session object
048         * @param argument
049         *            the argument passed in with the command by the SMTP client
050         */
051        private SMTPResponse doQUIT(SMTPSession session, String argument) {
052            SMTPResponse ret;
053            if ((argument == null) || (argument.length() == 0)) {
054                StringBuilder response = new StringBuilder();
055                response.append(
056                        DSNStatus.getStatus(DSNStatus.SUCCESS,
057                                DSNStatus.UNDEFINED_STATUS)).append(" ").append(
058                        session.getHelloName()).append(
059                        " Service closing transmission channel");
060                ret = new SMTPResponse(SMTPRetCode.SYSTEM_QUIT, response);
061            } else {
062                ret = new SMTPResponse(
063                        SMTPRetCode.SYNTAX_ERROR_COMMAND_UNRECOGNIZED, DSNStatus
064                                .getStatus(DSNStatus.PERMANENT,
065                                        DSNStatus.DELIVERY_INVALID_ARG)
066                                + " Unexpected argument provided with QUIT command");
067            }
068            ret.setEndSession(true);
069            return ret;
070        }
071    
072        /**
073         * @see org.apache.james.protocols.api.handler.CommandHandler#getImplCommands()
074         */
075        public Collection<String> getImplCommands() {
076            Collection<String> implCommands = new ArrayList<String>();
077            implCommands.add(COMMAND_NAME);
078    
079            return implCommands;
080        }
081    
082        /**
083         * @see org.apache.james.protocols.smtp.core.AbstractHookableCmdHandler#doCoreCmd(org.apache.james.protocols.smtp.SMTPSession,
084         *      java.lang.String, java.lang.String)
085         */
086        protected SMTPResponse doCoreCmd(SMTPSession session, String command,
087                String parameters) {
088            return doQUIT(session, parameters);
089        }
090    
091        /**
092         * @see org.apache.james.protocols.smtp.core.AbstractHookableCmdHandler#doFilterChecks(org.apache.james.protocols.smtp.SMTPSession,
093         *      java.lang.String, java.lang.String)
094         */
095        protected SMTPResponse doFilterChecks(SMTPSession session, String command,
096                String parameters) {
097            return null;
098        }
099    
100        /**
101         * @see org.apache.james.protocols.smtp.core.AbstractHookableCmdHandler#getHookInterface()
102         */
103        protected Class<QuitHook> getHookInterface() {
104            return QuitHook.class;
105        }
106    
107        /**
108         * {@inheritDoc}
109         */
110        protected HookResult callHook(QuitHook rawHook, SMTPSession session, String parameters) {
111            return rawHook.doQuit(session);
112        }
113    
114    }