1 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 ; 36 import javax.mail.Session ; 37 import javax.mail.internet.InternetAddress ; 38 import javax.mail.internet.MimeMessage ; 39 import javax.xml.namespace.QName ; 40 import javax.xml.stream.XMLInputFactory; 41 import javax.xml.stream.XMLStreamReader; 42 import java.io.ByteArrayInputStream ; 43 import java.util.Properties ; 44 45 public class MailWorker implements AxisWorker { 46 protected static Log log = LogFactory.getLog(MailWorker.class.getName()); 47 48 private String contentType = "text/xml"; 49 50 private ConfigurationContext reg = null; 51 52 private MimeMessage mimeMessage; 54 55 private Properties prop = new Properties (); 56 57 private Session session = Session.getDefaultInstance(prop, null); 58 59 65 public MailWorker(MimeMessage mimeMessage, ConfigurationContext reg) { 66 this.mimeMessage = mimeMessage; 67 this.reg = reg; 68 } 69 70 73 public void doWork() { 74 AxisEngine engine = new AxisEngine(reg); 76 MessageContext msgContext = null; 77 try { 79 TransportInDescription transportIn = reg.getAxisConfiguration().getTransportIn(new QName (Constants.TRANSPORT_MAIL)); 80 TransportOutDescription transportOut = reg.getAxisConfiguration().getTransportOut(new QName (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 soapAction = getMailHeader(MailConstants.HEADER_SOAP_ACTION); 86 msgContext.setWSAAction(soapAction); 87 msgContext.setSoapAction(soapAction); 88 89 String serviceURL = mimeMessage.getSubject(); 90 if (serviceURL == null) { 91 serviceURL = ""; 92 } 93 94 String replyTo = ((InternetAddress ) mimeMessage.getReplyTo()[0]).getAddress(); 95 if (replyTo != null) { 96 msgContext.setReplyTo( 97 new EndpointReference(AddressingConstants.WSA_REPLY_TO, replyTo)); 98 } 99 100 String recepainets = ((InternetAddress ) 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 String message = mimeMessage.getContent().toString(); 109 System.out.println("message["+message+"]"); 110 ByteArrayInputStream bais = 111 new ByteArrayInputStream (message.getBytes()); 112 XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(bais); 113 StAXBuilder builder = new StAXSOAPModelBuilder(reader); 114 msgContext.setEnvelope((SOAPEnvelope) builder.getDocumentElement()); 115 116 engine.receive(msgContext); 118 } catch (Exception e) { 119 e.printStackTrace(); 120 log.error(e); 121 } 122 123 130 } 131 132 private String getMailHeader(String headerName) throws AxisFault { 133 try { 134 String values[] = mimeMessage.getHeader(headerName); 135 if (values != null) { 136 return values[0]; 137 } else { 138 return null; 139 } 140 } catch (MessagingException e) { 141 throw new AxisFault(e); 142 } 143 144 } 145 146 } | Popular Tags |