KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > email > EmailWorker


1 package org.ofbiz.content.email;
2
3 import java.util.List JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10
11 import javax.mail.internet.MimeMessage JavaDoc;
12 import javax.mail.internet.InternetAddress JavaDoc;
13 import javax.mail.MessagingException JavaDoc;
14 import javax.mail.Multipart JavaDoc;
15 import javax.mail.Part JavaDoc;
16
17 import org.ofbiz.entity.GenericDelegator;
18 import org.ofbiz.entity.GenericValue;
19 import org.ofbiz.entity.GenericEntityException;
20 import org.ofbiz.base.util.UtilMisc;
21 import org.ofbiz.base.util.UtilValidate;
22 import org.ofbiz.base.util.Base64;
23 import org.ofbiz.base.util.Debug;
24 import org.ofbiz.service.LocalDispatcher;
25 import org.ofbiz.service.GenericServiceException;
26 import org.ofbiz.entity.util.ByteWrapper;
27
28 public class EmailWorker {
29
30     public final static String JavaDoc module = EmailWorker.class.getName();
31
32     public String JavaDoc getForwardedField(MimeMessage JavaDoc message) {
33         
34         String JavaDoc fieldValue = null;
35         
36         return fieldValue;
37     }
38     
39     /**
40      * Takes a message and store all its attachments as Content associated with a CommunicationEvent
41      * using the attachment's file name as the name of the content
42      *
43      * @param message
44      * @param communicationEventId
45      * @param bodyContentIndex identifies the index value of the multi-part message which is the main body, so it is not created as an attachment
46      * @param dispatcher
47      * @param userLogin
48      * @return
49      * @throws MessagingException
50      * @throws IOException
51      * @throws GenericServiceException
52      */

53     public static int addAttachmentsToCommEvent(MimeMessage JavaDoc message, String JavaDoc communicationEventId, int bodyContentIndex, LocalDispatcher dispatcher, GenericValue userLogin)
54         throws MessagingException JavaDoc, IOException JavaDoc, GenericServiceException {
55         int attachmentCount =0;
56         Map JavaDoc commEventMap = new HashMap JavaDoc();
57         commEventMap.put("communicationEventId", communicationEventId);
58         commEventMap.put("contentTypeId", "DOCUMENT");
59         commEventMap.put("mimeTypeId", "text/html");
60         commEventMap.put("userLogin", userLogin);
61         String JavaDoc subject = message.getSubject();
62         Map JavaDoc result = null;
63         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
64         
65         Multipart JavaDoc multipart = (Multipart JavaDoc)message.getContent();
66         int multipartCount = multipart.getCount();
67         for (int i=0; i < multipartCount; i++) {
68             Part JavaDoc part = multipart.getBodyPart(i);
69             String JavaDoc thisContentTypeRaw = part.getContentType();
70             
71             int idx2 = thisContentTypeRaw.indexOf(";");
72             if (idx2 == -1) idx2 = thisContentTypeRaw.length();
73             String JavaDoc thisContentType = thisContentTypeRaw.substring(0, idx2);
74             
75             String JavaDoc disposition = part.getDisposition();
76             
77             // The first test should not pass, because if it exists, it should be the bodyContentIndex part
78
if (((disposition == null) && (i == 0) && (i != bodyContentIndex) && thisContentType.startsWith("text"))
79                || ((disposition != null)
80                      && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))
81                      && (i != bodyContentIndex)) )
82             {
83                 commEventMap.put("contentName", subject + "-" + i + " " + part.getFileName());
84                 commEventMap.put("drMimeTypeId", thisContentType);
85                 if (thisContentType.startsWith("text")) {
86                     
87                     String JavaDoc content = (String JavaDoc)part.getContent();
88                     
89                     commEventMap.put("drDataResourceTypeId", "ELECTRONIC_TEXT");
90                     commEventMap.put("textData", content);
91                 } else {
92                     
93                     InputStream JavaDoc is = part.getInputStream();
94                     int c;
95                     while ((c = is.read()) > -1) {
96                         baos.write(c);
97                     }
98                     
99                     ByteWrapper imageData = new ByteWrapper(baos.toByteArray());
100                     int len = imageData.getLength();
101                     if (Debug.infoOn()) Debug.logInfo("imageData length: " + len, module);
102                     commEventMap.put("drDataResourceName", part.getFileName());
103                     commEventMap.put("imageData", imageData);
104                     commEventMap.put("drDataResourceTypeId", "IMAGE_OBJECT");
105                     commEventMap.put("_imageData_contentType", thisContentType);
106                 }
107                 result = dispatcher.runSync("createCommContentDataResource", commEventMap);
108                 attachmentCount++;
109             }
110         }
111
112         return attachmentCount;
113     }
114 }
115
Popular Tags