Search in sources :

Example 16 with Site

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

the class ArticleController method getReferences.

/**
 * Get a list of references.
 * References to PLOS articles are supplemented with the absolute link to the journal site.
 *
 * @param site
 * @param request
 * @param xml
 * @return
 * @throws IOException
 */
private List<Reference> getReferences(Site site, HttpServletRequest request, byte[] xml) throws IOException {
    final Document document = parseXmlService.getDocument(new ByteArrayInputStream(xml));
    // do not supply Solr related link service now
    List<Reference> references = parseXmlService.parseArticleReferences(document, null);
    // invoke the Solr API once to resolve all journal keys
    List<String> plosDois = references.stream().map(ref -> ref.getDoi()).filter(doi -> inPlosJournal(doi)).collect(Collectors.toList());
    List<String> plosJournalKeys = doiToJournalResolutionService.getJournalKeysFromDois(plosDois, site);
    // store the link text from journal key to references.
    // since Reference is immutable, need to create a new list of new reference objects.
    Iterator<Reference> refs = references.iterator();
    Iterator<String> journalKeys = plosJournalKeys.iterator();
    List<Reference> processedReferences = new ArrayList<Reference>();
    while (refs.hasNext()) {
        Reference ref = refs.next();
        if (!inPlosJournal(ref.getDoi())) {
            processedReferences.add(ref);
            continue;
        }
        String key = journalKeys.next();
        if (Strings.isNullOrEmpty(key)) {
            processedReferences.add(ref);
            continue;
        }
        Reference.Builder builder = new Reference.Builder(ref);
        Reference refWithLink = builder.setFullArticleLink(getFullArticleLink(site, request, ref.getDoi(), key)).build();
        processedReferences.add(refWithLink);
    }
    references = processedReferences;
    return references;
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) URLDecoder(java.net.URLDecoder) URISyntaxException(java.net.URISyntaxException) OrcidAuthenticationTokenExpiredException(org.ambraproject.wombat.service.remote.orcid.OrcidAuthenticationTokenExpiredException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) 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) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Objects(java.util.Objects) Base64(java.util.Base64) List(java.util.List) ByteStreams(com.google.common.io.ByteStreams) SiteParam(org.ambraproject.wombat.config.site.SiteParam) OrcidApi(org.ambraproject.wombat.service.remote.orcid.OrcidApi) RequestMappingContextDictionary(org.ambraproject.wombat.config.site.RequestMappingContextDictionary) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) Link(org.ambraproject.wombat.config.site.url.Link) Controller(org.springframework.stereotype.Controller) ArticleTransformService(org.ambraproject.wombat.service.ArticleTransformService) ArrayList(java.util.ArrayList) RequestedDoiVersion(org.ambraproject.wombat.identity.RequestedDoiVersion) 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) ParseXmlService(org.ambraproject.wombat.service.ParseXmlService) OrcidAuthenticationTokenReusedException(org.ambraproject.wombat.service.remote.orcid.OrcidAuthenticationTokenReusedException) OutputStream(java.io.OutputStream) Reference(org.ambraproject.wombat.model.Reference) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) StringWriter(java.io.StringWriter) CorpusContentApi(org.ambraproject.wombat.service.remote.CorpusContentApi) IOException(java.io.IOException) DoiToJournalResolutionService(org.ambraproject.wombat.service.DoiToJournalResolutionService) HttpStatus(org.springframework.http.HttpStatus) ResponseEntity(org.springframework.http.ResponseEntity) RuntimeConfiguration(org.ambraproject.wombat.config.RuntimeConfiguration) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Reference(org.ambraproject.wombat.model.Reference) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document)

Example 17 with Site

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

the class WombatControllerTestConfig method siteSetDependency.

