From: [EMAIL PROTECTED]
Date: November 23, 2006 10:42:25 PM PST
To: [email protected]
Subject: svn commit: r478781 - in /incubator/ofbiz/trunk/
applications/content/src/org/ofbiz/content/email:
EmailServices.java EmailWorker.java
Reply-To: [email protected]
Author: hansbak
Date: Thu Nov 23 22:42:24 2006
New Revision: 478781
URL: http://svn.apache.org/viewvc?view=rev&rev=478781
Log:
system now accepts incoming email messages with multilevel multiparts
Modified:
incubator/ofbiz/trunk/applications/content/src/org/ofbiz/
content/email/EmailServices.java
incubator/ofbiz/trunk/applications/content/src/org/ofbiz/
content/email/EmailWorker.java
Modified: incubator/ofbiz/trunk/applications/content/src/org/ofbiz/
content/email/EmailServices.java
URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/
applications/content/src/org/ofbiz/content/email/EmailServices.java?
view=diff&rev=478781&r1=478780&r2=478781
======================================================================
========
--- incubator/ofbiz/trunk/applications/content/src/org/ofbiz/
content/email/EmailServices.java (original)
+++ incubator/ofbiz/trunk/applications/content/src/org/ofbiz/
content/email/EmailServices.java Thu Nov 23 22:42:24 2006
@@ -642,6 +642,53 @@
return allResults;
}
+
+ public static String contentIndex = "";
+ private static Map addMessageBody( Map commEventMap, Multipart
multipart)
+ throws MessagingException, IOException {
+ try {
+ int multipartCount = multipart.getCount();
+ for (int i=0; i < multipartCount && i < 10; i++) {
+ Part part = multipart.getBodyPart(i);
+ String thisContentTypeRaw = part.getContentType();
+ String content = null;
+ int idx2 = thisContentTypeRaw.indexOf(";");
+ if (idx2 == -1) idx2 = thisContentTypeRaw.length();
+ String thisContentType = thisContentTypeRaw.substring(0,
idx2);
+ if (thisContentType == null || thisContentType.equals(""))
thisContentType = "text/html";
+ String disposition = part.getDisposition();
+
+ if (thisContentType.startsWith("multipart") ||
thisContentType.startsWith("Multipart")) {
+ contentIndex = contentIndex.concat("."
+ i);
+ return addMessageBody(commEventMap, (Multipart)
part.getContent());
+ }
+ // See this case where the disposition of the inline text
is null
+ else if ((disposition == null) && (i == 0) &&
thisContentType.startsWith("text")) {
+ content = (String)part.getContent();
+ if (UtilValidate.isNotEmpty(content)) {
+ contentIndex = contentIndex.concat("."
+ i);
+ commEventMap.put("content", content);
+ commEventMap.put("contentMimeTypeId",
thisContentType);
+ return commEventMap;
+ }
+ } else if ((disposition != null)
+ && (disposition.equals(Part.ATTACHMENT) ||
disposition.equals(Part.INLINE))
+ && thisContentType.startsWith("text")) {
+ contentIndex = contentIndex.concat("."
+ i);
+ commEventMap.put("content", part.getContent());
+ commEventMap.put("contentMimeTypeId",
thisContentType);
+ return commEventMap;
+ }
+ }
+ return commEventMap;
+ } catch (MessagingException e) {
+ Debug.logError(e, module);
+ return ServiceUtil.returnError(e.getMessage());
+ } catch (IOException e) {
+ Debug.logError(e, module);
+ return ServiceUtil.returnError(e.getMessage());
+ }
+ }
/**
* This service is the main one for processing incoming emails.
@@ -665,7 +712,9 @@
* CommunicationEvent.content field) are cycled thru and
attached to the CommunicationEvent entity using the
* createCommContentDataResource service. This happens in the
EmailWorker.addAttachmentsToCommEvent method.
*
- * -Al Byers
+ * However multiparts can contain multiparts. A recursive
function has been added.
+ *
+ * -Al Byers - Hans Bakker
* @param dctx
* @param context
* @return
@@ -681,7 +730,6 @@
String partyIdTo = null;
String partyIdFrom = null;
String contentType = null;
- String content = null;
String communicationEventId = null;
String contactMechIdFrom = null;
String contactMechIdTo = null;
@@ -770,42 +818,13 @@
commEventMap.put("datetimeEnded", UtilDateTime.toTimestamp
(message.getReceivedDate()));
// get the content(type) part
- int contentIndex = -1;
- Multipart multipart = null;
if (contentType.startsWith("text")) {
- content = (String)message.getContent();
+ commEventMap.put("content", message.getContent());
commEventMap.put("contentMimeTypeId", contentType);
} else if (contentType.startsWith("multipart") ||
contentType.startsWith("Multipart")) {
- multipart = (Multipart) message.getContent();
- int multipartCount = multipart.getCount();
- for (int i=0; i < multipartCount; i++) {
- Part part = multipart.getBodyPart(i);
- String thisContentTypeRaw =
part.getContentType();
- int idx2 = thisContentTypeRaw.indexOf(";");
- if (idx2 == -1) idx2 =
thisContentTypeRaw.length();
- String thisContentType =
thisContentTypeRaw.substring(0, idx2);
- if (thisContentType == null ||
thisContentType.equals("")) thisContentType = "text/html";
- String disposition = part.getDisposition();
-
- // See this case where the disposition of the inline text
is null
- if ((disposition == null) && (i == 0) &&
thisContentType.startsWith("text")) {
- content = (String)part.getContent();
- if (UtilValidate.isNotEmpty(content)) {
- contentIndex = i;
-
commEventMap.put("contentMimeTypeId", thisContentType);
- break;
- }
- } else if ((disposition != null)
- && (disposition.equals(Part.ATTACHMENT) ||
disposition.equals(Part.INLINE))
- &&
thisContentType.startsWith("text")) {
- content = (String)part.getContent();
- contentIndex = i;
- commEventMap.put("contentMimeTypeId",
thisContentType);
- break;
- }
- }
- }
- commEventMap.put("content", content);
+ contentIndex = "";
+ commEventMap = addMessageBody(commEventMap, (Multipart)
message.getContent());
+ }
// store from/to parties, but when not found make a
note of the email to/from address in the workEffort Note Section.
String commNote = "";
@@ -844,10 +863,9 @@
result = dispatcher.runSync("createCommunicationEvent",
commEventMap);
communicationEventId = (String)result.get
("communicationEventId");
- // 'system' Id has no partyId but needed for prefix
generation of the creation of related contentId and dataresourceId.
// store attachements
if (contentType.startsWith("multipart") ||
contentType.startsWith("Multipart")) {
- int attachmentCount = EmailWorker.addAttachmentsToCommEvent
(message, communicationEventId, contentIndex, dispatcher, userLogin);
+ int attachmentCount = EmailWorker.addAttachmentsToCommEvent
(message, communicationEventId, dispatcher, userLogin);
if (Debug.infoOn()) Debug.logInfo(attachmentCount + "
attachments added to CommunicationEvent:" +
communicationEventId,module);
}
Modified: incubator/ofbiz/trunk/applications/content/src/org/ofbiz/
content/email/EmailWorker.java
URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/
applications/content/src/org/ofbiz/content/email/EmailWorker.java?
view=diff&rev=478781&r1=478780&r2=478781
======================================================================
========
--- incubator/ofbiz/trunk/applications/content/src/org/ofbiz/
content/email/EmailWorker.java (original)
+++ incubator/ofbiz/trunk/applications/content/src/org/ofbiz/
content/email/EmailWorker.java Thu Nov 23 22:42:24 2006
@@ -30,6 +30,7 @@
import org.ofbiz.base.util.Debug;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.GenericServiceException;
+import org.ofbiz.service.ServiceUtil;
import org.ofbiz.entity.util.ByteWrapper;
public class EmailWorker {
@@ -43,9 +44,8 @@
return fieldValue;
}
- public static int addAttachmentsToCommEvent(MimeMessage
message, String communicationEventId, int bodyContentIndex,
LocalDispatcher dispatcher, GenericValue userLogin)
+ public static int addAttachmentsToCommEvent(MimeMessage
message, String communicationEventId, LocalDispatcher dispatcher,
GenericValue userLogin)
throws MessagingException, IOException,
GenericServiceException {
- int attachmentCount =0;
Map commEventMap = new HashMap();
commEventMap.put("communicationEventId", communicationEventId);
commEventMap.put("contentTypeId", "DOCUMENT");
@@ -55,55 +55,76 @@
if (subject != null && subject.length() > 80) {
subject = subject.substring(0,80); // make sure not too big for
database field. (20 characters for filename)
}
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- Multipart multipart = (Multipart)message.getContent();
- int multipartCount = multipart.getCount();
- for (int i=0; i < multipartCount; i++) {
- Part part = multipart.getBodyPart(i);
- String thisContentTypeRaw = part.getContentType();
- int idx2 = thisContentTypeRaw.indexOf(";");
- if (idx2 == -1) idx2 = thisContentTypeRaw.length();
- String thisContentType = thisContentTypeRaw.substring
(0, idx2);
- String disposition = part.getDisposition();
-
- // The first test should not pass, because if it exists, it
should be the bodyContentIndex part
- if (((disposition == null) && (i == 0) && (i !=
bodyContentIndex) && thisContentType.startsWith("text"))
- || ((disposition != null)
- && (disposition.equals(Part.ATTACHMENT) || disposition.equals
(Part.INLINE))
- && (i != bodyContentIndex)) )
- {
- String attFileName = part.getFileName();
- if (attFileName != null && attFileName.length()
> 17) {
- attFileName =
attFileName.substring(0,17);
- }
- commEventMap.put("contentName", subject + "-" + i + " " +
attFileName);
- commEventMap.put("drMimeTypeId", thisContentType);
- if (thisContentType.startsWith("text")) {
- String content = (String)part.getContent();
- commEventMap.put("drDataResourceTypeId",
"ELECTRONIC_TEXT");
- commEventMap.put("textData", content);
- } else {
-
- InputStream is = part.getInputStream();
- int c;
- while ((c = is.read()) > -1) {
- baos.write(c);
- }
-
- ByteWrapper imageData = new
ByteWrapper(baos.toByteArray());
- int len = imageData.getLength();
- if (Debug.infoOn()) Debug.logInfo("imageData length: " +
len, module);
- commEventMap.put("drDataResourceName",
part.getFileName());
- commEventMap.put("imageData", imageData);
- commEventMap.put("drDataResourceTypeId",
"IMAGE_OBJECT");
- commEventMap.put("_imageData_contentType",
thisContentType);
- }
- dispatcher.runSync("createCommContentDataResource",
commEventMap);
- attachmentCount++;
- }
- }
+ currentIndex = "";
+ attachmentCount = 0;
+ return addMultipartAttachementToComm((Multipart)
message.getContent(), commEventMap, subject, dispatcher, userLogin);
+
+ }
+ private static String currentIndex = "";
+ private static int attachmentCount = 0;
+ private static int addMultipartAttachementToComm(Multipart
multipart, Map commEventMap, String subject, LocalDispatcher
dispatcher, GenericValue userLogin)
+ throws MessagingException, IOException, GenericServiceException {
+ try {
+ int multipartCount = multipart.getCount();
+ for (int i=0; i < multipartCount; i++) {
+ Part part = multipart.getBodyPart(i);
+ String thisContentTypeRaw = part.getContentType();
+ int idx2 = thisContentTypeRaw.indexOf(";");
+ if (idx2 == -1) idx2 = thisContentTypeRaw.length();
+ String thisContentType = thisContentTypeRaw.substring(0,
idx2);
+ String disposition = part.getDisposition();
+ ByteArrayOutputStream baos = new
ByteArrayOutputStream();
+
+ if (thisContentType.startsWith("multipart") ||
thisContentType.startsWith("Multipart")) {
+ currentIndex = currentIndex.concat("." + i);
+ return addMultipartAttachementToComm((Multipart)
part.getContent(), commEventMap, subject, dispatcher, userLogin);
+ }
+
+ if(currentIndex.concat("." + i).equals
(EmailServices.contentIndex)) continue;
+
+ // The first test should not pass, because if it exists, it
should be the bodyContentIndex part
+ if (((disposition == null) && (i == 0) &&
thisContentType.startsWith("text"))
+ || ((disposition != null)
+ && (disposition.equals(Part.ATTACHMENT) ||
disposition.equals(Part.INLINE))
+ ) )
+ {
+ String attFileName = part.getFileName();
+ if (attFileName != null && attFileName.length()
> 17) {
+ attFileName =
attFileName.substring(0,17);
+ }
+ commEventMap.put("contentName", subject + "-" +
attachmentCount + " " + attFileName);
+ commEventMap.put("drMimeTypeId",
thisContentType);
+ if (thisContentType.startsWith("text")) {
+ String content =
(String)part.getContent();
+ commEventMap.put("drDataResourceTypeId",
"ELECTRONIC_TEXT");
+ commEventMap.put("textData", content);
+ } else {
+
+ InputStream is = part.getInputStream();
+ int c;
+ while ((c = is.read()) > -1) {
+ baos.write(c);
+ }
+ ByteWrapper imageData = new
ByteWrapper(baos.toByteArray());
+ int len = imageData.getLength();
+ if (Debug.infoOn()) Debug.logInfo("imageData length: " +
len, module);
+ commEventMap.put("drDataResourceName",
part.getFileName());
+ commEventMap.put("imageData",
imageData);
+ commEventMap.put("drDataResourceTypeId",
"IMAGE_OBJECT");
+
commEventMap.put("_imageData_contentType", thisContentType);
+ }
+ dispatcher.runSync("createCommContentDataResource",
commEventMap);
+ attachmentCount++;
+ }
+ }
+ } catch (MessagingException e) {
+ Debug.logError(e, module);
+ } catch (IOException e) {
+ Debug.logError(e, module);
+ } catch (GenericServiceException e) {
+ Debug.logError(e, module);
+ }
return attachmentCount;
}
}