KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.ByteArrayOutputStream JavaDoc;
37
38 import java.util.Date JavaDoc;
39
40 import java.sql.Timestamp JavaDoc;
41
42 import javax.mail.Address JavaDoc;
43 import javax.mail.MessagingException JavaDoc;
44 import javax.mail.internet.AddressException JavaDoc;
45 import javax.mail.internet.MimeMessage JavaDoc;
46 import javax.mail.internet.MimeUtility JavaDoc;
47
48 import com.knowgate.misc.Gadgets;
49 import com.knowgate.debug.DebugFile;
50 import com.knowgate.misc.MD5;
51 import javax.mail.Flags JavaDoc;
52
53 /**
54  * Set of utility functions for managing MimeMessage headers
55  * @author Sergio Montoro Ten
56  * @version 3.0
57  */

58
59 public class HeadersHelper {
60   private MimeMessage JavaDoc oMsg;
61   private final static String JavaDoc EmptyString = "";
62
63   public HeadersHelper(MimeMessage JavaDoc oMimeMsg) {
64     oMsg = oMimeMsg;
65   }
66
67   // ---------------------------------------------------------------------------
68

69   /**
70    * <p>Get decoded Message Id</p>
71    * This method first calls MimeMessage.getMessageID() If nothing is returned
72    * then it tries to retrieve header X-Qmail-Scanner-Message-ID or Resent-Message-ID.<br>
73    * If neither Message-Id nor X-Qmail-Scanner-Message-ID nor Resent-Message-ID headers are found
74    * then a message id is assigned by using the message sent date and from address like:<br>
75    * &lt;20050722140022.sender@domain.com&gt;
76    * The result is then decoded by calling MimeUtility.decodeText() before being returned
77    * @param oMimeMsg MimeMessage
78    * @return String Decoded value of Message Id.
79    * @throws MessagingException
80    * @throws UnsupportedEncodingException
81    */

82   public static String JavaDoc decodeMessageId (MimeMessage JavaDoc oMimeMsg)
83     throws MessagingException JavaDoc,UnsupportedEncodingException JavaDoc {
84     if (DebugFile.trace) {
85       DebugFile.writeln("Begin HeadersHelper.decodeMessageId([MimeMessage])");
86       DebugFile.incIdent();
87     }
88     String JavaDoc sRetId = oMimeMsg.getMessageID();
89     // If Message-Id header is not found try to use X-Qmail-Scanner-Message-ID
90
if (sRetId==null || EmptyString.equals(sRetId)) {
91       try { sRetId = oMimeMsg.getHeader("X-Qmail-Scanner-Message-ID", null); } catch (Exception JavaDoc ignore) {}
92     }
93     // If X-Qmail-Scanner-Message-ID header is not found then try to use Resent-Message-ID
94
if (sRetId==null || EmptyString.equals(sRetId)) {
95       try { sRetId = oMimeMsg.getHeader("Resent-Message-ID", null); } catch (Exception JavaDoc ignore) {}
96     }
97     // If no valid Message Id, is found then create a default one by using sent date and from address
98
if (sRetId==null) {
99       Date JavaDoc oDt = oMimeMsg.getSentDate();
100       Address JavaDoc[] aFrom = null;
101       try {
102         aFrom = oMimeMsg.getFrom();
103       } catch (AddressException JavaDoc bypass) { aFrom = null; }
104       if (oDt!=null && aFrom!=null) {
105         if (aFrom.length > 0)
106           if (aFrom[0]!=null)
107             sRetId = "<" + String.valueOf(oDt.getYear()) +
108                      String.valueOf(oDt.getMonth() + 1) + String.valueOf(oDt.getDate()) +
109                      String.valueOf(oDt.getHours()) + String.valueOf(oDt.getMinutes()) +
110                      String.valueOf(oDt.getSeconds()) + "." + aFrom[0].toString() + ">";
111       } // fi
112
} // fi (sRetId)
113
if (sRetId!=null) {
114       sRetId = MimeUtility.decodeText(sRetId);
115       // No quotes allowed on message identifiers
116
if (sRetId.indexOf('"')>=0) sRetId = Gadgets.removeChar(sRetId, '"');
117     }
118     if (DebugFile.trace) {
119       DebugFile.decIdent();
120       DebugFile.writeln("End HeadersHelper.decodeMessageId() : " + sRetId);
121     }
122     return sRetId;
123   } // decodeMessageId
124

125   // ---------------------------------------------------------------------------
126

127   /**
128    * <p>Get decoded Message Id</p>
129    * This method first calls MimeMessage.getMessageID() If nothing is returned
130    * then it tries to retrieve header X-Qmail-Scanner-Message-ID or Resent-Message-ID.<br>
131    * If neither Message-Id nor X-Qmail-Scanner-Message-ID nor Resent-Message-ID headers are found
132    * then a message id is assigned by using the message sent date and from address like:<br>
133    * &lt;20050722140022.sender@domain.com&gt;
134    * The result is then decoded by calling MimeUtility.decodeText() before being returned
135    * @return String Decoded value of Message Id.
136    * @throws MessagingException
137    * @throws UnsupportedEncodingException
138    */

139   public String JavaDoc decodeMessageId ()
140     throws MessagingException JavaDoc,UnsupportedEncodingException JavaDoc {
141     return HeadersHelper.decodeMessageId(oMsg);
142   }
143
144   // ---------------------------------------------------------------------------
145

146   /**
147    * <p>Get decoded Message Id</p>
148    * @param sDefault String Default value is a Message Id. cannot be found at message headers
149    * @return String
150    */

151   public String JavaDoc decodeMessageId (String JavaDoc sDefault) {
152     String JavaDoc sMessageID;
153     try {
154       sMessageID = decodeMessageId();
155     } catch (Exception JavaDoc xcpt) {
156       if (DebugFile.trace) DebugFile.writeln(xcpt.getClass().getName()+" "+xcpt.getMessage());
157       sMessageID = sDefault;
158     }
159     if (sMessageID==null) sMessageID = sDefault; else if (sMessageID.length()==0) sMessageID = sDefault;
160     return sMessageID;
161   } // decodeMessageId
162

163   // ---------------------------------------------------------------------------
164

165   public static String JavaDoc getContentType(MimeMessage JavaDoc oMsg)
166     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
167     String JavaDoc sRetVal = oMsg.getContentType();
168     if (sRetVal!=null)
169       sRetVal = MimeUtility.decodeText(sRetVal);
170     return sRetVal;
171   }
172
173   // ---------------------------------------------------------------------------
174

