KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > mail > server > MailSorter


1 package org.apache.axis2.transport.mail.server;
2
3 import org.apache.axis2.Constants;
4 import org.apache.axis2.addressing.AddressingConstants;
5 import org.apache.axis2.addressing.EndpointReference;
6 import org.apache.axis2.context.ConfigurationContext;
7 import org.apache.axis2.context.MessageContext;
8 import org.apache.axis2.engine.AxisEngine;
9 import org.apache.axis2.engine.AxisFault;
10 import org.apache.axis2.om.impl.llom.builder.StAXBuilder;
11 import org.apache.axis2.soap.SOAPEnvelope;
12 import org.apache.axis2.soap.impl.llom.builder.StAXSOAPModelBuilder;
13 import org.apache.axis2.transport.mail.MailConstants;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 import javax.mail.MessagingException JavaDoc;
18 import javax.mail.internet.InternetAddress JavaDoc;
19 import javax.mail.internet.MimeMessage JavaDoc;
20 import javax.xml.namespace.QName JavaDoc;
21 import javax.xml.stream.XMLInputFactory;
22 import javax.xml.stream.XMLStreamReader;
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 /**
27  * This class will be used to sort the messages into normal messages and mails
28  * being sent to the Axis engine. If a mail is to be sent to the engine then a
29  * new Axis engine is created using the configuration in the MailServer class
30  * and the receive method is called.
31  * @author Chamil Thanthrimudalige
32  */

33 public class MailSorter {
34     Storage st = null;
35     private ArrayList JavaDoc sUsers = new ArrayList JavaDoc(); // Special users. They are hard coded for the time being to axis2-server@localhost and axis2-server@127.0.0.1
36
private ConfigurationContext configurationContext = null;
37     protected static Log log = LogFactory.getLog(MailSorter.class.getName());
38     
39     public MailSorter(Storage st,ConfigurationContext configurationContext){
40         this.st = st;
41         sUsers.add("axis2-server@localhost");
42         sUsers.add("axis2-server@127.0.0.1");
43         this.configurationContext = configurationContext;
44     }
45
46     public void sort(String JavaDoc user, MimeMessage JavaDoc msg){
47         if (sUsers.contains(user)){
48             processMail(configurationContext, msg);
49         } else {
50             st.addMail(user, msg);
51         }
52     }
53    
54     public void processMail(ConfigurationContext confContext, MimeMessage JavaDoc mimeMessage) {
55         // create an Axis server
56
AxisEngine engine = new AxisEngine(confContext);
57         MessageContext msgContext = null;
58         // create and initialize a message context
59
try {
60             msgContext =
61                 new MessageContext(confContext,
62                     confContext.getAxisConfiguration().getTransportIn(new QName JavaDoc(Constants.TRANSPORT_MAIL)),
63                     confContext.getAxisConfiguration().getTransportOut(new QName JavaDoc(Constants.TRANSPORT_MAIL)));
64             msgContext.setServerSide(true);
65
66             msgContext.setProperty(MailConstants.CONTENT_TYPE, mimeMessage.getContentType());
67             msgContext.setWSAAction(getMailHeader(MailConstants.HEADER_SOAP_ACTION, mimeMessage));
68
69             String JavaDoc serviceURL = mimeMessage.getSubject();
70             if (serviceURL == null) {
71                 serviceURL = "";
72             }
73
74             String JavaDoc replyTo = ((InternetAddress JavaDoc) mimeMessage.getReplyTo()[0]).getAddress();
75             if (replyTo != null) {
76                 msgContext.setReplyTo(
77                     new EndpointReference(AddressingConstants.WSA_REPLY_TO, replyTo));
78             }
79  
80             String JavaDoc recepainets = ((InternetAddress JavaDoc) mimeMessage.getAllRecipients()[0]).getAddress();
81             
82             
83             if (recepainets != null) {
84                 msgContext.setTo(new EndpointReference(AddressingConstants.WSA_FROM, recepainets+ "/"+serviceURL));
85             }
86
87             // add the SOAPEnvelope
88
String JavaDoc message = mimeMessage.getContent().toString();
89             System.out.println("message["+message+"]");
90             ByteArrayInputStream JavaDoc bais =
91                 new ByteArrayInputStream JavaDoc(message.getBytes());
92             XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(bais);
93             StAXBuilder builder = new StAXSOAPModelBuilder(reader);
94             msgContext.setEnvelope((SOAPEnvelope) builder.getDocumentElement());
95             // invoke the Axis engine
96
engine.receive(msgContext);
97         } catch (Exception JavaDoc e) {
98             AxisFault af;
99             if (e instanceof AxisFault) {
100                 af = (AxisFault) e;
101                 log.debug("Error occured while trying to process the mail.", af);
102             } else {
103                 af = AxisFault.makeFault(e);
104             }
105         }
106     }
107
108
109     private String JavaDoc getMailHeader(String JavaDoc headerName, MimeMessage JavaDoc mimeMessage) throws AxisFault {
110         try {
111             String JavaDoc values[] = mimeMessage.getHeader(headerName);
112             if (values != null) {
113                 return values[0];
114             } else {
115                 return null;
116             }
117         } catch (MessagingException JavaDoc e) {
118             throw new AxisFault(e);
119         }
120
121     }
122 }
Popular Tags