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.server;
021:
022: import org.apache.axiom.om.impl.builder.StAXBuilder;
023: import org.apache.axiom.soap.SOAP11Constants;
024: import org.apache.axiom.soap.SOAP12Constants;
025: import org.apache.axiom.soap.SOAPEnvelope;
026: import org.apache.axis2.AxisFault;
027: import org.apache.axis2.addressing.EndpointReference;
028: import org.apache.axis2.builder.BuilderUtil;
029: import org.apache.axis2.context.ConfigurationContext;
030: import org.apache.axis2.context.MessageContext;
031: import org.apache.axis2.engine.AxisEngine;
032: import org.apache.axis2.transport.mail.Constants;
033: import org.apache.axis2.util.MessageContextBuilder;
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: import javax.mail.MessagingException;
038: import javax.mail.internet.InternetAddress;
039: import javax.mail.internet.MimeMessage;
040: import java.io.ByteArrayInputStream;
041: import java.util.ArrayList;
042:
043: /**
044: * This class will be used to sort the messages into normal messages and mails
045: * being sent to the Axis engine. If a mail is to be sent to the engine then a
046: * new Axis engine is created using the configuration in the MailServer class
047: * and the receive method is called.
048: */
049: public class MailSorter {
050: private static final Log log = LogFactory.getLog(MailSorter.class);
051: Storage st = null;
052: private ArrayList sUsers = new ArrayList();
053:
054: // Special users. They are hard coded for the time being to axis2-server@localhost and axis2-server@127.0.0.1
055: private ConfigurationContext configurationContext = null;
056: private boolean actAsMailet = false;
057:
058: public MailSorter(Storage st,
059: ConfigurationContext configurationContext) {
060: this .st = st;
061: sUsers.add("axis2-server@localhost");
062: sUsers.add("axis2-server@127.0.0.1");
063:
064: if (configurationContext == null) {
065: actAsMailet = false;
066: } else {
067: this .configurationContext = configurationContext;
068: actAsMailet = true;
069: }
070: }
071:
072: public void processMail(ConfigurationContext confContext,
073: MimeMessage mimeMessage) {
074: // create an Axis server
075: AxisEngine engine = new AxisEngine(confContext);
076: MessageContext msgContext = null;
077:
078: // create and initialize a message context
079: try {
080: msgContext = confContext.createMessageContext();
081: msgContext.setTransportIn(confContext
082: .getAxisConfiguration().getTransportIn(
083: org.apache.axis2.Constants.TRANSPORT_MAIL));
084: msgContext.setTransportOut(confContext
085: .getAxisConfiguration().getTransportOut(
086: org.apache.axis2.Constants.TRANSPORT_MAIL));
087:
088: msgContext.setServerSide(true);
089: msgContext.setProperty(Constants.CONTENT_TYPE, mimeMessage
090: .getContentType());
091: msgContext
092: .setProperty(
093: org.apache.axis2.Constants.Configuration.CHARACTER_SET_ENCODING,
094: mimeMessage.getEncoding());
095: String soapAction = getMailHeader(
096: Constants.HEADER_SOAP_ACTION, mimeMessage);
097: if (soapAction == null) {
098: soapAction = mimeMessage.getSubject();
099: }
100:
101: msgContext.setSoapAction(soapAction);
102: msgContext
103: .setIncomingTransportName(org.apache.axis2.Constants.TRANSPORT_MAIL);
104:
105: String serviceURL = mimeMessage.getSubject();
106:
107: if (serviceURL == null) {
108: serviceURL = "";
109: }
110:
111: String replyTo = ((InternetAddress) mimeMessage
112: .getReplyTo()[0]).getAddress();
113:
114: if (replyTo != null) {
115: msgContext.setReplyTo(new EndpointReference(replyTo));
116: }
117:
118: String recepainets = ((InternetAddress) mimeMessage
119: .getAllRecipients()[0]).getAddress();
120:
121: if (recepainets != null) {
122: msgContext.setTo(new EndpointReference(recepainets
123: + "/" + serviceURL));
124: }
125:
126: // add the SOAPEnvelope
127: String message = mimeMessage.getContent().toString();
128:
129: log.info("message[" + message + "]");
130:
131: ByteArrayInputStream bais = new ByteArrayInputStream(
132: message.getBytes());
133: String soapNamespaceURI = "";
134:
135: if (mimeMessage.getContentType().indexOf(
136: SOAP12Constants.SOAP_12_CONTENT_TYPE) > -1) {
137: soapNamespaceURI = SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI;
138: } else if (mimeMessage.getContentType().indexOf(
139: SOAP11Constants.SOAP_11_CONTENT_TYPE) > -1) {
140: soapNamespaceURI = SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI;
141: }
142:
143: StAXBuilder builder = BuilderUtil.getSOAPBuilder(bais,
144: soapNamespaceURI);
145:
146: SOAPEnvelope envelope = (SOAPEnvelope) builder
147: .getDocumentElement();
148:
149: msgContext.setEnvelope(envelope);
150:
151: AxisEngine.receive(msgContext);
152: } catch (Exception e) {
153: try {
154: if (msgContext != null) {
155: MessageContext faultContext = MessageContextBuilder
156: .createFaultMessageContext(msgContext, e);
157:
158: engine.sendFault(faultContext);
159: }
160: } catch (Exception e1) {
161: log.error(e.getMessage(), e);
162: }
163: }
164: }
165:
166: public void sort(String user, MimeMessage msg) {
167: if (actAsMailet) {
168: if (sUsers.contains(user)) {
169: processMail(configurationContext, msg);
170: } else {
171: st.addMail(user, msg);
172: }
173: } else {
174: st.addMail(user, msg);
175: }
176: }
177:
178: private String getMailHeader(String headerName,
179: MimeMessage mimeMessage) throws AxisFault {
180: try {
181: String values[] = mimeMessage.getHeader(headerName);
182:
183: if (values != null) {
184: return values[0];
185: } else {
186: return null;
187: }
188: } catch (MessagingException e) {
189: throw new AxisFault(e.getMessage(), e);
190: }
191: }
192: }
|