Index: java/org/apache/james/smtpserver/AddHeaderHandler.java =================================================================== --- java/org/apache/james/smtpserver/AddHeaderHandler.java (revision 234466) +++ java/org/apache/james/smtpserver/AddHeaderHandler.java (working copy) @@ -17,16 +17,19 @@ package org.apache.james.smtpserver; +import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.ConfigurationException; import javax.mail.internet.MimeMessage; + /** * Adds the header to the message */ -public class AddHeaderHandler implements MessageHandler, - Configurable { +public class AddHeaderHandler + extends AbstractLogEnabled + implements MessageHandler, Configurable { /** * The header name and value that needs to be added @@ -65,7 +68,7 @@ } } catch (javax.mail.MessagingException me) { - session.getLogger().error(me.getMessage()); + getLogger().error(me.getMessage()); } } Index: java/org/apache/james/smtpserver/MailCmdHandler.java =================================================================== --- java/org/apache/james/smtpserver/MailCmdHandler.java (revision 234466) +++ java/org/apache/james/smtpserver/MailCmdHandler.java (working copy) @@ -17,6 +17,7 @@ package org.apache.james.smtpserver; +import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.james.util.mail.dsn.DSNStatus; import org.apache.mailet.MailAddress; import java.util.Locale; @@ -25,7 +26,9 @@ /** * Handles MAIL command */ -public class MailCmdHandler implements CommandHandler { +public class MailCmdHandler + extends AbstractLogEnabled + implements CommandHandler { private final static String COMMAND_NAME = "MAIL"; @@ -100,14 +103,14 @@ } } else { // Unexpected option attached to the Mail command - if (session.getLogger().isDebugEnabled()) { + if (getLogger().isDebugEnabled()) { StringBuffer debugBuffer = new StringBuffer(128) .append("MAIL command had unrecognized/unexpected option ") .append(mailOptionName) .append(" with value ") .append(mailOptionValue); - session.getLogger().debug(debugBuffer.toString()); + getLogger().debug(debugBuffer.toString()); } } } @@ -115,13 +118,13 @@ if (!sender.startsWith("<") || !sender.endsWith(">")) { responseString = "501 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+" Syntax error in MAIL command"; session.writeResponse(responseString); - if (session.getLogger().isErrorEnabled()) { + if (getLogger().isErrorEnabled()) { StringBuffer errorBuffer = new StringBuffer(128) .append("Error parsing sender address: ") .append(sender) .append(": did not start and end with < >"); - session.getLogger().error(errorBuffer.toString()); + getLogger().error(errorBuffer.toString()); } return; } @@ -139,14 +142,14 @@ } catch (Exception pe) { responseString = "501 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+" Syntax error in sender address"; session.writeResponse(responseString); - if (session.getLogger().isErrorEnabled()) { + if (getLogger().isErrorEnabled()) { StringBuffer errorBuffer = new StringBuffer(256) .append("Error parsing sender address: ") .append(sender) .append(": ") .append(pe.getMessage()); - session.getLogger().error(errorBuffer.toString()); + getLogger().error(errorBuffer.toString()); } return; } @@ -175,16 +178,16 @@ // This is a malformed option value. We return an error String responseString = "501 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.DELIVERY_INVALID_ARG)+" Syntactically incorrect value for SIZE parameter"; session.writeResponse(responseString); - session.getLogger().error("Rejected syntactically incorrect value for SIZE parameter."); + getLogger().error("Rejected syntactically incorrect value for SIZE parameter."); return false; } - if (session.getLogger().isDebugEnabled()) { + if (getLogger().isDebugEnabled()) { StringBuffer debugBuffer = new StringBuffer(128) .append("MAIL command option SIZE received with value ") .append(size) .append("."); - session.getLogger().debug(debugBuffer.toString()); + getLogger().debug(debugBuffer.toString()); } long maxMessageSize = session.getConfigurationData().getMaxMessageSize(); if ((maxMessageSize > 0) && (size > maxMessageSize)) { @@ -204,7 +207,7 @@ .append(" exceeding system maximum message size of ") .append(maxMessageSize) .append("based on SIZE option."); - session.getLogger().error(errorBuffer.toString()); + getLogger().error(errorBuffer.toString()); return false; } else { // put the message size in the message state so it can be used Index: java/org/apache/james/smtpserver/SMTPHandler.java =================================================================== --- java/org/apache/james/smtpserver/SMTPHandler.java (revision 234466) +++ java/org/apache/james/smtpserver/SMTPHandler.java (working copy) @@ -59,7 +59,7 @@ */ public class SMTPHandler extends AbstractLogEnabled - implements ConnectionHandler, Poolable { + implements ConnectionHandler, Poolable, SMTPSession { /** * The constants to indicate the current processing mode of the session @@ -94,32 +94,28 @@ * The name of the currently parsed command */ String curCommandName = null; - + /** * The value of the currently parsed command */ String curCommandArgument = null; - + /** * The SMTPHandlerChain object set by SMTPServer */ SMTPHandlerChain handlerChain = null; - + + /** - * The per SMTPHandler Session object - */ - private SMTPSession session = new SMTPSessionImpl(); - - /** * The mode of the current session */ private byte mode; - + /** * The MailImpl object set by the DATA command */ private MailImpl mail = null; - + /** * The session termination status */ @@ -328,22 +324,38 @@ .append(rfc822DateFormat.format(new Date())); String responseString = clearResponseBuffer(); writeLoggedFlushedResponse(responseString); - - //the core in protocol handling logic + + //the core in-protocol handling logic //run all the connection handlers, if it fast fails, end the session - //parse the command command, look up for the list command handlers - //execute each of the command handlers. If any command handlers writes - //response then, end the command handler processing and start parsing new command - //Once the message is received, run all the message handlers - //the message handlers can either terminate message or terminate session + //parse the command command, look up for the list of command handlers + //Execute each of the command handlers. If any command handlers writes + //response then, End the subsequent command handler processing and + //start parsing new command. Once the message is received, run all + //the message handlers. The message handlers can either terminate + //message or terminate session + //At the beginning + //mode = command_mode + //once the commandHandler writes response, the mode is changed to RESPONSE_MODE. + //This will cause to skip the subsequent command handlers configured for that command. + //For instance: + //There are 2 commandhandlers MailAddressChecker and MailCmdHandler for + //MAIL command. If MailAddressChecker validation of the MAIL FROM + //address is successful, the MailCmdHandlers will be executed. + //Incase it fails, it has to write response. Once we write response + //there is no need to execute the MailCmdHandler. + //Next, Once MAIL message is received the DataCmdHandler and any other + //equivalent commandHandler will call setMail method. this will change + //he mode to MAIL_RECEIVED_MODE. This mode will trigger the message + //handlers to be execute. Message handlers can abort message. In that case, + //message will not spooled. //Session started - RUN all connect handlers List connectHandlers = handlerChain.getConnectHandlers(); if(connectHandlers != null) { int count = connectHandlers.size(); for(int i = 0; i < count; i++) { - ((ConnectHandler)connectHandlers.get(i)).onConnect(session); + ((ConnectHandler)connectHandlers.get(i)).onConnect(this); if(sessionEnded) { break; } @@ -379,7 +391,7 @@ } else { int count = commandHandlers.size(); for(int i = 0; i < count; i++) { - ((CommandHandler)commandHandlers.get(i)).onCommand(session); + ((CommandHandler)commandHandlers.get(i)).onCommand(this); theWatchdog.reset(); //if the response is received, stop processing of command handlers if(mode != COMMAND_MODE) { @@ -395,7 +407,7 @@ List messageHandlers = handlerChain.getMessageHandlers(); int count = messageHandlers.size(); for(int i =0; i < count; i++) { - ((MessageHandler)messageHandlers.get(i)).onMessage(session); + ((MessageHandler)messageHandlers.get(i)).onMessage(this); //if the response is received, stop processing of command handlers if(mode == MESSAGE_ABORT_MODE) { break; @@ -502,16 +514,6 @@ } - /** - * Clears the response buffer, returning the String of characters in the buffer. - * - * @return the data in the response buffer - */ - private String clearResponseBuffer() { - String responseString = responseBuffer.toString(); - responseBuffer.delete(0,responseBuffer.length()); - return responseString; - } /** * This method logs at a "DEBUG" level the response string that @@ -551,55 +553,8 @@ logResponseString(responseString); } - /** - * Reads a line of characters off the command line. - * - * @return the trimmed input line - * @throws IOException if an exception is generated reading in the input characters - */ - final String readCommandLine() throws IOException { - for (;;) try { - String commandLine = inReader.readLine(); - if (commandLine != null) { - commandLine = commandLine.trim(); - } - return commandLine; - } catch (CRLFTerminatedReader.TerminationException te) { - writeLoggedFlushedResponse("501 Syntax error at character position " + te.position() + ". CR and LF must be CRLF paired. See RFC 2821 #2.7.1."); - } - } /** - * Sets the user name associated with this SMTP interaction. - * - * @param userID the user name - */ - private void setUser(String userID) { - authenticatedUser = userID; - } - - /** - * Returns the user name associated with this SMTP interaction. - * - * @return the user name - */ - private String getUser() { - return authenticatedUser; - } - - /** - * Resets message-specific, but not authenticated user, state. - * - */ - private void resetState() { - ArrayList recipients = (ArrayList)state.get(RCPT_LIST); - if (recipients != null) { - recipients.clear(); - } - state.clear(); - } - - /** * A private inner class which serves as an adaptor * between the WatchdogTarget interface and this * handler class. @@ -624,23 +579,23 @@ this.handlerChain = handlerChain; } - /** - * delivers the mail to the spool. - * - * @param Mail the mail object - */ - public void sendMail(MailImpl mail) { - String responseString = null; - try { - session.setMail(mail); - theConfigData.getMailServer().sendMail(mail); - Collection theRecipients = mail.getRecipients(); - String recipientString = ""; - if (theRecipients != null) { - recipientString = theRecipients.toString(); - } - if (getLogger().isInfoEnabled()) { - StringBuffer infoBuffer = + /** + * delivers the mail to the spool. + * + * @param Mail the mail object + */ + public void sendMail(MailImpl mail) { + String responseString = null; + try { + setMail(mail); + theConfigData.getMailServer().sendMail(mail); + Collection theRecipients = mail.getRecipients(); + String recipientString = ""; + if (theRecipients != null) { + recipientString = theRecipients.toString(); + } + if (getLogger().isInfoEnabled()) { + StringBuffer infoBuffer = new StringBuffer(256) .append("Successfully spooled mail ") .append(mail.getName()) @@ -650,24 +605,24 @@ .append(remoteIP) .append(" for ") .append(recipientString); - getLogger().info(infoBuffer.toString()); - } + getLogger().info(infoBuffer.toString()); + } } catch (MessagingException me) { - // Grab any exception attached to this one. - Exception e = me.getNextException(); - // If there was an attached exception, and it's a - // MessageSizeException - if (e != null && e instanceof MessageSizeException) { - // Add an item to the state to suppress - // logging of extra lines of data - // that are sent after the size limit has - // been hit. - state.put(MESG_FAILED, Boolean.TRUE); - // then let the client know that the size - // limit has been hit. - responseString = "552 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.SYSTEM_MSG_TOO_BIG)+" Error processing message: " + // Grab any exception attached to this one. + Exception e = me.getNextException(); + // If there was an attached exception, and it's a + // MessageSizeException + if (e != null && e instanceof MessageSizeException) { + // Add an item to the state to suppress + // logging of extra lines of data + // that are sent after the size limit has + // been hit. + state.put(MESG_FAILED, Boolean.TRUE); + // then let the client know that the size + // limit has been hit. + responseString = "552 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.SYSTEM_MSG_TOO_BIG)+" Error processing message: " + e.getMessage(); - StringBuffer errorBuffer = + StringBuffer errorBuffer = new StringBuffer(256) .append("Rejected message from ") .append(state.get(SENDER).toString()) @@ -677,213 +632,213 @@ .append(remoteIP) .append(") exceeding system maximum message size of ") .append(theConfigData.getMaxMessageSize()); - getLogger().error(errorBuffer.toString()); - } else { - responseString = "451 "+DSNStatus.getStatus(DSNStatus.TRANSIENT,DSNStatus.UNDEFINED_STATUS)+" Error processing message: " + getLogger().error(errorBuffer.toString()); + } else { + responseString = "451 "+DSNStatus.getStatus(DSNStatus.TRANSIENT,DSNStatus.UNDEFINED_STATUS)+" Error processing message: " + me.getMessage(); - getLogger().error("Unknown error occurred while processing DATA.", me); - } - session.writeResponse(responseString); - return; + getLogger().error("Unknown error occurred while processing DATA.", me); + } + writeResponse(responseString); + return; } responseString = "250 "+DSNStatus.getStatus(DSNStatus.SUCCESS,DSNStatus.CONTENT_OTHER)+" Message received"; - session.writeResponse(responseString); - } - + writeResponse(responseString); + } + /** - * The SMTPSession implementation data to be passed to each handler + * @see org.apache.james.smtpserver.SMTPSession#writeResponse(String) */ - private class SMTPSessionImpl implements SMTPSession { - - /** - * @see org.apache.james.smtpserver.SMTPSession#writeResponse(String) - */ - public void writeResponse(String respString) { - SMTPHandler.this.writeLoggedFlushedResponse(respString); - //TODO Explain this well - if(SMTPHandler.this.mode == COMMAND_MODE) { - SMTPHandler.this.mode = SMTPHandler.RESPONSE_MODE; - } + public void writeResponse(String respString) { + SMTPHandler.this.writeLoggedFlushedResponse(respString); + //TODO Explain this well + if(SMTPHandler.this.mode == COMMAND_MODE) { + mode = RESPONSE_MODE; } + } - /** - * @see org.apache.james.smtpserver.SMTPSession#getCommandName() - */ - public String getCommandName() { - return SMTPHandler.this.curCommandName; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#getCommandName() + */ + public String getCommandName() { + return curCommandName; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#getCommandArgument() - */ - public String getCommandArgument() { - return SMTPHandler.this.curCommandArgument; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#getCommandArgument() + */ + public String getCommandArgument() { + return curCommandArgument; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#getMail() - */ - public Mail getMail() { - return SMTPHandler.this.mail; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#getMail() + */ + public Mail getMail() { + return mail; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#setMail(MailImpl) - */ - public void setMail(MailImpl mail) { - SMTPHandler.this.mail = mail; - SMTPHandler.this.mode = SMTPHandler.MESSAGE_RECEIVED_MODE; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#setMail(MailImpl) + */ + public void setMail(MailImpl mail) { + this.mail = mail; + this.mode = MESSAGE_RECEIVED_MODE; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#getRemoteHost() - */ - public String getRemoteHost() { - return SMTPHandler.this.remoteHost; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#getRemoteHost() + */ + public String getRemoteHost() { + return remoteHost; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#getRemoteIPAddress() - */ - public String getRemoteIPAddress() { - return SMTPHandler.this.remoteIP; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#getRemoteIPAddress() + */ + public String getRemoteIPAddress() { + return remoteIP; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#endSession() - */ - public void endSession() { - SMTPHandler.this.sessionEnded = true; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#endSession() + */ + public void endSession() { + sessionEnded = true; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#isSessionEnded() - */ - public boolean isSessionEnded() { - return SMTPHandler.this.sessionEnded; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#isSessionEnded() + */ + public boolean isSessionEnded() { + return sessionEnded; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#resetState() - */ - public void resetState() { - SMTPHandler.this.resetState(); + /** + * @see org.apache.james.smtpserver.SMTPSession#resetState() + */ + public void resetState() { + ArrayList recipients = (ArrayList)state.get(RCPT_LIST); + if (recipients != null) { + recipients.clear(); } + state.clear(); + } - /** - * @see org.apache.james.smtpserver.SMTPSession#getState() - */ - public HashMap getState() { - return SMTPHandler.this.state; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#getState() + */ + public HashMap getState() { + return SMTPHandler.this.state; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#getLogger() - */ - public Logger getLogger() { - return SMTPHandler.this.getLogger(); - } + /** + * @see org.apache.james.smtpserver.SMTPSession#getConfigurationData() + */ + public SMTPHandlerConfigurationData getConfigurationData() { + return theConfigData; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#getConfigurationData() - */ - public SMTPHandlerConfigurationData getConfigurationData() { - return SMTPHandler.this.theConfigData; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#isBlockListed() + */ + public boolean isBlockListed() { + return blocklisted; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#isBlockListed() - */ - public boolean isBlockListed() { - return SMTPHandler.this.blocklisted; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#setBlockListed(boolean) + */ + public void setBlockListed(boolean blocklisted ) { + blocklisted = blocklisted; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#setBlockListed(boolean) - */ - public void setBlockListed(boolean blocklisted ) { - SMTPHandler.this.blocklisted = blocklisted; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#isRelayingAllowed() + */ + public boolean isRelayingAllowed() { + return relayingAllowed; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#isRelayingAllowed() - */ - public boolean isRelayingAllowed() { - return SMTPHandler.this.relayingAllowed; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#isAuthRequired() + */ + public boolean isAuthRequired() { + return SMTPHandler.this.authRequired; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#isAuthRequired() - */ - public boolean isAuthRequired() { - return SMTPHandler.this.authRequired; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#getUser() + */ + public String getUser() { + return authenticatedUser; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#getUser() - */ - public String getUser() { - return SMTPHandler.this.getUser(); - } + /** + * @see org.apache.james.smtpserver.SMTPSession#setUser() + */ + public void setUser(String userID) { + authenticatedUser = userID; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#setUser() - */ - public void setUser(String user) { - SMTPHandler.this.setUser(user); - } - - /** - * @see org.apache.james.smtpserver.SMTPSession#getResponseBuffer() - */ - public StringBuffer getResponseBuffer() { - return SMTPHandler.this.responseBuffer; - } - - /** - * @see org.apache.james.smtpserver.SMTPSession#clearResponseBuffer() - */ - public String clearResponseBuffer() { - return SMTPHandler.this.clearResponseBuffer(); - } + /** + * @see org.apache.james.smtpserver.SMTPSession#getResponseBuffer() + */ + public StringBuffer getResponseBuffer() { + return responseBuffer; + } + /** + * @see org.apache.james.smtpserver.SMTPSession#clearResponseBuffer() + */ + public String clearResponseBuffer() { + String responseString = responseBuffer.toString(); + responseBuffer.delete(0,responseBuffer.length()); + return responseString; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#readCommandLine() - */ - public String readCommandLine() throws IOException { - return SMTPHandler.this.readCommandLine(); - } - /** - * @see org.apache.james.smtpserver.SMTPSession#getWatchdog() - */ - public Watchdog getWatchdog() { - return SMTPHandler.this.theWatchdog; + /** + * @see org.apache.james.smtpserver.SMTPSession#readCommandLine() + */ + public final String readCommandLine() throws IOException { + for (;;) try { + String commandLine = inReader.readLine(); + if (commandLine != null) { + commandLine = commandLine.trim(); + } + return commandLine; + } catch (CRLFTerminatedReader.TerminationException te) { + writeLoggedFlushedResponse("501 Syntax error at character position " + te.position() + ". CR and LF must be CRLF paired. See RFC 2821 #2.7.1."); } + } - /** - * @see org.apache.james.smtpserver.SMTPSession#getInputStream() - */ - public InputStream getInputStream() { - return SMTPHandler.this.in; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#getWatchdog() + */ + public Watchdog getWatchdog() { + return theWatchdog; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#getSessionID() - */ - public String getSessionID() { - return SMTPHandler.this.smtpID; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#getInputStream() + */ + public InputStream getInputStream() { + return in; + } - /** - * @see org.apache.james.smtpserver.SMTPSession#abortMessage() - */ - public void abortMessage() { - SMTPHandler.this.mode = SMTPHandler.MESSAGE_ABORT_MODE; - } + /** + * @see org.apache.james.smtpserver.SMTPSession#getSessionID() + */ + public String getSessionID() { + return smtpID; + } + /** + * @see org.apache.james.smtpserver.SMTPSession#abortMessage() + */ + public void abortMessage() { + mode = MESSAGE_ABORT_MODE; } } Index: java/org/apache/james/smtpserver/DNSRBLHandler.java =================================================================== --- java/org/apache/james/smtpserver/DNSRBLHandler.java (revision 234466) +++ java/org/apache/james/smtpserver/DNSRBLHandler.java (working copy) @@ -17,6 +17,7 @@ package org.apache.james.smtpserver; +import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.ConfigurationException; @@ -26,8 +27,9 @@ /** * Connect handler for DNSRBL processing */ -public class DNSRBLHandler implements ConnectHandler, - Configurable { +public class DNSRBLHandler + extends AbstractLogEnabled + implements ConnectHandler, Configurable { /** * The lists of rbl servers to be checked to limit spam */ @@ -47,9 +49,9 @@ for ( int i = 0 ; i < children.length ; i++ ) { String rblServerName = children[i].getValue(); rblserverCollection.add(rblServerName); - //if (getLogger().isInfoEnabled()) { - // getLogger().info("Adding RBL server to whitelist: " + rblServerName); - //} + if (getLogger().isInfoEnabled()) { + getLogger().info("Adding RBL server to whitelist: " + rblServerName); + } } if (rblserverCollection != null && rblserverCollection.size() > 0) { whitelist = (String[]) rblserverCollection.toArray(new String[rblserverCollection.size()]); @@ -61,9 +63,9 @@ for ( int i = 0 ; i < children.length ; i++ ) { String rblServerName = children[i].getValue(); rblserverCollection.add(rblServerName); - //if (getLogger().isInfoEnabled()) { - // getLogger().info("Adding RBL server to blacklist: " + rblServerName); - //} + if (getLogger().isInfoEnabled()) { + getLogger().info("Adding RBL server to blacklist: " + rblServerName); + } } if (rblserverCollection != null && rblserverCollection.size() > 0) { blacklist = (String[]) rblserverCollection.toArray(new String[rblserverCollection.size()]); @@ -108,13 +110,13 @@ String[] rblList = whitelist; for (int i = 0 ; i < rblList.length ; i++) try { org.apache.james.dnsserver.DNSServer.getByName(reversedOctets + rblList[i]); - if (session.getLogger().isInfoEnabled()) { - session.getLogger().info("Connection from " + ipAddress + " whitelisted by " + rblList[i]); + if (getLogger().isInfoEnabled()) { + getLogger().info("Connection from " + ipAddress + " whitelisted by " + rblList[i]); } return false; } catch (java.net.UnknownHostException uhe) { - if (session.getLogger().isInfoEnabled()) { - session.getLogger().info("unknown host exception thrown:" + rblList[i]); + if (getLogger().isInfoEnabled()) { + getLogger().info("unknown host exception thrown:" + rblList[i]); } } } @@ -123,14 +125,14 @@ String[] rblList = blacklist; for (int i = 0 ; i < rblList.length ; i++) try { org.apache.james.dnsserver.DNSServer.getByName(reversedOctets + rblList[i]); - if (session.getLogger().isInfoEnabled()) { - session.getLogger().info("Connection from " + ipAddress + " restricted by " + rblList[i] + " to SMTP AUTH/postmaster/abuse."); + if (getLogger().isInfoEnabled()) { + getLogger().info("Connection from " + ipAddress + " restricted by " + rblList[i] + " to SMTP AUTH/postmaster/abuse."); } return true; } catch (java.net.UnknownHostException uhe) { // if it is unknown, it isn't blocked - if (session.getLogger().isInfoEnabled()) { - session.getLogger().info("unknown host exception thrown:" + rblList[i]); + if (getLogger().isInfoEnabled()) { + getLogger().info("unknown host exception thrown:" + rblList[i]); } } } Index: java/org/apache/james/smtpserver/AuthCmdHandler.java =================================================================== --- java/org/apache/james/smtpserver/AuthCmdHandler.java (revision 234466) +++ java/org/apache/james/smtpserver/AuthCmdHandler.java (working copy) @@ -18,15 +18,19 @@ package org.apache.james.smtpserver; import org.apache.james.util.mail.dsn.DSNStatus; +import org.apache.avalon.framework.logger.AbstractLogEnabled; import java.util.Locale; import java.util.StringTokenizer; import org.apache.james.util.Base64; import java.io.IOException; + /** * handles AUTH command */ -public class AuthCmdHandler implements CommandHandler { +public class AuthCmdHandler + extends AbstractLogEnabled + implements CommandHandler { /** * The name of the command handled by the command handler @@ -55,7 +59,7 @@ try{ doAUTH(session, session.getCommandArgument()); } catch (Exception ex) { - session.getLogger().error("Exception occured:" + ex.getMessage()); + getLogger().error("Exception occured:" + ex.getMessage()); session.endSession(); } } @@ -183,11 +187,11 @@ session.setUser(user); responseString = "235 Authentication Successful"; session.writeResponse(responseString); - session.getLogger().info("AUTH method PLAIN succeeded"); + getLogger().info("AUTH method PLAIN succeeded"); } else { responseString = "535 Authentication Failed"; session.writeResponse(responseString); - session.getLogger().error("AUTH method PLAIN failed"); + getLogger().error("AUTH method PLAIN failed"); } return; } @@ -235,14 +239,14 @@ } else if (session.getConfigurationData().getUsersRepository().test(user, pass)) { session.setUser(user); responseString = "235 Authentication Successful"; - if (session.getLogger().isDebugEnabled()) { + if (getLogger().isDebugEnabled()) { // TODO: Make this string a more useful debug message - session.getLogger().debug("AUTH method LOGIN succeeded"); + getLogger().debug("AUTH method LOGIN succeeded"); } } else { responseString = "535 Authentication Failed"; // TODO: Make this string a more useful error message - session.getLogger().error("AUTH method LOGIN failed"); + getLogger().error("AUTH method LOGIN failed"); } session.writeResponse(responseString); return; @@ -258,13 +262,13 @@ private void doUnknownAuth(SMTPSession session, String authType, String initialResponse) { String responseString = "504 Unrecognized Authentication Type"; session.writeResponse(responseString); - if (session.getLogger().isErrorEnabled()) { + if (getLogger().isErrorEnabled()) { StringBuffer errorBuffer = new StringBuffer(128) .append("AUTH method ") .append(authType) .append(" is an unrecognized authentication type"); - session.getLogger().error(errorBuffer.toString()); + getLogger().error(errorBuffer.toString()); } return; } Index: java/org/apache/james/smtpserver/SMTPHandlerChain.java =================================================================== --- java/org/apache/james/smtpserver/SMTPHandlerChain.java (revision 234466) +++ java/org/apache/james/smtpserver/SMTPHandlerChain.java (working copy) @@ -25,6 +25,7 @@ import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.container.ContainerUtil; import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.avalon.framework.logger.LogEnabled; /** * The SMTPHandlerChain is per service object providing access @@ -38,7 +39,9 @@ private final CommandHandler unknownHandler = new UnknownCmdHandler(); + private final static String[] mandatoryCommands = { "MAIL" , "RCPT", "DATA"}; + /** * loads the various handlers from the configuration * @param configuration configuration under handlerchain node @@ -56,6 +59,11 @@ try { Object handler = classLoader.loadClass(className).newInstance(); + //enable logging + if (handler instanceof LogEnabled) { + ((LogEnabled)handler).enableLogging(getLogger()); + } + //configure the handler ContainerUtil.configure(handler,children[i]); @@ -103,6 +111,30 @@ } } } + + //the size must be greater than 1 because we added UnknownCmdHandler to the map + if(commandHandlerMap.size() < 2) { + if (getLogger().isErrorEnabled()) { + getLogger().error("No commandhandlers configured"); + } + throw new ConfigurationException("No commandhandlers configured"); + } else { + boolean found = true; + for (int i = 0; i < mandatoryCommands.length; i++) { + if(!commandHandlerMap.containsKey(mandatoryCommands[i])) { + if (getLogger().isErrorEnabled()) { + getLogger().error("No commandhandlers configured for the command:" + mandatoryCommands[i]); + } + found = false; + break; + } + } + + if(!found) { + throw new ConfigurationException("No commandhandlers configured for mandatory commands"); + } + + } } /** Index: java/org/apache/james/smtpserver/SMTPSession.java =================================================================== --- java/org/apache/james/smtpserver/SMTPSession.java (revision 234466) +++ java/org/apache/james/smtpserver/SMTPSession.java (working copy) @@ -41,18 +41,13 @@ void writeResponse(String respString); /** - * Returns the next command string sent by the client + * Reads a line of characters off the command line. * - * @return reads the characters of the commandline + * @return the trimmed input line + * @throws IOException if an exception is generated reading in the input characters */ String readCommandLine() throws IOException; - /** - * Returns Logger object - * - * @return Logger object - */ - Logger getLogger(); /** * Returns ResponseBuffer, this optimizes the unecessary creation of resources @@ -63,9 +58,9 @@ StringBuffer getResponseBuffer(); /** - * Returns response string and clears the response buffer + * Clears the response buffer, returning the String of characters in the buffer. * - * @return response string + * @return the data in the response buffer */ String clearResponseBuffer(); @@ -145,7 +140,7 @@ HashMap getState(); /** - * Resets the state + * Resets message-specific, but not authenticated user, state. * */ void resetState(); @@ -186,16 +181,16 @@ boolean isAuthRequired(); /** - * Returns currently authenticated user + * Returns the user name associated with this SMTP interaction. * - * @return the authenticated user name + * @return the user name */ String getUser(); /** - * set the user name after successful authentication + * Sets the user name associated with this SMTP interaction. * - * @param the authenticated user name + * @param userID the user name */ void setUser(String user); Index: java/org/apache/james/smtpserver/RcptCmdHandler.java =================================================================== --- java/org/apache/james/smtpserver/RcptCmdHandler.java (revision 234466) +++ java/org/apache/james/smtpserver/RcptCmdHandler.java (working copy) @@ -17,6 +17,7 @@ package org.apache.james.smtpserver; +import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.james.util.mail.dsn.DSNStatus; import org.apache.mailet.MailAddress; import java.util.Collection; @@ -27,7 +28,9 @@ /** * Handles RCPT command */ -public class RcptCmdHandler implements CommandHandler { +public class RcptCmdHandler + extends AbstractLogEnabled + implements CommandHandler { /** * The name of the command handled by the command handler @@ -94,13 +97,13 @@ if (!recipient.startsWith("<") || !recipient.endsWith(">")) { responseString = "501 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.DELIVERY_SYNTAX)+" Syntax error in parameters or arguments"; session.writeResponse(responseString); - if (session.getLogger().isErrorEnabled()) { + if (getLogger().isErrorEnabled()) { StringBuffer errorBuffer = new StringBuffer(192) .append("Error parsing recipient address: ") .append(recipient) .append(": did not start and end with < >"); - session.getLogger().error(errorBuffer.toString()); + getLogger().error(errorBuffer.toString()); } return; } @@ -121,14 +124,14 @@ responseString = "553 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX)+" Syntax error in recipient address"; session.writeResponse(responseString); - if (session.getLogger().isErrorEnabled()) { + if (getLogger().isErrorEnabled()) { StringBuffer errorBuffer = new StringBuffer(192) .append("Error parsing recipient address: ") .append(recipient) .append(": ") .append(pe.getMessage()); - session.getLogger().error(errorBuffer.toString()); + getLogger().error(errorBuffer.toString()); } return; } @@ -151,7 +154,7 @@ if (!session.getConfigurationData().getMailServer().isLocalServer(toDomain)) { responseString = "530 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.SECURITY_AUTH)+" Authentication Required"; session.writeResponse(responseString); - session.getLogger().error("Rejected message - authentication is required for mail request"); + getLogger().error("Rejected message - authentication is required for mail request"); return; } } else { @@ -165,14 +168,14 @@ (!session.getConfigurationData().getMailServer().isLocalServer(senderAddress.getHost()))) { responseString = "503 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.SECURITY_AUTH)+" Incorrect Authentication for Specified Email Address"; session.writeResponse(responseString); - if (session.getLogger().isErrorEnabled()) { + if (getLogger().isErrorEnabled()) { StringBuffer errorBuffer = new StringBuffer(128) .append("User ") .append(authUser) .append(" authenticated, however tried sending email as ") .append(senderAddress); - session.getLogger().error(errorBuffer.toString()); + getLogger().error(errorBuffer.toString()); } return; } @@ -183,7 +186,7 @@ if (!session.getConfigurationData().getMailServer().isLocalServer(toDomain)) { responseString = "550 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.SECURITY_AUTH)+" Requested action not taken: relaying denied"; session.writeResponse(responseString); - session.getLogger().error("Rejected message - " + session.getRemoteIPAddress() + " not authorized to relay to " + toDomain); + getLogger().error("Rejected message - " + session.getRemoteIPAddress() + " not authorized to relay to " + toDomain); return; } } @@ -200,14 +203,14 @@ rcptOptionValue = rcptOption.substring(equalIndex + 1); } // Unexpected option attached to the RCPT command - if (session.getLogger().isDebugEnabled()) { + if (getLogger().isDebugEnabled()) { StringBuffer debugBuffer = new StringBuffer(128) .append("RCPT command had unrecognized/unexpected option ") .append(rcptOptionName) .append(" with value ") .append(rcptOptionValue); - session.getLogger().debug(debugBuffer.toString()); + getLogger().debug(debugBuffer.toString()); } } optionTokenizer = null; Index: java/org/apache/james/smtpserver/DataCmdHandler.java =================================================================== --- java/org/apache/james/smtpserver/DataCmdHandler.java (revision 234466) +++ java/org/apache/james/smtpserver/DataCmdHandler.java (working copy) @@ -18,7 +18,7 @@ package org.apache.james.smtpserver; import org.apache.james.util.mail.dsn.DSNStatus; - +import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.james.core.MailHeaders; import org.apache.james.core.MailImpl; import org.apache.james.util.CharTerminatedInputStream; @@ -45,7 +45,9 @@ /** * handles DATA command */ -public class DataCmdHandler implements CommandHandler { +public class DataCmdHandler + extends AbstractLogEnabled + implements CommandHandler { /** * The name of the command handled by the command handler @@ -119,13 +121,13 @@ // wrap msgIn with a SizeLimitedInputStream long maxMessageSize = session.getConfigurationData().getMaxMessageSize(); if (maxMessageSize > 0) { - if (session.getLogger().isDebugEnabled()) { + if (getLogger().isDebugEnabled()) { StringBuffer logBuffer = new StringBuffer(128) .append("Using SizeLimitedInputStream ") .append(" with max message size: ") .append(maxMessageSize); - session.getLogger().debug(logBuffer.toString()); + getLogger().debug(logBuffer.toString()); } msgIn = new SizeLimitedInputStream(msgIn, maxMessageSize); } @@ -161,11 +163,11 @@ .append(session.getRemoteIPAddress()) .append(") exceeding system maximum message size of ") .append(session.getConfigurationData().getMaxMessageSize()); - session.getLogger().error(errorBuffer.toString()); + getLogger().error(errorBuffer.toString()); } else { responseString = "451 "+DSNStatus.getStatus(DSNStatus.TRANSIENT,DSNStatus.UNDEFINED_STATUS)+" Error processing message: " + me.getMessage(); - session.getLogger().error("Unknown error occurred while processing DATA.", me); + getLogger().error("Unknown error occurred while processing DATA.", me); } session.writeResponse(responseString); return;