Search in sources :

Example 1 with RequestedDoiVersion

use of org.ambraproject.wombat.identity.RequestedDoiVersion in project wombat by PLOS.

the class ArticleController method renderArticleCommentTree.

/**
 * Serves a request for an expanded view of a single comment and any replies.
 *
 * @param model      data to pass to the view
 * @param site       current site
 * @param commentDoi specifies the comment
 * @return path to the template
 * @throws IOException
 */
@RequestMapping(name = "articleCommentTree", value = "/article/comment")
public String renderArticleCommentTree(HttpServletRequest request, Model model, @SiteParam Site site, @RequestParam("id") String commentDoi) throws IOException {
    requireNonemptyParameter(commentDoi);
    Map<String, Object> comment;
    try {
        comment = commentService.getComment(commentDoi, site);
    } catch (CommentService.CommentNotFoundException e) {
        throw new NotFoundException(e);
    } catch (UserApi.UserApiException e) {
        log.error(e.getMessage(), e);
        model.addAttribute("userApiError", e);
        // Get a copy of the comment that is not populated with userApi data.
        // This articleApi call is redundant to one that commentService.getComment would have made before throwing.
        // TODO: Prevent extra articleApi call
        comment = getComment(commentDoi);
    }
    RequestedDoiVersion doi = RequestedDoiVersion.of(getParentArticleDoiFromComment(comment));
    articleMetadataFactory.get(site, doi).validateVisibility("articleCommentTree").populate(request, model);
    model.addAttribute("comment", comment);
    addCommentAvailability(model);
    return site + "/ftl/article/comment/comment";
}
Also used : CommentService(org.ambraproject.wombat.service.CommentService) RequestedDoiVersion(org.ambraproject.wombat.identity.RequestedDoiVersion) EntityNotFoundException(org.ambraproject.wombat.service.EntityNotFoundException) UserApi(org.ambraproject.wombat.service.remote.UserApi) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with RequestedDoiVersion

use of org.ambraproject.wombat.identity.RequestedDoiVersion 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)

Example 3 with RequestedDoiVersion

use of org.ambraproject.wombat.identity.RequestedDoiVersion in project wombat by PLOS.

the class ArticleResolutionServiceTest method testToParentIngestionNotAnAsset.

@Test(expected = NotFoundException.class)
public void testToParentIngestionNotAnAsset() throws IOException {
    Map<String, Object> doiOverview = makeSampleDoiOverview();
    doiOverview.put("type", "not_an_asset_type");
    when(articleApi.requestObject(any(), eq(Map.class))).thenReturn(doiOverview);
    RequestedDoiVersion assetId = RequestedDoiVersion.of("info:doi/10.1371/journal.pcbi.1002012.g002");
    articleResolutionService.toParentIngestion(assetId);
}
Also used : RequestedDoiVersion(org.ambraproject.wombat.identity.RequestedDoiVersion) Map(java.util.Map) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) Test(org.junit.Test)

Example 4 with RequestedDoiVersion

use of org.ambraproject.wombat.identity.RequestedDoiVersion in project wombat by PLOS.

the class FigurePageController method renderFigurePage.

/**
 * Serve a page displaying a single figure.
 */
@RequestMapping(name = "figurePage", value = "/article/figure")
public String renderFigurePage(Model model, @SiteParam Site site, RequestedDoiVersion figureId) throws IOException {
    AssetPointer assetPointer = articleResolutionService.toParentIngestion(figureId);
    model.addAttribute("figurePtr", assetPointer.asParameterMap());
    ArticlePointer articlePointer = assetPointer.getParentArticle();
    RequestedDoiVersion articleId = figureId.forDoi(articlePointer.getDoi());
    ArticleMetadata articleMetadata = articleMetadataFactory.get(site, articleId, articlePointer);
    model.addAttribute("article", articleMetadata.getIngestionMetadata());
    Map<String, ?> figureMetadata = articleMetadata.getFigureView().stream().filter((Map<String, ?> fig) -> fig.get("doi").equals(assetPointer.getAssetDoi())).collect(MoreCollectors.onlyElement());
    model.addAttribute("figure", figureMetadata);
    String descriptionHtml = getDescriptionHtml(site, articlePointer, figureMetadata);
    model.addAttribute("descriptionHtml", descriptionHtml);
    return site + "/ftl/article/figure";
}
Also used : RequestedDoiVersion(org.ambraproject.wombat.identity.RequestedDoiVersion) AssetPointer(org.ambraproject.wombat.identity.AssetPointer) ArticlePointer(org.ambraproject.wombat.identity.ArticlePointer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with RequestedDoiVersion

use of org.ambraproject.wombat.identity.RequestedDoiVersion in project wombat by PLOS.

the class CommentController method renderArticleCommentTree.

/**
 * Serves a request for an expanded view of a single comment and any replies.
 *
 * @param model      data to pass to the view
 * @param site       current site
 * @param commentDoi specifies the comment
 * @return path to the template
 * @throws IOException
 */
@RequestMapping(name = "articleCommentTree", value = "/article/comment")
public String renderArticleCommentTree(HttpServletRequest request, Model model, @SiteParam Site site, @RequestParam("id") String commentDoi) throws IOException {
    requireNonemptyParameter(commentDoi);
    Map<String, Object> comment;
    try {
        comment = commentService.getComment(commentDoi, site);
    } catch (CommentService.CommentNotFoundException e) {
        throw new NotFoundException(e);
    } catch (UserApi.UserApiException e) {
        log.error(e.getMessage(), e);
        model.addAttribute("userApiError", e);
        // Get a copy of the comment that is not populated with userApi data.
        // This articleApi call is redundant to one that commentService.getComment would have made before throwing.
        // TODO: Prevent extra articleApi call
        comment = getComment(commentDoi);
    }
    RequestedDoiVersion doi = RequestedDoiVersion.of(getParentArticleDoiFromComment(comment));
    articleMetadataFactory.get(site, doi).validateVisibility("articleCommentTree").populate(request, model);
    model.addAttribute("comment", comment);
    addCommentAvailability(model);
    return site + "/ftl/article/comment/comment";
}
Also used : CommentService(org.ambraproject.wombat.service.CommentService) RequestedDoiVersion(org.ambraproject.wombat.identity.RequestedDoiVersion) UserApi(org.ambraproject.wombat.service.remote.UserApi) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RequestedDoiVersion (org.ambraproject.wombat.identity.RequestedDoiVersion)12 Map (java.util.Map)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)4 ArticlePointer (org.ambraproject.wombat.identity.ArticlePointer)4 Test (org.junit.Test)4 AssetPointer (org.ambraproject.wombat.identity.AssetPointer)3 CommentService (org.ambraproject.wombat.service.CommentService)3 EntityNotFoundException (org.ambraproject.wombat.service.EntityNotFoundException)3 UserApi (org.ambraproject.wombat.service.remote.UserApi)3 ImmutableMap (com.google.common.collect.ImmutableMap)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 ByteStreams (com.google.common.io.ByteStreams)1 Gson (com.google.gson.Gson)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1