1 25 package org.ofbiz.service.mail; 26 27 import java.io.IOException ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.Map ; 31 import javax.mail.Address ; 32 import javax.mail.BodyPart ; 33 import javax.mail.MessagingException ; 34 import javax.mail.Multipart ; 35 import javax.mail.Part ; 36 import javax.mail.internet.MimeMessage ; 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 ; 46 47 48 54 public class ServiceMcaCondition implements java.io.Serializable { 55 56 public static final String 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 serviceName = null; 62 protected String headerName = null; 63 protected String fieldName = null; 64 protected String operator = null; 65 protected String value = null; 66 67 public ServiceMcaCondition(Element condElement, int condType) { 68 switch (condType) { 69 case CONDITION_FIELD: 70 this.fieldName = condElement.getAttribute("field-name"); 72 this.operator = condElement.getAttribute("operator"); 74 this.value = condElement.getAttribute("value"); 76 break; 77 case CONDITION_HEADER: 78 this.headerName = condElement.getAttribute("header-name"); 80 this.operator = condElement.getAttribute("operator"); 82 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 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 reply = (Boolean ) result.get("conditionReply"); 110 if (reply == null) { 111 reply = Boolean.FALSE; 112 } 113 return reply.booleanValue(); 114 } 115 } 116 } else if (headerName != null) { 118 MimeMessage message = messageWrapper.getMessage(); 120 String [] headerValue = null; 121 try { 122 headerValue = message.getHeader(headerName); 123 } catch (MessagingException 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 message = messageWrapper.getMessage(); 161 String [] fieldValue = null; 162 try { 163 fieldValue = this.getFieldValue(message, fieldName); 164 } catch (MessagingException e) { 165 Debug.logError(e, module); 166 } catch (IOException 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 [] getFieldValue(MimeMessage message, String fieldName) throws MessagingException , IOException { 210 String [] values = null; 211 if ("to".equals(fieldName)) { 212 Address [] addrs = message.getRecipients(MimeMessage.RecipientType.TO); 213 if (addrs != null) { 214 values = new String [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 [] addrs = message.getRecipients(MimeMessage.RecipientType.CC); 221 if (addrs != null) { 222 values = new String [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 [] addrs = message.getRecipients(MimeMessage.RecipientType.BCC); 229 if (addrs != null) { 230 values = new String [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 [] addrs = message.getFrom(); 237 if (addrs != null) { 238 values = new String [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 [1]; 245 values[0] = message.getSubject(); 246 } else if ("send-date".equals(fieldName)) { 247 values = new String [1]; 248 values[0] = message.getSentDate().toString(); 249 } else if ("received-date".equals(fieldName)) { 250 values = new String [1]; 251 values[0] = message.getReceivedDate().toString(); 252 } else if ("body".equals(fieldName)) { 253 List bodyParts = this.getBodyText(message); 254 values = new String [bodyParts.size()]; 255 for (int i = 0; i < bodyParts.size(); i++) { 256 values[i] = (String ) bodyParts.get(i); 257 } 258 } 259 return values; 260 } 261 262 private List getBodyText(Part part) throws MessagingException , IOException { 263 Object c = part.getContent(); 264 if (c instanceof String ) { 265 return UtilMisc.toList(c); 266 } else if (c instanceof Multipart ) { 267 List textContent = new ArrayList (); 268 int count = ((Multipart ) c).getCount(); 269 for (int i = 0; i < count; i++) { 270 BodyPart bp = ((Multipart ) c).getBodyPart(i); 271 textContent.addAll(this.getBodyText(bp)); 272 } 273 return textContent; 274 } else { 275 return new ArrayList (); 276 } 277 } 278 } 279 | Popular Tags |