KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipermail > RecipientsHelper


1 /*
2   Copyright (C) 2005 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.hipermail;
34
35 import java.io.UnsupportedEncodingException JavaDoc;
36
37 import java.sql.SQLException JavaDoc;
38 import java.sql.PreparedStatement JavaDoc;
39
40 import javax.mail.Address JavaDoc;
41 import javax.mail.Message JavaDoc;
42 import javax.mail.MessagingException JavaDoc;
43 import javax.mail.internet.AddressException JavaDoc;
44 import javax.mail.internet.InternetAddress JavaDoc;
45 import javax.mail.internet.MimeMessage JavaDoc;
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 /**
55  * Helper class for working with recipients lists
56  * @author Sergio Montoro Ten
57  * @version 3.0
58  */

59 public class RecipientsHelper {
60
61   private InternetAddress JavaDoc[] aToAddrs;
62   private InternetAddress JavaDoc[] aCcAddrs;
63   private InternetAddress JavaDoc[] aBccAddrs;
64   private boolean bHasLists;
65   private String JavaDoc sWorkAreaId;
66   // ---------------------------------------------------------------------------
67

68   /**
69    * Default Constructor
70    */

71   public RecipientsHelper() {
72     bHasLists = false;
73     aToAddrs = null;
74     aCcAddrs = null;
75     aBccAddrs = null;
76     sWorkAreaId = null;
77   }
78
79   // ---------------------------------------------------------------------------
80

81   /**
82    * Construct and set default workarea for this recipients helper
83    */

84   public RecipientsHelper(String JavaDoc sWorkAreaGUID) {
85     bHasLists = false;
86     aToAddrs = null;
87     aCcAddrs = null;
88     aBccAddrs = null;
89     sWorkAreaId = sWorkAreaGUID;
90   }
91
92   // ---------------------------------------------------------------------------
93

94   /**
95    * Create RecipientsHelper and fill it with MimeMessage recipients
96    * @param oMsg MimeMessage
97    * @throws MessagingException
98    */

99   public RecipientsHelper(MimeMessage JavaDoc oMsg)
100     throws MessagingException JavaDoc {
101     bHasLists = false;
102     setRecipients(oMsg);
103   }
104
105   // ---------------------------------------------------------------------------
106

107   /**
108    * If called after parseRecipientsList(), this methods returns whether or not
109    * any distribution list was expanded during the parsing process
110    * @return boolean
111    */

112   public boolean hasLists() {
113     return bHasLists;
114   }
115
116   // ---------------------------------------------------------------------------
117

118   /**
119    * Get array with recipients of a given type
120    * @param oRecTp RecipientType
121    * @return InternetAddress[]
122    */

123   public String JavaDoc[] getAddresses(Message.RecipientType JavaDoc oRecTp) {
124     String JavaDoc[] aEmails = null;
125     if (oRecTp.equals(Message.RecipientType.TO)) {
126       if (aToAddrs==null) {
127         aEmails = null;
128       } else {
129         aEmails = new String JavaDoc[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 JavaDoc[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 JavaDoc[aBccAddrs.length];
148         for (int a=aBccAddrs.length-1; a>=0; a--)
149           aEmails[a] = aBccAddrs[a].getAddress();
150       }
151     }
152     return aEmails;
153   } // getAddresses
154

155   // ---------------------------------------------------------------------------
156

157   /**
158    * Get array with recipients of a given type
159    * @param oRecTp RecipientType
160    * @return InternetAddress[]
161    */

162   public Address JavaDoc[] getRecipients(Message.RecipientType JavaDoc 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   } // getRecipients
172

173   // ---------------------------------------------------------------------------
174

175   public void setRecipients(Address JavaDoc[] oAddrs, Message.RecipientType JavaDoc oRecTp)
176     throws ClassCastException JavaDoc {
177     if (oRecTp.equals(Message.RecipientType.TO))
178       aToAddrs = (InternetAddress JavaDoc[]) oAddrs;
179     else if (oRecTp.equals(Message.RecipientType.CC))
180       aCcAddrs = (InternetAddress JavaDoc[]) oAddrs;
181     else if (oRecTp.equals(Message.RecipientType.BCC))
182       aBccAddrs = (InternetAddress JavaDoc[]) oAddrs;
183   }
184
185   // ---------------------------------------------------------------------------
186

187   public void setRecipients(MimeMessage JavaDoc oMsg)
188     throws ClassCastException JavaDoc, MessagingException JavaDoc {
189     try {
190       aToAddrs = (InternetAddress JavaDoc[]) oMsg.getRecipients(Message.RecipientType.TO);
191     } catch (AddressException JavaDoc adre) { if (DebugFile.trace) DebugFile.writeln("Recipient AddressException " + adre.getMessage()); }
192     try {
193       aCcAddrs = (InternetAddress JavaDoc[]) oMsg.getRecipients(Message.RecipientType.CC);
194     } catch (AddressException JavaDoc adre) { if (DebugFile.trace) DebugFile.writeln("Recipient AddressException " + adre.getMessage()); }
195     try {
196       aBccAddrs= (InternetAddress JavaDoc[]) oMsg.getRecipients(Message.RecipientType.BCC);
197     } catch (AddressException JavaDoc adre) { if (DebugFile.trace) DebugFile.writeln("Recipient AddressException " + adre.getMessage()); }
198   }
199
200   // ---------------------------------------------------------------------------
201

202   /**
203    * Add recipients of a given type
204    * @param oAddrs InternetAddress[]
205    * @param oRecTp RecipientType
206    * @throws ClassCastException
207    */

208   public void addRecipients(Address JavaDoc[] oAddrs, Message.RecipientType JavaDoc oRecTp)
209     throws ClassCastException JavaDoc {
210     InternetAddress JavaDoc[] 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 JavaDoc[oAddrs.length];
216         System.arraycopy(oAddrs, 0, aToAddrs, 0, oAddrs.length);
217       } else {
218         aTmpAddrs = new InternetAddress JavaDoc[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 JavaDoc[oAddrs.length];
226         System.arraycopy(oAddrs, 0, aCcAddrs, 0, oAddrs.length);
227       } else {
228         aTmpAddrs = new InternetAddress JavaDoc[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 JavaDoc[oAddrs.length];
236         System.arraycopy(oAddrs, 0, aBccAddrs, 0, oAddrs.length);
237       } else {
238         aTmpAddrs = new InternetAddress JavaDoc[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     } // addRecipients
244

245   // ---------------------------------------------------------------------------
246

247   /**
248    * Join mail addresses array on a single String
249    * @param aRecipients Address[]
250    * @return String Mail addresses delimited by semicolons
251    */

252   public static String JavaDoc joinAddressList (Address JavaDoc[] aRecipients) {
253     InternetAddress JavaDoc oInetAdr;
254     String JavaDoc 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 JavaDoc) aRecipients[a];
264           if (0!=a) sList += ";";
265           sList += oInetAdr.getAddress();
266         } // next
267
} // fi (cRecipients>0)
268
} // fi (aRecipients)
269
if (DebugFile.trace) {
270       DebugFile.writeln("End RecipientsHelper.joinAddressList() : " + sList);
271       DebugFile.incIdent();
272     }
273     return sList;
274     } // joinAddressList
275

276   // ---------------------------------------------------------------------------
277

278   /**
279    * <p>Parse a String of comma or semicolon delimited addresses</p>
280    * Addresses may be of any format accepted by DBInetAddr.parseAddress() method.<br>
281    * Distribution lists present at input string are expanded into BCC internal array.<br>
282    * Distribution list names begin with "list@" and end with ".list".<br>
283    * Or, also distribution list names are enclosed by brackets like "{this is a list}"<br>
284    * Thus if "engineers" is the GUID of a list containing members luke@engineers.com,peter@engineers.com
285    * then TO "jhon@code.com,martin@maths.com,list@engineers.list,steve@maths.com"
286    * will be parsed as<br>
287    * TO jhon@code.com,martin@maths.com,steve@maths.com<br>
288    * BCC luke@engineers.com,peter@engineers.com
289    * @param oAdCn JDCConnection
290    * @param sDelimitedList String with addresses to be parsed
291    * @param oRecTp RecipientType
292    * @throws SQLException
293    * @throws IndexOutOfBoundsException
294    * @throws NullPointerException
295    * @throws AddressException
296    * @throws UnsupportedEncodingException
297    */

298   public void parseRecipientsList(JDCConnection oAdCn,
299                                   String JavaDoc sDelimitedList,
300                                   Message.RecipientType JavaDoc oRecTp)
301     throws SQLException JavaDoc,IndexOutOfBoundsException JavaDoc,NullPointerException JavaDoc,
302            AddressException JavaDoc,UnsupportedEncodingException JavaDoc{
303     int nRecipients;
304
305     if (DebugFile.trace) {
306       DebugFile.writeln("Begin RecipientsHelper.parseRecipientsList([JDCConnection],"+sDelimitedList+","+oRecTp+")");
307       DebugFile.incIdent();
308     }
309     String JavaDoc sLId;
310     String JavaDoc[] aRecipients = Gadgets.split(sDelimitedList, new char[]{',',';'});
311     InternetAddress JavaDoc[] aAdrSet;
312     InternetAddress JavaDoc[] 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               // *******************************
327
// Resolve list name to member set
328
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 JavaDoc("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 JavaDoc("RecipientsHelper.parseRecipientsList() list "+sLId.substring(sLId.indexOf('@')+1,sLId.indexOf('.'))+" not found");
340                 }
341               }
342               String JavaDoc sPer = oLst.getStringNull(DB.de_list,null);
343               String JavaDoc sLst = oLst.activeMembers(oAdCn);
344               if (sLst.length()>0) {
345                 String JavaDoc[] aLst = Gadgets.split(sLst,',');
346                 int iLst = aLst.length;
347                 if (null==aBccAddrs) {
348                   iPos = 0;
349                   aBccSet = new InternetAddress JavaDoc[iLst];
350                 } else {
351                   iPos = aBccAddrs.length;
352                   aBccSet = new InternetAddress JavaDoc[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 JavaDoc(aLst[l]);
357                 } else {
358                   for (int l=0; l<iLst; l++) aBccSet[l+iPos] = new InternetAddress JavaDoc(aLst[l],sPer);
359                 } // fi (sPer)
360
aBccAddrs = aBccSet;
361                 aRecipients[a] = null;
362               } // fi (sLst!="")
363
} // fi (aRecipients[a] LIKE list@%.list)
364
} // fi (sLId.length>4)
365
} // next (a)
366
aAdrSet = new InternetAddress JavaDoc[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         } // next
374
addRecipients(aAdrSet, oRecTp);
375       } // fi (nRecipients==0)
376
} // fi (aTo!=null)
377
if (DebugFile.trace) {
378       DebugFile.decIdent();
379       DebugFile.writeln("End RecipientsHelper.parseRecipientsList()");
380     }
381   } // parseRecipientsList
382

