1 25 package org.ofbiz.content.email; 26 27 import java.io.IOException ; 28 import java.io.InputStreamReader ; 29 import java.io.StringWriter ; 30 import java.io.Writer ; 31 import java.net.InetAddress ; 32 import java.net.URL ; 33 import java.net.UnknownHostException ; 34 import java.util.HashMap ; 35 import java.util.Map ; 36 37 import freemarker.template.TemplateException; 38 39 import org.ofbiz.base.util.Debug; 40 import org.ofbiz.base.util.UtilMisc; 41 import org.ofbiz.base.util.UtilProperties; 42 import org.ofbiz.base.util.UtilURL; 43 import org.ofbiz.base.util.UtilValidate; 44 import org.ofbiz.base.util.template.FreeMarkerWorker; 45 import org.ofbiz.entity.GenericDelegator; 46 import org.ofbiz.entity.GenericEntityException; 47 import org.ofbiz.entity.GenericValue; 48 import org.ofbiz.service.DispatchContext; 49 import org.ofbiz.service.GenericServiceException; 50 import org.ofbiz.service.LocalDispatcher; 51 import org.ofbiz.service.ModelService; 52 import org.ofbiz.service.ServiceUtil; 53 54 110 public class NotificationServices { 111 112 public static final String module = NotificationServices.class.getName(); 113 114 130 public static Map sendNotification(DispatchContext ctx, Map context) { 131 LocalDispatcher dispatcher = ctx.getDispatcher(); 132 Map result = null; 133 134 try { 135 String body = (String ) context.get("body"); 138 139 if (body == null) { 140 Map bodyResult = prepareNotification(ctx, context); 142 143 if (bodyResult.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS)) { 145 body = (String ) bodyResult.get("body"); 146 } else { 147 Debug.logError("prepareNotification failed: " + bodyResult.get(ModelService.ERROR_MESSAGE), module); 149 body = null; 150 } 151 } 152 153 if (body != null) { 155 Map emailContext = new HashMap (); 157 emailContext.put("sendTo", context.get("sendTo")); 158 emailContext.put("body", body); 159 emailContext.put("sendCc", context.get("sendCc")); 160 emailContext.put("sendBcc", context.get("sendBcc")); 161 emailContext.put("sendFrom", context.get("sendFrom")); 162 emailContext.put("subject", context.get("subject")); 163 emailContext.put("sendVia", context.get("sendVia")); 164 emailContext.put("sendType", context.get("sendType")); 165 emailContext.put("contentType", context.get("contentType")); 166 167 result = dispatcher.runSync("sendMail", emailContext); 169 } else { 170 Debug.logError("Invalid email body; null is not allowed", module); 171 result = ServiceUtil.returnError("Invalid email body; null is not allowed"); 172 } 173 } catch (GenericServiceException serviceException) { 174 Debug.logError(serviceException, "Error sending email", module); 175 result = ServiceUtil.returnError("Email delivery error, see error log"); 176 } 177 178 return result; 179 } 180 181 196 public static Map prepareNotification(DispatchContext ctx, Map context) { 197 GenericDelegator delegator = ctx.getDelegator(); 198 String templateName = (String ) context.get("templateName"); 199 Map templateData = (Map ) context.get("templateData"); 200 String webSiteId = (String ) context.get("webSiteId"); 201 202 Map result = null; 203 if (templateData == null) { 204 templateData = new HashMap (); 205 } 206 207 try { 208 setBaseUrl(delegator, webSiteId, templateData); 210 211 URL templateUrl = UtilURL.fromResource(templateName); 213 214 if (templateUrl == null) { 215 Debug.logError("Problem getting the template URL: " + templateName + " not found", module); 216 return ServiceUtil.returnError("Problem finding template; see logs"); 217 } 218 219 InputStreamReader templateReader = new InputStreamReader (templateUrl.openStream()); 220 221 Writer writer = new StringWriter (); 224 FreeMarkerWorker.renderTemplate(templateName, templateReader, templateData, writer); 225 226 String notificationBody = writer.toString(); 228 229 result = ServiceUtil.returnSuccess("Message body generated successfully"); 231 result.put("body", notificationBody); 232 } catch (IOException ie) { 233 Debug.logError(ie, "Problems reading template", module); 234 result = ServiceUtil.returnError("Template reading problem, see error logs"); 235 } catch (TemplateException te) { 236 Debug.logError(te, "Problems processing template", module); 237 result = ServiceUtil.returnError("Template processing problem, see error log"); 238 } 239 240 return result; 241 } 242 243 264 public static void setBaseUrl(GenericDelegator delegator, String webSiteId, Map context) { 265 if (!context.containsKey("baseUrl")) { 267 StringBuffer httpBase = null; 268 StringBuffer httpsBase = null; 269 270 String localServer = null; 271 272 String httpsPort = null; 273 String httpsServer = null; 274 String httpPort = null; 275 String httpServer = null; 276 Boolean enableHttps = null; 277 278 try { 279 InetAddress localHost = InetAddress.getLocalHost(); 281 localServer = localHost.getHostAddress(); 282 } catch (UnknownHostException hostException) { 283 Debug.logWarning(hostException, "Could not determine localhost, using '127.0.0.1'", module); 284 localServer = "127.0.0.1"; 285 } 286 287 GenericValue webSite = null; 289 if (webSiteId != null) { 290 try { 291 webSite = delegator.findByPrimaryKeyCache("WebSite", UtilMisc.toMap("webSiteId", webSiteId)); 292 if (webSite != null) { 293 httpsPort = webSite.getString("httpsPort"); 294 httpsServer = webSite.getString("httpsHost"); 295 httpPort = webSite.getString("httpPort"); 296 httpServer = webSite.getString("httpHost"); 297 enableHttps = webSite.getBoolean("enableHttps"); 298 } 299 } catch (GenericEntityException e) { 300 Debug.logWarning(e, "Problems with WebSite entity; using global defaults", module); 301 } 302 } 303 304 if (UtilValidate.isEmpty(httpsPort)) { 306 httpsPort = UtilProperties.getPropertyValue("url.properties", "port.https", "443"); 307 } 308 if (UtilValidate.isEmpty(httpsServer)) { 309 httpsServer = UtilProperties.getPropertyValue("url.properties", "force.https.host", localServer); 310 } 311 if (UtilValidate.isEmpty(httpPort)) { 312 httpPort = UtilProperties.getPropertyValue("url.properties", "port.http", "80"); 313 } 314 if (UtilValidate.isEmpty(httpServer)) { 315 httpServer = UtilProperties.getPropertyValue("url.properties", "force.http.host", localServer); 316 } 317 if (UtilValidate.isEmpty(enableHttps)) { 318 enableHttps = (UtilProperties.propertyValueEqualsIgnoreCase("url.properties", "port.https.enabled", "Y")) ? Boolean.TRUE : Boolean.FALSE; 319 } 320 321 httpBase = new StringBuffer ("http://"); 323 httpBase.append(httpServer); 324 if (!"80".equals(httpPort)) { 325 httpBase.append(":"); 326 httpBase.append(httpPort); 327 } 328 329 context.put("baseUrl", httpBase.toString()); 331 332 if (enableHttps.booleanValue()) { 333 httpsBase = new StringBuffer ("https://"); 335 httpsBase.append(httpsServer); 336 if (!"443".equals(httpsPort)) { 337 httpsBase.append(":"); 338 httpsBase.append(httpsPort); 339 } 340 341 context.put("baseSecureUrl", httpsBase.toString()); 343 } else { 344 context.put("baseSecureUrl", httpBase.toString()); 345 } 346 } 347 } 348 } 349 | Popular Tags |