1 32 33 package com.knowgate.hipermail; 34 35 import java.io.UnsupportedEncodingException ; 36 37 import java.sql.SQLException ; 38 import java.sql.PreparedStatement ; 39 40 import javax.mail.Address ; 41 import javax.mail.Message ; 42 import javax.mail.MessagingException ; 43 import javax.mail.internet.AddressException ; 44 import javax.mail.internet.InternetAddress ; 45 import javax.mail.internet.MimeMessage ; 46 47 import com.knowgate.jdc.JDCConnection; 48 import com.knowgate.debug.DebugFile; 49 import com.knowgate.dataobjs.DB; 50 import com.knowgate.crm.DistributionList; 51 import com.knowgate.misc.Gadgets; 52 import com.knowgate.workareas.WorkArea; 53 54 59 public class RecipientsHelper { 60 61 private InternetAddress [] aToAddrs; 62 private InternetAddress [] aCcAddrs; 63 private InternetAddress [] aBccAddrs; 64 private boolean bHasLists; 65 private String sWorkAreaId; 66 68 71 public RecipientsHelper() { 72 bHasLists = false; 73 aToAddrs = null; 74 aCcAddrs = null; 75 aBccAddrs = null; 76 sWorkAreaId = null; 77 } 78 79 81 84 public RecipientsHelper(String sWorkAreaGUID) { 85 bHasLists = false; 86 aToAddrs = null; 87 aCcAddrs = null; 88 aBccAddrs = null; 89 sWorkAreaId = sWorkAreaGUID; 90 } 91 92 94 99 public RecipientsHelper(MimeMessage oMsg) 100 throws MessagingException { 101 bHasLists = false; 102 setRecipients(oMsg); 103 } 104 105 107 112 public boolean hasLists() { 113 return bHasLists; 114 } 115 116 118 123 public String [] getAddresses(Message.RecipientType oRecTp) { 124 String [] aEmails = null; 125 if (oRecTp.equals(Message.RecipientType.TO)) { 126 if (aToAddrs==null) { 127 aEmails = null; 128 } else { 129 aEmails = new String [aToAddrs.length]; 130 for (int a=aToAddrs.length-1; a>=0; a--) 131 aEmails[a] = aToAddrs[a].getAddress(); 132 } 133 } 134 else if (oRecTp.equals(Message.RecipientType.CC)) { 135 if (aCcAddrs==null) { 136 aEmails = null; 137 } else { 138 aEmails = new String [aCcAddrs.length]; 139 for (int a=aCcAddrs.length-1; a>=0; a--) 140 aEmails[a] = aCcAddrs[a].getAddress(); 141 } 142 } 143 else if (oRecTp.equals(Message.RecipientType.BCC)) { 144 if (aBccAddrs==null) { 145 aEmails = null; 146 } else { 147 aEmails = new String [aBccAddrs.length]; 148 for (int a=aBccAddrs.length-1; a>=0; a--) 149 aEmails[a] = aBccAddrs[a].getAddress(); 150 } 151 } 152 return aEmails; 153 } 155 157 162 public Address [] getRecipients(Message.RecipientType oRecTp) { 163 if (oRecTp.equals(Message.RecipientType.TO)) 164 return aToAddrs; 165 else if (oRecTp.equals(Message.RecipientType.CC)) 166 return aCcAddrs; 167 else if (oRecTp.equals(Message.RecipientType.BCC)) 168 return aBccAddrs; 169 else 170 return null; 171 } 173 175 public void setRecipients(Address [] oAddrs, Message.RecipientType oRecTp) 176 throws ClassCastException { 177 if (oRecTp.equals(Message.RecipientType.TO)) 178 aToAddrs = (InternetAddress []) oAddrs; 179 else if (oRecTp.equals(Message.RecipientType.CC)) 180 aCcAddrs = (InternetAddress []) oAddrs; 181 else if (oRecTp.equals(Message.RecipientType.BCC)) 182 aBccAddrs = (InternetAddress []) oAddrs; 183 } 184 185 187 public void setRecipients(MimeMessage oMsg) 188 throws ClassCastException , MessagingException { 189 try { 190 aToAddrs = (InternetAddress []) oMsg.getRecipients(Message.RecipientType.TO); 191 } catch (AddressException adre) { if (DebugFile.trace) DebugFile.writeln("Recipient AddressException " + adre.getMessage()); } 192 try { 193 aCcAddrs = (InternetAddress []) oMsg.getRecipients(Message.RecipientType.CC); 194 } catch (AddressException adre) { if (DebugFile.trace) DebugFile.writeln("Recipient AddressException " + adre.getMessage()); } 195 try { 196 aBccAddrs= (InternetAddress []) oMsg.getRecipients(Message.RecipientType.BCC); 197 } catch (AddressException adre) { if (DebugFile.trace) DebugFile.writeln("Recipient AddressException " + adre.getMessage()); } 198 } 199 200 202 208 public void addRecipients(Address [] oAddrs, Message.RecipientType oRecTp) 209 throws ClassCastException { 210 InternetAddress [] aTmpAddrs; 211 if (null==oAddrs) return; 212 if (oAddrs.length==0) return; 213 if (oRecTp.equals(Message.RecipientType.TO)) { 214 if (null==aToAddrs) { 215 aToAddrs = new InternetAddress [oAddrs.length]; 216 System.arraycopy(oAddrs, 0, aToAddrs, 0, oAddrs.length); 217 } else { 218 aTmpAddrs = new InternetAddress [aToAddrs.length+oAddrs.length]; 219 System.arraycopy(aToAddrs, 0, aTmpAddrs, 0, aToAddrs.length); 220 System.arraycopy(oAddrs, 0, aTmpAddrs, aToAddrs.length, oAddrs.length); 221 aToAddrs = aTmpAddrs; 222 } 223 } else if (oRecTp.equals(Message.RecipientType.CC)) { 224 if (null==aCcAddrs) { 225 aCcAddrs = new InternetAddress [oAddrs.length]; 226 System.arraycopy(oAddrs, 0, aCcAddrs, 0, oAddrs.length); 227 } else { 228 aTmpAddrs = new InternetAddress [aCcAddrs.length+oAddrs.length]; 229 System.arraycopy(aCcAddrs, 0, aTmpAddrs, 0, aCcAddrs.length); 230 System.arraycopy(oAddrs, 0, aTmpAddrs, aCcAddrs.length, oAddrs.length); 231 aCcAddrs = aTmpAddrs; 232 } 233 } else if (oRecTp.equals(Message.RecipientType.BCC)) 234 if (null==aBccAddrs) { 235 aBccAddrs = new InternetAddress [oAddrs.length]; 236 System.arraycopy(oAddrs, 0, aBccAddrs, 0, oAddrs.length); 237 } else { 238 aTmpAddrs = new InternetAddress [aBccAddrs.length+oAddrs.length]; 239 System.arraycopy(aBccAddrs, 0, aTmpAddrs, 0, aBccAddrs.length); 240 System.arraycopy(oAddrs, 0, aTmpAddrs, aBccAddrs.length, oAddrs.length); 241 aBccAddrs = aTmpAddrs; 242 } 243 } 245 247 252 public static String joinAddressList (Address [] aRecipients) { 253 InternetAddress oInetAdr; 254 String sList = ""; 255 if (DebugFile.trace) { 256 DebugFile.writeln("Begin RecipientsHelper.joinAddressList(Address[])"); 257 DebugFile.incIdent(); 258 } 259 if (aRecipients!=null) { 260 int cRecipients = aRecipients.length; 261 if (cRecipients>0) { 262 for (int a=0; a<cRecipients; a++) { 263 oInetAdr = (InternetAddress ) aRecipients[a]; 264 if (0!=a) sList += ";"; 265 sList += oInetAdr.getAddress(); 266 } } } if (DebugFile.trace) { 270 DebugFile.writeln("End RecipientsHelper.joinAddressList() : " + sList); 271 DebugFile.incIdent(); 272 } 273 return sList; 274 } 276 278 298 public void parseRecipientsList(JDCConnection oAdCn, 299 String sDelimitedList, 300 Message.RecipientType oRecTp) 301 throws SQLException ,IndexOutOfBoundsException ,NullPointerException , 302 AddressException ,UnsupportedEncodingException { 303 int nRecipients; 304 305 if (DebugFile.trace) { 306 DebugFile.writeln("Begin RecipientsHelper.parseRecipientsList([JDCConnection],"+sDelimitedList+","+oRecTp+")"); 307 DebugFile.incIdent(); 308 } 309 String sLId; 310 String [] aRecipients = Gadgets.split(sDelimitedList, new char[]{',',';'}); 311 InternetAddress [] aAdrSet; 312 InternetAddress [] aBccSet; 313 int iPos; 314 int iListCount = 0; 315 316 if (aRecipients!=null) { 317 nRecipients = aRecipients.length; 318 if (nRecipients>0) { 319 for (int a=0; a<nRecipients; a++) { 320 sLId = aRecipients[a].trim(); 321 if (sLId.length()>4) { 322 if (sLId.startsWith("list@") && sLId.endsWith(".list") || 323 sLId.charAt(0)=='{' && sLId.charAt(sLId.length()-1)=='}') { 324 bHasLists=true; 325 iListCount++; 326 DistributionList oLst; 329 if (sLId.charAt(0)=='{') { 330 oLst = new DistributionList(oAdCn, sLId.substring(1,sLId.length()-1), sWorkAreaId); 331 if (oLst.isNull(DB.gu_list)) { 332 if (DebugFile.trace) DebugFile.decIdent(); 333 throw new AddressException ("RecipientsHelper.parseRecipientsList() list "+sLId.substring(1,sLId.length()-1)+" not found"); 334 } 335 } else { 336 oLst = new DistributionList(oAdCn, sLId.substring(sLId.indexOf('@')+1,sLId.indexOf('.'))); 337 if (oLst.isNull(DB.gu_list)) { 338 if (DebugFile.trace) DebugFile.decIdent(); 339 throw new AddressException ("RecipientsHelper.parseRecipientsList() list "+sLId.substring(sLId.indexOf('@')+1,sLId.indexOf('.'))+" not found"); 340 } 341 } 342 String sPer = oLst.getStringNull(DB.de_list,null); 343 String sLst = oLst.activeMembers(oAdCn); 344 if (sLst.length()>0) { 345 String [] aLst = Gadgets.split(sLst,','); 346 int iLst = aLst.length; 347 if (null==aBccAddrs) { 348 iPos = 0; 349 aBccSet = new InternetAddress [iLst]; 350 } else { 351 iPos = aBccAddrs.length; 352 aBccSet = new InternetAddress [iLst+iPos]; 353 System.arraycopy(aBccSet, 0, aBccAddrs, 0, iPos); 354 } 355 if (sPer==null) { 356 for (int l=0; l<iLst; l++) aBccSet[l+iPos] = new InternetAddress (aLst[l]); 357 } else { 358 for (int l=0; l<iLst; l++) aBccSet[l+iPos] = new InternetAddress (aLst[l],sPer); 359 } aBccAddrs = aBccSet; 361 aRecipients[a] = null; 362 } } } } aAdrSet = new InternetAddress [nRecipients-iListCount]; 367 iPos = 0; 368 for (int a=0; a<nRecipients; a++) { 369 if (null!=aRecipients[a]) { 370 aAdrSet[iPos] = DBInetAddr.parseAddress(aRecipients[a]); 371 iPos++; 372 } 373 } addRecipients(aAdrSet, oRecTp); 375 } } if (DebugFile.trace) { 378 DebugFile.decIdent(); 379 DebugFile.writeln("End RecipientsHelper.parseRecipientsList()"); 380 } 381 } 383 385 391 public static void clearRecipientsForMessage (JDCConnection oConn, String sGuMimeMsg) throws SQLException { 392 PreparedStatement oStmt = oConn.prepareStatement("DELETE FROM "+DB.k_inet_addrs+" WHERE "+DB.gu_mimemsg+"=?"); 393 oStmt.setString(1, sGuMimeMsg); 394 oStmt.executeUpdate(); 395 oStmt.close(); 396 } 398 400 public static InternetAddress getFromAddress(MimeMessage oMsg) throws MessagingException { 401 Address [] aFrom = null; 402 try { 403 aFrom = oMsg.getFrom(); 404 } catch (AddressException adre) { 405 if (DebugFile.trace) DebugFile.writeln("From AddressException " + adre.getMessage()); 406 } 407 InternetAddress oFrom; 408 if (aFrom!=null) { 409 if (aFrom.length>0) 410 oFrom = (InternetAddress ) aFrom[0]; 411 else 412 oFrom = null; 413 } 414 else 415 oFrom = null; 416 return oFrom; 417 } 419 421 public static InternetAddress getReplyAddress(MimeMessage oMsg) throws MessagingException { 422 Address [] aReply = null; 423 InternetAddress oReply; 424 try { 425 aReply = oMsg.getReplyTo(); 426 } catch (AddressException adre) { 427 if (DebugFile.trace) DebugFile.writeln("Reply-To AddressException " + adre.getMessage()); 428 } 429 430 if (aReply!=null) { 431 if (aReply.length>0) 432 oReply = (InternetAddress ) aReply[0]; 433 else 434 oReply = null; 435 } else { 436 if (DebugFile.trace) DebugFile.writeln("no reply-to address found"); 437 oReply = null; 438 } 439 return oReply; 440 } 441 } 442 | Popular Tags |