175   public String JavaDoc getContentType()
176     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
177     return HeadersHelper.getContentType(oMsg);
178   }
179
180   // ---------------------------------------------------------------------------
181

182   public static String JavaDoc getContentID(MimeMessage JavaDoc oMsg)
183     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
184     String JavaDoc sRetVal = oMsg.getContentID();
185     if (sRetVal!=null)
186       sRetVal = MimeUtility.decodeText(sRetVal);
187     return sRetVal;
188   }
189
190   // ---------------------------------------------------------------------------
191

192   public String JavaDoc getContentID()
193     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
194     return HeadersHelper.getContentID(oMsg);
195   }
196
197   // ---------------------------------------------------------------------------
198

199   public static String JavaDoc getDisposition(MimeMessage JavaDoc oMsg)
200     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
201     String JavaDoc sRetVal = oMsg.getDisposition();
202     if (sRetVal!=null)
203       sRetVal = MimeUtility.decodeText(sRetVal);
204     return sRetVal;
205   }
206
207   // ---------------------------------------------------------------------------
208

209   public String JavaDoc getDisposition()
210     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
211     return HeadersHelper.getDisposition(oMsg);
212   }
213
214   // ---------------------------------------------------------------------------
215

216   public static String JavaDoc getContentMD5(MimeMessage JavaDoc oMsg)
217     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
218     String JavaDoc sRetVal = oMsg.getContentMD5();
219     if (sRetVal!=null)
220       sRetVal = MimeUtility.decodeText(sRetVal);
221     return sRetVal;
222   }
223
224   // ---------------------------------------------------------------------------
225

226   public static String JavaDoc computeContentMD5(byte[] byArray) {
227     String JavaDoc sContentMD5;
228     MD5 oMd5 = new MD5();
229     oMd5.Init();
230     oMd5.Update(byArray);
231     sContentMD5 = Gadgets.toHexString(oMd5.Final());
232     oMd5 = null;
233     return sContentMD5;
234   }
235
236   // ---------------------------------------------------------------------------
237

238   public String JavaDoc getContentMD5()
239     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
240     return HeadersHelper.getContentMD5(oMsg);
241   }
242
243   // ---------------------------------------------------------------------------
244

245   public static String JavaDoc getDescription(MimeMessage JavaDoc oMsg)
246     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
247     String JavaDoc sRetVal = oMsg.getDescription();
248     if (sRetVal!=null)
249       sRetVal = MimeUtility.decodeText(sRetVal);
250     return sRetVal;
251   }
252
253   // ---------------------------------------------------------------------------
254

255   public String JavaDoc getDescription()
256     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
257     return HeadersHelper.getDescription(oMsg);
258   }
259
260   // ---------------------------------------------------------------------------
261

262   public static String JavaDoc getFileName(MimeMessage JavaDoc oMsg)
263     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
264     String JavaDoc sRetVal = oMsg.getFileName();
265     if (sRetVal!=null)
266       sRetVal = MimeUtility.decodeText(sRetVal);
267     return sRetVal;
268   }
269
270   // ---------------------------------------------------------------------------
271

272   public String JavaDoc getFileName()
273     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
274     return HeadersHelper.getFileName(oMsg);
275   }
276
277   // ---------------------------------------------------------------------------
278

