Search in sources :

Example 1 with EmailMessage

use of org.ambraproject.wombat.model.EmailMessage in project wombat by PLOS.

the class FeedbackController method receiveFeedback.

@RequestMapping(name = "feedbackPost", value = "/feedback", method = RequestMethod.POST)
public String receiveFeedback(HttpServletRequest request, HttpServletResponse response, Model model, @SiteParam Site site, @RequestParam("fromEmailAddress") String fromEmailAddress, @RequestParam("note") String note, @RequestParam("subject") String subject, @RequestParam("name") String name, @RequestParam("userId") String userId, @RequestParam(value = "authorPhone", required = false) String authorPhone, @RequestParam(value = "authorAffiliation", required = false) String authorAffiliation) throws IOException, MessagingException {
    validateFeedbackConfig(site);
    // Fill input parameters into model. (These can be used in two ways: in the generated email if all input is valid,
    // or in the form in case we need to display validation errors.)
    model.addAttribute("fromEmailAddress", fromEmailAddress);
    model.addAttribute("note", note);
    model.addAttribute("name", name);
    model.addAttribute("subject", subject);
    Set<String> errors = validateInput(fromEmailAddress, note, subject, name);
    if (applyValidation(response, model, errors)) {
        return serveFeedbackPage(model, site);
    }
    if (subject.isEmpty()) {
        model.addAttribute("subject", getFeedbackConfig(site).get("defaultSubject"));
    }
    model.addAttribute("id", userId);
    model.addAttribute("userInfo", formatUserInfo(request));
    if (honeypotService.checkHoneypot(request, authorPhone, authorAffiliation)) {
        return site + "/ftl/feedback/success";
    }
    Multipart content = freemarkerMailService.createContent(site, "feedback", model);
    String destinationAddress = (String) getFeedbackConfig(site).get("destination");
    EmailMessage message = EmailMessage.builder().addToEmailAddress(EmailMessage.createAddress(null, destinationAddress)).setSenderAddress(EmailMessage.createAddress(name, fromEmailAddress)).setSubject(subject).setContent(content).setEncoding(freeMarkerConfig.getConfiguration().getDefaultEncoding()).build();
    message.send(javaMailSender);
    return site + "/ftl/feedback/success";
}
Also used : Multipart(javax.mail.Multipart) EmailMessage(org.ambraproject.wombat.model.EmailMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with EmailMessage

use of org.ambraproject.wombat.model.EmailMessage in project wombat by PLOS.

the class ArticleController method emailArticle.

/**
 * @param model data passed in from the view
 * @param site  current site
 * @return path to the template
 * @throws IOException
 */
@RequestMapping(name = "emailPost", value = "/article/email", method = RequestMethod.POST)
public String emailArticle(HttpServletRequest request, HttpServletResponse response, Model model, @SiteParam Site site, RequestedDoiVersion articleId, @RequestParam("articleUri") String articleUri, @RequestParam("emailToAddresses") String emailToAddresses, @RequestParam("emailFrom") String emailFrom, @RequestParam("senderName") String senderName, @RequestParam("note") String note, @RequestParam(value = "authorPhone", required = false) String authorPhone, @RequestParam(value = "authorAffiliation", required = false) String authorAffiliation) throws IOException, MessagingException {
    requireNonemptyParameter(articleUri);
    model.addAttribute("emailToAddresses", emailToAddresses);
    model.addAttribute("emailFrom", emailFrom);
    model.addAttribute("senderName", senderName);
    model.addAttribute("note", note);
    model.addAttribute("articleUri", articleUri);
    List<InternetAddress> toAddresses = Splitter.on(CharMatcher.anyOf("\n\r")).omitEmptyStrings().splitToList(emailToAddresses).stream().map(email -> EmailMessage.createAddress(null, /*name*/
    email)).collect(Collectors.toList());
    Set<String> errors = validateEmailArticleInput(toAddresses, emailFrom, senderName);
    if (applyValidation(response, model, errors)) {
        return renderEmailThisArticle(request, model, site, articleId);
    }
    Map<String, ?> articleMetadata = articleMetadataFactory.get(site, articleId).validateVisibility("emailPost").getIngestionMetadata();
    String title = articleMetadata.get("title").toString();
    model.addAttribute("article", articleMetadata);
    model.addAttribute("journalName", site.getJournalName());
    if (honeypotService.checkHoneypot(request, authorPhone, authorAffiliation)) {
        response.setStatus(HttpStatus.CREATED.value());
        return site + "/ftl/article/emailSuccess";
    }
    Multipart content = freemarkerMailService.createContent(site, "emailThisArticle", model);
    EmailMessage message = EmailMessage.builder().addToEmailAddresses(toAddresses).setSenderAddress(EmailMessage.createAddress(senderName, emailFrom)).setSubject("An Article from " + site.getJournalName() + ": " + title).setContent(content).setEncoding(freeMarkerConfig.getConfiguration().getDefaultEncoding()).build();
    message.send(javaMailSender);
    response.setStatus(HttpStatus.CREATED.value());
    return site + "/ftl/article/emailSuccess";
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringUtils(org.apache.commons.lang.StringUtils) RequestParam(org.springframework.web.bind.annotation.RequestParam) URLDecoder(java.net.URLDecoder) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) CommentValidationService(org.ambraproject.wombat.service.CommentValidationService) URISyntaxException(java.net.URISyntaxException) OrcidAuthenticationTokenExpiredException(org.ambraproject.wombat.service.remote.orcid.OrcidAuthenticationTokenExpiredException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) MessagingException(javax.mail.MessagingException) FreeMarkerConfig(org.springframework.web.servlet.view.freemarker.FreeMarkerConfig) EmailValidator(org.apache.commons.validator.routines.EmailValidator) StatusLine(org.apache.http.StatusLine) EntityUtils(org.apache.http.util.EntityUtils) ArticlePointer(org.ambraproject.wombat.identity.ArticlePointer) Model(org.springframework.ui.Model) ByteArrayInputStream(java.io.ByteArrayInputStream) Gson(com.google.gson.Gson) Document(org.w3c.dom.Document) Map(java.util.Map) HoneypotService(org.ambraproject.wombat.service.HoneypotService) Splitter(com.google.common.base.Splitter) ArticleCommentFlag(org.ambraproject.wombat.model.ArticleCommentFlag) ImmutableMap(com.google.common.collect.ImmutableMap) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) FreemarkerMailService(org.ambraproject.wombat.service.FreemarkerMailService) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) Reader(java.io.Reader) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) UrlValidator(org.apache.commons.validator.routines.UrlValidator) Objects(java.util.Objects) DateTimeParseException(java.time.format.DateTimeParseException) Base64(java.util.Base64) List(java.util.List) CommentService(org.ambraproject.wombat.service.CommentService) LocalDate(java.time.LocalDate) ByteStreams(com.google.common.io.ByteStreams) SiteParam(org.ambraproject.wombat.config.site.SiteParam) NameValuePair(org.apache.http.NameValuePair) EmailMessage(org.ambraproject.wombat.model.EmailMessage) ServiceRequestException(org.ambraproject.wombat.service.remote.ServiceRequestException) OrcidApi(org.ambraproject.wombat.service.remote.orcid.OrcidApi) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) RequestMappingContextDictionary(org.ambraproject.wombat.config.site.RequestMappingContextDictionary) CachedRemoteService(org.ambraproject.wombat.service.remote.CachedRemoteService) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress) Multipart(javax.mail.Multipart) HashMap(java.util.HashMap) Link(org.ambraproject.wombat.config.site.url.Link) Controller(org.springframework.stereotype.Controller) JsonService(org.ambraproject.wombat.service.remote.JsonService) Function(java.util.function.Function) ArticleTransformService(org.ambraproject.wombat.service.ArticleTransformService) ArrayList(java.util.ArrayList) RequestedDoiVersion(org.ambraproject.wombat.identity.RequestedDoiVersion) InternetAddress(javax.mail.internet.InternetAddress) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) Site(org.ambraproject.wombat.config.site.Site) HttpServletRequest(javax.servlet.http.HttpServletRequest) ImmutableList(com.google.common.collect.ImmutableList) Charset(java.nio.charset.Charset) SiteSet(org.ambraproject.wombat.config.site.SiteSet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CitationDownloadService(org.ambraproject.wombat.service.CitationDownloadService) ParseXmlService(org.ambraproject.wombat.service.ParseXmlService) OrcidAuthenticationTokenReusedException(org.ambraproject.wombat.service.remote.orcid.OrcidAuthenticationTokenReusedException) OutputStream(java.io.OutputStream) Charsets(com.google.common.base.Charsets) Reference(org.ambraproject.wombat.model.Reference) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) CharMatcher(com.google.common.base.CharMatcher) HttpServletResponse(javax.servlet.http.HttpServletResponse) CorpusContentApi(org.ambraproject.wombat.service.remote.CorpusContentApi) SolrUndefinedException(org.ambraproject.wombat.service.remote.SolrUndefinedException) IOException(java.io.IOException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) DoiToJournalResolutionService(org.ambraproject.wombat.service.DoiToJournalResolutionService) ArticleApi(org.ambraproject.wombat.service.remote.ArticleApi) HttpStatus(org.springframework.http.HttpStatus) URLEncoder(java.net.URLEncoder) EntityNotFoundException(org.ambraproject.wombat.service.EntityNotFoundException) ArticleComment(org.ambraproject.wombat.model.ArticleComment) UserApi(org.ambraproject.wombat.service.remote.UserApi) HttpResponse(org.apache.http.HttpResponse) ResponseEntity(org.springframework.http.ResponseEntity) RuntimeConfiguration(org.ambraproject.wombat.config.RuntimeConfiguration) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) InputStream(java.io.InputStream) InternetAddress(javax.mail.internet.InternetAddress) Multipart(javax.mail.Multipart) EmailMessage(org.ambraproject.wombat.model.EmailMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Multipart (javax.mail.Multipart)2 EmailMessage (org.ambraproject.wombat.model.EmailMessage)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 CharMatcher (com.google.common.base.CharMatcher)1 Charsets (com.google.common.base.Charsets)1 Splitter (com.google.common.base.Splitter)1 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ByteStreams (com.google.common.io.ByteStreams)1 Gson (com.google.gson.Gson)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Reader (java.io.Reader)1 Serializable (java.io.Serializable)1 StringWriter (java.io.StringWriter)1 URISyntaxException (java.net.URISyntaxException)1 URLDecoder (java.net.URLDecoder)1