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    package org.apache.james.protocols.smtp;
020    
021    import java.util.Collection;
022    
023    import org.apache.james.protocols.api.ProtocolTransport;
024    import org.apache.james.protocols.api.AbstractSession;
025    import org.apache.james.protocols.api.Response;
026    import org.apache.james.protocols.api.handler.LineHandler;
027    import org.apache.james.protocols.smtp.SMTPConfiguration;
028    import org.apache.james.protocols.smtp.SMTPSession;
029    import org.slf4j.Logger;
030    
031    /**
032     * {@link SMTPSession} implementation
033     */
034    public class SMTPSessionImpl extends AbstractSession implements SMTPSession {
035        public final static String SMTP_SESSION = "SMTP_SESSION";
036    
037        private boolean relayingAllowed;
038    
039    
040        private SMTPConfiguration theConfigData;
041    
042        public SMTPSessionImpl(SMTPConfiguration theConfigData, Logger logger, ProtocolTransport transport) {
043            super(logger, transport);
044            this.theConfigData = theConfigData;
045            relayingAllowed = theConfigData.isRelayingAllowed(getRemoteIPAddress());
046        }
047    
048    
049        /**
050         * @see org.apache.james.protocols.smtp.SMTPSession#isRelayingAllowed()
051         */
052        public boolean isRelayingAllowed() {
053            return relayingAllowed;
054        }
055    
056        /**
057         * @see org.apache.james.protocols.smtp.SMTPSession#resetState()
058         */
059        public void resetState() {
060            // remember the ehlo mode between resets
061            Object currentHeloMode = getState().get(CURRENT_HELO_MODE);
062    
063            getState().clear();
064    
065            // start again with the old helo mode
066            if (currentHeloMode != null) {
067                getState().put(CURRENT_HELO_MODE, currentHeloMode);
068            }
069        }
070    
071        /**
072         * @see org.apache.james.protocols.smtp.SMTPSession#popLineHandler()
073         */
074        public void popLineHandler() {
075            transport.popLineHandler();
076        }
077    
078        /**
079         * @see org.apache.james.protocols.smtp.SMTPSession#pushLineHandler(LineHandler)
080         */
081        public void pushLineHandler(LineHandler<SMTPSession> overrideCommandHandler) {
082            transport.pushLineHandler(overrideCommandHandler, this);
083        }
084    
085        /**
086         * @see org.apache.james.protocols.smtp.SMTPSession#getHelloName()
087         */
088        public String getHelloName() {
089            return theConfigData.getHelloName();
090        }
091    
092        /**
093         * @see org.apache.james.protocols.smtp.SMTPSession#getMaxMessageSize()
094         */
095        public long getMaxMessageSize() {
096            return theConfigData.getMaxMessageSize();
097        }
098    
099        /**
100         * @see org.apache.james.protocols.smtp.SMTPSession#getRcptCount()
101         */
102        @SuppressWarnings("unchecked")
103        public int getRcptCount() {
104            int count = 0;
105    
106            // check if the key exists
107            if (getState().get(SMTPSession.RCPT_LIST) != null) {
108                count = ((Collection) getState().get(SMTPSession.RCPT_LIST)).size();
109            }
110    
111            return count;
112        }
113    
114        /**
115         * @see org.apache.james.protocols.smtp.SMTPSession#getSMTPGreeting()
116         */
117        public String getSMTPGreeting() {
118            return theConfigData.getSMTPGreeting();
119        }
120    
121        /**
122         * @see org.apache.james.protocols.smtp.SMTPSession#isAuthSupported()
123         */
124        public boolean isAuthSupported() {
125            return theConfigData.isAuthRequired(socketAddress.getAddress().getHostAddress());
126        }
127    
128        /**
129         * @see org.apache.james.protocols.smtp.SMTPSession#setRelayingAllowed(boolean)
130         */
131        public void setRelayingAllowed(boolean relayingAllowed) {
132            this.relayingAllowed = relayingAllowed;
133        }
134    
135        /**
136         * @see org.apache.james.protocols.smtp.SMTPSession#useAddressBracketsEnforcement()
137         */
138        public boolean useAddressBracketsEnforcement() {
139            return theConfigData.useAddressBracketsEnforcement();
140        }
141    
142        /**
143         * @see org.apache.james.protocols.smtp.SMTPSession#useHeloEhloEnforcement()
144         */
145        public boolean useHeloEhloEnforcement() {
146            return theConfigData.useHeloEhloEnforcement();
147        }
148    
149        /**
150         * @see
151         * org.apache.james.protocols.smtp.SMTPSession#getPushedLineHandlerCount()
152         */
153        public int getPushedLineHandlerCount() {
154            return transport.getPushedLineHandlerCount();
155        }
156    
157        public Response newLineTooLongResponse() {
158            return new SMTPResponse(SMTPRetCode.SYNTAX_ERROR_COMMAND_UNRECOGNIZED, "Line length exceeded. See RFC 2821 #4.5.3.1.");
159        }
160    
161        public Response newFatalErrorResponse() {
162            return new SMTPResponse(SMTPRetCode.LOCAL_ERROR, "Unable to process request");
163        }
164    
165        @Override
166        public boolean isStartTLSSupported() {
167            return super.isStartTLSSupported() && theConfigData.isStartTLSSupported();
168        }
169        
170        
171    }