Search in sources :

Example 1 with Site

use of org.ambraproject.wombat.config.site.Site 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 2 with Site

use of org.ambraproject.wombat.config.site.Site in project wombat by PLOS.

the class SearchControllerTest method testCommonParams.

@Test
public void testCommonParams() throws IOException {
    Map<String, List<String>> params = new HashMap<>();
    params.put("page", Collections.singletonList("7"));
    params.put("filterSubjects", Arrays.asList("subject1", "subject2"));
    Site site = MockSiteUtil.getByUniqueJournalKey(siteSet, "journal1Key");
    SearchController.CommonParams commonParams = new SearchController.CommonParams(siteSet, site);
    commonParams.parseParams(params, false);
    // Default results per page should be 15
    assertEquals(commonParams.start, 90);
    assertEquals(commonParams.sortOrder, SolrSearchApiImpl.SolrSortOrder.RELEVANCE);
    assertEquals(commonParams.dateRange, SolrSearchApiImpl.SolrEnumeratedDateRange.ALL_TIME);
    assertTrue(commonParams.articleTypes.isEmpty());
    assertTrue(commonParams.journalKeys.isEmpty());
    assertTrue(commonParams.filterJournalNames.isEmpty());
    assertEquals(commonParams.subjectList.size(), 2);
    assertEquals(commonParams.subjectList.get(0), "subject1");
    assertEquals(commonParams.subjectList.get(1), "subject2");
    assertTrue(commonParams.isFiltered);
}
Also used : Site(org.ambraproject.wombat.config.site.Site) HashMap(java.util.HashMap) List(java.util.List) Test(org.testng.annotations.Test)

Example 3 with Site

use of org.ambraproject.wombat.config.site.Site in project wombat by PLOS.

the class TestTheme method testResolveForeignJournalKey.

@Test(dataProvider = "booleans")
public void testResolveForeignJournalKey(final boolean useJournalKeyMap) throws Exception {
    SiteRequestScheme dummyScheme = SiteRequestScheme.builder().build();
    Theme homeTheme = new StubTheme("homeTheme", "homeJournal") {

        @Override
        protected Map<String, Object> getJournalConfigMap() {
            Map<String, Object> map = super.getJournalConfigMap();
            if (useJournalKeyMap) {
                ImmutableMap<String, String> otherJournals = ImmutableMap.of("targetJournal", "targetSite");
                map.put("otherJournals", otherJournals);
            }
            return map;
        }
    };
    Site homeSite = new Site("homeSite", homeTheme, dummyScheme, "");
    Theme targetTheme = new StubTheme("targetTheme", "targetJournal");
    Site targetSite = new Site("targetSite", targetTheme, dummyScheme, "");
    SiteSet siteSet = new SiteSet(ImmutableList.of(homeSite, targetSite));
    Site resolved = homeTheme.resolveForeignJournalKey(siteSet, "targetJournal");
    assertEquals(resolved, targetSite);
}
Also used : Site(org.ambraproject.wombat.config.site.Site) SiteRequestScheme(org.ambraproject.wombat.config.site.url.SiteRequestScheme) SiteSet(org.ambraproject.wombat.config.site.SiteSet) Test(org.testng.annotations.Test)

Example 4 with Site

use of org.ambraproject.wombat.config.site.Site in project wombat by PLOS.

the class DoiToJournalResolutionService method getJournalKeysFromDois.

public List<String> getJournalKeysFromDois(List<String> dois, Site site) throws IOException {
    if (dois.isEmpty()) {
        return new ArrayList<String>();
    }
    List<String> quoted = dois.stream().map(doi -> {
        return "\"" + QueryParser.escape(doi) + "\"";
    }).collect(Collectors.toList());
    ArticleSearchQuery explicitDoiSearchQuery = ArticleSearchQuery.builder().setSimple(false).setRows(dois.size()).setQuery("id:(" + String.join(" OR ", quoted) + ")").setFields(ImmutableList.of("journal_key", "journal_name")).build();
    List<Map<String, Object>> docs = solrSearchApi.search(explicitDoiSearchQuery).getDocs();
    Map<String, String> keysMap = new HashMap<String, String>();
    docs.forEach(data -> {
        keysMap.put((String) data.get("id"), (String) data.get("journal_key"));
    });
    List<String> journalKeys = dois.stream().map(id -> {
        if (keysMap.containsKey(id)) {
            return keysMap.get(id);
        } else {
            return "";
        }
    }).collect(Collectors.toList());
    return journalKeys;
}
Also used : Logger(org.slf4j.Logger) ArticleSearchQuery(org.ambraproject.wombat.service.remote.ArticleSearchQuery) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) SolrSearchApi(org.ambraproject.wombat.service.remote.SolrSearchApi) Site(org.ambraproject.wombat.config.site.Site) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Map(java.util.Map) ArticleSearchQuery(org.ambraproject.wombat.service.remote.ArticleSearchQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with Site

use of org.ambraproject.wombat.config.site.Site in project wombat by PLOS.

the class TopLevelLockssManifestService method printPage.

/**
 * @param request the HTTP request whose response we are building
 * @param writer  the buffer into which to print the response
 */
private void printPage(HttpServletRequest request, PrintWriter writer) {
    for (String line : PREAMBLE) {
        writer.println(line);
    }
    for (Site site : getSitesByDistinctJournalKey()) {
        String journalName = site.getJournalName();
        Link.Factory.PatternBuilder pattern = Link.toAbsoluteAddress(site).toPattern(requestMappingContextDictionary, "lockssYears");
        Link manifestLink;
        try {
            manifestLink = pattern.build();
        } catch (Link.PatternNotFoundException e) {
            // omit link for this site
            continue;
        }
        String manifestUrl = manifestLink.get(request);
        writer.println(String.format("        <li><a href=\"%s\">%s</a></li>", manifestUrl, journalName));
    }
    for (String line : POSTAMBLE) {
        writer.println(line);
    }
}
Also used : Site(org.ambraproject.wombat.config.site.Site) Link(org.ambraproject.wombat.config.site.url.Link)

Aggregations

Site (org.ambraproject.wombat.config.site.Site)27 HashMap (java.util.HashMap)11 Map (java.util.Map)10 ArrayList (java.util.ArrayList)9 List (java.util.List)7 SiteRequestScheme (org.ambraproject.wombat.config.site.url.SiteRequestScheme)7 Test (org.junit.Test)7 Link (org.ambraproject.wombat.config.site.url.Link)6 ImmutableList (com.google.common.collect.ImmutableList)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 SiteSet (org.ambraproject.wombat.config.site.SiteSet)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 StringWriter (java.io.StringWriter)4 ArticlePointer (org.ambraproject.wombat.identity.ArticlePointer)4 RequestedDoiVersion (org.ambraproject.wombat.identity.RequestedDoiVersion)4 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 RequestMappingContextDictionary (org.ambraproject.wombat.config.site.RequestMappingContextDictionary)3 Theme (org.ambraproject.wombat.config.theme.Theme)3 Logger (org.slf4j.Logger)3