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.HeloHook;
030    import org.apache.james.protocols.smtp.hook.HookResult;
031    
032    /**
033     * Handles HELO command
034     */
035    public class HeloCmdHandler extends AbstractHookableCmdHandler<HeloHook> {
036    
037        /**
038         * The name of the command handled by the command handler
039         */
040        private final static String COMMAND_NAME = "HELO";
041    
042        /**
043         * @see org.apache.james.protocols.api.handler.CommandHandler#getImplCommands()
044         */
045        public Collection<String> getImplCommands() {
046            Collection<String> implCommands = new ArrayList<String>();
047            implCommands.add(COMMAND_NAME);
048    
049            return implCommands;
050        }
051    
052        /**
053         * @see org.apache.james.protocols.smtp.core.AbstractHookableCmdHandler#doCoreCmd(org.apache.james.protocols.smtp.SMTPSession,
054         *      java.lang.String, java.lang.String)
055         */
056        protected SMTPResponse doCoreCmd(SMTPSession session, String command,
057                String parameters) {
058            session.getConnectionState().put(SMTPSession.CURRENT_HELO_MODE,
059                    COMMAND_NAME);
060            StringBuilder response = new StringBuilder();
061            response.append(session.getHelloName()).append(
062                    " Hello ").append(parameters).append(" (").append(
063                    session.getRemoteHost()).append(" [").append(
064                    session.getRemoteIPAddress()).append("])");
065            return new SMTPResponse(SMTPRetCode.MAIL_OK, response);
066        }
067    
068        /**
069         * @see org.apache.james.protocols.smtp.core.AbstractHookableCmdHandler#doFilterChecks(org.apache.james.protocols.smtp.SMTPSession,
070         *      java.lang.String, java.lang.String)
071         */
072        protected SMTPResponse doFilterChecks(SMTPSession session, String command,
073                String parameters) {
074            session.resetState();
075    
076            if (parameters == null) {
077                return new SMTPResponse(SMTPRetCode.SYNTAX_ERROR_ARGUMENTS,
078                        DSNStatus.getStatus(DSNStatus.PERMANENT,
079                                DSNStatus.DELIVERY_INVALID_ARG)
080                                + " Domain address required: " + COMMAND_NAME);
081            } else {
082                // store provided name
083                session.getState().put(SMTPSession.CURRENT_HELO_NAME, parameters);
084                return null;
085            }
086        }
087    
088        /**
089         * {@inheritDoc}
090         */
091        protected Class<HeloHook> getHookInterface() {
092            return HeloHook.class;
093        }
094    
095    
096        /**
097         * {@inheritDoc}
098         */
099        protected HookResult callHook(HeloHook rawHook, SMTPSession session, String parameters) {
100            return rawHook.doHelo(session, parameters);
101        }
102    
103    
104    }