Author: jaz
Date: Tue May 12 21:11:09 2009 New Revision: 774088 URL: http://svn.apache.org/viewvc?rev=774088&view=rev Log: refactored bounce email service to use methods from the message wrapper, added a new email logging service to log information about incoming emails Modified: ofbiz/trunk/applications/party/servicedef/mcas.xml ofbiz/trunk/applications/party/servicedef/services.xml ofbiz/trunk/applications/party/src/org/ofbiz/party/communication/CommunicationEventServices.java Modified: ofbiz/trunk/applications/party/servicedef/mcas.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/party/servicedef/mcas.xml?rev=774088&r1=774087&r2=774088&view=diff ============================================================================== --- ofbiz/trunk/applications/party/servicedef/mcas.xml (original) +++ ofbiz/trunk/applications/party/servicedef/mcas.xml Tue May 12 21:11:09 2009 @@ -21,6 +21,9 @@ <service-mca xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/service-mca.xsd"> + <mca mail-rule-name="logIncomingMessage"> + <action service="logIncomingMessage" mode="sync"/> + </mca> <mca mail-rule-name="processBouncedMessage"> <action service="processBouncedMessage" mode="sync"/> </mca> Modified: ofbiz/trunk/applications/party/servicedef/services.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/party/servicedef/services.xml?rev=774088&r1=774087&r2=774088&view=diff ============================================================================== --- ofbiz/trunk/applications/party/servicedef/services.xml (original) +++ ofbiz/trunk/applications/party/servicedef/services.xml Tue May 12 21:11:09 2009 @@ -1177,4 +1177,10 @@ location="org.ofbiz.party.communication.CommunicationEventServices" invoke="processBouncedMessage"> <implements service="mailProcessInterface"/> </service> + + <!-- log incoming message MCA --> + <service name="logIncomingMessage" engine="java" + location="org.ofbiz.party.communication.CommunicationEventServices" invoke="logIncomingMessage"> + <implements service="mailProcessInterface"/> + </service> </services> Modified: ofbiz/trunk/applications/party/src/org/ofbiz/party/communication/CommunicationEventServices.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/party/src/org/ofbiz/party/communication/CommunicationEventServices.java?rev=774088&r1=774087&r2=774088&view=diff ============================================================================== --- ofbiz/trunk/applications/party/src/org/ofbiz/party/communication/CommunicationEventServices.java (original) +++ ofbiz/trunk/applications/party/src/org/ofbiz/party/communication/CommunicationEventServices.java Tue May 12 21:11:09 2009 @@ -961,149 +961,152 @@ */ public static Map<String,Object> processBouncedMessage(DispatchContext dctx, Map<String, ? extends Object> context) { Debug.logInfo("Running process bounced message check...", module); - MimeMessageWrapper wrapper = (MimeMessageWrapper) context.get("messageWrapper"); - MimeMessage message = wrapper.getMessage(); + MimeMessageWrapper wrapper = (MimeMessageWrapper) context.get("messageWrapper"); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); - - try { - Object content = message.getContent(); - if (content instanceof Multipart) { - Multipart mp = (Multipart) content; - int parts = mp.getCount(); - if (parts >= 3) { // it must have all three parts in order to process correctly - - // get the second part (delivery report) - BodyPart part2 = mp.getBodyPart(1); // index 1 should be the second part - String contentType = part2.getContentType(); - if (contentType != null && "message/delivery-status".equalsIgnoreCase(contentType)) { - Debug.logInfo("Delivery status report part found; processing...", module); - - // message is only available as an input stream; read the stream - InputStream insPart2 = (InputStream) part2.getInputStream(); - StringBuilder p2Builder = new StringBuilder(); - byte[] p2Buf = new byte[4096]; - for (int n; (n = insPart2.read(p2Buf)) != -1;) { - p2Builder.append(new String(p2Buf, 0, n)); - } - String part2Text = p2Builder.toString(); - Debug.logInfo("Part 2 Content :\n\n" + part2Text, module); - - // find the "Action" element and obtain its value (looking for "failed") - Pattern p2 = Pattern.compile("^Action: (.*)$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); - Matcher m2 = p2.matcher(part2Text); - String action = null; - if (m2.find()) { - action = m2.group(1); + int parts = wrapper.getMainPartCount(); + + if (parts >= 3) { // it must have all three parts in order to process correctly + // get the second part (delivery report) + String contentType = wrapper.getPartContentType("1"); // index 1 should be the second part + if (contentType != null && "message/delivery-status".equalsIgnoreCase(contentType)) { + Debug.logInfo("Delivery status report part found; processing...", module); + + // get the content of the part + String part2Text = wrapper.getPartRawText("1"); + if (part2Text == null) { + part2Text = ""; + } + if (Debug.verboseOn()) + Debug.logVerbose("Part 2 Text :\n\n" + part2Text, module); + + // find the "Action" element and obtain its value (looking for "failed") + Pattern p2 = Pattern.compile("^Action: (.*)$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); + Matcher m2 = p2.matcher(part2Text); + String action = null; + if (m2.find()) { + action = m2.group(1); + } + + if (action != null && "failed".equalsIgnoreCase(action)) { + // message bounced -- get the original message + String part3Text = wrapper.getPartRawText("2"); // index 2 should be the third part + if (part3Text == null) { + part3Text = ""; + } + if (Debug.verboseOn()) + Debug.logVerbose("Part 3 Text :\n\n" + part3Text, module); + + // find the "Message-Id" element and obtain its value (looking for "failed") + Pattern p3 = Pattern.compile("^Message-Id: (.*)$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); + Matcher m3 = p3.matcher(part3Text); + String messageId = null; + if (m3.find()) { + Debug.logInfo("Found message-id : " + m3.group(), module); + messageId = m3.group(1); + } + + // find the matching communication event + if (messageId != null) { + List<GenericValue> values; + try { + values = delegator.findByAnd("CommunicationEvent", UtilMisc.toMap("messageId", messageId)); + } catch (GenericEntityException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); } - - if (action != null && "failed".equalsIgnoreCase(action)) { - // message bounced -- get the original message - BodyPart part3 = mp.getBodyPart(2); // index 2 should be the third part - - // read part 3 message - InputStream insPart3 = (InputStream) part3.getInputStream(); - StringBuilder p3Builder = new StringBuilder(); - byte[] p3Buf = new byte[4096]; - for (int n; (n = insPart3.read(p3Buf)) != -1;) { - p3Builder.append(new String(p3Buf, 0, n)); - } - String part3Text = p3Builder.toString(); - Debug.logInfo("Part 3 Content :\n\n" + part3Text, module); - - // find the "Message-Id" element and obtain its value (looking for "failed") - Pattern p3 = Pattern.compile("^Message-Id: (.*)$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); - Matcher m3 = p3.matcher(part3Text); - String messageId = null; - if (m3.find()) { - Debug.logInfo("Found message-id : " + m3.group(), module); - messageId = m3.group(1); + if (values != null && values.size() > 0) { + // there should be only one; unique key + GenericValue value = values.get(0); + + // update the communication event status + Map<String, Object> updateCtx = FastMap.newInstance(); + updateCtx.put("communicationEventId", value.getString("communicationEventId")); + updateCtx.put("statusId", "COM_BOUNCED"); + updateCtx.put("userLogin", context.get("userLogin")); + Map<String, Object> result; + try { + result = dispatcher.runSync("updateCommunicationEvent", updateCtx); + } catch (GenericServiceException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + if (ServiceUtil.isError(result)) { + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); + } + } else { + if (Debug.infoOn()) { + Debug.logInfo("Unable to find CommunicationEvent with the matching messageId : " + messageId, module); } - - // find the matching communication event - if (messageId != null) { - List<GenericValue> values; + + // no communication events found for that message ID; possible this is a NEWSLETTER + try { + values = delegator.findByAnd("ContactListCommStatus", UtilMisc.toMap("messageId", + messageId)); + } catch (GenericEntityException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + if (values != null && values.size() > 0) { + // there should be only one; unique key + GenericValue value = values.get(0); + + Map<String, Object> updateCtx = FastMap.newInstance(); + updateCtx.put("communicationEventId", value.getString("communicationEventId")); + updateCtx.put("contactListId", value.getString("contactListId")); + updateCtx.put("contactMechId", value.getString("contactMechId")); + updateCtx.put("partyId", value.getString("partyId")); + updateCtx.put("statusId", "COM_BOUNCED"); + updateCtx.put("userLogin", context.get("userLogin")); + Map<String, Object> result; try { - values = delegator.findByAnd("CommunicationEvent", UtilMisc.toMap("messageId", messageId)); - } catch (GenericEntityException e) { + result = dispatcher.runSync("updateContactListCommStatus", updateCtx); + } catch (GenericServiceException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } - if (values != null && values.size() > 0) { - // there should be only one; unique key - GenericValue value = values.get(0); - - // update the communication event status - Map<String,Object> updateCtx = FastMap.newInstance(); - updateCtx.put("communicationEventId", value.getString("communicationEventId")); - updateCtx.put("statusId", "COM_BOUNCED"); - updateCtx.put("userLogin", context.get("userLogin")); - Map<String,Object> result; - try { - result = dispatcher.runSync("updateCommunicationEvent", updateCtx); - } catch (GenericServiceException e) { - Debug.logError(e, module); - return ServiceUtil.returnError(e.getMessage()); - } - if (ServiceUtil.isError(result)) { - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } - } else { - if (Debug.infoOn()) { - Debug.logInfo("Unable to find CommunicationEvent with the matching messageId : " + messageId, module); - } - - // no communication events found for that message ID; possible this is a NEWSLETTER - try { - values = delegator.findByAnd("ContactListCommStatus", UtilMisc.toMap("messageId", messageId)); - } catch (GenericEntityException e) { - Debug.logError(e, module); - return ServiceUtil.returnError(e.getMessage()); - } - if (values != null && values.size() > 0) { - // there should be only one; unique key - GenericValue value = values.get(0); - - Map<String,Object> updateCtx = FastMap.newInstance(); - updateCtx.put("communicationEventId", value.getString("communicationEventId")); - updateCtx.put("contactListId", value.getString("contactListId")); - updateCtx.put("contactMechId", value.getString("contactMechId")); - updateCtx.put("partyId", value.getString("partyId")); - updateCtx.put("statusId", "COM_BOUNCED"); - updateCtx.put("userLogin", context.get("userLogin")); - Map<String,Object> result; - try { - result = dispatcher.runSync("updateContactListCommStatus", updateCtx); - } catch (GenericServiceException e) { - Debug.logError(e, module); - return ServiceUtil.returnError(e.getMessage()); - } - if (ServiceUtil.isError(result)) { - return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); - } - } else { - if (Debug.infoOn()) { - Debug.logInfo("Unable to find ContactListCommStatus with the matching messageId : " + messageId, module); - } - } + if (ServiceUtil.isError(result)) { + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); } } else { - Debug.logWarning("No message ID attached to part", module); + if (Debug.infoOn()) { + Debug.logInfo("Unable to find ContactListCommStatus with the matching messageId : " + messageId, module); + + } } } - } - } + } else { + Debug.logWarning("No message ID attached to part", module); + } + } } - } catch (MessagingException e) { - Debug.logError(e, module); - return ServiceUtil.returnError(e.getMessage()); - } catch (IOException e) { - Debug.logError(e, module); - return ServiceUtil.returnError(e.getMessage()); } - + return ServiceUtil.returnSuccess(); } + + public static Map<String,Object> logIncomingMessage(DispatchContext dctx, Map<String, ? extends Object> context) { + MimeMessageWrapper wrapper = (MimeMessageWrapper) context.get("messageWrapper"); + Debug.logInfo("Message recevied : " + wrapper.getSubject(), module); + Debug.logInfo("-- Content Type : " + wrapper.getContentType(), module); + Debug.logInfo("-- Number of parts : " + wrapper.getMainPartCount(), module); + Debug.logInfo("-- Number of attachments : " + wrapper.getAttachmentIndexes().size(), module); + Debug.logInfo("-- Message ID : " + wrapper.getMessageId(), module); + + Debug.logInfo("### MESSAGE ###\n\n" + wrapper.getMessageBody(), module); + + List<String> attachmentIndexes = wrapper.getAttachmentIndexes(); + if (attachmentIndexes.size() > 0) { + Debug.logInfo("### ATTACHMENTS ###", module); + for (String idx : attachmentIndexes) { + Debug.logInfo("### -- Filename : " + wrapper.getPartFilename(idx), module); + Debug.logInfo("### -- Content Type : " + wrapper.getPartContentType(idx), module); + } + } + + return ServiceUtil.returnSuccess(); + + } } |
Free forum by Nabble | Edit this page |