Search in sources :

Example 26 with OrcidError

use of org.orcid.jaxb.model.error_rc4.OrcidError in project ORCID-Source by ORCID.

the class Orcid20APIClient method fetchPublicProfile.

/**
     * Fetches the profile from the ORCID public API v1.2
     * 
     * @param orcid
     * @return
     */
public Record fetchPublicProfile(String orcid) throws LockedRecordException, DeprecatedRecordException {
    WebResource webResource = jerseyClient.resource(baseUri).path(orcid + "/record");
    webResource.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, false);
    Builder builder = webResource.accept(MediaType.APPLICATION_XML).header("Authorization", "Bearer " + accessToken);
    ClientResponse response = builder.get(ClientResponse.class);
    if (response.getStatus() != 200) {
        OrcidError orcidError = null;
        switch(response.getStatus()) {
            case 301:
                orcidError = response.getEntity(OrcidError.class);
                throw new DeprecatedRecordException(orcidError);
            case 409:
                orcidError = response.getEntity(OrcidError.class);
                throw new LockedRecordException(orcidError);
            default:
                LOG.error("Unable to fetch public record " + orcid + " on API 2.0 HTTP error code: " + response.getStatus());
                throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
        }
    }
    return response.getEntity(Record.class);
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) OrcidError(org.orcid.jaxb.model.error_v2.OrcidError) LockedRecordException(org.orcid.listener.exception.LockedRecordException) DeprecatedRecordException(org.orcid.listener.exception.DeprecatedRecordException) Builder(com.sun.jersey.api.client.WebResource.Builder) WebResource(com.sun.jersey.api.client.WebResource)

Example 27 with OrcidError

use of org.orcid.jaxb.model.error_rc4.OrcidError in project ORCID-Source by ORCID.

the class PublicV2ApiServiceVersionedDelegatorTest method testViewBulkWorks.

@Test
public void testViewBulkWorks() {
    Response response = serviceDelegator.viewBulkWorks("0000-0000-0000-0003", "11,12,13,16");
    WorkBulk workBulk = (WorkBulk) response.getEntity();
    assertNotNull(workBulk);
    assertNotNull(workBulk.getBulk());
    assertEquals(4, workBulk.getBulk().size());
    assertTrue(workBulk.getBulk().get(0) instanceof Work);
    assertTrue(workBulk.getBulk().get(1) instanceof OrcidError);
    assertTrue(workBulk.getBulk().get(2) instanceof OrcidError);
    assertTrue(workBulk.getBulk().get(3) instanceof OrcidError);
}
Also used : Response(javax.ws.rs.core.Response) OrcidError(org.orcid.jaxb.model.error_v2.OrcidError) WorkBulk(org.orcid.jaxb.model.record_v2.WorkBulk) Work(org.orcid.jaxb.model.record_v2.Work) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 28 with OrcidError

use of org.orcid.jaxb.model.error_rc4.OrcidError in project ORCID-Source by ORCID.

the class OrcidCoreExceptionMapper method getOrcidErrorV2Rc1.

private OrcidError getOrcidErrorV2Rc1(int errorCode, int status, Throwable t) {
    Locale locale = localeManager.getLocale();
    OrcidError orcidError = new OrcidError();
    orcidError.setResponseCode(status);
    orcidError.setErrorCode(errorCode);
    orcidError.setMoreInfo(messageSource.getMessage("apiError." + errorCode + ".moreInfo", null, locale));
    Map<String, String> params = null;
    if (t instanceof ApplicationException) {
        params = ((ApplicationException) t).getParams();
    }
    orcidError.setDeveloperMessage(getDeveloperMessage(errorCode, t, params));
    orcidError.setUserMessage(resolveMessage(messageSource.getMessage("apiError." + errorCode + ".userMessage", null, locale), params));
    return orcidError;
}
Also used : Locale(java.util.Locale) OrcidError(org.orcid.jaxb.model.error_rc1.OrcidError)

Example 29 with OrcidError

use of org.orcid.jaxb.model.error_rc4.OrcidError in project ORCID-Source by ORCID.

