1 9 package org.jboss.net.axis.transport.mailto.client; 10 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.util.Hashtable ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Properties ; 17 18 import javax.activation.DataHandler ; 19 import javax.mail.Address ; 20 import javax.mail.Message ; 21 import javax.mail.MessagingException ; 22 import javax.mail.Session ; 23 import javax.mail.Transport ; 24 import javax.mail.internet.AddressException ; 25 import javax.mail.internet.InternetAddress ; 26 import javax.naming.Context ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingException ; 29 30 import org.apache.axis.AxisFault; 31 import org.apache.axis.MessageContext; 32 import org.apache.axis.handlers.BasicHandler; 33 import org.apache.axis.message.addressing.AddressingHeaders; 34 import org.apache.axis.message.addressing.Constants; 35 import org.apache.axis.message.addressing.EndpointReference; 36 import org.apache.axis.message.addressing.RelatesTo; 37 import org.apache.log4j.Logger; 38 import org.jboss.net.axis.transport.mailto.MailConstants; 39 import org.jboss.net.axis.transport.mailto.MailMessage; 40 import org.jboss.net.axis.transport.mailto.SOAPDataSource; 41 42 74 public class BaseMailSender extends BasicHandler implements MailConstants 75 { 76 77 public static final String MAIL_PROPS = "MailTransportClient.properties"; 78 public static final String TRANS_PROPS = "transport.properties"; 79 80 public static final String SESSION_NAME = "SessionName"; 81 82 protected Logger log = Logger.getLogger(getClass()); 83 84 86 static protected Properties mailProps = null; 87 88 90 static protected String mailSessionName = null; 91 92 95 public void invoke(MessageContext ctx) throws AxisFault 96 { 97 98 if (mailSessionName == null && mailProps == null) 99 { 100 if ((mailSessionName = (String ) getOption(SESSION_NAME)) == null) 101 { 102 mailProps = getJavaMailProperties(); 103 } 104 } 105 106 String msgID = sendMail(ctx); 107 108 archiveMessage(msgID, ctx); 110 111 checkResponse(ctx); 113 } 114 115 protected String sendMail(MessageContext ctx) throws AxisFault 116 { 117 MailMessage mailMsg; 118 org.apache.axis.Message soapMsg = ctx.getRequestMessage(); 119 Session session = getMailSession(); 120 String msgID = null; 121 122 AddressingHeaders headers = (AddressingHeaders) ctx.getProperty(Constants.ENV_ADDRESSING_REQUEST_HEADERS); 123 124 try 125 { 126 mailMsg = new MailMessage(session); 127 128 mailMsg.setRecipients(Message.RecipientType.TO, new Address []{new InternetAddress (headers.getTo().getPath())}); 129 130 131 try 132 { 133 EndpointReference from = headers.getFrom(); 135 if (from != null && !Constants.NS_URI_ANONYMOUS.equals(from.getAddress().toString())) 136 { 137 mailMsg.setFrom(new InternetAddress (from.getAddress().getPath())); 138 } 139 else 140 { 141 mailMsg.setFrom(InternetAddress.getLocalAddress(session)); 142 } 143 } 144 catch (AddressException ae) 145 { 146 mailMsg.setFrom(InternetAddress.getLocalAddress(session)); 148 } 149 150 if (headers.getMessageID() != null) 151 { 152 mailMsg.setMessageID(headers.getMessageID().toString()); 153 } 154 if (headers.getRelatesTo() != null) 155 { 156 List relatesTo = headers.getRelatesTo(); 157 for (Iterator iter = relatesTo.iterator(); iter.hasNext();) 158 { 159 RelatesTo rt = (RelatesTo) iter.next(); 160 if (Constants.QNAME_RESPONSE.equals(rt.getType())) 161 { 162 mailMsg.setHeader(HEADER_IN_REPLY_TO, rt.getURI().toString()); 163 } 164 } 165 } 166 167 mailMsg.setDataHandler(new DataHandler (new SOAPDataSource(soapMsg))); 169 170 Transport.send(mailMsg); 171 172 msgID = mailMsg.getMessageID(); 173 174 } catch (MessagingException e) 175 { 176 log.fatal("There was a problem creating the request email message", e); 177 throw new AxisFault("Unable to create request message"); 178 } 179 180 if (log.isDebugEnabled()) 181 log.debug("message-id: " + msgID); 182 183 return msgID; 184 } 185 186 193 protected void archiveMessage(String msgID, MessageContext msgCtx) 194 { 195 } 196 197 203 protected void checkResponse(MessageContext ctx) throws AxisFault 204 { 205 } 206 207 217 protected Properties getJavaMailProperties() throws AxisFault 218 { 219 Properties props = null; 220 Hashtable opts = getOptions(); 221 if (opts != null) 222 { 223 for (Iterator iter = opts.keySet().iterator(); iter.hasNext();) 224 { 225 String key = (String ) iter.next(); 226 if (key != null && key.startsWith("mail.")) 227 { 228 if (props == null) 229 props = new Properties (); 230 props.setProperty(key, (String ) opts.get(key)); 231 } 232 } 233 } 234 235 if (props == null) 236 { 237 String propfileName = (String ) getOption(TRANS_PROPS); 238 239 propfileName = propfileName == null ? MAIL_PROPS : propfileName; 242 243 InputStream is = getClass().getClassLoader().getResourceAsStream(propfileName); 244 if (is == null) 245 throw new AxisFault(propfileName + " not found."); 246 247 props = new Properties (); 248 try 249 { 250 props.load(is); 251 } catch (IOException e) 252 { 253 throw new AxisFault("Unable to load mail properties.", e); 254 } 255 } 256 257 return props; 258 } 259 260 264 protected Session getMailSession() throws AxisFault 265 { 266 if (log.isDebugEnabled()) 267 log.debug("Entering: getMailSession()"); 268 269 Context ctx = null; 270 Session mail = null; 271 272 if (mailSessionName == null) 273 { 274 mail = Session.getInstance(mailProps); 275 } else 276 { 277 try 278 { 279 ctx = new InitialContext (); 280 mail = (Session ) ctx.lookup(mailSessionName); 281 } catch (NamingException ne) 282 { 283 log.fatal("NamingException: getMailSession()\n", ne); 284 throw new AxisFault("Unable to find Email Session."); 285 } finally 286 { 287 if (ctx != null) 288 try 289 { 290 ctx.close(); 291 } catch (NamingException ne) 292 { 293 } 294 } 295 } 296 297 if (log.isDebugEnabled()) 298 log.debug("Leaving: getMailSession()"); 299 return mail; 300 } 301 } | Popular Tags |