@Override protected Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) { try { Object obj = BshUtil.eval( new String(this.chars, this.parseStart, this.parseLength), UtilMisc.makeMapWritable(context)); if (obj != null) { return obj; } else { if (Debug.verboseOn()) { Debug.logVerbose( "BSH scriptlet evaluated to null [" + this + "], got no return so inserting nothing.", module); } } } catch (EvalError e) { Debug.logWarning( e, "Error evaluating BSH scriptlet [" + this + "], inserting nothing; error was: " + e, module); } return null; }
/** * JavaMail Service that gets body content from a URL * * @param ctx The DispatchContext that this service is operating in * @param rcontext Map containing the input parameters * @return Map with the result of the service, the output parameters */ public static Map<String, Object> sendMailFromUrl( DispatchContext ctx, Map<String, ? extends Object> rcontext) { // pretty simple, get the content and then call the sendMail method below Map<String, Object> sendMailContext = UtilMisc.makeMapWritable(rcontext); String bodyUrl = (String) sendMailContext.remove("bodyUrl"); Map<String, Object> bodyUrlParameters = UtilGenerics.checkMap(sendMailContext.remove("bodyUrlParameters")); Locale locale = (Locale) rcontext.get("locale"); LocalDispatcher dispatcher = ctx.getDispatcher(); URL url = null; try { url = new URL(bodyUrl); } catch (MalformedURLException e) { Debug.logWarning(e, module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "CommonEmailSendMalformedUrl", UtilMisc.toMap("bodyUrl", bodyUrl, "errorString", e.toString()), locale)); } HttpClient httpClient = new HttpClient(url, bodyUrlParameters); String body = null; try { body = httpClient.post(); } catch (HttpClientException e) { Debug.logWarning(e, module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "CommonEmailSendGettingError", UtilMisc.toMap("errorString", e.toString()), locale)); } sendMailContext.put("body", body); Map<String, Object> sendMailResult; try { sendMailResult = dispatcher.runSync("sendMail", sendMailContext); } catch (GenericServiceException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } // just return the same result; it contains all necessary information return sendMailResult; }
/** * Gets a Map of JMS Listeners. * * @return Map of JMS Listeners */ public Map<String, GenericMessageListener> getJMSListeners() { return UtilMisc.makeMapWritable(listeners); }
/** * JavaMail Service that gets body content from a Screen Widget defined in the product store * record and if available as attachment also. * * @param dctx The DispatchContext that this service is operating in * @param rServiceContext Map containing the input parameters * @return Map with the result of the service, the output parameters */ public static Map<String, Object> sendMailFromScreen( DispatchContext dctx, Map<String, ? extends Object> rServiceContext) { Map<String, Object> serviceContext = UtilMisc.makeMapWritable(rServiceContext); LocalDispatcher dispatcher = dctx.getDispatcher(); String webSiteId = (String) serviceContext.remove("webSiteId"); String bodyText = (String) serviceContext.remove("bodyText"); String bodyScreenUri = (String) serviceContext.remove("bodyScreenUri"); String xslfoAttachScreenLocationParam = (String) serviceContext.remove("xslfoAttachScreenLocation"); String attachmentNameParam = (String) serviceContext.remove("attachmentName"); List<String> xslfoAttachScreenLocationListParam = UtilGenerics.checkList(serviceContext.remove("xslfoAttachScreenLocationList")); List<String> attachmentNameListParam = UtilGenerics.checkList(serviceContext.remove("attachmentNameList")); List<String> xslfoAttachScreenLocationList = FastList.newInstance(); List<String> attachmentNameList = FastList.newInstance(); if (UtilValidate.isNotEmpty(xslfoAttachScreenLocationParam)) xslfoAttachScreenLocationList.add(xslfoAttachScreenLocationParam); if (UtilValidate.isNotEmpty(attachmentNameParam)) attachmentNameList.add(attachmentNameParam); if (UtilValidate.isNotEmpty(xslfoAttachScreenLocationListParam)) xslfoAttachScreenLocationList.addAll(xslfoAttachScreenLocationListParam); if (UtilValidate.isNotEmpty(attachmentNameListParam)) attachmentNameList.addAll(attachmentNameListParam); Locale locale = (Locale) serviceContext.get("locale"); Map<String, Object> bodyParameters = UtilGenerics.checkMap(serviceContext.remove("bodyParameters")); if (bodyParameters == null) { bodyParameters = MapStack.create(); } if (!bodyParameters.containsKey("locale")) { bodyParameters.put("locale", locale); } else { locale = (Locale) bodyParameters.get("locale"); } String partyId = (String) serviceContext.get("partyId"); if (partyId == null) { partyId = (String) bodyParameters.get("partyId"); } String orderId = (String) bodyParameters.get("orderId"); String custRequestId = (String) bodyParameters.get("custRequestId"); bodyParameters.put("communicationEventId", serviceContext.get("communicationEventId")); NotificationServices.setBaseUrl(dctx.getDelegator(), webSiteId, bodyParameters); String contentType = (String) serviceContext.remove("contentType"); StringWriter bodyWriter = new StringWriter(); MapStack<String> screenContext = MapStack.create(); screenContext.put("locale", locale); ScreenRenderer screens = new ScreenRenderer(bodyWriter, screenContext, htmlScreenRenderer); screens.populateContextForService(dctx, bodyParameters); screenContext.putAll(bodyParameters); if (bodyScreenUri != null) { try { screens.render(bodyScreenUri); } catch (GeneralException e) { Debug.logError(e, "Error rendering screen for email: " + e.toString(), module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "CommonEmailSendRenderingScreenEmailError", UtilMisc.toMap("errorString", e.toString()), locale)); } catch (IOException e) { Debug.logError(e, "Error rendering screen for email: " + e.toString(), module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "CommonEmailSendRenderingScreenEmailError", UtilMisc.toMap("errorString", e.toString()), locale)); } catch (SAXException e) { Debug.logError(e, "Error rendering screen for email: " + e.toString(), module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "CommonEmailSendRenderingScreenEmailError", UtilMisc.toMap("errorString", e.toString()), locale)); } catch (ParserConfigurationException e) { Debug.logError(e, "Error rendering screen for email: " + e.toString(), module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "CommonEmailSendRenderingScreenEmailError", UtilMisc.toMap("errorString", e.toString()), locale)); } } boolean isMultiPart = false; // check if attachment screen location passed in if (UtilValidate.isNotEmpty(xslfoAttachScreenLocationList)) { List<Map<String, ? extends Object>> bodyParts = FastList.newInstance(); if (bodyText != null) { bodyText = FlexibleStringExpander.expandString(bodyText, screenContext, locale); bodyParts.add(UtilMisc.<String, Object>toMap("content", bodyText, "type", "text/html")); } else { bodyParts.add( UtilMisc.<String, Object>toMap("content", bodyWriter.toString(), "type", "text/html")); } for (int i = 0; i < xslfoAttachScreenLocationList.size(); i++) { String xslfoAttachScreenLocation = xslfoAttachScreenLocationList.get(i); String attachmentName = "Details.pdf"; if (UtilValidate.isNotEmpty(attachmentNameList) && attachmentNameList.size() >= i) { attachmentName = attachmentNameList.get(i); } isMultiPart = true; // start processing fo pdf attachment try { Writer writer = new StringWriter(); MapStack<String> screenContextAtt = MapStack.create(); // substitute the freemarker variables... ScreenRenderer screensAtt = new ScreenRenderer(writer, screenContext, foScreenRenderer); screensAtt.populateContextForService(dctx, bodyParameters); screenContextAtt.putAll(bodyParameters); screensAtt.render(xslfoAttachScreenLocation); /* try { // save generated fo file for debugging String buf = writer.toString(); java.io.FileWriter fw = new java.io.FileWriter(new java.io.File("/tmp/file1.xml")); fw.write(buf.toString()); fw.close(); } catch (IOException e) { Debug.logError(e, "Couldn't save xsl-fo xml debug file: " + e.toString(), module); } */ // create the input stream for the generation StreamSource src = new StreamSource(new StringReader(writer.toString())); // create the output stream for the generation ByteArrayOutputStream baos = new ByteArrayOutputStream(); Fop fop = ApacheFopWorker.createFopInstance(baos, MimeConstants.MIME_PDF); ApacheFopWorker.transform(src, null, fop); // and generate the PDF baos.flush(); baos.close(); // store in the list of maps for sendmail.... bodyParts.add( UtilMisc.<String, Object>toMap( "content", baos.toByteArray(), "type", "application/pdf", "filename", attachmentName)); } catch (GeneralException ge) { Debug.logError(ge, "Error rendering PDF attachment for email: " + ge.toString(), module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "CommonEmailSendRenderingScreenPdfError", UtilMisc.toMap("errorString", ge.toString()), locale)); } catch (IOException ie) { Debug.logError(ie, "Error rendering PDF attachment for email: " + ie.toString(), module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "CommonEmailSendRenderingScreenPdfError", UtilMisc.toMap("errorString", ie.toString()), locale)); } catch (FOPException fe) { Debug.logError(fe, "Error rendering PDF attachment for email: " + fe.toString(), module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "CommonEmailSendRenderingScreenPdfError", UtilMisc.toMap("errorString", fe.toString()), locale)); } catch (SAXException se) { Debug.logError(se, "Error rendering PDF attachment for email: " + se.toString(), module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "CommonEmailSendRenderingScreenPdfError", UtilMisc.toMap("errorString", se.toString()), locale)); } catch (ParserConfigurationException pe) { Debug.logError(pe, "Error rendering PDF attachment for email: " + pe.toString(), module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "CommonEmailSendRenderingScreenPdfError", UtilMisc.toMap("errorString", pe.toString()), locale)); } serviceContext.put("bodyParts", bodyParts); } } else { isMultiPart = false; // store body and type for single part message in the context. if (bodyText != null) { bodyText = FlexibleStringExpander.expandString(bodyText, screenContext, locale); serviceContext.put("body", bodyText); } else { serviceContext.put("body", bodyWriter.toString()); } // Only override the default contentType in case of plaintext, since other contentTypes may be // multipart // and would require specific handling. if (contentType != null && contentType.equalsIgnoreCase("text/plain")) { serviceContext.put("contentType", "text/plain"); } else { serviceContext.put("contentType", "text/html"); } } // also expand the subject at this point, just in case it has the FlexibleStringExpander syntax // in it... String subject = (String) serviceContext.remove("subject"); subject = FlexibleStringExpander.expandString(subject, screenContext, locale); Debug.logInfo("Expanded email subject to: " + subject, module); serviceContext.put("subject", subject); serviceContext.put("partyId", partyId); if (UtilValidate.isNotEmpty(orderId)) { serviceContext.put("orderId", orderId); } if (UtilValidate.isNotEmpty(custRequestId)) { serviceContext.put("custRequestId", custRequestId); } if (Debug.verboseOn()) Debug.logVerbose("sendMailFromScreen sendMail context: " + serviceContext, module); Map<String, Object> result = ServiceUtil.returnSuccess(); Map<String, Object> sendMailResult; try { if (isMultiPart) { sendMailResult = dispatcher.runSync("sendMailMultiPart", serviceContext); } else { sendMailResult = dispatcher.runSync("sendMail", serviceContext); } } catch (Exception e) { Debug.logError(e, "Error send email:" + e.toString(), module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "CommonEmailSendError", UtilMisc.toMap("errorString", e.toString()), locale)); } if (ServiceUtil.isError(sendMailResult)) { return ServiceUtil.returnError(ServiceUtil.getErrorMessage(sendMailResult)); } result.put("messageWrapper", sendMailResult.get("messageWrapper")); result.put("body", bodyWriter.toString()); result.put("subject", subject); result.put("communicationEventId", sendMailResult.get("communicationEventId")); if (UtilValidate.isNotEmpty(orderId)) { result.put("orderId", orderId); } if (UtilValidate.isNotEmpty(custRequestId)) { result.put("custRequestId", custRequestId); } return result; }