| 1 34 35 36 package com.micronova.util; 37 38 import java.util.*; 39 import java.io.*; 40 41 import javax.mail.*; 42 import javax.mail.internet.*; 43 import javax.activation.*; 44 45 import com.micronova.util.*; 46 47 48 49 public class MailFolder 50 { 51 53 public static final String HEADER = "header"; 54 public static final String HEADERMAP = "headerMap"; 55 public static final String CONTENT = "content"; 56 public static final String MAXCONTENTSIZE = "maxContentSize"; 57 public static final String MAXPARTSIZE = "maxPartSize"; 58 public static final String CONTENTASSTRING = "contentAsString"; 59 60 62 public static final String RECEIVEDDATE = "receivedDate"; 63 public static final String MESSAGENUMBER = "messageNumber"; 64 public static final String SUBJECT = "subject"; 65 public static final String EXCEPTION = "exception"; 66 public static final String SENTDATE = "sentDate"; 67 public static final String TO = "to"; 68 public static final String CC = "cc"; 69 public static final String BCC = "bcc"; 70 public static final String REPLYTO = "replyTo"; 71 public static final String FROM = "from"; 72 public static final String FLAGS = "flags"; 73 public static final String NAME = "name"; 74 public static final String VALUE = "value"; 75 public static final String FULLNAME = "fullName"; 76 public static final String URLNAME = "urlName"; 77 public static final String MESSAGECOUNT = "messageCount"; 78 public static final String ADDRESS = "address"; 79 public static final String PERSONAL = "personal"; 80 public static final String DESCRIPTION = "description"; 81 public static final String DISPOSITION = "disposition"; 82 public static final String FILENAME = "fileName"; 83 public static final String LINECOUNT = "lineCount"; 84 public static final String SIZE = "size"; 85 public static final String CONTENTTYPE = "contentType"; 86 public static final String TYPE = "type"; 87 public static final String PARTNAME = "partName"; 88 89 public static final String PART = "_part"; 90 public static final String FOLDER = "_folder"; 91 92 public static final int[] ALLMESSAGES = new int[0]; 93 94 95 96 private static void setAddress(NestedMap map, InternetAddress address) 97 { 98 map.put(ADDRESS, address.getAddress()); 99 map.put(PERSONAL, address.getPersonal()); 100 } 101 102 103 104 private static void setAddresses(NestedMap map, String name, Address[] addresses) throws Exception  105 { 106 if (addresses != null) 107 { 108 NestedMap addressListMap = new NestedMap(); 109 110 map.put(name, addressListMap); 111 112 List list = addressListMap.getSubList(); 113 114 for (int i = 0; i < addresses.length; i ++) 115 { 116 InternetAddress address = (InternetAddress)addresses[i]; 117 118 if (address != null) 119 { 120 list.add(address); 121 } 122 } 123 } 124 } 125 126 public static String makePartMap(NestedMap partMap, Part part, Map controlMap) throws Exception  127 { 128 String name = null; 129 130 partMap.put(PART, part); 131 132 partMap.put(DESCRIPTION, part.getDescription()); 133 partMap.put(FILENAME, part.getFileName()); 134 partMap.put(LINECOUNT, new Long (part.getLineCount())); 135 partMap.put(SIZE, new Long (part.getSize())); 136 partMap.put(CONTENTTYPE, part.getContentType()); 137 partMap.put(TYPE, NetUtil.parseMime(part.getContentType())); 138 139 String [] dispositions = part.getHeader("Content-Disposition"); 140 141 if (dispositions != null) 142 { 143 for (int i = 0; i < dispositions.length; i ++) 144 { 145 NestedMap dispositionMap = (NestedMap)NetUtil.parseMime(dispositions[i]); 146 147 partMap.put(DISPOSITION, dispositionMap); 148 149 name = (String )dispositionMap.get("@parameter.name"); 150 } 151 } 152 153 if (controlMap != null) 154 { 155 if (!TypeUtil.isFalse(controlMap.get(HEADER))) 156 { 157 NestedMap headerMap = new NestedMap(); 158 159 partMap.put(HEADER, headerMap); 160 161 boolean isHeaderMap = (!TypeUtil.isFalse(controlMap.get(HEADERMAP))); 162 163 List headerList = headerMap.getSubList(); 164 165 Enumeration enumHeaders = part.getAllHeaders(); 166 167 while (enumHeaders.hasMoreElements()) 168 { 169 Header header = (Header)enumHeaders.nextElement(); 170 171 NestedMap headerElementMap = new NestedMap(); 172 173 String headerName = header.getName(); 174 String headerValue = header.getValue(); 175 176 headerElementMap.put(NAME, headerName); 177 headerElementMap.put(VALUE, headerValue); 178 179 headerList.add(headerElementMap); 180 181 if (isHeaderMap) 182 { 183 if ((headerName != null) && (headerValue != null)) 184 { 185 headerMap.put(headerName, headerValue); 186 } 187 } 188 } 189 } 190 191 if (!TypeUtil.isFalse(controlMap.get(CONTENT))) 192 { 193 Integer iMaxSize = TypeUtil.isInteger(controlMap.get(MAXPARTSIZE)); 194 195 int maxSize = 0; 196 197 if (iMaxSize != null) 198 { 199 maxSize = iMaxSize.intValue(); 200 } 201 202 if ((maxSize <= 0) || (part.getSize() < maxSize)) 203 { 204 Object content = part.getContent(); 205 206 if (content instanceof Multipart) 207 { 208 Multipart multipart = (Multipart)content; 209 List partMapList = partMap.getSubList(); 210 211 for (int i = 0; i < multipart.getCount(); i ++) 212 { 213 NestedMap subPartMap = new NestedMap(); 214 215 String partName = makePartMap(subPartMap, multipart.getBodyPart(i), controlMap); 216 217 partMapList.add(subPartMap); 218 219 if (partName != null) 220 { 221 NestedMap partNameMap = partMap.getSubMap(PARTNAME); 222 partNameMap.put(partName, subPartMap); 223 } 224 } 225 } 226 else 227 { 228 int partSize = part.getSize(); 229 230 Integer iMaxContentSize = TypeUtil.isInteger(controlMap.get(MAXCONTENTSIZE)); 231 232 int maxContentSize = 0; 233 234 if (iMaxContentSize != null) 235 { 236 maxContentSize = iMaxContentSize.intValue(); 237 } 238 239 if ((maxContentSize <= 0) || (partSize < maxContentSize)) 240 { 241 if (partSize > 0) 242 { 243 if (TypeUtil.isTrue(controlMap.get(CONTENTASSTRING))) 244 { 245 content = TypeUtil.isString(content); 246 } 247 248 partMap.put(CONTENT, content); 249 } 250 } 251 } 252 } 253 } 254 } 255 256 return name; 257 } 258 259 260 261 public static Map readMessages(Folder folder, int[] messageNumbers, Map controlMap) throws Exception  262 { 263 Message[] messages = null; 264 265 if (messageNumbers == ALLMESSAGES) 266 { 267 messages = folder.getMessages(); 268 } 269 else if (messageNumbers != null) 270 { 271 messages = folder.getMessages(messageNumbers); 272 } 273 274 return makeFolderMap(folder, messages, controlMap); 275 } 276 277 278 279 private static Map makeMessageMap(Message message, Map controlMap) 280 { 281 NestedMap map = new NestedMap(); 282 283 int messageNumber = message.getMessageNumber(); 284 285 map.put(MESSAGENUMBER, new Long (messageNumber)); 286 287 try 288 { 289 if (controlMap != null) 290 { 291 String subject = message.getSubject(); 292 293 if (subject != null) 294 { 295 map.put(SUBJECT, subject); 296 } 297 298 Date sentDate = message.getSentDate(); 299 300 if (sentDate != null) 301 { 302 map.put(SENTDATE, sentDate); 303 } 304 305 Date receivedDate = message.getReceivedDate(); 306 307 if (receivedDate != null) 308 { 309 map.put(RECEIVEDDATE, receivedDate); 310 } 311 312 setAddresses(map, TO, message.getRecipients(Message.RecipientType.TO)); 313 setAddresses(map, CC, message.getRecipients(Message.RecipientType.CC)); 314 setAddresses(map, BCC, message.getRecipients(Message.RecipientType.BCC)); 315 316 setAddresses(map, REPLYTO, message.getReplyTo()); 317 318 setAddresses(map, FROM, message.getFrom()); 319 320 makePartMap(map, message, controlMap); 321 } 322 } 323 catch (Exception e) 324 { 325 map.put(EXCEPTION, e); 326 } 327 328 return map; 329 } 330 331 private static Map makeFolderMap(Folder folder, Message[] messages, Map controlMap) throws Exception  332 { 333 NestedMap map = new NestedMap(); 334 335 map.put(FOLDER, folder); 336 337 map.put(NAME, folder.getName()); 338 map.put(FULLNAME, folder.getFullName()); 339 map.put(URLNAME, folder.getURLName()); 340 map.put(MESSAGECOUNT, new Integer (folder.getMessageCount())); 341 342 if (messages != null) 343 { 344 List list = map.getSubList(); 345 346 for (int i = 0; i < messages.length; i ++) 347 { 348 Message message = messages[i]; 349 350 Map messageMap = makeMessageMap(message, controlMap); 351 352 list.add(messageMap); 353 } 354 } 355 356 return map; 357 } 358 359 360 361 public static void deleteMessages(Folder folder, int[] messageNumbers) throws Exception  362 { 363 if (messageNumbers != null) 364 { 365 Message[] messages = folder.getMessages(messageNumbers); 366 367 for (int i = messages.length; --i >= 0;) 368 { 369 Message message = messages[i]; 370 371 message.setFlag(Flags.Flag.DELETED, true); 372 } 373 374 376 try 377 { 378 folder.expunge(); 379 } 380 catch (MethodNotSupportedException e) 381 { 382 } 383 } 384 } 385 386 387 388 public static NestedMap decodeMultipart(InputStream inputStream, String header, NestedMap controlMap) throws Exception  389 { 390 StringBuffer buffer = new StringBuffer (); 391 392 buffer.append("Content-Type:" + header + "\r\n\r\n"); 393 394 ByteArrayInputStream headerInputStream = new ByteArrayInputStream(buffer.toString().getBytes("ISO-8859-1")); 395 396 SequenceInputStream sequenceInputStream = new SequenceInputStream(headerInputStream, inputStream); 397 398 MimeMessage mimeMessage = new MimeMessage(null, sequenceInputStream); 399 400 NestedMap map = new NestedMap(); 401 402 makePartMap(map, mimeMessage, new NestedMap(controlMap)); 403 404 return map; 405 } 406 } 407 | Popular Tags |