383   // ---------------------------------------------------------------------------
384

385   /**
386    * Delete rows at k_inet_addrs table for given message
387    * @param oConn JDCConnection
388    * @param sGuMimeMsg String GUID of message which addresses are to be cleared
389    * @throws SQLException
390    */

391   public static void clearRecipientsForMessage (JDCConnection oConn, String JavaDoc sGuMimeMsg) throws SQLException JavaDoc {
392     PreparedStatement JavaDoc 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   } // clearRecipientsForMessage
397

398   // ---------------------------------------------------------------------------
399

400   public static InternetAddress JavaDoc getFromAddress(MimeMessage JavaDoc oMsg) throws MessagingException JavaDoc {
401     Address JavaDoc[] aFrom = null;
402     try {
403       aFrom = oMsg.getFrom();
404     } catch (AddressException JavaDoc adre) {
405       if (DebugFile.trace) DebugFile.writeln("From AddressException " + adre.getMessage());
406     }
407     InternetAddress JavaDoc oFrom;
408     if (aFrom!=null) {
409       if (aFrom.length>0)
410         oFrom = (InternetAddress JavaDoc) aFrom[0];
411       else
412         oFrom = null;
413     }
414     else
415       oFrom = null;
416     return oFrom;
417   } // getFromAddress
418

419   // ---------------------------------------------------------------------------
420

421   public static InternetAddress JavaDoc getReplyAddress(MimeMessage JavaDoc oMsg) throws MessagingException JavaDoc {
422     Address JavaDoc[] aReply = null;
423     InternetAddress JavaDoc oReply;
424     try {
425       aReply = oMsg.getReplyTo();
426     } catch (AddressException JavaDoc 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 JavaDoc) 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