279   public static String JavaDoc getEncoding(MimeMessage JavaDoc oMsg)
280     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
281     String JavaDoc sRetVal = oMsg.getEncoding();
282     if (sRetVal!=null)
283       sRetVal = MimeUtility.decodeText(sRetVal);
284     return sRetVal;
285   }
286
287   // ---------------------------------------------------------------------------
288

289   public String JavaDoc getEncoding()
290     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
291     return HeadersHelper.getEncoding(oMsg);
292   }
293
294   // ---------------------------------------------------------------------------
295

296   public static String JavaDoc getSubject(MimeMessage JavaDoc oMsg)
297     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
298     String JavaDoc sRetVal = oMsg.getSubject();
299     if (sRetVal!=null)
300       sRetVal = MimeUtility.decodeText(sRetVal);
301     return sRetVal;
302   }
303
304   // ---------------------------------------------------------------------------
305

306   public String JavaDoc getSubject()
307     throws UnsupportedEncodingException JavaDoc,MessagingException JavaDoc {
308     return HeadersHelper.getSubject(oMsg);
309   }
310
311   // ---------------------------------------------------------------------------
312

313   public static Timestamp JavaDoc getSentTimestamp(MimeMessage JavaDoc oMsg)
314     throws MessagingException JavaDoc {
315     Timestamp JavaDoc tsSent;
316     if (oMsg.getSentDate()!=null)
317       tsSent = new Timestamp JavaDoc(oMsg.getSentDate().getTime());
318     else
319       tsSent = null;
320     return tsSent;
321   }
322
323   // ---------------------------------------------------------------------------
324

325   public Timestamp JavaDoc getSentTimestamp()
326     throws MessagingException JavaDoc {
327     return HeadersHelper.getSentTimestamp(oMsg);
328   }
329
330   // ---------------------------------------------------------------------------
331

332   public static Timestamp JavaDoc getReceivedTimestamp(MimeMessage JavaDoc oMsg)
333     throws MessagingException JavaDoc {
334     Timestamp JavaDoc tsReceived;
335     if (oMsg.getReceivedDate()!=null)
336       tsReceived = new Timestamp JavaDoc(oMsg.getReceivedDate().getTime());
337     else
338       tsReceived = null;
339     return tsReceived;
340   }
341
342   // ---------------------------------------------------------------------------
343

344   public Timestamp JavaDoc getReceivedTimestamp()
345     throws MessagingException JavaDoc {
346     return HeadersHelper.getReceivedTimestamp(oMsg);
347   }
348
349   // ---------------------------------------------------------------------------
350

351   public static String JavaDoc getPriority(MimeMessage JavaDoc oMsg)
352     throws MessagingException JavaDoc {
353     String JavaDoc sPriority;
354     String JavaDoc sXPriority = oMsg.getHeader("X-Priority",null);
355     if (sXPriority==null) {
356       sPriority = null;
357     } else {
358       sPriority = "";
359       for (int x=0; x<sXPriority.length(); x++) {
360         char cAt = sXPriority.charAt(x);
361         if (cAt>=(char)48 && cAt<=(char)57) sPriority += cAt;
362       } // next
363
sPriority = Gadgets.left(sPriority, 10);
364     } // fi
365
return sPriority;
366   }
367
368   // ---------------------------------------------------------------------------
369

370   public String JavaDoc getPriority()
371     throws MessagingException JavaDoc {
372     return HeadersHelper.getPriority(oMsg);
373   }
374
375   // ---------------------------------------------------------------------------
376

377   public static Flags JavaDoc getFlags(MimeMessage JavaDoc oMsg)
378     throws MessagingException JavaDoc {
379     Flags JavaDoc oFlgs = oMsg.getFlags();
380     if (oFlgs==null) oFlgs = new Flags JavaDoc();
381     return oFlgs;
382   }
383
384   // ---------------------------------------------------------------------------
385

386   public Flags JavaDoc getFlags() throws MessagingException JavaDoc {
387     return HeadersHelper.getFlags(oMsg);
388   }
389
390   // ---------------------------------------------------------------------------
391

392   public static boolean isSpam(MimeMessage JavaDoc oMsg)
393     throws MessagingException JavaDoc {
394     boolean bIsSpam;
395     String JavaDoc sXSpam = oMsg.getHeader("X-Spam-Flag",null);
396     if (sXSpam!=null)
397       bIsSpam = (sXSpam.toUpperCase().indexOf("YES")>=0 || sXSpam.toUpperCase().indexOf("TRUE")>=0 || sXSpam.indexOf("1")>=0);
398     else
399       bIsSpam = false;
400     return bIsSpam;
401   }
402
403   // ---------------------------------------------------------------------------
404

405   public boolean isSpam()
406     throws MessagingException JavaDoc {
407     return HeadersHelper.isSpam(oMsg);
408   }
409 }
410
Popular Tags