Skip to content

Commit e81ec9b

Browse files
committed
Orcid 2.0 API update
1 parent 6cee4f0 commit e81ec9b

35 files changed

+1967
-47
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
<artifactId>httpclient</artifactId>
6464
<version>4.5.2</version>
6565
</dependency>
66+
<dependency>
67+
<groupId>commons-io</groupId>
68+
<artifactId>commons-io</artifactId>
69+
<version>2.4</version>
70+
</dependency>
6671
<dependency>
6772
<groupId>commons-logging</groupId>
6873
<artifactId>commons-logging</artifactId>

src/main/java/edu/cornell/mannlib/orcidclient/actions/ActionManager.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
import javax.servlet.http.HttpServletRequest;
66

7-
import edu.cornell.mannlib.orcidclient.actions.version_1_0.UpdateBioAction;
7+
import edu.cornell.mannlib.orcidclient.context.OrcidAPIConfig;
88
import edu.cornell.mannlib.orcidclient.context.OrcidClientContext;
9-
import edu.cornell.mannlib.orcidclient.responses.message_1_2.OrcidMessage;
109

1110
/**
1211
* TODO
@@ -21,18 +20,34 @@ public ActionManager(OrcidClientContext context, HttpServletRequest req) {
2120
}
2221

2322
public AddExternalIdAction createAddExternalIdAction() {
24-
return new edu.cornell.mannlib.orcidclient.actions.version_1_0.AddExternalIdAction();
23+
if (context.getApiVersion() == OrcidAPIConfig.Versions.V1_2) {
24+
return new edu.cornell.mannlib.orcidclient.actions.version_1_0.AddExternalIdAction();
25+
}
26+
27+
return new edu.cornell.mannlib.orcidclient.actions.version_2_0.AddExternalIdAction();
2528
}
2629

2730
public ReadProfileAction createReadProfileAction() {
28-
return new edu.cornell.mannlib.orcidclient.actions.version_1_0.ReadProfileAction();
31+
if (context.getApiVersion() == OrcidAPIConfig.Versions.V1_2) {
32+
return new edu.cornell.mannlib.orcidclient.actions.version_1_0.ReadProfileAction();
33+
}
34+
35+
return new edu.cornell.mannlib.orcidclient.actions.version_2_0.ReadProfileAction();
2936
}
3037

3138
public ReadPublicBioAction createReadPublicBioAction() {
32-
return new edu.cornell.mannlib.orcidclient.actions.version_1_0.ReadPublicBioAction();
39+
if (context.getApiVersion() == OrcidAPIConfig.Versions.V1_2) {
40+
return new edu.cornell.mannlib.orcidclient.actions.version_1_0.ReadPublicBioAction();
41+
}
42+
43+
return new edu.cornell.mannlib.orcidclient.actions.version_2_0.ReadPublicBioAction();
3344
}
3445

3546
public UpdateBioAction createUpdateBioAction() {
36-
return new edu.cornell.mannlib.orcidclient.actions.version_1_0.UpdateBioAction();
47+
if (context.getApiVersion() == OrcidAPIConfig.Versions.V1_2) {
48+
return new edu.cornell.mannlib.orcidclient.actions.version_1_0.UpdateBioAction();
49+
}
50+
51+
return new edu.cornell.mannlib.orcidclient.actions.version_2_0.UpdateBioAction();
3752
}
3853
}

src/main/java/edu/cornell/mannlib/orcidclient/actions/version_1_0/AddExternalIdAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private OrcidMessage buildOutgoingMessage(ExternalId externalId) {
141141

142142
OrcidMessage om = new OrcidMessage();
143143
om.setOrcidProfile(op);
144-
om.setMessageVersion(occ.getApiVersion());
144+
om.setMessageVersion(occ.getApiVersion().toString());
145145

146146
return om;
147147
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
2+
3+
package edu.cornell.mannlib.orcidclient.actions.version_2_0;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import edu.cornell.mannlib.orcidclient.OrcidClientException;
7+
import edu.cornell.mannlib.orcidclient.auth.AccessToken;
8+
import edu.cornell.mannlib.orcidclient.beans.ExternalId;
9+
import edu.cornell.mannlib.orcidclient.context.OrcidClientContext;
10+
import edu.cornell.mannlib.orcidclient.model.OrcidProfile;
11+
import edu.cornell.mannlib.orcidclient.responses.message_2_0.OrcidExternalIdentifier;
12+
import edu.cornell.mannlib.orcidclient.responses.message_2_0.OrcidString;
13+
import org.apache.commons.logging.Log;
14+
import org.apache.commons.logging.LogFactory;
15+
import org.apache.http.client.HttpResponseException;
16+
import org.apache.http.client.fluent.Content;
17+
import org.apache.http.client.fluent.Request;
18+
import org.apache.http.client.fluent.Response;
19+
import org.apache.http.client.utils.URIUtils;
20+
import org.apache.http.entity.ContentType;
21+
22+
import java.io.IOException;
23+
import java.net.URI;
24+
import java.net.URISyntaxException;
25+
26+
/**
27+
*/
28+
public class AddExternalIdAction implements edu.cornell.mannlib.orcidclient.actions.AddExternalIdAction {
29+
private static final Log log = LogFactory.getLog(AddExternalIdAction.class);
30+
31+
private final OrcidClientContext occ;
32+
33+
public AddExternalIdAction() {
34+
this.occ = OrcidClientContext.getInstance();
35+
}
36+
37+
@Override
38+
public OrcidProfile execute(ExternalId externalId, AccessToken accessToken)
39+
throws OrcidClientException {
40+
41+
try {
42+
URI baseUri = new URI(occ.getApiMemberUrl());
43+
String requestUrl = URIUtils.resolve(baseUri, accessToken.getOrcid() + "/external-identifiers").toString();
44+
45+
OrcidExternalIdentifier id = new OrcidExternalIdentifier();
46+
47+
id.setExtCommonName(externalId.getCommonName());
48+
id.setExtReference(externalId.getReference());
49+
OrcidString url = new OrcidString();
50+
url.setValue(externalId.getUrl());
51+
id.setExtUrl(url);
52+
id.setExtRelationship("SELF");
53+
id.setVisibility(externalId.getVisibility().toString());
54+
55+
ObjectMapper mapper = new ObjectMapper();
56+
String json = mapper.writeValueAsString(id);
57+
58+
log.debug("Outgoing string: " + json);
59+
60+
Request request = Request
61+
.Post(requestUrl)
62+
.addHeader("Content-Type", "application/vnd.orcid+json")
63+
.addHeader(
64+
"Authorization",
65+
accessToken.getTokenType() + " "
66+
+ accessToken.getAccessToken())
67+
.bodyString(json, ContentType.APPLICATION_FORM_URLENCODED);
68+
Response response = request.execute();
69+
Content content = response.returnContent();
70+
String string = content.asString();
71+
log.debug("Content from AddExternalID was: " + string);
72+
73+
ReadProfileAction readAction = new ReadProfileAction();
74+
return readAction.execute(accessToken);
75+
} catch (URISyntaxException e) {
76+
throw new OrcidClientException(
77+
"API_BASE_URL is not syntactically valid.", e);
78+
} catch (HttpResponseException e) {
79+
// Bad status code? Something funky.
80+
log.error("HttpResponse status code: " + e.getStatusCode());
81+
throw new OrcidClientException(
82+
"Failed to add external ID. HTTP status code="
83+
+ e.getStatusCode(), e);
84+
} catch (IOException e) {
85+
throw new OrcidClientException("Failed to add external ID", e);
86+
}
87+
}
88+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
2+
3+
package edu.cornell.mannlib.orcidclient.actions.version_2_0;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import edu.cornell.mannlib.orcidclient.OrcidClientException;
7+
import edu.cornell.mannlib.orcidclient.auth.AccessToken;
8+
import edu.cornell.mannlib.orcidclient.context.OrcidClientContext;
9+
import edu.cornell.mannlib.orcidclient.model.OrcidProfile;
10+
import edu.cornell.mannlib.orcidclient.responses.message_2_0.OrcidPerson;
11+
import org.apache.commons.logging.Log;
12+
import org.apache.commons.logging.LogFactory;
13+
import org.apache.http.NameValuePair;
14+
import org.apache.http.client.HttpResponseException;
15+
import org.apache.http.client.fluent.Content;
16+
import org.apache.http.client.fluent.Request;
17+
import org.apache.http.client.fluent.Response;
18+
import org.apache.http.client.utils.URIUtils;
19+
import org.apache.http.message.BasicNameValuePair;
20+
21+
import java.io.IOException;
22+
import java.net.URI;
23+
import java.net.URISyntaxException;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
/**
28+
* <pre>
29+
* curl -H 'Content-Type: application/vdn.orcid+xml'
30+
* -H 'Authorization: Bearer f6d49570-c048-45a9-951f-a81ebb1fa543'
31+
* -X GET 'http://api.sandbox-1.orcid.org/v1.0.23/0000-0003-1495-7122/orcid-profile'
32+
* -L -i
33+
* </pre>
34+
*/
35+
public class ReadProfileAction implements edu.cornell.mannlib.orcidclient.actions.ReadProfileAction {
36+
private static final Log log = LogFactory.getLog(ReadProfileAction.class);
37+
38+
private final OrcidClientContext occ;
39+
40+
public ReadProfileAction() {
41+
this.occ = OrcidClientContext.getInstance();
42+
}
43+
44+
@Override
45+
public OrcidProfile execute(AccessToken accessToken)
46+
throws OrcidClientException {
47+
try {
48+
URI baseUri = new URI(occ.getApiPublicUrl());
49+
String requestUrl = URIUtils.resolve(baseUri, accessToken.getOrcid() + "/person").toString();
50+
51+
List<NameValuePair> headers = new ArrayList<NameValuePair>();
52+
headers.add(new BasicNameValuePair("Authorization", accessToken.getTokenType() + " " + accessToken.getAccessToken()));
53+
54+
55+
String json = Util.readJSON(requestUrl, headers);
56+
if (json == null) {
57+
throw new OrcidClientException("Failed to read profile");
58+
}
59+
60+
log.debug("Public Bio: " + json);
61+
62+
ObjectMapper mapper = new ObjectMapper();
63+
OrcidPerson person = mapper.readValue(json, OrcidPerson.class);
64+
65+
return Util.toModel(person);
66+
} catch (URISyntaxException e) {
67+
throw new OrcidClientException(
68+
"API_BASE_URL is not syntactically valid.", e);
69+
} catch (HttpResponseException e) {
70+
// Bad status code? Something funky.
71+
log.error("HttpResponse status code: " + e.getStatusCode());
72+
throw new OrcidClientException(
73+
"Failed to read profile. HTTP status code="
74+
+ e.getStatusCode(), e);
75+
} catch (IOException e) {
76+
throw new OrcidClientException("Failed to read profile", e);
77+
}
78+
}
79+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
2+
3+
package edu.cornell.mannlib.orcidclient.actions.version_2_0;
4+
5+
import com.fasterxml.jackson.core.JsonParseException;
6+
import com.fasterxml.jackson.databind.JsonMappingException;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import edu.cornell.mannlib.orcidclient.OrcidClientException;
9+
import edu.cornell.mannlib.orcidclient.context.OrcidClientContext;
10+
import edu.cornell.mannlib.orcidclient.model.OrcidProfile;
11+
import edu.cornell.mannlib.orcidclient.responses.message_2_0.OrcidBiography;
12+
import edu.cornell.mannlib.orcidclient.responses.message_2_0.OrcidPerson;
13+
import org.apache.commons.logging.Log;
14+
import org.apache.commons.logging.LogFactory;
15+
import org.apache.http.client.utils.URIUtils;
16+
17+
import java.io.IOException;
18+
import java.net.URI;
19+
import java.net.URISyntaxException;
20+
21+
/**
22+
* <pre>
23+
* curl -H "Accept: application/json"
24+
* 'http://pub.sandbox-1.orcid.org/v2.0/0000-0001-7857-2795/orcid-bio'
25+
* -L -i
26+
* </pre>
27+
*/
28+
public class ReadPublicBioAction implements edu.cornell.mannlib.orcidclient.actions.ReadPublicBioAction {
29+
private static final Log log = LogFactory.getLog(ReadPublicBioAction.class);
30+
31+
private final OrcidClientContext occ;
32+
33+
public ReadPublicBioAction() {
34+
this.occ = OrcidClientContext.getInstance();
35+
}
36+
37+
@Override
38+
public OrcidProfile execute(String orcid) throws OrcidClientException {
39+
try {
40+
URI baseUri = new URI(occ.getApiPublicUrl());
41+
String requestUrl = URIUtils.resolve(baseUri, orcid + "/person").toString();
42+
String json = Util.readJSON(requestUrl, null);
43+
if (json == null) {
44+
throw new OrcidClientException("Failed to read profile");
45+
}
46+
47+
log.debug("Public Bio: " + json);
48+
49+
ObjectMapper mapper = new ObjectMapper();
50+
51+
OrcidPerson person = mapper.readValue(json, OrcidPerson.class);
52+
return Util.toModel(person);
53+
} catch (URISyntaxException e) {
54+
throw new OrcidClientException("API_BASE_URL is not syntactically valid.", e);
55+
} catch (JsonParseException e) {
56+
throw new OrcidClientException("Failed to read profile", e);
57+
} catch (JsonMappingException e) {
58+
throw new OrcidClientException("Failed to read profile", e);
59+
} catch (IOException e) {
60+
throw new OrcidClientException("Failed to read profile", e);
61+
}
62+
}
63+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
2+
3+
package edu.cornell.mannlib.orcidclient.actions.version_2_0;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import edu.cornell.mannlib.orcidclient.OrcidClientException;
7+
import edu.cornell.mannlib.orcidclient.actions.version_1_0.Util;
8+
import edu.cornell.mannlib.orcidclient.auth.AccessToken;
9+
import edu.cornell.mannlib.orcidclient.context.OrcidClientContext;
10+
import edu.cornell.mannlib.orcidclient.model.OrcidProfile;
11+
import edu.cornell.mannlib.orcidclient.responses.message_1_2.OrcidMessage;
12+
import edu.cornell.mannlib.orcidclient.responses.message_2_0.OrcidBiography;
13+
import org.apache.commons.logging.Log;
14+
import org.apache.commons.logging.LogFactory;
15+
import org.apache.http.client.HttpResponseException;
16+
import org.apache.http.client.fluent.Content;
17+
import org.apache.http.client.fluent.Request;
18+
import org.apache.http.client.fluent.Response;
19+
import org.apache.http.client.utils.URIUtils;
20+
import org.apache.http.entity.ContentType;
21+
22+
import java.io.IOException;
23+
import java.net.URI;
24+
import java.net.URISyntaxException;
25+
26+
/**
27+
*/
28+
public class UpdateBioAction implements edu.cornell.mannlib.orcidclient.actions.UpdateBioAction {
29+
private static final Log log = LogFactory.getLog(UpdateBioAction.class);
30+
31+
private final OrcidClientContext occ;
32+
33+
public UpdateBioAction() {
34+
this.occ = OrcidClientContext.getInstance();
35+
}
36+
37+
@Override
38+
public OrcidProfile execute(OrcidMessage profile, AccessToken accessToken)
39+
throws OrcidClientException {
40+
try {
41+
URI baseUri = new URI(occ.getApiMemberUrl());
42+
String requestUrl = URIUtils.resolve(baseUri, accessToken.getOrcid() + "/biography").toString();
43+
44+
OrcidBiography bio = new OrcidBiography();
45+
bio.setContent(profile.getOrcidProfile().getOrcidBio().getBiography().getValue());
46+
47+
ObjectMapper mapper = new ObjectMapper();
48+
String json = mapper.writeValueAsString(bio);
49+
50+
Request request = Request
51+
.Put(requestUrl)
52+
.addHeader("Content-Type", "application/vnd.orcid+json")
53+
.addHeader(
54+
"Authorization",
55+
accessToken.getTokenType() + " "
56+
+ accessToken.getAccessToken())
57+
.bodyString(json,
58+
ContentType.APPLICATION_FORM_URLENCODED);
59+
Response response = request.execute();
60+
Content content = response.returnContent();
61+
String string = content.asString();
62+
log.debug("Content from UpdateBio was: " + string);
63+
64+
ReadProfileAction readAction = new ReadProfileAction();
65+
return readAction.execute(accessToken);
66+
} catch (URISyntaxException e) {
67+
throw new OrcidClientException(
68+
"API_BASE_URL is not syntactically valid.", e);
69+
} catch (HttpResponseException e) {
70+
// Bad status code? Something funky.
71+
log.error("HttpResponse status code: " + e.getStatusCode());
72+
throw new OrcidClientException(
73+
"Failed to update bio. HTTP status code="
74+
+ e.getStatusCode(), e);
75+
} catch (IOException e) {
76+
throw new OrcidClientException("Failed to update bio", e);
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)