KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > mail > MailWorker


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16
17 package org.apache.axis2.transport.mail;
18
19 import org.apache.axis2.Constants;
20 import org.apache.axis2.addressing.AddressingConstants;
21 import org.apache.axis2.addressing.EndpointReference;
22 import org.apache.axis2.context.ConfigurationContext;
23 import org.apache.axis2.context.MessageContext;
24 import org.apache.axis2.description.TransportInDescription;
25 import org.apache.axis2.description.TransportOutDescription;
26 import org.apache.axis2.engine.AxisEngine;
27 import org.apache.axis2.engine.AxisFault;
28 import org.apache.axis2.om.impl.llom.builder.StAXBuilder;
29 import org.apache.axis2.soap.SOAPEnvelope;
30 import org.apache.axis2.soap.impl.llom.builder.StAXSOAPModelBuilder;
31 import org.apache.axis2.util.threadpool.AxisWorker;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import javax.mail.MessagingException JavaDoc;
36 import javax.mail.Session JavaDoc;
37 import javax.mail.internet.InternetAddress JavaDoc;
38 import javax.mail.internet.MimeMessage JavaDoc;
39 import javax.xml.namespace.QName JavaDoc;
40 import javax.xml.stream.XMLInputFactory;
41 import javax.xml.stream.XMLStreamReader;
42 import java.io.ByteArrayInputStream JavaDoc;
43 import java.util.Properties JavaDoc;
44
45 public class MailWorker implements AxisWorker {
46     protected static Log log = LogFactory.getLog(MailWorker.class.getName());
47
48     private String JavaDoc contentType = "text/xml";
49
50     private ConfigurationContext reg = null;
51
52     // Current message
53
private MimeMessage JavaDoc mimeMessage;
54
55     private Properties JavaDoc prop = new Properties JavaDoc();
56
57     private Session JavaDoc session = Session.getDefaultInstance(prop, null);
58
59     /**
60      * Constructor for MailWorker
61      *
62      * @param server
63      * @param mimeMessage
64      */

65     public MailWorker(MimeMessage JavaDoc mimeMessage, ConfigurationContext reg) {
66         this.mimeMessage = mimeMessage;
67         this.reg = reg;
68     }
69
70     /**
71      * The main workhorse method.
72      */

73     public void doWork() {
74         // create an Axis server
75
AxisEngine engine = new AxisEngine(reg);
76         MessageContext msgContext = null;
77         // create and initialize a message context
78
try {
79             TransportInDescription transportIn = reg.getAxisConfiguration().getTransportIn(new QName JavaDoc(Constants.TRANSPORT_MAIL));
80             TransportOutDescription transportOut = reg.getAxisConfiguration().getTransportOut(new QName JavaDoc(Constants.TRANSPORT_MAIL));
81             
82             msgContext = new MessageContext(reg,transportIn,transportOut);
83             msgContext.setServerSide(true);
84             msgContext.setProperty(MailConstants.CONTENT_TYPE, mimeMessage.getContentType());
85             String JavaDoc soapAction = getMailHeader(MailConstants.HEADER_SOAP_ACTION);
86             msgContext.setWSAAction(soapAction);
87             msgContext.setSoapAction(soapAction);
88
89             String JavaDoc serviceURL = mimeMessage.getSubject();
90             if (serviceURL == null) {
91                 serviceURL = "";
92             }
93
94             String JavaDoc replyTo = ((InternetAddress JavaDoc) mimeMessage.getReplyTo()[0]).getAddress();
95             if (replyTo != null) {
96                 msgContext.setReplyTo(
97                     new EndpointReference(AddressingConstants.WSA_REPLY_TO, replyTo));
98             }
99             
100             String JavaDoc recepainets = ((InternetAddress JavaDoc) mimeMessage.getAllRecipients()[0]).getAddress();
101             if (recepainets != null) {
102                 msgContext.setTo(new EndpointReference(AddressingConstants.WSA_FROM, recepainets+ "/"+serviceURL));
103             }else{
104                 throw new AxisFault("No receptineist found in the Email");
105             }
106
107             // add the SOAPEnvelope
108
String JavaDoc message = mimeMessage.getContent().toString();
109             System.out.println("message["+message+"]");
110             ByteArrayInputStream JavaDoc bais =
111                 new ByteArrayInputStream JavaDoc(message.getBytes());
112             XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(bais);
113             StAXBuilder builder = new StAXSOAPModelBuilder(reader);
114             msgContext.setEnvelope((SOAPEnvelope) builder.getDocumentElement());
115
116             // invoke the Axis engine
117
engine.receive(msgContext);
118         } catch (Exception JavaDoc e) {
119             e.printStackTrace();
120             log.error(e);
121         }
122
123         /*
124          *
125          * This part is ignored for the time being. CT 07-Feb-2005.
126          *
127          * if (msgContext.getProperty(MessageContext.QUIT_REQUESTED) != null) { //
128          * why then, quit! try { server.stop(); } catch (Exception e) { } }
129          */

130     }
131
132     private String JavaDoc getMailHeader(String JavaDoc headerName) throws AxisFault {
133         try {
134             String JavaDoc values[] = mimeMessage.getHeader(headerName);
135             if (values != null) {
136                 return values[0];
137             } else {
138                 return null;
139             }
140         } catch (MessagingException JavaDoc e) {
141             throw new AxisFault(e);
142         }
143
144     }
145
146 }
Popular Tags