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.test.mock.james;
019:
020: import org.apache.james.core.MimeMessageCopyOnWriteProxy;
021: import org.apache.james.services.MailRepository;
022: import org.apache.james.services.MailServer;
023: import org.apache.james.smtpserver.MessageSizeException;
024: import org.apache.james.userrepository.MockUsersRepository;
025: import org.apache.mailet.Mail;
026: import org.apache.mailet.MailAddress;
027:
028: import javax.mail.Address;
029: import javax.mail.MessagingException;
030: import javax.mail.internet.InternetAddress;
031: import javax.mail.internet.MimeMessage;
032: import java.io.InputStream;
033: import java.util.*;
034:
035: public class MockMailServer implements MailServer {
036:
037: private final MockUsersRepository m_users = new MockUsersRepository();
038:
039: private int m_counter = 0;
040: private int m_maxMessageSizeBytes = 0;
041:
042: private final ArrayList mails = new ArrayList();
043:
044: private HashMap inboxes;
045:
046: public MockUsersRepository getUsersRepository() {
047: return m_users;
048: }
049:
050: public void sendMail(MailAddress sender, Collection recipients,
051: MimeMessage msg) throws MessagingException {
052: Object[] mailObjects = new Object[] { sender, recipients,
053: new MimeMessageCopyOnWriteProxy(msg) };
054: mails.add(mailObjects);
055: }
056:
057: public void sendMail(MailAddress sender, Collection recipients,
058: InputStream msg) throws MessagingException {
059: Object[] mailObjects = new Object[] { sender, recipients, msg };
060: mails.add(mailObjects);
061: }
062:
063: public void sendMail(Mail mail) throws MessagingException {
064: int bodySize = mail.getMessage().getSize();
065: try {
066: if (m_maxMessageSizeBytes != 0
067: && m_maxMessageSizeBytes < bodySize)
068: throw new MessageSizeException();
069: } catch (MessageSizeException e) {
070: throw new MessagingException(
071: "message size exception is nested", e);
072: }
073: sendMail(mail.getSender(), mail.getRecipients(), mail
074: .getMessage());
075: }
076:
077: public void sendMail(MimeMessage message) throws MessagingException {
078: // taken from class org.apache.james.James
079: MailAddress sender = new MailAddress((InternetAddress) message
080: .getFrom()[0]);
081: Collection recipients = new HashSet();
082: Address addresses[] = message.getAllRecipients();
083: if (addresses != null) {
084: for (int i = 0; i < addresses.length; i++) {
085: // Javamail treats the "newsgroups:" header field as a
086: // recipient, so we want to filter those out.
087: if (addresses[i] instanceof InternetAddress) {
088: recipients.add(new MailAddress(
089: (InternetAddress) addresses[i]));
090: }
091: }
092: }
093: sendMail(sender, recipients, message);
094: }
095:
096: public MailRepository getUserInbox(String userName) {
097: if (inboxes == null) {
098: return null;
099: } else {
100: return (MailRepository) inboxes.get(userName);
101: }
102:
103: }
104:
105: public void setUserInbox(String userName, MailRepository inbox) {
106: if (inboxes == null) {
107: inboxes = new HashMap();
108: }
109: inboxes.put(userName, inbox);
110: }
111:
112: public Map getRepositoryCounters() {
113: return null; // trivial implementation
114: }
115:
116: public synchronized String getId() {
117: m_counter++;
118: return "MockMailServer-ID-" + m_counter;
119: }
120:
121: public boolean addUser(String userName, String password) {
122: m_users.addUser(userName, password);
123: return true;
124: }
125:
126: public boolean isLocalServer(String serverName) {
127: return "localhost".equals(serverName);
128: }
129:
130: public Object[] getLastMail() {
131: if (mails.size() == 0)
132: return null;
133: return (Object[]) mails.get(mails.size() - 1);
134: }
135:
136: public void setMaxMessageSizeBytes(int maxMessageSizeBytes) {
137: m_maxMessageSizeBytes = maxMessageSizeBytes;
138: }
139: }
|