001 /*
002 * Copyright 2010 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package com.hs.mail.mailet;
017
018 import java.io.IOException;
019 import java.io.PushbackInputStream;
020 import java.util.Collection;
021 import java.util.Set;
022
023 import javax.mail.MessagingException;
024
025 import org.apache.commons.collections.CollectionUtils;
026 import org.apache.james.mime4j.MimeException;
027 import org.apache.james.mime4j.field.AbstractField;
028 import org.apache.james.mime4j.message.Header;
029 import org.apache.log4j.Logger;
030
031 import com.hs.mail.imap.ImapConstants;
032 import com.hs.mail.imap.message.MailMessage;
033 import com.hs.mail.sieve.Sieve;
034 import com.hs.mail.smtp.message.Recipient;
035 import com.hs.mail.smtp.message.SmtpMessage;
036
037 /**
038 *
039 * @author Won Chul Doh
040 * @since 29 Jun, 2010
041 *
042 */
043 public class ToRepository extends AbstractMailet {
044
045 static Logger logger = Logger.getLogger(ToRepository.class);
046
047 public boolean accept(Set<Recipient> recipients, SmtpMessage message) {
048 return CollectionUtils.isNotEmpty(recipients);
049 }
050
051 public void service(Set<Recipient> recipients, SmtpMessage message)
052 throws MessagingException {
053 try {
054 deliver(recipients, message);
055 } catch (IOException e) {
056 }
057 }
058
059 private void deliver(Collection<Recipient> recipients, SmtpMessage message)
060 throws IOException {
061 String returnPath = (message.getNode() != SmtpMessage.LOCAL)
062 ? "Return-Path: <" + message.getFrom().getMailbox() + ">\r\n"
063 : null;
064 MailMessage msg = message.getMailMessage();
065 try {
066 if (returnPath != null) {
067 Header header = msg.getHeader().getHeader();
068 header.setField(AbstractField.parse(returnPath));
069 msg.setSize(msg.getSize() + returnPath.getBytes().length);
070 }
071 for (Recipient rcpt : recipients) {
072 try {
073 if (rcpt.getID() != -1) {
074 if (!Sieve.runSieve(context, rcpt, message)) {
075 context.storeMail(rcpt.getID(),
076 ImapConstants.INBOX_NAME, message);
077 }
078 }
079 } catch (Exception e) {
080 StringBuilder errorBuffer = new StringBuilder(256)
081 .append("Error while delivering message to ")
082 .append(rcpt);
083 logger.error(errorBuffer.toString(), e);
084
085 if (!message.isNotificationMessage()) {
086 errorBuffer.append(": ")
087 .append(e.getMessage().trim())
088 .append("\r\n");
089 message.appendErrorMessage(errorBuffer.toString());
090 }
091 }
092 }
093 } catch (MimeException e) {
094 // impossible really
095 }
096 if (msg != null && msg.getPhysMessageID() != 0) {
097 try {
098 if (returnPath != null) {
099 PushbackInputStream is = new PushbackInputStream(msg
100 .getInputStream(), returnPath.length());
101 is.unread(returnPath.getBytes("ASCII"));
102 msg.save(is);
103 } else {
104 msg.save(true);
105 }
106 } catch (IOException e) {
107 logger.error(e.getMessage(), e);
108 }
109 }
110 }
111
112 }