the class OrcidSecurityManagerImpl method checkAndFilter.

@Override
public void checkAndFilter(String orcid, WorkBulk workBulk, ScopePathType scopePathType) {
    isMyToken(orcid);
    List<BulkElement> bulkElements = workBulk.getBulk();
    List<BulkElement> filteredElements = new ArrayList<>();
    for (int i = 0; i < bulkElements.size(); i++) {
        BulkElement element = bulkElements.get(i);
        if (element instanceof OrcidError) {
            filteredElements.add(element);
            continue;
        }
        try {
            checkAndFilter(orcid, (Work) element, scopePathType, true);
            filteredElements.add(element);
        } catch (Exception e) {
            if (e instanceof OrcidUnauthorizedException) {
                throw e;
            }
            OrcidError error = orcidCoreExceptionMapper.getOrcidError(e);
            filteredElements.add(error);
        }
    }
    workBulk.setBulk(filteredElements);
}
Also used : OrcidError(org.orcid.jaxb.model.error_v2.OrcidError) BulkElement(org.orcid.jaxb.model.record_v2.BulkElement) OrcidUnauthorizedException(org.orcid.core.exception.OrcidUnauthorizedException) ArrayList(java.util.ArrayList) NoResultException(javax.persistence.NoResultException) OrcidNotClaimedException(org.orcid.core.exception.OrcidNotClaimedException) OrcidDeprecatedException(org.orcid.core.exception.OrcidDeprecatedException) WrongSourceException(org.orcid.core.exception.WrongSourceException) LockedException(org.orcid.core.security.aop.LockedException) AccessControlException(java.security.AccessControlException) OrcidAccessControlException(org.orcid.core.exception.OrcidAccessControlException) OrcidVisibilityException(org.orcid.core.exception.OrcidVisibilityException) OrcidUnauthorizedException(org.orcid.core.exception.OrcidUnauthorizedException)

Example 30 with OrcidError

use of org.orcid.jaxb.model.error_rc4.OrcidError in project ORCID-Source by ORCID.

the class WorkManagerTest method testCreateWorksWithBulkSomeOKSomeErrors.

