1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21 package org.apache.james.postage.smtpserver;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.james.postage.result.MailProcessingRecord;
26 import org.apache.james.postage.result.PostageRunnerResult;
27 import org.apache.james.postage.mail.HeaderConstants;
28 import org.apache.james.postage.mail.MailMatchingUtils;
29 import org.apache.james.postage.mail.MailAnalyzeStrategy;
30 import org.apache.james.services.MailRepository;
31 import org.apache.james.services.MailServer;
32 import org.apache.mailet.Mail;
33 import org.apache.mailet.MailAddress;
34
35 import javax.mail.Address;
36 import javax.mail.MessagingException;
37 import javax.mail.internet.InternetAddress;
38 import javax.mail.internet.MimeMessage;
39 import javax.mail.internet.MimeMultipart;
40
41 import java.io.InputStream;
42 import java.util.Collection;
43 import java.util.HashSet;
44
45
46 /***
47 * a quite simple (only receiving) SMTP server which reads mails and tries to match them with sent test mails.<br/>
48 * reuses James' own SMTP server components
49 */
50 public class SimpleMailServer implements MailServer {
51
52 private static Log log = LogFactory.getLog(SimpleMailServer.class);
53
54 private int m_counter = 0;
55 private PostageRunnerResult m_results;
56
57 public void sendMail(MailAddress sender, Collection recipients, MimeMessage message) throws MessagingException {
58 try {
59 new SMTPMailAnalyzeStrategy("smtpOutbound", m_results, message).handle();
60 } catch (Exception e) {
61 throw new MessagingException("error handling message", e);
62 }
63 }
64
65 public void sendMail(MailAddress sender, Collection recipients, InputStream msg) throws MessagingException {
66
67 throw new IllegalStateException("not supported");
68 }
69
70 public void sendMail(Mail mail) throws MessagingException {
71 sendMail(mail.getSender(), mail.getRecipients(), mail.getMessage());
72 }
73
74 public void sendMail(MimeMessage message) throws MessagingException {
75
76 MailAddress sender = new MailAddress((InternetAddress)message.getFrom()[0]);
77 Collection recipients = new HashSet();
78 Address addresses[] = message.getAllRecipients();
79 if (addresses != null) {
80 for (int i = 0; i < addresses.length; i++) {
81
82
83 if ( addresses[i] instanceof InternetAddress ) {
84 recipients.add(new MailAddress((InternetAddress)addresses[i]));
85 }
86 }
87 }
88 sendMail(sender, recipients, message);
89 }
90
91 public MailRepository getUserInbox(String userName) {
92 throw new IllegalStateException("not implemented");
93 }
94
95 public synchronized String getId() {
96 m_counter++;
97 return "SimpleMailServer-ID-" + m_counter;
98 }
99
100 public boolean addUser(String userName, String password) {
101 throw new IllegalStateException("not implemented");
102 }
103
104 public boolean isLocalServer(String serverName) {
105 return true;
106 }
107
108 public void setResults(PostageRunnerResult results) {
109 m_results = results;
110 }
111 }
112