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: ****************************************************************/package org.apache.james.smtpserver;
019:
020: import org.apache.avalon.framework.logger.AbstractLogEnabled;
021: import org.apache.avalon.framework.service.ServiceException;
022: import org.apache.avalon.framework.service.ServiceManager;
023: import org.apache.avalon.framework.service.Serviceable;
024: import org.apache.james.services.MailServer;
025: import org.apache.james.util.mail.dsn.DSNStatus;
026: import org.apache.mailet.Mail;
027:
028: import javax.mail.MessagingException;
029:
030: import java.util.Collection;
031:
032: /**
033: * Adds the header to the message
034: */
035: public class SendMailHandler extends AbstractLogEnabled implements
036: MessageHandler, Serviceable {
037:
038: private MailServer mailServer;
039:
040: /**
041: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
042: */
043: public void service(ServiceManager serviceManager)
044: throws ServiceException {
045: mailServer = (MailServer) serviceManager
046: .lookup(MailServer.ROLE);
047: }
048:
049: /**
050: * Adds header to the message
051: * @see org.apache.james.smtpserver#onMessage(SMTPSession)
052: */
053: public void onMessage(SMTPSession session) {
054: getLogger().debug("sending mail");
055:
056: Mail mail = session.getMail();
057:
058: String responseString = null;
059: try {
060: mailServer.sendMail(mail);
061: Collection theRecipients = mail.getRecipients();
062: String recipientString = "";
063: if (theRecipients != null) {
064: recipientString = theRecipients.toString();
065: }
066: if (getLogger().isInfoEnabled()) {
067: StringBuffer infoBuffer = new StringBuffer(256).append(
068: "Successfully spooled mail ").append(
069: mail.getName()).append(" from ").append(
070: mail.getSender()).append(" on ").append(
071: session.getRemoteIPAddress()).append(" for ")
072: .append(recipientString);
073: getLogger().info(infoBuffer.toString());
074: }
075: } catch (MessagingException me) {
076: // Grab any exception attached to this one.
077: Exception e = me.getNextException();
078: // If there was an attached exception, and it's a
079: // MessageSizeException
080: if (e != null && e instanceof MessageSizeException) {
081: // Add an item to the state to suppress
082: // logging of extra lines of data
083: // that are sent after the size limit has
084: // been hit.
085: session.getState().put(SMTPSession.MESG_FAILED,
086: Boolean.TRUE);
087: // then let the client know that the size
088: // limit has been hit.
089: responseString = "552 "
090: + DSNStatus.getStatus(DSNStatus.PERMANENT,
091: DSNStatus.SYSTEM_MSG_TOO_BIG)
092: + " Error processing message.";
093: StringBuffer errorBuffer = new StringBuffer(256)
094: .append("Rejected message from ")
095: .append(
096: session.getState().get(
097: SMTPSession.SENDER).toString())
098: .append(" from host ")
099: .append(session.getRemoteHost())
100: .append(" (")
101: .append(session.getRemoteIPAddress())
102: .append(
103: ") exceeding system maximum message size of ")
104: .append(
105: session.getConfigurationData()
106: .getMaxMessageSize());
107: getLogger().error(errorBuffer.toString());
108: } else {
109: responseString = "451 "
110: + DSNStatus.getStatus(DSNStatus.TRANSIENT,
111: DSNStatus.UNDEFINED_STATUS)
112: + " Error processing message.";
113: getLogger()
114: .error(
115: "Unknown error occurred while processing DATA.",
116: me);
117: }
118: session.writeResponse(responseString);
119: return;
120: }
121: responseString = "250 "
122: + DSNStatus.getStatus(DSNStatus.SUCCESS,
123: DSNStatus.CONTENT_OTHER) + " Message received";
124: session.writeResponse(responseString);
125:
126: }
127:
128: }
|