@Test
public void testCreateWorksWithBulkSomeOKSomeErrors() {
    String orcid = "0000-0000-0000-0003";
    when(sourceManager.retrieveSourceEntity()).thenReturn(new SourceEntity(new ClientDetailsEntity(CLIENT_2_ID)));
    //Lets send a bulk of 6 works
    WorkBulk bulk = new WorkBulk();
    //Work # 1 - Fine
    Work work1 = getWork(null);
    ExternalID extId = new ExternalID();
    extId.setRelationship(Relationship.SELF);
    extId.setType("doi");
    extId.setUrl(new Url("http://doi/1"));
    extId.setValue("doi-1");
    work1.getExternalIdentifiers().getExternalIdentifier().clear();
    work1.getExternalIdentifiers().getExternalIdentifier().add(extId);
    work1.getWorkTitle().getTitle().setContent("Work # 1");
    bulk.getBulk().add(work1);
    //Work # 2 - Fine
    Work work2 = getWork(null);
    work2.getWorkTitle().getTitle().setContent("Work # 2");
    bulk.getBulk().add(work2);
    //Work # 3 - Duplicated of Work # 1
    Work work3 = getWork(null);
    work3.getExternalIdentifiers().getExternalIdentifier().clear();
    work3.getExternalIdentifiers().getExternalIdentifier().add(extId);
    work3.getWorkTitle().getTitle().setContent("Work # 3");
    bulk.getBulk().add(work3);
    //Work # 4 - Fine
    Work work4 = getWork("new-ext-id-" + System.currentTimeMillis());
    work4.getWorkTitle().getTitle().setContent("Work # 4");
    bulk.getBulk().add(work4);
    //Work # 5 - Duplicated of existing work
    Work work5 = getWork(null);
    ExternalID dupExtId = new ExternalID();
    dupExtId.setRelationship(Relationship.SELF);
    dupExtId.setType("doi");
    dupExtId.setValue("1");
    work5.getExternalIdentifiers().getExternalIdentifier().clear();
    work5.getExternalIdentifiers().getExternalIdentifier().add(dupExtId);
    work5.getWorkTitle().getTitle().setContent("Work # 5");
    bulk.getBulk().add(work5);
    //Work # 6 - No title specified
    Work work6 = getWork(null);
    work6.getWorkTitle().getTitle().setContent(null);
    bulk.getBulk().add(work6);
    bulk = workManager.createWorks(orcid, bulk);
    assertNotNull(bulk);
    assertEquals(6, bulk.getBulk().size());
    List<Long> worksToDelete = new ArrayList<Long>();
    for (int i = 0; i < bulk.getBulk().size(); i++) {
        BulkElement element = bulk.getBulk().get(i);
        switch(i) {
            case 0:
            case 1:
            case 3:
                assertTrue(Work.class.isAssignableFrom(element.getClass()));
                Work work = (Work) element;
                assertNotNull(work);
                assertNotNull(work.getPutCode());
                if (i == 0) {
                    assertEquals("Work # 1", work.getWorkTitle().getTitle().getContent());
                } else if (i == 1) {
                    assertEquals("Work # 2", work.getWorkTitle().getTitle().getContent());
                } else {
                    assertEquals("Work # 4", work.getWorkTitle().getTitle().getContent());
                }
                worksToDelete.add(work.getPutCode());
                break;
            case 2:
            case 4:
            case 5:
                assertTrue("Error on id: " + i, OrcidError.class.isAssignableFrom(element.getClass()));
                OrcidError error = (OrcidError) element;
                if (i == 2) {
                    assertEquals(Integer.valueOf(9021), error.getErrorCode());
                } else if (i == 4) {
                    assertEquals(Integer.valueOf(9021), error.getErrorCode());
                } else {
                    assertEquals(Integer.valueOf(9022), error.getErrorCode());
                }
                break;
        }
    }
    //Delete new works
    for (Long putCode : worksToDelete) {
        assertTrue(workManager.checkSourceAndRemoveWork(orcid, putCode));
    }
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) OrcidError(org.orcid.jaxb.model.error_v2.OrcidError) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) WorkBulk(org.orcid.jaxb.model.record_v2.WorkBulk) ExternalID(org.orcid.jaxb.model.record_v2.ExternalID) ArrayList(java.util.ArrayList) Url(org.orcid.jaxb.model.common_v2.Url) BulkElement(org.orcid.jaxb.model.record_v2.BulkElement) Work(org.orcid.jaxb.model.record_v2.Work) Test(org.junit.Test) BaseTest(org.orcid.core.BaseTest)

Aggregations

Test (org.junit.Test)65 ClientResponse (com.sun.jersey.api.client.ClientResponse)54 OrcidError (org.orcid.jaxb.model.error_v2.OrcidError)43 OrcidError (org.orcid.jaxb.model.error_rc1.OrcidError)24 WorkBulk (org.orcid.jaxb.model.record_v2.WorkBulk)21 Work (org.orcid.jaxb.model.record_v2.Work)19 Visibility (org.orcid.jaxb.model.common_v2.Visibility)10 BulkElement (org.orcid.jaxb.model.record_v2.BulkElement)9 ExternalID (org.orcid.jaxb.model.record_v2.ExternalID)9 OrcidError (org.orcid.jaxb.model.error_rc3.OrcidError)8 OrcidError (org.orcid.jaxb.model.error_rc4.OrcidError)8 Response (javax.ws.rs.core.Response)7 Locale (java.util.Locale)6 DBUnitTest (org.orcid.test.DBUnitTest)6 Visibility (org.orcid.jaxb.model.common_rc1.Visibility)5 Visibility (org.orcid.jaxb.model.common_rc2.Visibility)5 Visibility (org.orcid.jaxb.model.common_rc4.Visibility)5 Visibility (org.orcid.jaxb.model.common_rc3.Visibility)4 Url (org.orcid.jaxb.model.common_v2.Url)4 ArrayList (java.util.ArrayList)3