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: */
019:
020: package org.apache.axis2.transport.mail;
021:
022: import org.apache.axiom.attachments.ByteArrayDataSource;
023: import org.apache.axiom.om.OMOutputFormat;
024: import org.apache.axiom.soap.SOAP11Constants;
025: import org.apache.axiom.soap.SOAP12Constants;
026: import org.apache.axis2.AxisFault;
027: import org.apache.axis2.i18n.Messages;
028: import org.apache.axis2.addressing.EndpointReference;
029: import org.apache.axis2.client.Options;
030: import org.apache.axis2.context.ConfigurationContext;
031: import org.apache.axis2.context.MessageContext;
032: import org.apache.axis2.description.TransportInDescription;
033: import org.apache.axis2.description.AxisOperation;
034: import org.apache.axis2.description.OutOnlyAxisOperation;
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: import javax.activation.CommandMap;
039: import javax.activation.DataHandler;
040: import javax.activation.DataSource;
041: import javax.activation.MailcapCommandMap;
042: import javax.mail.*;
043: import javax.mail.internet.AddressException;
044: import javax.mail.internet.InternetAddress;
045: import javax.mail.internet.MimeMessage;
046: import javax.mail.internet.MimeMultipart;
047: import java.io.ByteArrayOutputStream;
048: import java.io.OutputStream;
049: import java.util.Hashtable;
050: import java.util.Properties;
051:
052: public class EMailSender {
053: private Properties properties;
054: private MessageContext messageContext;
055: private PasswordAuthentication passwordAuthentication;
056: private OutputStream outputStream;
057: private String inReplyTo;
058: private EndpointReference from;
059: private OMOutputFormat format;
060:
061: protected static Log log = LogFactory.getLog(EMailSender.class);
062:
063: static {
064: //Initializing the proper mime types
065: MailcapCommandMap mc = (MailcapCommandMap) CommandMap
066: .getDefaultCommandMap();
067: mc
068: .addMailcap("application/soap+xml;;x-java-content-handler=com.sun.mail.handlers.text_xml");
069: CommandMap.setDefaultCommandMap(mc);
070: }
071:
072: public EMailSender() {
073: }
074:
075: public void setMessageContext(MessageContext messageContext) {
076: this .messageContext = messageContext;
077: }
078:
079: public void setProperties(Properties properties) {
080: this .properties = properties;
081: }
082:
083: public OutputStream getOutputStream() {
084: return outputStream;
085: }
086:
087: public void setOutputStream(OutputStream outputStream) {
088: this .outputStream = outputStream;
089: }
090:
091: public void setPasswordAuthentication(
092: PasswordAuthentication passwordAuthentication) {
093: this .passwordAuthentication = passwordAuthentication;
094: }
095:
096: public void send() throws AxisFault {
097:
098: try {
099:
100: Session session = Session.getInstance(properties,
101: new Authenticator() {
102: protected PasswordAuthentication getPasswordAuthentication() {
103: return passwordAuthentication;
104: }
105: });
106: MimeMessage msg = new MimeMessage(session);
107:
108: EndpointReference epr = null;
109: MailToInfo mailToInfo;
110:
111: if (messageContext.getTo() != null
112: && !messageContext.getTo().hasAnonymousAddress()) {
113: epr = messageContext.getTo();
114: }
115:
116: if (epr != null) {
117: if (!epr.hasNoneAddress()) {
118: mailToInfo = new MailToInfo(epr);
119: msg.addRecipient(Message.RecipientType.TO,
120: new InternetAddress(mailToInfo
121: .getEmailAddress()));
122:
123: } else {
124: if (from != null) {
125: mailToInfo = new MailToInfo(from);
126: msg.addRecipient(Message.RecipientType.TO,
127: new InternetAddress(mailToInfo
128: .getEmailAddress()));
129: } else {
130: String error = EMailSender.class.getName()
131: + "Couldn't countinue due to"
132: + " FROM addressing is NULL";
133: log.error(error);
134: throw new AxisFault(error);
135: }
136: }
137: } else {
138: // replyto : from : or reply-path;
139: if (from != null) {
140: mailToInfo = new MailToInfo(from);
141: msg.addRecipient(Message.RecipientType.TO,
142: new InternetAddress(mailToInfo
143: .getEmailAddress()));
144: } else {
145: String error = EMailSender.class.getName()
146: + "Couldn't countinue due to"
147: + " FROM addressing is NULL and EPR is NULL";
148: log.error(error);
149: throw new AxisFault(error);
150: }
151:
152: }
153:
154: msg.setSubject("__ Axis2/Java Mail Message __");
155:
156: if (mailToInfo.isxServicePath()) {
157: msg.setHeader(Constants.X_SERVICE_PATH, "\""
158: + mailToInfo.getContentDescription() + "\"");
159: }
160:
161: if (inReplyTo != null) {
162: msg.setHeader(Constants.IN_REPLY_TO, inReplyTo);
163: }
164:
165: createMailMimeMessage(msg, mailToInfo, format);
166: Transport.send(msg);
167:
168: log.info("Message being send. [Action = ]"
169: + messageContext.getOptions().getAction());
170:
171: sendReceive(messageContext, msg.getMessageID());
172: } catch (AddressException e) {
173: throw new AxisFault(e.getMessage(), e);
174: } catch (MessagingException e) {
175: throw new AxisFault(e.getMessage(), e);
176: }
177: }
178:
179: private void createMailMimeMessage(final MimeMessage msg,
180: MailToInfo mailToInfo, OMOutputFormat format)
181: throws MessagingException {
182:
183: // Create the message part
184: BodyPart messageBodyPart = new MimeBase64BodyPart();
185: messageBodyPart.setText("");
186: Multipart multipart = new MimeMultipart();
187: multipart.addBodyPart(messageBodyPart);
188:
189: DataSource source = null;
190:
191: // Part two is attachment
192: if (outputStream instanceof ByteArrayOutputStream) {
193: source = new ByteArrayDataSource(
194: ((ByteArrayOutputStream) outputStream)
195: .toByteArray());
196: }
197: messageBodyPart = new MimeBase64BodyPart();
198: messageBodyPart.setDataHandler(new DataHandler(source));
199: messageBodyPart.setDisposition(Part.ATTACHMENT);
200:
201: messageBodyPart.addHeader("Content-Description", "\""
202: + mailToInfo.getContentDescription() + "\"");
203:
204: String contentType = format.getContentType() != null ? format
205: .getContentType() : Constants.DEFAULT_CONTENT_TYPE;
206: if (contentType.indexOf(SOAP11Constants.SOAP_11_CONTENT_TYPE) > -1) {
207: if (messageContext.getSoapAction() != null) {
208: messageBodyPart.setHeader(Constants.HEADER_SOAP_ACTION,
209: messageContext.getSoapAction());
210: }
211: }
212:
213: if (contentType.indexOf(SOAP12Constants.SOAP_12_CONTENT_TYPE) > -1) {
214: if (messageContext.getSoapAction() != null) {
215: messageBodyPart.setHeader("Content-Type", contentType
216: + "; charset=" + format.getCharSetEncoding()
217: + " ; action=\""
218: + messageContext.getSoapAction() + "\"");
219: }
220: } else {
221: messageBodyPart.setHeader("Content-Type", contentType
222: + "; charset=" + format.getCharSetEncoding());
223: }
224:
225: multipart.addBodyPart(messageBodyPart);
226: msg.setContent(multipart);
227:
228: }
229:
230: public void setInReplyTo(String inReplyTo) {
231: this .inReplyTo = inReplyTo;
232: }
233:
234: public void setFrom(EndpointReference from) {
235: this .from = from;
236: }
237:
238: public void setFormat(OMOutputFormat format) {
239: this .format = format;
240: }
241:
242: private void sendReceive(MessageContext msgContext, String msgId)
243: throws AxisFault {
244: storeMessageContext(msgContext, msgId);
245: ConfigurationContext cc = msgContext.getConfigurationContext();
246: //While sysncmial listner .not complete
247: Options options = msgContext.getOptions();
248: long outInMilliSeconds = options.getTimeOutInMilliSeconds();
249: SynchronousMailListener synchronousMailListener = null;
250: //No need to stor the message context if the mep is out-only
251: AxisOperation axisOperation = msgContext.getAxisOperation();
252: if (axisOperation instanceof OutOnlyAxisOperation) {
253: return;
254: }
255:
256: if (!options.isUseSeparateListener()
257: && !msgContext.isServerSide()) {
258: if (!cc.getListenerManager().isListenerRunning(
259: Constants.MAILTO)) {
260: TransportInDescription mailTo = cc
261: .getAxisConfiguration().getTransportIn(
262: Constants.MAILTO);
263: if (mailTo == null) {
264: throw new AxisFault(
265: "Could not found transport for "
266: + Constants.MAILTO);
267: }
268: cc.getListenerManager().addListener(mailTo, false);
269: }
270: Hashtable callBackTable = (Hashtable) cc
271: .getProperty(Constants.CALLBACK_TABLE);
272:
273: if (callBackTable != null) {
274: synchronousMailListener = new SynchronousMailListener(
275: messageContext, outInMilliSeconds);
276: callBackTable.put(msgId, synchronousMailListener);
277: }
278: while (!synchronousMailListener.isComplete()) {
279: try {
280: Thread.sleep(6000);
281: } catch (InterruptedException e) {
282: throw new AxisFault(e.getMessage(), e);
283: }
284: }
285: callBackTable.remove(msgId);
286: }
287: }
288:
289: private void storeMessageContext(MessageContext msgContext,
290: String msgId) {
291: Hashtable mappingTable = (Hashtable) msgContext
292: .getConfigurationContext().getProperty(
293: Constants.MAPPING_TABLE);
294:
295: if (mappingTable == null) {
296: mappingTable = new Hashtable();
297: msgContext.setProperty(Constants.MAPPING_TABLE,
298: mappingTable);
299: }
300: if (msgContext.getMessageID() != null) {
301: mappingTable.put(msgId, msgContext.getMessageID());
302: }
303:
304: }
305: }
|