1 25 26 package org.objectweb.jonas.mail.factory; 27 28 29 import java.io.ByteArrayInputStream ; 31 import java.io.IOException ; 32 import java.io.ObjectInputStream ; 33 import java.io.OptionalDataException ; 34 import java.util.Hashtable ; 35 import java.util.Properties ; 36 import java.util.StringTokenizer ; 37 38 import javax.mail.internet.MimeMessage ; 40 import javax.mail.internet.MimePart ; 41 import javax.mail.internet.MimePartDataSource ; 42 import javax.mail.internet.InternetAddress ; 43 import javax.mail.Message ; 44 import javax.mail.Session ; 45 import javax.naming.Context ; 46 import javax.naming.Name ; 47 import javax.naming.Reference ; 48 import javax.naming.RefAddr ; 49 import javax.naming.spi.ObjectFactory ; 50 51 import org.objectweb.util.monolog.api.BasicLevel; 53 import org.objectweb.util.monolog.api.Logger; 54 55 import org.objectweb.jonas.common.Log; 57 import org.objectweb.jonas.common.PropDump; 58 import org.objectweb.jonas.common.JNDIUtils; 59 import org.objectweb.jonas.mail.lib.JAuthenticator; 60 61 67 public class JavaMailMimePartDSFactory implements ObjectFactory { 68 69 72 protected static final String FACTORY_TYPE = "javax.mail.internet.MimePartDataSource"; 73 74 77 private static Logger logger = null; 78 79 80 81 98 public Object getObjectInstance(Object obj, Name name, Context nameCtx, 99 Hashtable environment) throws Exception { 100 101 if (logger == null) { 103 logger = Log.getLogger(Log.JONAS_MAIL_PREFIX); 104 } 105 106 Reference ref = (Reference ) obj; 108 109 String clname = ref.getClassName(); 111 112 if (!ref.getClassName().equals(FACTORY_TYPE)) { 114 logger.log(BasicLevel.ERROR, "Cannot create object : required type is '" + FACTORY_TYPE + "', but found type is '" + clname + "'."); 115 return (null); 116 } 117 118 Properties sessionProps = new Properties (); 119 Properties mimeMessageProps = new Properties (); 120 Properties authenticationProps = new Properties (); 121 RefAddr refAddr = null; 122 123 refAddr = ref.get("javaxmailSession.properties"); 124 if (refAddr != null) { 125 sessionProps = (Properties ) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent()); 126 if (logger.isLoggable(BasicLevel.DEBUG)) { 127 PropDump.print("These are the properties used to obtain a new Session object", sessionProps, logger, BasicLevel.DEBUG); 128 } 129 } 130 131 refAddr = ref.get("javaxInternetMimeMessage.properties"); 132 if (refAddr != null) { 133 mimeMessageProps = (Properties ) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent()); 134 if (logger.isLoggable(BasicLevel.DEBUG)) { 135 PropDump.print("These are the properties specific to Internet mail", 136 mimeMessageProps, logger, BasicLevel.DEBUG); 137 } 138 } 139 140 refAddr = ref.get("authentication.properties"); 141 if (refAddr != null) { 142 authenticationProps = (Properties ) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent()); 143 if (logger.isLoggable(BasicLevel.DEBUG)) { 144 PropDump.print("These are the authentication properties", 145 authenticationProps, logger, BasicLevel.DEBUG); 146 } 147 } 148 149 String mailAuthenticationUsername = authenticationProps.getProperty("mail.authentication.username"); 151 152 String mailAuthenticationPassword = authenticationProps.getProperty("mail.authentication.password"); 154 155 String mailTo = mimeMessageProps.getProperty("mail.to"); 157 InternetAddress [] toRecipients = null; 158 if (mailTo != null) { 159 toRecipients = getInternetAddressFromString(mailTo); 160 } 161 162 String mailCc = mimeMessageProps.getProperty("mail.cc"); 164 InternetAddress [] ccRecipients = null; 165 if (ccRecipients != null) { 166 ccRecipients = getInternetAddressFromString(mailCc); 167 } 168 169 String mailBcc = mimeMessageProps.getProperty("mail.bcc"); 171 InternetAddress [] bccRecipients = null; 172 if (bccRecipients != null) { 173 getInternetAddressFromString(mailBcc); 174 } 175 176 String mailSubject = mimeMessageProps.getProperty("mail.subject"); 178 179 180 JAuthenticator jAuthenticator = null; 181 if ((mailAuthenticationUsername != null) && (mailAuthenticationPassword != null)) { 182 jAuthenticator = new JAuthenticator(mailAuthenticationUsername, mailAuthenticationPassword); 183 } 184 185 MimeMessage mimeMessage = new MimeMessage (Session.getInstance(sessionProps, jAuthenticator)); 187 188 190 if (toRecipients != null) { 192 mimeMessage.setRecipients(Message.RecipientType.TO, toRecipients); 193 } 194 195 if (ccRecipients != null) { 197 mimeMessage.setRecipients(Message.RecipientType.CC, ccRecipients); 198 } 199 200 if (bccRecipients != null) { 202 mimeMessage.setRecipients(Message.RecipientType.BCC, bccRecipients); 203 } 204 205 if (mailSubject != null) { 207 mimeMessage.setSubject(mailSubject); 208 } 209 210 MimePartDataSource mimePartDS = new MimePartDataSource ((MimePart ) mimeMessage); 211 return mimePartDS; 212 } 213 214 219 220 private InternetAddress [] getInternetAddressFromString(String txt) { 221 if (txt == null) { 222 return null; 223 } 224 StringTokenizer st = new StringTokenizer (txt, ","); 225 InternetAddress [] addresses = new InternetAddress [st.countTokens()]; 226 int i = 0; 227 try { 228 while (st.hasMoreTokens()) { 229 addresses[i] = new InternetAddress ((String ) st.nextToken()); 230 i++; 231 } 232 } catch (Exception e) { 233 return null; 234 } 235 return addresses; 236 } 237 } 238 | Popular Tags |