Search in sources :

Example 6 with ApiAddress

use of org.ambraproject.wombat.service.remote.ApiAddress in project wombat by PLOS.

the class ArticleResolutionService method toParentIngestion.

public AssetPointer toParentIngestion(RequestedDoiVersion assetId) throws IOException {
    Map<String, ?> doiOverview;
    try {
        ApiAddress apiAddress = ApiAddress.builder("dois").embedDoi(assetId.getDoi()).build();
        doiOverview = articleApi.requestObject(apiAddress, Map.class);
    } catch (EntityNotFoundException e) {
        throw new NotFoundException(e);
    }
    String type = (String) doiOverview.get("type");
    if (!ARTICLE_ASSET_TYPES.contains(type)) {
        throw new NotFoundException("Not an article asset: " + assetId);
    }
    Map<String, ?> parentArticle = (Map<String, ?>) doiOverview.get("article");
    ArticlePointer parentArticlePtr = resolve(assetId, parentArticle);
    String canonicalAssetDoi = (String) doiOverview.get("doi");
    return new AssetPointer(canonicalAssetDoi, parentArticlePtr);
}
Also used : AssetPointer(org.ambraproject.wombat.identity.AssetPointer) NotFoundException(org.ambraproject.wombat.controller.NotFoundException) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress) ArticlePointer(org.ambraproject.wombat.identity.ArticlePointer)

Example 7 with ApiAddress

use of org.ambraproject.wombat.service.remote.ApiAddress in project wombat by PLOS.

the class AlertService method removeSubjectAlert.

/**
 * Remove the subject alert for the loggedin user for the journal.
 * After removing the subject, if other subjects exist in the alert object for the journal,
 * then it uses PUT individuals/{userId}/alerts/{alertId} to modify the alert object,
 * otherwise it uses DELETE individuals/{userId}/alerts/{alertId} to remove the
 * alert object.
 *
 * @param authId The authentication ID of the logged in user.
 * @param journalKey The journal key.
 * @param subjectName The subject to remove alert for.
 * @throws IOException
 */
public void removeSubjectAlert(String authId, String journalKey, String subjectName) throws IOException {
    String userId = userApi.getUserIdFromAuthId(authId);
    List<Alert> alerts = fetchAlerts(userId);
    Alert alert = findMatchingAlert(alerts, journalKey).orElseThrow(() -> new AlertException("no subject alert found"));
    RemoveResult result = removeSubjectFromAlert(alert, subjectName);
    if (result == RemoveResult.NOT_FOUND) {
        throw new AlertException("matching subject alert not found");
    }
    String alertId = alert.getId().toString();
    ApiAddress address = buildAlertAddress(userId).addToken(alertId).build();
    if (result == RemoveResult.EMPTY_AFTER_REMOVE) {
        userApi.deleteObject(address);
    } else {
        userApi.putObject(address, alert);
    }
}
Also used : Alert(org.plos.ned_client.model.Alert) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress)

Example 8 with ApiAddress

use of org.ambraproject.wombat.service.remote.ApiAddress in project wombat by PLOS.

the class ArticleController method receiveNewComment.

/**
 * @param parentArticleDoi null if a reply to another comment
 * @param parentCommentUri null if a direct reply to an article
 */
@RequestMapping(name = "postComment", method = RequestMethod.POST, value = "/article/comments/new")
@ResponseBody
public Object receiveNewComment(HttpServletRequest request, @SiteParam Site site, @RequestParam("commentTitle") String commentTitle, @RequestParam("comment") String commentBody, @RequestParam("isCompetingInterest") boolean hasCompetingInterest, @RequestParam(value = "authorEmailAddress", required = false) String authorEmailAddress, @RequestParam(value = "authorName", required = false) String authorName, @RequestParam(value = "authorPhone", required = false) String authorPhone, @RequestParam(value = "authorAffiliation", required = false) String authorAffiliation, @RequestParam(value = "ciStatement", required = false) String ciStatement, @RequestParam(value = "target", required = false) String parentArticleDoi, @RequestParam(value = "inReplyTo", required = false) String parentCommentUri) throws IOException {
    if (honeypotService.checkHoneypot(request, authorPhone, authorAffiliation)) {
        return ImmutableMap.of("status", "success");
    }
    checkCommentsAreEnabled();
    Map<String, Object> validationErrors = commentValidationService.validateComment(site, commentTitle, commentBody, hasCompetingInterest, ciStatement);
    if (!validationErrors.isEmpty()) {
        return ImmutableMap.of("validationErrors", validationErrors);
    }
    if (parentArticleDoi == null) {
        Map<String, Object> comment = getComment(parentCommentUri);
        parentArticleDoi = getParentArticleDoiFromComment(comment);
    }
    ApiAddress address = ApiAddress.builder("articles").embedDoi(parentArticleDoi).addToken("comments").build();
    String authId = request.getRemoteUser();
    final String creatorUserId = authId == null ? null : userApi.getUserIdFromAuthId(authId);
    ArticleComment comment = new ArticleComment(parentArticleDoi, creatorUserId, parentCommentUri, commentTitle, commentBody, ciStatement, authorEmailAddress, authorName);
    HttpResponse response = articleApi.postObject(address, comment);
    String responseJson = EntityUtils.toString(response.getEntity());
    Map<String, Object> commentJson = gson.fromJson(responseJson, HashMap.class);
    return ImmutableMap.of("createdCommentUri", commentJson.get("commentUri"));
}
Also used : ArticleComment(org.ambraproject.wombat.model.ArticleComment) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 9 with ApiAddress

use of org.ambraproject.wombat.service.remote.ApiAddress in project wombat by PLOS.

