KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > mail > ServiceMcaCondition


1 /*
2  * $Id: ServiceMcaCondition.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.service.mail;
26
27 import java.io.IOException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.mail.Address JavaDoc;
32 import javax.mail.BodyPart JavaDoc;
33 import javax.mail.MessagingException JavaDoc;
34 import javax.mail.Multipart JavaDoc;
35 import javax.mail.Part JavaDoc;
36 import javax.mail.internet.MimeMessage JavaDoc;
37
38 import org.ofbiz.base.util.Debug;
39 import org.ofbiz.base.util.UtilMisc;
40 import org.ofbiz.service.LocalDispatcher;
41 import org.ofbiz.service.GenericServiceException;
42 import org.ofbiz.service.ServiceUtil;
43 import org.ofbiz.entity.GenericValue;
44
45 import org.w3c.dom.Element JavaDoc;
46
47
48 /**
49  *
50  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
51  * @version $Rev: 5462 $
52  * @since 3.3
53  */

54 public class ServiceMcaCondition implements java.io.Serializable JavaDoc {
55
56     public static final String JavaDoc module = ServiceMcaCondition.class.getName();
57     public static final int CONDITION_FIELD = 1;
58     public static final int CONDITION_HEADER = 2;
59     public static final int CONDITION_SERVICE = 3;
60
61     protected String JavaDoc serviceName = null;
62     protected String JavaDoc headerName = null;
63     protected String JavaDoc fieldName = null;
64     protected String JavaDoc operator = null;
65     protected String JavaDoc value = null;
66
67     public ServiceMcaCondition(Element JavaDoc condElement, int condType) {
68         switch (condType) {
69             case CONDITION_FIELD:
70                 // fields: from|to|subject|body|sent-date|receieved-date
71
this.fieldName = condElement.getAttribute("field-name");
72                 // operators: equals|not-equals|empty|not-empty|matches|not-matches
73
this.operator = condElement.getAttribute("operator");
74                 // value to compare
75
this.value = condElement.getAttribute("value");
76                 break;
77             case CONDITION_HEADER:
78                 // free form header name
79
this.headerName = condElement.getAttribute("header-name");
80                 // operators: equals|not-equals|empty|not-empty|matches|not-matches
81
this.operator = condElement.getAttribute("operator");
82                 // value to compare
83
this.value = condElement.getAttribute("value");
84                 break;
85             case CONDITION_SERVICE:
86                 this.serviceName = condElement.getAttribute("service-name");
87                 break;
88         }
89     }
90
91     public boolean eval(LocalDispatcher dispatcher, MimeMessageWrapper messageWrapper, GenericValue userLogin) {
92         boolean passedCondition = false;
93         if (serviceName != null) {
94             Map JavaDoc result = null;
95             try {
96                 result = dispatcher.runSync(serviceName, UtilMisc.toMap("messageWrapper", messageWrapper, "userLogin", userLogin));
97             } catch (GenericServiceException e) {
98                 Debug.logError(e, module);
99                 return false;
100             }
101             if (result == null) {
102                 Debug.logError("Service MCA Condition Service [" + serviceName + "] returned null!", module);
103                 return false;
104             } else {
105                 if (ServiceUtil.isError(result)) {
106                     Debug.logError(ServiceUtil.getErrorMessage(result), module);
107                     return false;
108                 } else {
109                     Boolean JavaDoc reply = (Boolean JavaDoc) result.get("conditionReply");
110                     if (reply == null) {
111                         reply = Boolean.FALSE;
112                     }
113                     return reply.booleanValue();
114                 }
115             }
116             // invoke the condition service
117
} else if (headerName != null) {
118             // compare the header field
119
MimeMessage JavaDoc message = messageWrapper.getMessage();
120             String JavaDoc[] headerValue = null;
121             try {
122                 headerValue = message.getHeader(headerName);
123             } catch (MessagingException JavaDoc e) {
124                 Debug.logError(e, module);
125             }
126
127             if (headerValue != null) {
128                 for (int i = 0; i < headerValue.length; i++) {
129                     if ("equals".equals(operator)) {
130                         if (headerValue[i].equals(value)) {
131                             passedCondition = true;
132                             break;
133                         }
134                     } else if ("not-equals".equals(operator)) {
135                         if (!headerValue[i].equals(value)) {
136                             passedCondition = true;
137                         } else {
138                             passedCondition = false;
139                         }
140                     } else if ("matches".equals(operator)) {
141                         if (headerValue[i].matches(value)) {
142                             passedCondition = true;
143                             break;
144                         }
145                     } else if ("not-matches".equals(operator)) {
146                         if (!headerValue[i].matches(value)) {
147                             passedCondition = true;
148                         } else {
149                             passedCondition = false;
150                         }
151                     } else if ("not-empty".equals(operator)) {
152                         passedCondition = true;
153                         break;
154                     }
155                 }
156             } else if ("empty".equals(operator)) {
157                 passedCondition = true;
158             }
159         } else if (fieldName != null) {
160             MimeMessage JavaDoc message = messageWrapper.getMessage();
161             String JavaDoc[] fieldValue = null;
162             try {
163                 fieldValue = this.getFieldValue(message, fieldName);
164             } catch (MessagingException JavaDoc e) {
165                 Debug.logError(e, module);
166             } catch (IOException JavaDoc e) {
167                 Debug.logError(e, module);
168             }
169
170             if (fieldValue != null) {
171                 for (int i = 0; i < fieldValue.length; i++) {
172                     if ("equals".equals(operator)) {
173                         if (fieldValue[i].equals(value)) {
174                             passedCondition = true;
175                             break;
176                         }
177                     } else if ("not-equals".equals(operator)) {
178                         if (!fieldValue[i].equals(value)) {
179                             passedCondition = true;
180                         } else {
181                             passedCondition = false;
182                         }
183                     } else if ("matches".equals(operator)) {
184                         if (fieldValue[i].matches(value)) {
185                             passedCondition = true;
186                             break;
187                         }
188                     } else if ("not-matches".equals(operator)) {
189                         if (!fieldValue[i].matches(value)) {
190                             passedCondition = true;
191                         } else {
192                             passedCondition = false;
193                         }
194                     } else if ("not-empty".equals(operator)) {
195                         passedCondition = true;
196                         break;
197                     }
198                 }
199             } else if ("empty".equals(operator)) {
200                 passedCondition = true;
201             }
202         } else {
203             passedCondition = false;
204         }
205
206         return passedCondition;
207     }
208
209     protected String JavaDoc[] getFieldValue(MimeMessage JavaDoc message, String JavaDoc fieldName) throws MessagingException JavaDoc, IOException JavaDoc {
210         String JavaDoc[] values = null;
211         if ("to".equals(fieldName)) {
212             Address JavaDoc[] addrs = message.getRecipients(MimeMessage.RecipientType.TO);
213             if (addrs != null) {
214                 values = new String JavaDoc[addrs.length];
215                 for (int i = 0; i < addrs.length; i++) {
216                     values[i] = addrs[i].toString();
217                 }
218             }
219         } else if ("cc".equals(fieldName)) {
220             Address JavaDoc[] addrs = message.getRecipients(MimeMessage.RecipientType.CC);
221             if (addrs != null) {
222                 values = new String JavaDoc[addrs.length];
223                 for (int i = 0; i < addrs.length; i++) {
224                     values[i] = addrs[i].toString();
225                 }
226             }
227         } else if ("bcc".equals(fieldName)) {
228             Address JavaDoc[] addrs = message.getRecipients(MimeMessage.RecipientType.BCC);
229             if (addrs != null) {
230                 values = new String JavaDoc[addrs.length];
231                 for (int i = 0; i < addrs.length; i++) {
232                     values[i] = addrs[i].toString();
233                 }
234             }
235         } else if ("from".equals(fieldName)) {
236             Address JavaDoc[] addrs = message.getFrom();
237             if (addrs != null) {
238                 values = new String JavaDoc[addrs.length];
239                 for (int i = 0; i < addrs.length; i++) {
240                     values[i] = addrs[i].toString();
241                 }
242             }
243         } else if ("subject".equals(fieldName)) {
244             values = new String JavaDoc[1];
245             values[0] = message.getSubject();
246         } else if ("send-date".equals(fieldName)) {
247             values = new String JavaDoc[1];
248             values[0] = message.getSentDate().toString();
249         } else if ("received-date".equals(fieldName)) {
250             values = new String JavaDoc[1];
251             values[0] = message.getReceivedDate().toString();
252         } else if ("body".equals(fieldName)) {
253             List JavaDoc bodyParts = this.getBodyText(message);
254             values = new String JavaDoc[bodyParts.size()];
255             for (int i = 0; i < bodyParts.size(); i++) {
256                 values[i] = (String JavaDoc) bodyParts.get(i);
257             }
258         }
259         return values;
260     }
261
262     private List JavaDoc getBodyText(Part JavaDoc part) throws MessagingException JavaDoc, IOException JavaDoc {
263         Object JavaDoc c = part.getContent();
264         if (c instanceof String JavaDoc) {
265             return UtilMisc.toList(c);
266         } else if (c instanceof Multipart JavaDoc) {
267             List JavaDoc textContent = new ArrayList JavaDoc();
268             int count = ((Multipart JavaDoc) c).getCount();
269             for (int i = 0; i < count; i++) {
270                 BodyPart JavaDoc bp = ((Multipart JavaDoc) c).getBodyPart(i);
271                 textContent.addAll(this.getBodyText(bp));
272             }
273             return textContent;
274         } else {
275             return new ArrayList JavaDoc();
276         }
277     }
278 }
279
Popular Tags