* @return
*/
public static Map<String, Object> storeIncomingEmail(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
MimeMessageWrapper wrapper = (MimeMessageWrapper) context.get("messageWrapper");
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
String partyIdTo = null;
String partyIdFrom = null;
String contentType = null;
String communicationEventId = null;
String contactMechIdFrom = null;
String contactMechIdTo = null;
Map<String, Object> result = null;
try {
String contentTypeRaw = wrapper.getContentType();
int idx = contentTypeRaw.indexOf(";");
if (idx == -1) idx = contentTypeRaw.length();
contentType = contentTypeRaw.substring(0, idx);
if (contentType == null || contentType.equals("")) contentType = "text/html";
contentType = contentType.toLowerCase();
Address[] addressesFrom = wrapper.getFrom();
Address[] addressesTo = wrapper.getTo();
Address[] addressesCC = wrapper.getCc();
Address[] addressesBCC = wrapper.getBcc();
String messageId = wrapper.getMessageId().replaceAll("[<>]", "");
String aboutThisEmail = "message [" + messageId + "] from [" +
((addressesFrom == null || addressesFrom[0] == null) ? "not found" : addressesFrom[0].toString()) + "] to [" +
((addressesTo == null || addressesTo[0] == null) ? "not found" : addressesTo[0].toString()) + "]";
if (Debug.verboseOn()) Debug.logVerbose("Processing Incoming Email " + aboutThisEmail, module);
// ignore the message when the spam status = yes
String spamHeaderName = UtilProperties.getPropertyValue("general.properties", "mail.spam.name", "N");
String configHeaderValue = UtilProperties.getPropertyValue("general.properties", "mail.spam.value");
// only execute when config file has been set && header variable found
if (!spamHeaderName.equals("N") && wrapper.getHeader(spamHeaderName) != null && wrapper.getHeader(spamHeaderName).length > 0) {
String msgHeaderValue = wrapper.getHeader(spamHeaderName)[0];
if (msgHeaderValue != null && msgHeaderValue.startsWith(configHeaderValue)) {
Debug.logInfo("Incoming Email message ignored, was detected by external spam checker", module);
return ServiceUtil.returnSuccess(UtilProperties.getMessage(resource,
"PartyCommEventMessageIgnoredDetectedByExternalSpamChecker", locale));
}
}
// if no 'from' addresses specified ignore the message
if (addressesFrom == null) {
Debug.logInfo("Incoming Email message ignored, had not 'from' email address", module);
return ServiceUtil.returnSuccess(UtilProperties.getMessage(resource,
"PartyCommEventMessageIgnoredNoFromAddressSpecified", locale));
}
// make sure this isn't a duplicate
List<GenericValue> commEvents;
try {
commEvents = delegator.findByAnd("CommunicationEvent", UtilMisc.toMap("messageId", messageId));
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (!commEvents.isEmpty()) {
Debug.logInfo("Ignoring Duplicate Email: " + aboutThisEmail, module);
return ServiceUtil.returnSuccess(UtilProperties.getMessage(resource,
"PartyCommEventMessageIgnoredDuplicateMessageId", locale));
}
// get the related partId's
List<Map<String, Object>> toParties = buildListOfPartyInfoFromEmailAddresses(addressesTo, userLogin, dispatcher);
List<Map<String, Object>> ccParties = buildListOfPartyInfoFromEmailAddresses(addressesCC, userLogin, dispatcher);
List<Map<String, Object>> bccParties = buildListOfPartyInfoFromEmailAddresses(addressesBCC, userLogin, dispatcher);
//Get the first address from the list - this is the partyIdTo field of the CommunicationEvent
if (!toParties.isEmpty()) {
Iterator<Map<String, Object>> itr = toParties.iterator();
Map<String, Object> firstAddressTo = itr.next();
partyIdTo = (String)firstAddressTo.get("partyId");
contactMechIdTo = (String)firstAddressTo.get("contactMechId");
}
String deliveredTo = wrapper.getFirstHeader("Delivered-To");
if (deliveredTo != null) {
// check if started with the domain name if yes remove including the dash.
String dn = deliveredTo.substring(deliveredTo.indexOf("@")+1, deliveredTo.length());
if (deliveredTo.startsWith(dn)) {
deliveredTo = deliveredTo.substring(dn.length()+1, deliveredTo.length());
}
}
// if partyIdTo not found try to find the "to" address using the delivered-to header
if ((partyIdTo == null) && (deliveredTo != null)) {
result = dispatcher.runSync("findPartyFromEmailAddress", UtilMisc.<String, Object>toMap("address", deliveredTo, "userLogin", userLogin));
partyIdTo = (String)result.get("partyId");
contactMechIdTo = (String)result.get("contactMechId");
}
if (userLogin.get("partyId") == null && partyIdTo != null) {
int ch = 0;
for (ch=partyIdTo.length(); ch > 0 && Character.isDigit(partyIdTo.charAt(ch-1)); ch--) {}
userLogin.put("partyId", partyIdTo.substring(0,ch)); //allow services to be called to have prefix
}
// get the 'from' partyId
result = getParyInfoFromEmailAddress(addressesFrom, userLogin, dispatcher);
partyIdFrom = (String)result.get("partyId");
contactMechIdFrom = (String)result.get("contactMechId");
Map<String, Object> commEventMap = FastMap.newInstance();
commEventMap.put("communicationEventTypeId", "AUTO_EMAIL_COMM");
commEventMap.put("contactMechTypeId", "EMAIL_ADDRESS");
commEventMap.put("messageId", messageId);
String subject = wrapper.getSubject();
commEventMap.put("subject", subject);
// Set sent and received dates
commEventMap.put("entryDate", nowTimestamp);
commEventMap.put("datetimeStarted", UtilDateTime.toTimestamp(wrapper.getSentDate()));
commEventMap.put("datetimeEnded", UtilDateTime.toTimestamp(wrapper.getReceivedDate()));
// default role types (_NA_)
commEventMap.put("roleTypeIdFrom", "_NA_");
commEventMap.put("roleTypeIdTo", "_NA_");
// get the content(type) part
String messageBodyContentType = wrapper.getMessageBodyContentType();
if (messageBodyContentType.indexOf(";") > -1) {
messageBodyContentType = messageBodyContentType.substring(0, messageBodyContentType.indexOf(";"));
}
// select the plain text bodypart
String messageBody = null;
if (wrapper.getMainPartCount() > 1) {
for (int ind=0; ind < wrapper.getMainPartCount(); ind++) {
BodyPart p = wrapper.getPart(ind + "");
if (p.getContentType().toLowerCase().indexOf("text/plain") > -1) {
messageBody = (String) p.getContent();
break;
}
}
}
if (messageBody == null) {
messageBody = wrapper.getMessageBody();
}
commEventMap.put("content", messageBody);
commEventMap.put("contentMimeTypeId", messageBodyContentType.toLowerCase());
// check for for a reply to communication event (using in-reply-to the parent messageID)
String[] inReplyTo = wrapper.getHeader("In-Reply-To");
if (inReplyTo != null && inReplyTo[0] != null) {
GenericValue parentCommEvent = null;
try {
List<GenericValue> events = delegator.findByAnd("CommunicationEvent", UtilMisc.toMap("messageId", inReplyTo[0].replaceAll("[<>]", "")));
parentCommEvent = EntityUtil.getFirst(events);
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
if (parentCommEvent != null) {
String parentCommEventId = parentCommEvent.getString("communicationEventId");
String orgCommEventId = parentCommEvent.getString("origCommEventId");
if (orgCommEventId == null) orgCommEventId = parentCommEventId;
commEventMap.put("parentCommEventId", parentCommEventId);
commEventMap.put("origCommEventId", orgCommEventId);
}
}
// populate the address (to/from/cc/bcc) data
populateAddressesFromMessage(wrapper, commEventMap);
// store from/to parties, but when not found make a note of the email to/from address in the workEffort Note Section.
String commNote = "";
if (partyIdFrom != null) {
commEventMap.put("partyIdFrom", partyIdFrom);
commEventMap.put("contactMechIdFrom", contactMechIdFrom);
} else {
commNote += "Sent from: " + ((InternetAddress)addressesFrom[0]).getAddress() + "; ";
commNote += "Sent Name from: " + ((InternetAddress)addressesFrom[0]).getPersonal() + "; ";
}
if (partyIdTo != null) {
commEventMap.put("partyIdTo", partyIdTo);
commEventMap.put("contactMechIdTo", contactMechIdTo);
} else {
commNote += "Sent to: " + ((InternetAddress)addressesTo[0]).getAddress() + "; ";
if (deliveredTo != null) {
commNote += "Delivered-To: " + deliveredTo + "; ";
}
}
commNote += "Sent to: " + ((InternetAddress)addressesTo[0]).getAddress() + "; ";
commNote += "Delivered-To: " + deliveredTo + "; ";
if (partyIdTo != null && partyIdFrom != null) {
commEventMap.put("statusId", "COM_ENTERED");
} else {
commEventMap.put("statusId", "COM_UNKNOWN_PARTY");
}
if (commNote.length() > 255) commNote = commNote.substring(0,255);
if (!("".equals(commNote))) {
commEventMap.put("note", commNote);
}
commEventMap.put("userLogin", userLogin);
// Populate the CommunicationEvent.headerString field with the email headers
StringBuilder headerString = new StringBuilder();
Enumeration<?> headerLines = wrapper.getMessage().getAllHeaderLines();
while (headerLines.hasMoreElements()) {
headerString.append(System.getProperty("line.separator"));
headerString.append(headerLines.nextElement());
}
String header = headerString.toString();