📄 emailservices.java
字号:
body = httpClient.post(); } catch (HttpClientException e) { Debug.logWarning(e, module); return ServiceUtil.returnError("Error getting content: " + e.toString()); } context.put("body", body); Map result = sendMail(ctx, context); result.put("body", body); return result; } /** * 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 serviceContext Map containing the input parameters *@return Map with the result of the service, the output parameters */ public static Map sendMailFromScreen(DispatchContext dctx, Map serviceContext) { LocalDispatcher dispatcher = dctx.getDispatcher(); String webSiteId = (String) serviceContext.remove("webSiteId"); String bodyText = (String) serviceContext.remove("bodyText"); String bodyScreenUri = (String) serviceContext.remove("bodyScreenUri"); String xslfoAttachScreenLocation = (String) serviceContext.remove("xslfoAttachScreenLocation"); Map bodyParameters = (Map) serviceContext.remove("bodyParameters"); String partyId = (String) bodyParameters.get("partyId"); NotificationServices.setBaseUrl(dctx.getDelegator(), webSiteId, bodyParameters); StringWriter bodyWriter = new StringWriter(); MapStack screenContext = MapStack.create(); ScreenRenderer screens = new ScreenRenderer(bodyWriter, screenContext, htmlScreenRenderer); screens.populateContextForService(dctx, bodyParameters); screenContext.putAll(bodyParameters); if (bodyScreenUri != null) { try { screens.render(bodyScreenUri); } catch (GeneralException e) { String errMsg = "Error rendering screen for email: " + e.toString(); Debug.logError(e, errMsg, module); return ServiceUtil.returnError(errMsg); } catch (IOException e) { String errMsg = "Error rendering screen for email: " + e.toString(); Debug.logError(e, errMsg, module); return ServiceUtil.returnError(errMsg); } catch (SAXException e) { String errMsg = "Error rendering screen for email: " + e.toString(); Debug.logError(e, errMsg, module); return ServiceUtil.returnError(errMsg); } catch (ParserConfigurationException e) { String errMsg = "Error rendering screen for email: " + e.toString(); Debug.logError(e, errMsg, module); return ServiceUtil.returnError(errMsg); } } boolean isMultiPart = false; // check if attachement screen location passed in if (UtilValidate.isNotEmpty(xslfoAttachScreenLocation)) { isMultiPart = true; // start processing fo pdf attachment try { Writer writer = new StringWriter(); MapStack screenContextAtt = MapStack.create(); // substitute the freemarker variables... ScreenRenderer screensAtt = new ScreenRenderer(writer, screenContext, htmlScreenRenderer); 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); } */ // configure logging for the FOP Logger logger = new Log4JLogger(Debug.getLogger(module)); MessageHandler.setScreenLogger(logger); // load the FOP driver Driver driver = new Driver(); driver.setRenderer(Driver.RENDER_PDF); driver.setLogger(logger); // read the XSL-FO XML into the W3 Document Document xslfo = UtilXml.readXmlDocument(writer.toString()); // create the in/output stream for the generation ByteArrayOutputStream baos = new ByteArrayOutputStream(); driver.setOutputStream(baos); driver.setInputSource(new DocumentInputSource(xslfo)); // and generate the PDF driver.run(); FopImageFactory.resetCache(); baos.flush(); baos.close(); /* try { // save generated pdf file for debugging FileOutputStream fos = new FileOutputStream(new java.io.File("/tmp/file2.pdf")); baos.writeTo(fos); fos.close(); } catch (IOException e) { Debug.logError(e, "Couldn't save xsl-fo pdf debug file: " + e.toString(), module); } */ // store in the list of maps for sendmail.... List bodyParts = FastList.newInstance(); if (bodyText != null) { bodyText = FlexibleStringExpander.expandString(bodyText, screenContext, (Locale) screenContext.get("locale")); bodyParts.add(UtilMisc.toMap("content", bodyText, "type", "text/html")); } else { bodyParts.add(UtilMisc.toMap("content", bodyWriter.toString(), "type", "text/html")); } bodyParts.add(UtilMisc.toMap("content", baos.toByteArray(), "type", "application/pdf", "filename", "Details.pdf")); serviceContext.put("bodyParts", bodyParts); } catch (GeneralException ge) { String errMsg = "Error rendering PDF attachment for email: " + ge.toString(); Debug.logError(ge, errMsg, module); return ServiceUtil.returnError(errMsg); } catch (IOException ie) { String errMsg = "Error rendering PDF attachment for email: " + ie.toString(); Debug.logError(ie, errMsg, module); return ServiceUtil.returnError(errMsg); } catch (FOPException fe) { String errMsg = "Error rendering PDF attachment for email: " + fe.toString(); Debug.logError(fe, errMsg, module); return ServiceUtil.returnError(errMsg); } catch (SAXException se) { String errMsg = "Error rendering PDF attachment for email: " + se.toString(); Debug.logError(se, errMsg, module); return ServiceUtil.returnError(errMsg); } catch (ParserConfigurationException pe) { String errMsg = "Error rendering PDF attachment for email: " + pe.toString(); Debug.logError(pe, errMsg, module); return ServiceUtil.returnError(errMsg); } } else { isMultiPart = false; // store body and type for single part message in the context. if (bodyText != null) { bodyText = FlexibleStringExpander.expandString(bodyText, screenContext, (Locale) screenContext.get("locale")); serviceContext.put("body", bodyText); } else { serviceContext.put("body", bodyWriter.toString()); } 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) screenContext.get("locale")); serviceContext.put("subject", subject); serviceContext.put("partyId", partyId); if (Debug.verboseOn()) Debug.logVerbose("sendMailFromScreen sendMail context: " + serviceContext, module); Map result = ServiceUtil.returnSuccess(); try { if (isMultiPart) { dispatcher.runSync("sendMailMultiPart", serviceContext); } else { dispatcher.runSync("sendMail", serviceContext); } } catch (Exception e) { String errMsg = "Error send email :" + e.toString(); Debug.logError(e, errMsg, module); return ServiceUtil.returnError(errMsg); } result.put("body", bodyWriter.toString()); return result; } /** * Store email as communication event *@param dctx The DispatchContext that this service is operating in *@param serviceContext Map containing the input parameters *@return Map with the result of the service, the output parameters */ public static Map storeEmailAsCommunication(DispatchContext dctx, Map serviceContext) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue) serviceContext.get("userLogin"); String subject = (String) serviceContext.get("subject"); String body = (String) serviceContext.get("body"); String partyId = (String) serviceContext.get("partyId"); String communicationEventId = (String) serviceContext.get("communicationEventId"); String contentType = (String) serviceContext.get("contentType"); // only create a new communication event if the email is not already associated with one if (communicationEventId == null) { String partyIdFrom = (String) userLogin.get("partyId"); Map commEventMap = FastMap.newInstance(); commEventMap.put("communicationEventTypeId", "EMAIL_COMMUNICATION"); commEventMap.put("statusId", "COM_COMPLETE"); commEventMap.put("contactMechTypeId", "EMAIL_ADDRESS"); commEventMap.put("partyIdFrom", partyIdFrom); commEventMap.put("partyIdTo", partyId); commEventMap.put("subject", subject); commEventMap.put("content", body); commEventMap.put("userLogin", userLogin); commEventMap.put("contentMimeTypeId", contentType); try { dispatcher.runSync("createCommunicationEvent", commEventMap); } catch (Exception e) { Debug.logError(e, "Cannot store email as communication event", module); return ServiceUtil.returnError("Cannot store email as communication event; see logs"); } } return ServiceUtil.returnSuccess(); } /** class to create a file in memory required for sending as an attachment */ public static class StringDataSource implements DataSource { private String contentType; private ByteArrayOutputStream contentArray; public StringDataSource(String content, String contentType) throws IOException { this.contentType = contentType; contentArray = new ByteArrayOutputStream(); contentArray.write(content.getBytes("iso-8859-1")); contentArray.flush(); contentArray.close(); } public String getContentType() { return contentType == null ? "application/octet-stream" : contentType; } public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(contentArray.toByteArray()); } public String getName() { return "stringDatasource"; } public OutputStream getOutputStream() throws IOException { throw new IOException("Cannot write to this read-only resource"); } } /** class to create a file in memory required for sending as an attachment */ public static class ByteArrayDataSource implements DataSource { private String contentType; private byte[] contentArray; public ByteArrayDataSource(byte[] content, String contentType) throws IOException { this.contentType = contentType; this.contentArray = content; } public String getContentType() { return contentType == null ? "application/octet-stream" : contentType; } public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(contentArray); } public String getName() { return "ByteArrayDataSource"; } public OutputStream getOutputStream() throws IOException { throw new IOException("Cannot write to this read-only resource"); } } /* * Helper method to retrieve the party information from the first email address of the Address[] specified. */ private static Map getParyInfoFromEmailAddress(Address [] addresses, GenericValue userLogin, LocalDispatcher dispatcher) throws GenericServiceException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -