1 16 17 package org.apache.axis.transport.mail; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.Constants; 21 import org.apache.axis.Message; 22 import org.apache.axis.MessageContext; 23 import org.apache.axis.components.logger.LogFactory; 24 import org.apache.axis.message.SOAPEnvelope; 25 import org.apache.axis.message.SOAPFault; 26 import org.apache.axis.server.AxisServer; 27 import org.apache.axis.transport.http.HTTPConstants; 28 import org.apache.axis.utils.Messages; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.net.smtp.SMTPClient; 31 import org.apache.commons.net.smtp.SMTPReply; 32 33 import javax.mail.Session ; 34 import javax.mail.internet.InternetAddress ; 35 import javax.mail.internet.MimeMessage ; 36 import javax.mail.internet.MimePart ; 37 import java.io.ByteArrayOutputStream ; 38 import java.io.Writer ; 39 import java.util.Properties ; 40 41 42 public class MailWorker implements Runnable { 43 protected static Log log = 44 LogFactory.getLog(MailWorker.class.getName()); 45 46 private MailServer server; 48 49 private MimeMessage mimeMessage; 51 52 private static String transportName = "Mail"; 54 55 private Properties prop = new Properties (); 56 private Session session = Session.getDefaultInstance(prop, null); 57 58 63 public MailWorker(MailServer server, MimeMessage mimeMessage) { 64 this.server = server; 65 this.mimeMessage = mimeMessage; 66 } 67 68 71 public void run() { 72 AxisServer engine = MailServer.getAxisServer(); 74 75 MessageContext msgContext = new MessageContext(engine); 77 Message requestMsg; 78 79 StringBuffer soapAction = new StringBuffer (); 81 StringBuffer fileName = new StringBuffer (); 82 StringBuffer contentType = new StringBuffer (); 83 StringBuffer contentLocation = new StringBuffer (); 84 85 Message responseMsg = null; 86 87 try { 90 msgContext.setTargetService(null); 91 } catch (AxisFault fault) { 92 } 93 msgContext.setResponseMessage(null); 94 msgContext.reset(); 95 msgContext.setTransportName(transportName); 96 97 responseMsg = null; 98 99 try { 100 try { 101 parseHeaders(mimeMessage, contentType, 103 contentLocation, soapAction); 104 105 msgContext.setProperty(Constants.MC_REALPATH, 108 fileName.toString()); 109 msgContext.setProperty(Constants.MC_RELATIVE_PATH, 110 fileName.toString()); 111 msgContext.setProperty(Constants.MC_JWS_CLASSDIR, 112 "jwsClasses"); 113 114 String soapActionString = soapAction.toString(); 117 if (soapActionString != null) { 118 msgContext.setUseSOAPAction(true); 119 msgContext.setSOAPActionURI(soapActionString); 120 } 121 requestMsg = new Message(mimeMessage.getInputStream(), false, 122 contentType.toString(), contentLocation.toString()); 123 msgContext.setRequestMessage(requestMsg); 124 125 engine.invoke(msgContext); 127 128 responseMsg = msgContext.getResponseMessage(); 130 if (responseMsg == null) { 131 throw new AxisFault(Messages.getMessage("nullResponse00")); 132 } 133 } catch (Exception e) { 134 e.printStackTrace(); 135 AxisFault af; 136 if (e instanceof AxisFault) { 137 af = (AxisFault) e; 138 log.debug(Messages.getMessage("serverFault00"), af); 139 } else { 140 af = AxisFault.makeFault(e); 141 } 142 143 responseMsg = msgContext.getResponseMessage(); 147 if (responseMsg == null) { 148 responseMsg = new Message(af); 149 } else { 150 try { 151 SOAPEnvelope env = responseMsg.getSOAPEnvelope(); 152 env.clearBody(); 153 env.addBodyElement(new SOAPFault((AxisFault) e)); 154 } catch (AxisFault fault) { 155 } 157 } 158 } 159 160 String replyTo = ((InternetAddress ) mimeMessage.getReplyTo()[0]).getAddress(); 161 String sendFrom = ((InternetAddress ) mimeMessage.getAllRecipients()[0]).getAddress(); 162 String subject = "Re: " + mimeMessage.getSubject(); 163 writeUsingSMTP(msgContext, server.getHost(), sendFrom, replyTo, subject, responseMsg); 164 } catch (Exception e) { 165 e.printStackTrace(); 166 log.debug(Messages.getMessage("exception00"), e); 167 } 168 if (msgContext.getProperty(MessageContext.QUIT_REQUESTED) != null) { 169 try { 171 server.stop(); 172 } catch (Exception e) { 173 } 174 } 175 176 } 177 178 188 private void writeUsingSMTP(MessageContext msgContext, 189 String smtpHost, 190 String sendFrom, 191 String replyTo, 192 String subject, 193 Message output) 194 throws Exception { 195 SMTPClient client = new SMTPClient(); 196 client.connect(smtpHost); 197 198 System.out.print(client.getReplyString()); 201 int reply = client.getReplyCode(); 202 if (!SMTPReply.isPositiveCompletion(reply)) { 203 client.disconnect(); 204 AxisFault fault = new AxisFault("SMTP", "( SMTP server refused connection )", null, null); 205 throw fault; 206 } 207 208 client.login(smtpHost); 209 System.out.print(client.getReplyString()); 210 reply = client.getReplyCode(); 211 if (!SMTPReply.isPositiveCompletion(reply)) { 212 client.disconnect(); 213 AxisFault fault = new AxisFault("SMTP", "( SMTP server refused connection )", null, null); 214 throw fault; 215 } 216 217 MimeMessage msg = new MimeMessage (session); 218 msg.setFrom(new InternetAddress (sendFrom)); 219 msg.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress (replyTo)); 220 msg.setDisposition(MimePart.INLINE); 221 msg.setSubject(subject); 222 223 ByteArrayOutputStream out = new ByteArrayOutputStream (8 * 1024); 224 output.writeTo(out); 225 msg.setContent(out.toString(), output.getContentType(msgContext.getSOAPConstants())); 226 227 ByteArrayOutputStream out2 = new ByteArrayOutputStream (8 * 1024); 228 msg.writeTo(out2); 229 230 client.setSender(sendFrom); 231 System.out.print(client.getReplyString()); 232 client.addRecipient(replyTo); 233 System.out.print(client.getReplyString()); 234 235 Writer writer = client.sendMessageData(); 236 System.out.print(client.getReplyString()); 237 writer.write(out2.toString()); 238 writer.flush(); 239 writer.close(); 240 241 System.out.print(client.getReplyString()); 242 if (!client.completePendingCommand()) { 243 System.out.print(client.getReplyString()); 244 AxisFault fault = new AxisFault("SMTP", "( Failed to send email )", null, null); 245 throw fault; 246 } 247 System.out.print(client.getReplyString()); 248 client.logout(); 249 client.disconnect(); 250 } 251 252 260 private void parseHeaders(MimeMessage mimeMessage, 261 StringBuffer contentType, 262 StringBuffer contentLocation, 263 StringBuffer soapAction) 264 throws Exception { 265 contentType.append(mimeMessage.getContentType()); 266 contentLocation.append(mimeMessage.getContentID()); 267 String values[] = mimeMessage.getHeader(HTTPConstants.HEADER_SOAP_ACTION); 268 if (values != null) 269 soapAction.append(values[0]); 270 } 271 } 272 | Popular Tags |