implemented service to process bounced emails and update the communication event status

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@772780 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew Zeneski
2009-05-07 21:29:06 +00:00
parent d3403f89f5
commit 7929598a75
4 changed files with 155 additions and 2 deletions
+2 -1
View File
@@ -34,7 +34,8 @@ under the License.
<service-resource type="model" loader="main" location="servicedef/services.xml"/>
<service-resource type="model" loader="main" location="servicedef/services_view.xml"/>
<service-resource type="eca" loader="main" location="servicedef/secas.xml"/>
<service-resource type="mca" loader="main" location="servicedef/mcas.xml"/>
<test-suite loader="main" location="testdef/PartyTests.xml"/>
<webapp name="party"
+28
View File
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<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="processBouncedMessage">
<action service="processBouncedMessage" mode="sync"/>
</mca>
</service-mca>
+10 -1
View File
@@ -93,7 +93,10 @@ under the License.
<service name="updatePerson" engine="java" default-entity-name="Person"
location="org.ofbiz.party.party.PartyServices" invoke="updatePerson" auth="true">
<description>Update a Person</description>
<permission-service service-name="partyGroupPermissionCheck" main-action="UPDATE"/>
<required-permissions join-type="AND">
<check-permission permission="update:party:${partyId}"/>
</required-permissions>
<!-- <permission-service service-name="partyGroupPermissionCheck" main-action="UPDATE"/> -->
<auto-attributes mode="IN" include="pk" optional="true"><!-- if no partyId specified will use userLogin.partyId --></auto-attributes>
<auto-attributes mode="IN" include="nonpk" optional="true"/>
<attribute name="preferredCurrencyUomId" type="String" mode="IN" optional="true"/>
@@ -1167,4 +1170,10 @@ under the License.
<attribute name="contactMechId" type="String" mode="IN" optional="true"/>
<attribute name="contactMechId" type="String" mode="OUT" optional="false"/>
</service>
<!-- bounced message processing -->
<service name="processBouncedMessage" engine="java"
location="org.ofbiz.party.communication.CommunicationEventServices" invoke="processBouncedMessage">
<implements service="mailProcessInterface"/>
</service>
</services>
@@ -31,8 +31,12 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Header;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
@@ -946,4 +950,115 @@ public class CommunicationEventServices {
}
return attachmentCount;
}
/*
* Service to process incoming email and look for a bounce message. If the email is indeed a bounce message
* the CommunicationEvent will be updated with the proper COM_BOUNCED status.
*/
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();
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();
int part2Size = part2.getSize();
byte[] part2Bytes = new byte[part2Size];
insPart2.read(part2Bytes, 0, part2Size);
String part2Text = new String(part2Bytes);
// 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
BodyPart part3 = mp.getBodyPart(2); // index 2 should be the third part
// read part 3 message
InputStream insPart3 = (InputStream) part3.getInputStream();
int part3Size = part3.getSize();
byte[] part3Bytes = new byte[part3Size];
insPart3.read(part3Bytes, 0, part3Size);
String part3Text = new String(part3Bytes);
// 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()) {
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 (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 communication event 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();
}
}