1 32 33 package com.knowgate.hipermail; 34 35 import java.io.UnsupportedEncodingException ; 36 import java.io.ByteArrayOutputStream ; 37 38 import java.util.Date ; 39 40 import java.sql.Timestamp ; 41 42 import javax.mail.Address ; 43 import javax.mail.MessagingException ; 44 import javax.mail.internet.AddressException ; 45 import javax.mail.internet.MimeMessage ; 46 import javax.mail.internet.MimeUtility ; 47 48 import com.knowgate.misc.Gadgets; 49 import com.knowgate.debug.DebugFile; 50 import com.knowgate.misc.MD5; 51 import javax.mail.Flags ; 52 53 58 59 public class HeadersHelper { 60 private MimeMessage oMsg; 61 private final static String EmptyString = ""; 62 63 public HeadersHelper(MimeMessage oMimeMsg) { 64 oMsg = oMimeMsg; 65 } 66 67 69 82 public static String decodeMessageId (MimeMessage oMimeMsg) 83 throws MessagingException ,UnsupportedEncodingException { 84 if (DebugFile.trace) { 85 DebugFile.writeln("Begin HeadersHelper.decodeMessageId([MimeMessage])"); 86 DebugFile.incIdent(); 87 } 88 String sRetId = oMimeMsg.getMessageID(); 89 if (sRetId==null || EmptyString.equals(sRetId)) { 91 try { sRetId = oMimeMsg.getHeader("X-Qmail-Scanner-Message-ID", null); } catch (Exception ignore) {} 92 } 93 if (sRetId==null || EmptyString.equals(sRetId)) { 95 try { sRetId = oMimeMsg.getHeader("Resent-Message-ID", null); } catch (Exception ignore) {} 96 } 97 if (sRetId==null) { 99 Date oDt = oMimeMsg.getSentDate(); 100 Address [] aFrom = null; 101 try { 102 aFrom = oMimeMsg.getFrom(); 103 } catch (AddressException 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 } } if (sRetId!=null) { 114 sRetId = MimeUtility.decodeText(sRetId); 115 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 } 125 127 139 public String decodeMessageId () 140 throws MessagingException ,UnsupportedEncodingException { 141 return HeadersHelper.decodeMessageId(oMsg); 142 } 143 144 146 151 public String decodeMessageId (String sDefault) { 152 String sMessageID; 153 try { 154 sMessageID = decodeMessageId(); 155 } catch (Exception 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 } 163 165 public static String getContentType(MimeMessage oMsg) 166 throws UnsupportedEncodingException ,MessagingException { 167 String sRetVal = oMsg.getContentType(); 168 if (sRetVal!=null) 169 sRetVal = MimeUtility.decodeText(sRetVal); 170 return sRetVal; 171 } 172 173 175 public String getContentType() 176 throws UnsupportedEncodingException ,MessagingException { 177 return HeadersHelper.getContentType(oMsg); 178 } 179 180 182 public static String getContentID(MimeMessage oMsg) 183 throws UnsupportedEncodingException ,MessagingException { 184 String sRetVal = oMsg.getContentID(); 185 if (sRetVal!=null) 186 sRetVal = MimeUtility.decodeText(sRetVal); 187 return sRetVal; 188 } 189 190 192 public String getContentID() 193 throws UnsupportedEncodingException ,MessagingException { 194 return HeadersHelper.getContentID(oMsg); 195 } 196 197 199 public static String getDisposition(MimeMessage oMsg) 200 throws UnsupportedEncodingException ,MessagingException { 201 String sRetVal = oMsg.getDisposition(); 202 if (sRetVal!=null) 203 sRetVal = MimeUtility.decodeText(sRetVal); 204 return sRetVal; 205 } 206 207 209 public String getDisposition() 210 throws UnsupportedEncodingException ,MessagingException { 211 return HeadersHelper.getDisposition(oMsg); 212 } 213 214 216 public static String getContentMD5(MimeMessage oMsg) 217 throws UnsupportedEncodingException ,MessagingException { 218 String sRetVal = oMsg.getContentMD5(); 219 if (sRetVal!=null) 220 sRetVal = MimeUtility.decodeText(sRetVal); 221 return sRetVal; 222 } 223 224 226 public static String computeContentMD5(byte[] byArray) { 227 String 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 238 public String getContentMD5() 239 throws UnsupportedEncodingException ,MessagingException { 240 return HeadersHelper.getContentMD5(oMsg); 241 } 242 243 245 public static String getDescription(MimeMessage oMsg) 246 throws UnsupportedEncodingException ,MessagingException { 247 String sRetVal = oMsg.getDescription(); 248 if (sRetVal!=null) 249 sRetVal = MimeUtility.decodeText(sRetVal); 250 return sRetVal; 251 } 252 253 255 public String getDescription() 256 throws UnsupportedEncodingException ,MessagingException { 257 return HeadersHelper.getDescription(oMsg); 258 } 259 260 262 public static String getFileName(MimeMessage oMsg) 263 throws UnsupportedEncodingException ,MessagingException { 264 String sRetVal = oMsg.getFileName(); 265 if (sRetVal!=null) 266 sRetVal = MimeUtility.decodeText(sRetVal); 267 return sRetVal; 268 } 269 270 272 public String getFileName() 273 throws UnsupportedEncodingException ,MessagingException { 274 return HeadersHelper.getFileName(oMsg); 275 } 276 277 279 public static String getEncoding(MimeMessage oMsg) 280 throws UnsupportedEncodingException ,MessagingException { 281 String sRetVal = oMsg.getEncoding(); 282 if (sRetVal!=null) 283 sRetVal = MimeUtility.decodeText(sRetVal); 284 return sRetVal; 285 } 286 287 289 public String getEncoding() 290 throws UnsupportedEncodingException ,MessagingException { 291 return HeadersHelper.getEncoding(oMsg); 292 } 293 294 296 public static String getSubject(MimeMessage oMsg) 297 throws UnsupportedEncodingException ,MessagingException { 298 String sRetVal = oMsg.getSubject(); 299 if (sRetVal!=null) 300 sRetVal = MimeUtility.decodeText(sRetVal); 301 return sRetVal; 302 } 303 304 306 public String getSubject() 307 throws UnsupportedEncodingException ,MessagingException { 308 return HeadersHelper.getSubject(oMsg); 309 } 310 311 313 public static Timestamp getSentTimestamp(MimeMessage oMsg) 314 throws MessagingException { 315 Timestamp tsSent; 316 if (oMsg.getSentDate()!=null) 317 tsSent = new Timestamp (oMsg.getSentDate().getTime()); 318 else 319 tsSent = null; 320 return tsSent; 321 } 322 323 325 public Timestamp getSentTimestamp() 326 throws MessagingException { 327 return HeadersHelper.getSentTimestamp(oMsg); 328 } 329 330 332 public static Timestamp getReceivedTimestamp(MimeMessage oMsg) 333 throws MessagingException { 334 Timestamp tsReceived; 335 if (oMsg.getReceivedDate()!=null) 336 tsReceived = new Timestamp (oMsg.getReceivedDate().getTime()); 337 else 338 tsReceived = null; 339 return tsReceived; 340 } 341 342 344 public Timestamp getReceivedTimestamp() 345 throws MessagingException { 346 return HeadersHelper.getReceivedTimestamp(oMsg); 347 } 348 349 351 public static String getPriority(MimeMessage oMsg) 352 throws MessagingException { 353 String sPriority; 354 String 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 } sPriority = Gadgets.left(sPriority, 10); 364 } return sPriority; 366 } 367 368 370 public String getPriority() 371 throws MessagingException { 372 return HeadersHelper.getPriority(oMsg); 373 } 374 375 377 public static Flags getFlags(MimeMessage oMsg) 378 throws MessagingException { 379 Flags oFlgs = oMsg.getFlags(); 380 if (oFlgs==null) oFlgs = new Flags (); 381 return oFlgs; 382 } 383 384 386 public Flags getFlags() throws MessagingException { 387 return HeadersHelper.getFlags(oMsg); 388 } 389 390 392 public static boolean isSpam(MimeMessage oMsg) 393 throws MessagingException { 394 boolean bIsSpam; 395 String 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 405 public boolean isSpam() 406 throws MessagingException { 407 return HeadersHelper.isSpam(oMsg); 408 } 409 } 410 | Popular Tags |