@Bean
public SiteSet siteSetDependency() {
    Site mockSite = mock(Site.class);
    ImmutableSet<Site> sitesSet = new ImmutableSet.Builder<Site>().add(mockSite).build();
    when(mockSite.getJournalKey()).thenReturn("daJournalKey");
    SiteRequestScheme mockRequestScheme = mock(SiteRequestScheme.class);
    when(mockRequestScheme.isForSite(any(HttpServletRequest.class))).thenReturn(true);
    when(mockSite.getRequestScheme()).thenReturn(mockRequestScheme);
    when(mockSite.toString()).thenReturn(MOCK_SITE_URL);
    SiteSet mockSet = mock(SiteSet.class);
    when(mockSet.getSites()).thenReturn(sitesSet);
    return mockSet;
}
Also used : Site(org.ambraproject.wombat.config.site.Site) HttpServletRequest(javax.servlet.http.HttpServletRequest) SiteRequestScheme(org.ambraproject.wombat.config.site.url.SiteRequestScheme) SiteSet(org.ambraproject.wombat.config.site.SiteSet) Bean(org.springframework.context.annotation.Bean)

Example 18 with Site

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

the class SolrSearchApiTest method testAddArticleLinks.

@Test
public void testAddArticleLinks() throws IOException {
    SolrSearchApi solrSearchApiForTest = new SearchApiForAddArticleLinksTest();
    Map<String, List<Map>> searchResults = new HashMap<>();
    List<Map> docs = new ArrayList<>(1);
    Map doc = new HashMap();
    List<String> crossPubbedJournals = new ArrayList<>(1);
    crossPubbedJournals.add("journal1Key");
    doc.put("id", "12345");
    doc.put("eissn", "123");
    docs.add(doc);
    searchResults.put("docs", docs);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("someContextPath");
    Site site = MockSiteUtil.getByUniqueJournalKey(siteSet, "journal2Key");
    Map<?, ?> actual = solrSearchApiForTest.addArticleLinks(searchResults, request, site, siteSet);
    List<Map> actualDocs = (List) actual.get("docs");
    assertEquals(actualDocs.size(), 1);
    Map actualDoc = (Map) actualDocs.get(0);
    assertEquals(actualDoc.get("id"), "12345");
    assertTrue(actualDoc.get("link").toString().endsWith("someContextPath/site1/article?id=12345"));
}
Also used : Site(org.ambraproject.wombat.config.site.Site) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test)

Example 19 with Site

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

the class SolrSearchApiImpl method initializeEIssnToJournalKeyMap.

/**
 * Initializes the eIssnToJournalKey map if necessary by calling rhino to get eISSNs for all journals.
 *
 * @param siteSet     set of all sites
 * @param currentSite site associated with the current request
 * @throws IOException
 */
@VisibleForTesting
protected synchronized void initializeEIssnToJournalKeyMap(SiteSet siteSet, Site currentSite) throws IOException {
    if (eIssnToJournalKey == null) {
        Map<String, String> mutable = new HashMap<>();
        for (Site site : siteSet.getSites()) {
            Map<String, String> rhinoResult = (Map<String, String>) articleApi.requestObject(ApiAddress.builder("journals").addToken(site.getJournalKey()).build(), Map.class);
            mutable.put(rhinoResult.get("eIssn"), site.getJournalKey());
        }
        eIssnToJournalKey = ImmutableMap.copyOf(mutable);
    }
}
Also used : Site(org.ambraproject.wombat.config.site.Site) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 20 with Site

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

the class SpringSecurityConfiguration method validateHostname.

private void validateHostname(HttpServletRequest request) {
    ClientEndpoint clientEndpoint = ClientEndpoint.get(request);
    Set<String> hostNames = siteSet.getSites().stream().map((Site site) -> site.getRequestScheme().getHostName()).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
    if (!hostNames.isEmpty() && !hostNames.contains(clientEndpoint.getHostname())) {
        throw new AccessDeniedException(String.format("Attempt to validate against foreign hostname %s. " + "Possible hijack attempt.", clientEndpoint.getHostname()));
    }
}
Also used : Site(org.ambraproject.wombat.config.site.Site) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Optional(java.util.Optional) ClientEndpoint(org.ambraproject.wombat.util.ClientEndpoint)

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