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.client;
22
23 import org.apache.james.postage.SamplingException;
24 import org.apache.james.postage.StartupException;
25 import org.apache.james.postage.mail.HeaderConstants;
26 import org.apache.james.postage.configuration.MailSender;
27 import org.apache.james.postage.execution.Sampler;
28 import org.apache.james.postage.result.MailProcessingRecord;
29 import org.apache.james.postage.result.PostageRunnerResult;
30 import org.apache.james.postage.user.UserList;
31
32 import javax.mail.Message;
33 import javax.mail.MessagingException;
34 import javax.mail.Session;
35 import javax.mail.Transport;
36 import javax.mail.internet.InternetAddress;
37 import java.util.Properties;
38
39 /***
40 * connects as a SMTP client and handles all mail according to its configuration.<br/>
41 * it is threadsafe and reentrant and thus can be reused over multiple parallel client session<br/>
42 */
43 public class SMTPClient implements Sampler {
44
45 private String m_host;
46 private int m_port;
47 private UserList m_internalUsers;
48 private UserList m_externalUsers;
49 private PostageRunnerResult m_results;
50 private MailSender m_mailSender;
51
52 public SMTPClient(String host, int port, UserList internalUsers, UserList externalUsers, PostageRunnerResult results, MailSender mailSender) {
53 m_host = host;
54 m_port = port;
55 m_internalUsers = internalUsers;
56 m_externalUsers = externalUsers;
57 m_mailSender = mailSender;
58 m_results = results;
59 }
60
61 public boolean checkAvailability() throws StartupException {
62 try {
63
64 MailProcessingRecord proformaMailProcessingRecord = new MailProcessingRecord();
65 Session session = getMailSession();
66 proformaMailProcessingRecord.setMailId(HeaderConstants.JAMES_POSTAGE_STARTUPCHECK_HEADER_ID);
67 Message message = m_mailSender.createMail(session, proformaMailProcessingRecord);
68 setMailFromAndTo(message, proformaMailProcessingRecord);
69 Transport.send(message);
70 } catch (Exception e) {
71 throw new StartupException("inbound SMTP service not available", e);
72 }
73 return true;
74 }
75
76 private void setMailFromAndTo(Message message, MailProcessingRecord mailProcessingRecord) throws MessagingException {
77
78 String senderUsername;
79 String senderMailAddress;
80 if (m_mailSender.getParentProfile().isSourceInternal()) {
81 senderUsername = m_internalUsers.getRandomUsername();
82 } else {
83 senderUsername = m_externalUsers.getRandomUsername();
84 }
85 if (m_mailSender.getParentProfile().isSourceInternal()) {
86 senderMailAddress = m_internalUsers.getEmailAddress(senderUsername);
87 } else {
88 senderMailAddress = m_externalUsers.getEmailAddress(senderUsername);
89 }
90 mailProcessingRecord.setSender(senderUsername);
91 mailProcessingRecord.setSenderMailAddress(senderMailAddress);
92 message.setFrom(new InternetAddress(senderMailAddress));
93
94 String recepientUsername;
95 String recepientMailAddress;
96 if (m_mailSender.getParentProfile().isTargetInternal()) {
97 recepientUsername = m_internalUsers.getRandomUsername();
98 } else {
99 recepientUsername = m_externalUsers.getRandomUsername();
100 }
101 if (m_mailSender.getParentProfile().isTargetInternal()) {
102 recepientMailAddress = m_internalUsers.getEmailAddress(recepientUsername);
103 } else {
104 recepientMailAddress = m_externalUsers.getEmailAddress(recepientUsername);
105 }
106 mailProcessingRecord.setReceiver(recepientUsername);
107 mailProcessingRecord.setReceiverMailAddress(recepientMailAddress);
108 message.addRecipient(Message.RecipientType.TO, new InternetAddress(recepientMailAddress));
109 }
110
111 public synchronized void doSample() throws SamplingException {
112
113 MailProcessingRecord mailProcessingRecord = new MailProcessingRecord();
114 mailProcessingRecord.setMailId(MailProcessingRecord.getNextId());
115 m_results.addNewMailRecord(mailProcessingRecord);
116 mailProcessingRecord.setTimeConnectStart(System.currentTimeMillis());
117
118 Message message = null;
119 try {
120 try {
121 Session session = getMailSession();
122 message = m_mailSender.createMail(session, mailProcessingRecord);
123 } catch (Exception e) {
124 mailProcessingRecord.setErrorTextSending("Could not send mail");
125 throw e;
126 }
127 try {
128 setMailFromAndTo(message, mailProcessingRecord);
129 } catch (Exception e) {
130 mailProcessingRecord.setErrorTextSending("Could not set recipient");
131 throw e;
132 }
133 try {
134 mailProcessingRecord.setTimeSendStart(System.currentTimeMillis());
135 Transport.send(message);
136 mailProcessingRecord.setTimeSendEnd(System.currentTimeMillis());
137 } catch (MessagingException e) {
138 mailProcessingRecord.setErrorTextSending("Could not be transported.");
139 throw e;
140 }
141 } catch (Exception e) {
142 throw new SamplingException("sample failed", e);
143 }
144 }
145
146 private Session getMailSession() {
147 Properties props = System.getProperties();
148 props.put("mail.smtp.host", m_host);
149 props.put("mail.smtp.port", Integer.toString(m_port));
150 return Session.getDefaultInstance(props, null);
151 }
152 }