Java tutorial
/** * Copyright 2012 meltmedia * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.meltmedia.cadmium.email.jersey; import com.meltmedia.cadmium.captcha.CaptchaRequest; import com.meltmedia.cadmium.captcha.CaptchaValidator; import com.meltmedia.cadmium.core.CadmiumApiEndpoint; import com.meltmedia.cadmium.core.ContentService; import com.meltmedia.cadmium.email.EmailException; import com.meltmedia.cadmium.email.VelocityHtmlTextEmail; import com.meltmedia.cadmium.email.config.EmailComponentConfiguration; import com.meltmedia.cadmium.email.config.EmailComponentConfiguration.Field; import com.meltmedia.cadmium.email.internal.EmailServiceImpl; import org.apache.commons.io.FileUtils; import org.eclipse.jgit.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.*; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import java.io.*; @CadmiumApiEndpoint @Path("/email") public class EmailResource { private Logger log = LoggerFactory.getLogger(getClass()); @Inject private EmailServiceImpl emailService; @Inject private ContentService contentService; public EmailResource() { log.debug("Initialized EmailResource..."); } @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) public Response emailThisPage(@Context HttpServletRequest request, @FormParam(Constants.DIR) String dir, final MultivaluedMap<String, String> formData) { log.info("Entering Email This Method"); VelocityHtmlTextEmail email = new VelocityHtmlTextEmail(); Yaml yamlParser; // Setting up template location/files if (dir != null) { File absoluteTemplateDir = new File(contentService.getContentRoot(), "META-INF"); absoluteTemplateDir = new File(absoluteTemplateDir, dir); File textTemplateFile = new File(absoluteTemplateDir, Constants.TEMPLATE_NAME + ".txt"); log.info("textTemplateFile: {}", textTemplateFile.getPath()); File htmlTemplateFile = new File(absoluteTemplateDir, Constants.TEMPLATE_NAME + ".html"); log.info("htmlTemplateFile: {}", htmlTemplateFile.getPath()); File componentConfig = new File(absoluteTemplateDir, Constants.CONFIG_NAME); if (textTemplateFile.exists() && htmlTemplateFile.exists() && componentConfig.exists()) { yamlParser = new Yaml(); try { EmailComponentConfiguration config = yamlParser .loadAs(FileUtils.readFileToString(componentConfig), EmailComponentConfiguration.class); CaptchaRequest captcha = new CaptchaRequest() { @Override public String getRecaptcha_challenge_field() { return formData.getFirst(CaptchaValidator.CHALLENGE_FIELD_NAME); } @Override public String getRecaptcha_response_field() { return formData.getFirst(CaptchaValidator.RESPONSE_FIELD_NAME); } }; //Check captcha if configured if (!emailService.validateCaptcha(request, captcha, config, log)) { throw new ValidationException("Incorrect captcha response", new ValidationError[] { new ValidationError("captcha", "Incorrect captcha response") }); } EmailFormValidator.validate(formData, config, contentService); email.addTo(getFieldValueWithOverride("toAddress", config.getToAddress(), formData)); String fromAddress = emailService.getFromAddress(config.getFromAddress()); log.info("Setting from address {}", fromAddress); email.setFrom(fromAddress); email.setSubject(getFieldValueWithOverride("subject", config.getSubject(), formData)); email.setProperty("subject", getFieldValueWithOverride("subject", config.getSubject(), formData)); // Set HTML Template email.setHtml(readFromFile(htmlTemplateFile.getAbsolutePath())); // Set Text Template email.setText(readFromFile(textTemplateFile.getAbsolutePath())); // Populate template properties for (Field field : config.getFields()) { if ("replyTo".equals(field.name)) { String value = field.getValue(request, formData); if (!StringUtils.isEmptyOrNull(value)) { email.setReplyTo(value); } } if ("cc".equals(field.name)) { String value = field.getValue(request, formData); if (!StringUtils.isEmptyOrNull(value)) { email.addCC(value); } } if ("bcc".equals(field.name)) { String value = field.getValue(request, formData); if (!StringUtils.isEmptyOrNull(value)) { email.addBCC(value); } } email.setProperty(field.name, field.getValue(request, formData)); } // Set Properties String toName = getFieldValueWithOverride("toName", config.getToName(), formData); String fromName = getFieldValueWithOverride("fromName", config.getFromName(), formData); if (toName != null) { email.setProperty(Constants.TO_NAME, toName); } if (fromName != null) { email.setProperty(Constants.FROM_NAME, fromName); } // Send Email log.debug("Before Sending Email"); emailService.send(email); log.debug("After Sending Email"); } catch (EmailException e) { log.info("EmailException Caught " + e.getMessage(), e); return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); } catch (ValidationException e) { log.info("ValidationException Caught"); if (e.getErrors() != null && e.getErrors()[0] != null) { log.info("First Error {}", e.getErrors()[0].getMessage()); } else { log.info("No error messages were set for this Validation Exception."); } return Response.status(Response.Status.BAD_REQUEST).entity(e.getErrors()).build(); } catch (IOException e) { return Response.status(Response.Status.BAD_REQUEST).entity("Unable to load Configuration") .build(); } return Response.ok().build(); } else { log.info("Couldn't Find Email Templates"); return Response.status(Response.Status.BAD_REQUEST).entity("Invalid Template Location").build(); } } else return Response.status(Response.Status.BAD_REQUEST).entity("Invalid Template Location").build(); } /* * Reads data from a given file */ public String readFromFile(String fileName) { String content = null; BufferedReader br = null; try { File inFile = new File(fileName); br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile))); content = ""; while (br.ready()) { content += br.readLine(); } br.close(); } catch (FileNotFoundException ex) { log.error("Failed to find file.", ex); } catch (IOException ex) { log.error("Failed to read file.", ex); } finally { if (br != null) { try { br.close(); } catch (Exception e) { } } } return content; } private String getFieldValueWithOverride(String name, String overrideValue, MultivaluedMap<String, String> formData) { return StringUtils.isEmptyOrNull(overrideValue) ? formData.get(name).get(0) : overrideValue; } }