📄 smtphandler.java
字号:
break; } } } //handle messages if(mode == MESSAGE_RECEIVED_MODE) { try { getLogger().debug("executing message handlers"); List messageHandlers = handlerChain.getMessageHandlers(); int count = messageHandlers.size(); for(int i =0; i < count; i++) { ((MessageHandler)messageHandlers.get(i)).onMessage(this); //if the response is received, stop processing of command handlers if(mode == MESSAGE_ABORT_MODE) { break; } } } finally { //do the clean up if(mail != null) { if (mail instanceof Disposable) { ((Disposable) mail).dispose(); } // remember the ehlo mode Object currentHeloMode = state.get(CURRENT_HELO_MODE); mail = null; resetState(); // start again with the old helo mode if (currentHeloMode != null) { state.put(CURRENT_HELO_MODE,currentHeloMode); } } } } } theWatchdog.stop(); getLogger().debug("Closing socket."); } catch (SocketException se) { if (getLogger().isErrorEnabled()) { StringBuffer errorBuffer = new StringBuffer(64) .append("Socket to ") .append(remoteHost) .append(" (") .append(remoteIP) .append(") closed remotely."); getLogger().error(errorBuffer.toString(), se ); } } catch ( InterruptedIOException iioe ) { if (getLogger().isErrorEnabled()) { StringBuffer errorBuffer = new StringBuffer(64) .append("Socket to ") .append(remoteHost) .append(" (") .append(remoteIP) .append(") timeout."); getLogger().error( errorBuffer.toString(), iioe ); } } catch ( IOException ioe ) { if (getLogger().isErrorEnabled()) { StringBuffer errorBuffer = new StringBuffer(256) .append("Exception handling socket to ") .append(remoteHost) .append(" (") .append(remoteIP) .append(") : ") .append(ioe.getMessage()); getLogger().error( errorBuffer.toString(), ioe ); } } catch (Exception e) { if (getLogger().isErrorEnabled()) { getLogger().error( "Exception opening socket: " + e.getMessage(), e ); } } finally { //Clear all the session state variables resetHandler(); } } /** * Resets the handler data to a basic state. */ private void resetHandler() { resetState(); clearResponseBuffer(); in = null; inReader = null; out = null; remoteHost = null; remoteIP = null; authenticatedUser = null; smtpID = null; if (theWatchdog != null) { ContainerUtil.dispose(theWatchdog); theWatchdog = null; } try { if (socket != null) { socket.close(); } } catch (IOException e) { if (getLogger().isErrorEnabled()) { getLogger().error("Exception closing socket: " + e.getMessage()); } } finally { socket = null; } synchronized (this) { handlerThread = null; } } /** * This method logs at a "DEBUG" level the response string that * was sent to the SMTP client. The method is provided largely * as syntactic sugar to neaten up the code base. It is declared * private and final to encourage compiler inlining. * * @param responseString the response string sent to the client */ private final void logResponseString(String responseString) { if (getLogger().isDebugEnabled()) { getLogger().debug("Sent: " + responseString); } } /** * Write and flush a response string. The response is also logged. * Should be used for the last line of a multi-line response or * for a single line response. * * @param responseString the response string sent to the client */ final void writeLoggedFlushedResponse(String responseString) { out.println(responseString); out.flush(); logResponseString(responseString); } /** * Write a response string. The response is also logged. * Used for multi-line responses. * * @param responseString the response string sent to the client */ final void writeLoggedResponse(String responseString) { out.println(responseString); logResponseString(responseString); } /** * A private inner class which serves as an adaptor * between the WatchdogTarget interface and this * handler class. */ private class SMTPWatchdogTarget implements WatchdogTarget { /** * @see org.apache.james.util.watchdog.WatchdogTarget#execute() */ public void execute() { SMTPHandler.this.idleClose(); } } /** * Sets the SMTPHandlerChain * * @param handlerChain SMTPHandler object */ public void setHandlerChain(SMTPHandlerChain handlerChain) { this.handlerChain = handlerChain; } /** * @see org.apache.james.smtpserver.SMTPSession#writeResponse(String) */ public void writeResponse(String respString) { writeLoggedFlushedResponse(respString); //TODO Explain this well if(mode == COMMAND_MODE) { mode = RESPONSE_MODE; } } /** * @see org.apache.james.smtpserver.SMTPSession#getCommandName() */ public String getCommandName() { return curCommandName; } /** * @see org.apache.james.smtpserver.SMTPSession#getCommandArgument() */ public String getCommandArgument() { return curCommandArgument; } /** * @see org.apache.james.smtpserver.SMTPSession#getMail() */ public Mail getMail() { return mail; } /** * @see org.apache.james.smtpserver.SMTPSession#setMail(Mail) */ public void setMail(Mail mail) { this.mail = mail; this.mode = MESSAGE_RECEIVED_MODE; } /** * @see org.apache.james.smtpserver.SMTPSession#getRemoteHost() */ public String getRemoteHost() { return remoteHost; } /** * @see org.apache.james.smtpserver.SMTPSession#getRemoteIPAddress() */ public String getRemoteIPAddress() { return remoteIP; } /** * @see org.apache.james.smtpserver.SMTPSession#endSession() */ public void endSession() { sessionEnded = true; } /** * @see org.apache.james.smtpserver.SMTPSession#isSessionEnded() */ public boolean isSessionEnded() { return sessionEnded; } /** * @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 state; } /** * @see org.apache.james.smtpserver.SMTPSession#getConfigurationData() */ public SMTPHandlerConfigurationData getConfigurationData() { return theConfigData; } /** * @see org.apache.james.smtpserver.SMTPSession#isBlockListed() */ public boolean isBlockListed() { return blocklisted; } /** * @see org.apache.james.smtpserver.SMTPSession#setBlockListed(boolean) */ public void setBlockListed(boolean blocklisted ) { this.blocklisted = blocklisted; } /** * @see org.apache.james.smtpserver.SMTPSession#isRelayingAllowed() */ public boolean isRelayingAllowed() { return relayingAllowed; } /** * @see org.apache.james.smtpserver.SMTPSession#isAuthRequired() */ public boolean isAuthRequired() { return authRequired; } /** * @see org.apache.james.smtpserver.SMTPSession#useHeloEhloEnforcement() */ public boolean useHeloEhloEnforcement() { return heloEhloEnforcement; } /** * @see org.apache.james.smtpserver.SMTPSession#getUser() */ public String getUser() { return authenticatedUser; } /** * @see org.apache.james.smtpserver.SMTPSession#setUser() */ public void setUser(String userID) { authenticatedUser = userID; } /** * @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 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."); } catch (CRLFTerminatedReader.LineLengthExceededException llee) { writeLoggedFlushedResponse("500 Line length exceeded. See RFC 2821 #4.5.3.1."); } } /** * @see org.apache.james.smtpserver.SMTPSession#getWatchdog() */ public Watchdog getWatchdog() { return theWatchdog; } /** * @see org.apache.james.smtpserver.SMTPSession#getInputStream() */ public InputStream getInputStream() { return in; } /** * @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; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -