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 ; 18 import javax.mail.internet.InternetAddress ; 19 import javax.mail.internet.MimeMessage ; 20 import javax.xml.namespace.QName ; 21 import javax.xml.stream.XMLInputFactory; 22 import javax.xml.stream.XMLStreamReader; 23 import java.io.ByteArrayInputStream ; 24 import java.util.ArrayList ; 25 26 33 public class MailSorter { 34 Storage st = null; 35 private ArrayList sUsers = new ArrayList (); 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 user, MimeMessage 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 mimeMessage) { 55 AxisEngine engine = new AxisEngine(confContext); 57 MessageContext msgContext = null; 58 try { 60 msgContext = 61 new MessageContext(confContext, 62 confContext.getAxisConfiguration().getTransportIn(new QName (Constants.TRANSPORT_MAIL)), 63 confContext.getAxisConfiguration().getTransportOut(new QName (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 serviceURL = mimeMessage.getSubject(); 70 if (serviceURL == null) { 71 serviceURL = ""; 72 } 73 74 String replyTo = ((InternetAddress ) mimeMessage.getReplyTo()[0]).getAddress(); 75 if (replyTo != null) { 76 msgContext.setReplyTo( 77 new EndpointReference(AddressingConstants.WSA_REPLY_TO, replyTo)); 78 } 79 80 String recepainets = ((InternetAddress ) mimeMessage.getAllRecipients()[0]).getAddress(); 81 82 83 if (recepainets != null) { 84 msgContext.setTo(new EndpointReference(AddressingConstants.WSA_FROM, recepainets+ "/"+serviceURL)); 85 } 86 87 String message = mimeMessage.getContent().toString(); 89 System.out.println("message["+message+"]"); 90 ByteArrayInputStream bais = 91 new ByteArrayInputStream (message.getBytes()); 92 XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(bais); 93 StAXBuilder builder = new StAXSOAPModelBuilder(reader); 94 msgContext.setEnvelope((SOAPEnvelope) builder.getDocumentElement()); 95 engine.receive(msgContext); 97 } catch (Exception 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 getMailHeader(String headerName, MimeMessage mimeMessage) throws AxisFault { 110 try { 111 String values[] = mimeMessage.getHeader(headerName); 112 if (values != null) { 113 return values[0]; 114 } else { 115 return null; 116 } 117 } catch (MessagingException e) { 118 throw new AxisFault(e); 119 } 120 121 } 122 } | Popular Tags |