001: /*
002: Copyright (C) 2005 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.hipermail;
034:
035: import java.io.UnsupportedEncodingException;
036: import java.io.ByteArrayOutputStream;
037:
038: import java.util.Date;
039:
040: import java.sql.Timestamp;
041:
042: import javax.mail.Address;
043: import javax.mail.MessagingException;
044: import javax.mail.internet.AddressException;
045: import javax.mail.internet.MimeMessage;
046: import javax.mail.internet.MimeUtility;
047:
048: import com.knowgate.misc.Gadgets;
049: import com.knowgate.debug.DebugFile;
050: import com.knowgate.misc.MD5;
051: import javax.mail.Flags;
052:
053: /**
054: * Set of utility functions for managing MimeMessage headers
055: * @author Sergio Montoro Ten
056: * @version 3.0
057: */
058:
059: public class HeadersHelper {
060: private MimeMessage oMsg;
061: private final static String EmptyString = "";
062:
063: public HeadersHelper(MimeMessage oMimeMsg) {
064: oMsg = oMimeMsg;
065: }
066:
067: // ---------------------------------------------------------------------------
068:
069: /**
070: * <p>Get decoded Message Id</p>
071: * This method first calls MimeMessage.getMessageID() If nothing is returned
072: * then it tries to retrieve header X-Qmail-Scanner-Message-ID or Resent-Message-ID.<br>
073: * If neither Message-Id nor X-Qmail-Scanner-Message-ID nor Resent-Message-ID headers are found
074: * then a message id is assigned by using the message sent date and from address like:<br>
075: * <20050722140022.sender@domain.com>
076: * The result is then decoded by calling MimeUtility.decodeText() before being returned
077: * @param oMimeMsg MimeMessage
078: * @return String Decoded value of Message Id.
079: * @throws MessagingException
080: * @throws UnsupportedEncodingException
081: */
082: public static String decodeMessageId(MimeMessage oMimeMsg)
083: throws MessagingException, UnsupportedEncodingException {
084: if (DebugFile.trace) {
085: DebugFile
086: .writeln("Begin HeadersHelper.decodeMessageId([MimeMessage])");
087: DebugFile.incIdent();
088: }
089: String sRetId = oMimeMsg.getMessageID();
090: // If Message-Id header is not found try to use X-Qmail-Scanner-Message-ID
091: if (sRetId == null || EmptyString.equals(sRetId)) {
092: try {
093: sRetId = oMimeMsg.getHeader(
094: "X-Qmail-Scanner-Message-ID", null);
095: } catch (Exception ignore) {
096: }
097: }
098: // If X-Qmail-Scanner-Message-ID header is not found then try to use Resent-Message-ID
099: if (sRetId == null || EmptyString.equals(sRetId)) {
100: try {
101: sRetId = oMimeMsg.getHeader("Resent-Message-ID", null);
102: } catch (Exception ignore) {
103: }
104: }
105: // If no valid Message Id, is found then create a default one by using sent date and from address
106: if (sRetId == null) {
107: Date oDt = oMimeMsg.getSentDate();
108: Address[] aFrom = null;
109: try {
110: aFrom = oMimeMsg.getFrom();
111: } catch (AddressException bypass) {
112: aFrom = null;
113: }
114: if (oDt != null && aFrom != null) {
115: if (aFrom.length > 0)
116: if (aFrom[0] != null)
117: sRetId = "<" + String.valueOf(oDt.getYear())
118: + String.valueOf(oDt.getMonth() + 1)
119: + String.valueOf(oDt.getDate())
120: + String.valueOf(oDt.getHours())
121: + String.valueOf(oDt.getMinutes())
122: + String.valueOf(oDt.getSeconds())
123: + "." + aFrom[0].toString() + ">";
124: } // fi
125: } // fi (sRetId)
126: if (sRetId != null) {
127: sRetId = MimeUtility.decodeText(sRetId);
128: // No quotes allowed on message identifiers
129: if (sRetId.indexOf('"') >= 0)
130: sRetId = Gadgets.removeChar(sRetId, '"');
131: }
132: if (DebugFile.trace) {
133: DebugFile.decIdent();
134: DebugFile.writeln("End HeadersHelper.decodeMessageId() : "
135: + sRetId);
136: }
137: return sRetId;
138: } // decodeMessageId
139:
140: // ---------------------------------------------------------------------------
141:
142: /**
143: * <p>Get decoded Message Id</p>
144: * This method first calls MimeMessage.getMessageID() If nothing is returned
145: * then it tries to retrieve header X-Qmail-Scanner-Message-ID or Resent-Message-ID.<br>
146: * If neither Message-Id nor X-Qmail-Scanner-Message-ID nor Resent-Message-ID headers are found
147: * then a message id is assigned by using the message sent date and from address like:<br>
148: * <20050722140022.sender@domain.com>
149: * The result is then decoded by calling MimeUtility.decodeText() before being returned
150: * @return String Decoded value of Message Id.
151: * @throws MessagingException
152: * @throws UnsupportedEncodingException
153: */
154: public String decodeMessageId() throws MessagingException,
155: UnsupportedEncodingException {
156: return HeadersHelper.decodeMessageId(oMsg);
157: }
158:
159: // ---------------------------------------------------------------------------
160:
161: /**
162: * <p>Get decoded Message Id</p>
163: * @param sDefault String Default value is a Message Id. cannot be found at message headers
164: * @return String
165: */
166: public String decodeMessageId(String sDefault) {
167: String sMessageID;
168: try {
169: sMessageID = decodeMessageId();
170: } catch (Exception xcpt) {
171: if (DebugFile.trace)
172: DebugFile.writeln(xcpt.getClass().getName() + " "
173: + xcpt.getMessage());
174: sMessageID = sDefault;
175: }
176: if (sMessageID == null)
177: sMessageID = sDefault;
178: else if (sMessageID.length() == 0)
179: sMessageID = sDefault;
180: return sMessageID;
181: } // decodeMessageId
182:
183: // ---------------------------------------------------------------------------
184:
185: public static String getContentType(MimeMessage oMsg)
186: throws UnsupportedEncodingException, MessagingException {
187: String sRetVal = oMsg.getContentType();
188: if (sRetVal != null)
189: sRetVal = MimeUtility.decodeText(sRetVal);
190: return sRetVal;
191: }
192:
193: // ---------------------------------------------------------------------------
194:
195: public String getContentType() throws UnsupportedEncodingException,
196: MessagingException {
197: return HeadersHelper.getContentType(oMsg);
198: }
199:
200: // ---------------------------------------------------------------------------
201:
202: public static String getContentID(MimeMessage oMsg)
203: throws UnsupportedEncodingException, MessagingException {
204: String sRetVal = oMsg.getContentID();
205: if (sRetVal != null)
206: sRetVal = MimeUtility.decodeText(sRetVal);
207: return sRetVal;
208: }
209:
210: // ---------------------------------------------------------------------------
211:
212: public String getContentID() throws UnsupportedEncodingException,
213: MessagingException {
214: return HeadersHelper.getContentID(oMsg);
215: }
216:
217: // ---------------------------------------------------------------------------
218:
219: public static String getDisposition(MimeMessage oMsg)
220: throws UnsupportedEncodingException, MessagingException {
221: String sRetVal = oMsg.getDisposition();
222: if (sRetVal != null)
223: sRetVal = MimeUtility.decodeText(sRetVal);
224: return sRetVal;
225: }
226:
227: // ---------------------------------------------------------------------------
228:
229: public String getDisposition() throws UnsupportedEncodingException,
230: MessagingException {
231: return HeadersHelper.getDisposition(oMsg);
232: }
233:
234: // ---------------------------------------------------------------------------
235:
236: public static String getContentMD5(MimeMessage oMsg)
237: throws UnsupportedEncodingException, MessagingException {
238: String sRetVal = oMsg.getContentMD5();
239: if (sRetVal != null)
240: sRetVal = MimeUtility.decodeText(sRetVal);
241: return sRetVal;
242: }
243:
244: // ---------------------------------------------------------------------------
245:
246: public static String computeContentMD5(byte[] byArray) {
247: String sContentMD5;
248: MD5 oMd5 = new MD5();
249: oMd5.Init();
250: oMd5.Update(byArray);
251: sContentMD5 = Gadgets.toHexString(oMd5.Final());
252: oMd5 = null;
253: return sContentMD5;
254: }
255:
256: // ---------------------------------------------------------------------------
257:
258: public String getContentMD5() throws UnsupportedEncodingException,
259: MessagingException {
260: return HeadersHelper.getContentMD5(oMsg);
261: }
262:
263: // ---------------------------------------------------------------------------
264:
265: public static String getDescription(MimeMessage oMsg)
266: throws UnsupportedEncodingException, MessagingException {
267: String sRetVal = oMsg.getDescription();
268: if (sRetVal != null)
269: sRetVal = MimeUtility.decodeText(sRetVal);
270: return sRetVal;
271: }
272:
273: // ---------------------------------------------------------------------------
274:
275: public String getDescription() throws UnsupportedEncodingException,
276: MessagingException {
277: return HeadersHelper.getDescription(oMsg);
278: }
279:
280: // ---------------------------------------------------------------------------
281:
282: public static String getFileName(MimeMessage oMsg)
283: throws UnsupportedEncodingException, MessagingException {
284: String sRetVal = oMsg.getFileName();
285: if (sRetVal != null)
286: sRetVal = MimeUtility.decodeText(sRetVal);
287: return sRetVal;
288: }
289:
290: // ---------------------------------------------------------------------------
291:
292: public String getFileName() throws UnsupportedEncodingException,
293: MessagingException {
294: return HeadersHelper.getFileName(oMsg);
295: }
296:
297: // ---------------------------------------------------------------------------
298:
299: public static String getEncoding(MimeMessage oMsg)
300: throws UnsupportedEncodingException, MessagingException {
301: String sRetVal = oMsg.getEncoding();
302: if (sRetVal != null)
303: sRetVal = MimeUtility.decodeText(sRetVal);
304: return sRetVal;
305: }
306:
307: // ---------------------------------------------------------------------------
308:
309: public String getEncoding() throws UnsupportedEncodingException,
310: MessagingException {
311: return HeadersHelper.getEncoding(oMsg);
312: }
313:
314: // ---------------------------------------------------------------------------
315:
316: public static String getSubject(MimeMessage oMsg)
317: throws UnsupportedEncodingException, MessagingException {
318: String sRetVal = oMsg.getSubject();
319: if (sRetVal != null)
320: sRetVal = MimeUtility.decodeText(sRetVal);
321: return sRetVal;
322: }
323:
324: // ---------------------------------------------------------------------------
325:
326: public String getSubject() throws UnsupportedEncodingException,
327: MessagingException {
328: return HeadersHelper.getSubject(oMsg);
329: }
330:
331: // ---------------------------------------------------------------------------
332:
333: public static Timestamp getSentTimestamp(MimeMessage oMsg)
334: throws MessagingException {
335: Timestamp tsSent;
336: if (oMsg.getSentDate() != null)
337: tsSent = new Timestamp(oMsg.getSentDate().getTime());
338: else
339: tsSent = null;
340: return tsSent;
341: }
342:
343: // ---------------------------------------------------------------------------
344:
345: public Timestamp getSentTimestamp() throws MessagingException {
346: return HeadersHelper.getSentTimestamp(oMsg);
347: }
348:
349: // ---------------------------------------------------------------------------
350:
351: public static Timestamp getReceivedTimestamp(MimeMessage oMsg)
352: throws MessagingException {
353: Timestamp tsReceived;
354: if (oMsg.getReceivedDate() != null)
355: tsReceived = new Timestamp(oMsg.getReceivedDate().getTime());
356: else
357: tsReceived = null;
358: return tsReceived;
359: }
360:
361: // ---------------------------------------------------------------------------
362:
363: public Timestamp getReceivedTimestamp() throws MessagingException {
364: return HeadersHelper.getReceivedTimestamp(oMsg);
365: }
366:
367: // ---------------------------------------------------------------------------
368:
369: public static String getPriority(MimeMessage oMsg)
370: throws MessagingException {
371: String sPriority;
372: String sXPriority = oMsg.getHeader("X-Priority", null);
373: if (sXPriority == null) {
374: sPriority = null;
375: } else {
376: sPriority = "";
377: for (int x = 0; x < sXPriority.length(); x++) {
378: char cAt = sXPriority.charAt(x);
379: if (cAt >= (char) 48 && cAt <= (char) 57)
380: sPriority += cAt;
381: } // next
382: sPriority = Gadgets.left(sPriority, 10);
383: } // fi
384: return sPriority;
385: }
386:
387: // ---------------------------------------------------------------------------
388:
389: public String getPriority() throws MessagingException {
390: return HeadersHelper.getPriority(oMsg);
391: }
392:
393: // ---------------------------------------------------------------------------
394:
395: public static Flags getFlags(MimeMessage oMsg)
396: throws MessagingException {
397: Flags oFlgs = oMsg.getFlags();
398: if (oFlgs == null)
399: oFlgs = new Flags();
400: return oFlgs;
401: }
402:
403: // ---------------------------------------------------------------------------
404:
405: public Flags getFlags() throws MessagingException {
406: return HeadersHelper.getFlags(oMsg);
407: }
408:
409: // ---------------------------------------------------------------------------
410:
411: public static boolean isSpam(MimeMessage oMsg)
412: throws MessagingException {
413: boolean bIsSpam;
414: String sXSpam = oMsg.getHeader("X-Spam-Flag", null);
415: if (sXSpam != null)
416: bIsSpam = (sXSpam.toUpperCase().indexOf("YES") >= 0
417: || sXSpam.toUpperCase().indexOf("TRUE") >= 0 || sXSpam
418: .indexOf("1") >= 0);
419: else
420: bIsSpam = false;
421: return bIsSpam;
422: }
423:
424: // ---------------------------------------------------------------------------
425:
426: public boolean isSpam() throws MessagingException {
427: return HeadersHelper.isSpam(oMsg);
428: }
429: }
|