the class ArticleControllerTest method testRenderArticleShouldSucceed.

/**
 * Test successful rendering of an article.
 *
 * @throws URISyntaxException if an invalid URI
 * @throws Exception if failed to render article
 */
@Test
public void testRenderArticleShouldSucceed() throws URISyntaxException, Exception {
    when(articleMetadata.validateVisibility(anyString())).thenReturn(articleMetadata);
    when(articleMetadata.populate(any(), any())).thenReturn(articleMetadata);
    when(articleMetadata.fillAmendments(any())).thenReturn(articleMetadata);
    doReturn(articleMetadata).when(articleMetadataFactory).newInstance(any(), any(), any(), any(), any(), any());
    RequestedDoiVersion expectedRequestedDoi = RequestedDoiVersion.of(EXPECTED_DOI);
    ArticlePointer expectedArticlePointer = new ArticlePointer(expectedRequestedDoi, EXPECTED_DOI, EXPECTED_INGESTION_NUMBER, expectedRequestedDoi.getRevisionNumber());
    when(articleResolutionService.toIngestion(expectedRequestedDoi)).thenReturn(expectedArticlePointer);
    Map<String, Object> itemResponse = ImmutableMap.of("items", ImmutableMap.of());
    doAnswer(invocation -> {
        return itemResponse;
    }).when(articleService).getItemTable(expectedArticlePointer);
    ImmutableMap<String, String> journal = ImmutableMap.of("journalKey", DESKTOP_PLOS_ONE);
    ImmutableMap<String, Object> ingestionMetadata = ImmutableMap.of("journal", journal);
    ApiAddress address = ApiAddress.builder("articles").embedDoi(EXPECTED_DOI).addToken("relationships").build();
    when(articleApi.requestObject(address, ArticleMetadata.Factory.RELATED_ARTICLE_GSON_TYPE)).thenReturn(ImmutableList.<RelatedArticle>of());
    when(articleApi.requestObject(any(), eq(Map.class))).thenReturn(ingestionMetadata);
    when(articleMetadata.getArticlePointer()).thenReturn(expectedArticlePointer);
    String expectedHtml = "<html><title>This is a title</title><body>This is the body</body></html>";
    Reference reference = Reference.build().setTitle("Reference title").setPublisherName("Publisher Name").setAuthors(ImmutableList.of()).setCollabAuthors(ImmutableList.of()).build();
    ImmutableList<Reference> expectedReferences = ImmutableList.of(reference);
    ArticleController.HtmlWithReferences expectedHtmlWithReferences = new ArticleController.HtmlWithReferences(expectedHtml, expectedReferences);
    when(corpusContentApi.readManuscript(any(), any(), any(), any())).thenReturn(expectedHtmlWithReferences);
    String expectedViewName = NOSPACE_JOINER.join(DESKTOP_PLOS_ONE, "/ftl/article/article");
    String requestUri = NOSPACE_JOINER.join("/article?id=", EXPECTED_DOI);
    mockMvc.perform(get(new URI(requestUri))).andExpect(status().isOk()).andExpect(model().attribute("articleText", expectedHtml)).andExpect(model().attribute("references", expectedReferences)).andExpect(view().name(expectedViewName));
    verify(articleResolutionService).toIngestion(expectedRequestedDoi);
    verify(articleService).getItemTable(expectedArticlePointer);
    verify(articleApi, times(1)).requestObject(any(), eq(Map.class));
    verify(articleMetadata).getArticlePointer();
    verify(corpusContentApi).readManuscript(any(), any(), any(), any());
}
Also used : Reference(org.ambraproject.wombat.model.Reference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URI(java.net.URI) ArticlePointer(org.ambraproject.wombat.identity.ArticlePointer) RequestedDoiVersion(org.ambraproject.wombat.identity.RequestedDoiVersion) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 10 with ApiAddress

use of org.ambraproject.wombat.service.remote.ApiAddress in project wombat by PLOS.

the class ArticleMetadataTest method testFetchRelatedArticlesWithSpecificUse.

@Test
public void testFetchRelatedArticlesWithSpecificUse() throws Exception {
    String doi = "10.9999/journal.xxxx.0";
    List<RelatedArticle> map = new Gson().fromJson(read("articleMeta/ppat.1005446.related2.json"), ArticleMetadata.Factory.RELATED_ARTICLE_GSON_TYPE);
    ApiAddress address = ApiAddress.builder("articles").embedDoi(doi).addToken("relationships").build();
    when(articleApi.requestObject(address, ArticleMetadata.Factory.RELATED_ARTICLE_GSON_TYPE)).thenReturn(map);
    List<RelatedArticle> raList = articleMetadataFactory.fetchRelatedArticles(doi);
    assertEquals(1, raList.size());
    RelatedArticle ra = raList.get(0);
    assertEquals("foo", ra.getType().getSpecificUse());
}
Also used : RelatedArticle(org.ambraproject.wombat.model.RelatedArticle) Gson(com.google.gson.Gson) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress) Test(org.junit.Test)

Aggregations

ApiAddress (org.ambraproject.wombat.service.remote.ApiAddress)15 Map (java.util.Map)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ArticlePointer (org.ambraproject.wombat.identity.ArticlePointer)3 Test (org.junit.Test)3 Gson (com.google.gson.Gson)2 ArticleComment (org.ambraproject.wombat.model.ArticleComment)2 ArticleCommentFlag (org.ambraproject.wombat.model.ArticleCommentFlag)2 RelatedArticle (org.ambraproject.wombat.model.RelatedArticle)2 HttpResponse (org.apache.http.HttpResponse)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Type (java.lang.reflect.Type)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 NotFoundException (org.ambraproject.wombat.controller.NotFoundException)1