use of org.orcid.jaxb.model.error_rc2.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);
}
use of org.orcid.jaxb.model.error_rc2.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);
}
use of org.orcid.jaxb.model.error_rc2.OrcidError in project ORCID-Source by ORCID.
the class WorkManagerImpl method createWorks.
/**
* Add a list of works to the given profile
*
* @param works
* The list of works that want to be added
* @param orcid
* The id of the user we want to add the works to
*
* @return the work bulk with the put codes of the new works or the error
* that indicates why a work can't be added
*/
@Override
@Transactional
public WorkBulk createWorks(String orcid, WorkBulk workBulk) {
SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
Set<ExternalID> existingExternalIdentifiers = buildExistingExternalIdsSet(orcid, sourceEntity.getSourceId());
if (workBulk.getBulk() != null && !workBulk.getBulk().isEmpty()) {
List<BulkElement> bulk = workBulk.getBulk();
//Check bulk size
if (bulk.size() > maxBulkSize) {
Locale locale = localeManager.getLocale();
throw new IllegalArgumentException(messageSource.getMessage("apiError.validation_too_many_elements_in_bulk.exception", new Object[] { maxBulkSize }, locale));
}
for (int i = 0; i < bulk.size(); i++) {
if (Work.class.isAssignableFrom(bulk.get(i).getClass())) {
Work work = (Work) bulk.get(i);
try {
//Validate the work
activityValidator.validateWork(work, sourceEntity, true, true, null);
//Validate it is not duplicated
if (work.getExternalIdentifiers() != null) {
for (ExternalID extId : work.getExternalIdentifiers().getExternalIdentifier()) {
if (existingExternalIdentifiers.contains(extId)) {
Map<String, String> params = new HashMap<String, String>();
params.put("clientName", sourceEntity.getSourceName());
throw new OrcidDuplicatedActivityException(params);
}
}
}
//Save the work
WorkEntity workEntity = jpaJaxbWorkAdapter.toWorkEntity(work);
ProfileEntity profile = profileEntityCacheManager.retrieve(orcid);
workEntity.setProfile(profile);
workEntity.setAddedToProfileDate(new Date());
// Set source id
if (sourceEntity.getSourceProfile() != null) {
workEntity.setSourceId(sourceEntity.getSourceProfile().getId());
}
if (sourceEntity.getSourceClient() != null) {
workEntity.setClientSourceId(sourceEntity.getSourceClient().getId());
}
setIncomingWorkPrivacy(workEntity, profile);
DisplayIndexCalculatorHelper.setDisplayIndexOnNewEntity(workEntity, true);
workDao.persist(workEntity);
//Update the element in the bulk
Work updatedWork = jpaJaxbWorkAdapter.toWork(workEntity);
bulk.set(i, updatedWork);
//Add the work extIds to the list of existing external identifiers
addExternalIdsToExistingSet(updatedWork, existingExternalIdentifiers);
} catch (Exception e) {
//Get the exception
OrcidError orcidError = orcidCoreExceptionMapper.getOrcidError(e);
bulk.set(i, orcidError);
}
}
}
workDao.flush();
}
return workBulk;
}
use of org.orcid.jaxb.model.error_rc2.OrcidError in project ORCID-Source by ORCID.
the class MemberV2Test method createViewUpdateAndDeleteFunding.
@Test
public void createViewUpdateAndDeleteFunding() throws JSONException, InterruptedException, URISyntaxException {
long time = System.currentTimeMillis();
Funding funding = (Funding) unmarshallFromPath("/record_2.0/samples/read_samples/funding-2.0.xml", Funding.class);
funding.setPutCode(null);
funding.setVisibility(Visibility.PUBLIC);
funding.getExternalIdentifiers().getExternalIdentifier().clear();
ExternalID fExtId = new ExternalID();
fExtId.setType(FundingExternalIdentifierType.GRANT_NUMBER.value());
fExtId.setValue("Funding Id " + time);
fExtId.setRelationship(Relationship.SELF);
funding.getExternalIdentifiers().getExternalIdentifier().add(fExtId);
String accessToken = getAccessToken();
ClientResponse postResponse = memberV2ApiClient.createFundingXml(this.getUser1OrcidId(), funding, accessToken);
assertNotNull(postResponse);
assertEquals(Response.Status.CREATED.getStatusCode(), postResponse.getStatus());
String locationPath = postResponse.getLocation().getPath();
assertTrue("Location header path should match pattern, but was " + locationPath, locationPath.matches(".*/v2.0/" + this.getUser1OrcidId() + "/funding/\\d+"));
ClientResponse getResponse = memberV2ApiClient.viewLocationXml(postResponse.getLocation(), accessToken);
assertEquals(Response.Status.OK.getStatusCode(), getResponse.getStatus());
Funding gotFunding = getResponse.getEntity(Funding.class);
assertEquals("common:title", gotFunding.getTitle().getTitle().getContent());
assertEquals("common:translated-title", gotFunding.getTitle().getTranslatedTitle().getContent());
assertEquals("en", gotFunding.getTitle().getTranslatedTitle().getLanguageCode());
//Save the original visibility
Visibility originalVisibility = gotFunding.getVisibility();
Visibility updatedVisibility = Visibility.PRIVATE.equals(originalVisibility) ? Visibility.LIMITED : Visibility.PRIVATE;
//Verify you cant update the visibility
gotFunding.setVisibility(updatedVisibility);
ClientResponse putResponse = memberV2ApiClient.updateLocationXml(postResponse.getLocation(), accessToken, gotFunding);
assertEquals(Response.Status.FORBIDDEN.getStatusCode(), putResponse.getStatus());
OrcidError error = putResponse.getEntity(OrcidError.class);
assertNotNull(error);
assertEquals(Integer.valueOf(9035), error.getErrorCode());
//Set the visibility again to the initial one
gotFunding.setVisibility(originalVisibility);
gotFunding.getTitle().getTitle().setContent("Updated title");
gotFunding.getTitle().getTranslatedTitle().setContent("Updated translated title");
gotFunding.getTitle().getTranslatedTitle().setLanguageCode("es");
putResponse = memberV2ApiClient.updateLocationXml(postResponse.getLocation(), accessToken, gotFunding);
assertEquals(Response.Status.OK.getStatusCode(), putResponse.getStatus());
ClientResponse getAfterUpdateResponse = memberV2ApiClient.viewLocationXml(postResponse.getLocation(), accessToken);
assertEquals(Response.Status.OK.getStatusCode(), getAfterUpdateResponse.getStatus());
Funding gotAfterUpdateFunding = getAfterUpdateResponse.getEntity(Funding.class);
assertEquals("Updated title", gotAfterUpdateFunding.getTitle().getTitle().getContent());
assertEquals("Updated translated title", gotAfterUpdateFunding.getTitle().getTranslatedTitle().getContent());
assertEquals("es", gotAfterUpdateFunding.getTitle().getTranslatedTitle().getLanguageCode());
ClientResponse deleteResponse = memberV2ApiClient.deleteFundingXml(this.getUser1OrcidId(), gotFunding.getPutCode(), accessToken);
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), deleteResponse.getStatus());
}
use of org.orcid.jaxb.model.error_rc2.OrcidError in project ORCID-Source by ORCID.
the class S3Updater method updateS3.
public void updateS3(String orcid, Object object) throws JsonProcessingException, AmazonClientException, JAXBException {
// API 1.2
if (OrcidMessage.class.isAssignableFrom(object.getClass())) {
OrcidMessage orcidProfile = (OrcidMessage) object;
putJsonElement(orcid, orcidProfile);
putXmlElement(orcid, orcidProfile);
return;
}
// API 1.2 ERROR
if (OrcidDeprecated.class.isAssignableFrom(object.getClass())) {
OrcidDeprecated error = (OrcidDeprecated) object;
putJsonElement(orcid, error);
putXmlElement(orcid, error);
return;
}
// API 2.0_v2
if (Record.class.isAssignableFrom(object.getClass())) {
Record record = (Record) object;
putJsonElement(orcid, record);
putXmlElement(orcid, record);
return;
}
// API 2.0 Error
if (OrcidError.class.isAssignableFrom(object.getClass())) {
OrcidError error = (OrcidError) object;
putJsonElement(orcid, error);
putXmlElement(orcid, error);
return;
}
}
Aggregations