Compare View

switch
from
...
to
 
Commits (2)
src/main/java/org/um/dsi/gavea/orcid/client/BaseOrcidOAuthClient.java
... ... @@ -44,26 +44,13 @@ public class BaseOrcidOAuthClient implements Serializable {
44 44 this.clientSecret = clientSecret;
45 45 this.redirectUri = redirectUri;
46 46 }
47   -
48   - protected <T> Response post(final String authorizationCode, final MultivaluedMap<String, String> formData) throws OrcidClientException {
49   - createConnection(OrcidConstants.ENDPOINT_OAUTH_TOKEN, null, null);
50   -
51   - Response response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.form(formData));
52   - _log.debug("[getAccessTokens].[Response.Status] " + response.getStatus());
53   - _log.debug("[getAccessTokens].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
54   -
55   - exceptionBuilder(response);
56   -
57   - return response;
58   - }
59   -
  47 +
60 48 protected <T> Response get(final String endpoint, final OrcidAccessToken token, final String putCode) throws OrcidClientException {
61   - Response response;
  49 + Response response = null;
62 50 try {
63 51 createConnection(endpoint, token.getOrcid(), putCode);
64 52  
65   - response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
66   - .header("Authorization", "Bearer" + token.getAccess_token()).get();
  53 + response = target.request(OrcidConstants.APPLICATION_ORCID_XML).header("Authorization", "Bearer" + token.getAccess_token()).get();
67 54 _log.debug("[get].[Response.Status] " + response.getStatus());
68 55 _log.debug("[get].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
69 56  
... ... @@ -76,6 +63,18 @@ public class BaseOrcidOAuthClient implements Serializable {
76 63 return response;
77 64 }
78 65  
  66 + protected <T> Response post(final String authorizationCode, final MultivaluedMap<String, String> formData) throws OrcidClientException {
  67 + createConnection(OrcidConstants.ENDPOINT_OAUTH_TOKEN, null, null);
  68 +
  69 + Response response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.form(formData));
  70 + _log.debug("[getAccessTokens].[Response.Status] " + response.getStatus());
  71 + _log.debug("[getAccessTokens].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
  72 +
  73 + exceptionBuilder(response);
  74 +
  75 + return response;
  76 + }
  77 +
79 78 protected <T> Response post(final String endpoint, final OrcidAccessToken token, final Entity<T> entity) throws OrcidClientException {
80 79 Response response;
81 80 try {
... ... @@ -133,15 +132,13 @@ public class BaseOrcidOAuthClient implements Serializable {
133 132 return response;
134 133 }
135 134  
136   -
137 135 private void createConnection(final String endpoint, final String orcidId, final String putCode) {
138   - if (client != null)
139   - client.close();
140   -
141   - client = ClientBuilder.newClient();
142   - client.property(ClientProperties.CONNECT_TIMEOUT, OrcidConstants.CLIENT_CONNECT_TIMEOUT);
143   - client.property(ClientProperties.READ_TIMEOUT, OrcidConstants.CLIENT_READ_TIMEOUT);
144   -
  136 + if (client == null) {
  137 + client = ClientBuilder.newClient();
  138 + client.property(ClientProperties.CONNECT_TIMEOUT, OrcidConstants.CLIENT_CONNECT_TIMEOUT);
  139 + client.property(ClientProperties.READ_TIMEOUT, OrcidConstants.CLIENT_READ_TIMEOUT);
  140 + }
  141 +
145 142 target = client.target(apiUri).path(buildPath(endpoint, orcidId, putCode));
146 143 }
147 144  
... ...
src/main/java/org/um/dsi/gavea/orcid/client/OrcidOAuthClient.java
... ... @@ -29,32 +29,28 @@ public class OrcidOAuthClient extends BaseOrcidOAuthClient {
29 29 }
30 30  
31 31  
32   - public String getAuthorizeUriLogin(final List<ScopePathType> scopes, final String state, final String orcidOrEmail)
33   - throws Exception {
  32 + public String getAuthorizeUriLogin(final List<ScopePathType> scopes, final String state, final String orcidOrEmail) throws Exception {
34 33  
35 34 String orcidId = null;
36 35 String email = null;
37   - if (orcidOrEmail != null && !"".equals(orcidOrEmail)) {
38   - if (isValidOrcidId(orcidOrEmail)) {
  36 + if(orcidOrEmail != null && !"".equals(orcidOrEmail)) {
  37 + if(isValidOrcidId(orcidOrEmail))
39 38 orcidId = orcidOrEmail;
40   - } else {
  39 + else
41 40 email = orcidOrEmail;
42   - }
43 41 }
44 42  
45 43 return getAuthorizeUri(scopes, state, orcidId, null, null, email, null, true);
46 44 }
47 45  
48   - public String getAuthorizeUriRegistration(final List<ScopePathType> scopes, final String state,
49   - final String familyNames, final String givenNames) throws Exception {
  46 + public String getAuthorizeUriRegistration(final List<ScopePathType> scopes, final String state, final String familyNames, final String givenNames) throws Exception {
50 47 return getAuthorizeUri(scopes, state, null, familyNames, givenNames, null, null, false);
51 48 }
52 49  
53   - private String getAuthorizeUri(final List<ScopePathType> scopes, final String state, final String orcidId,
54   - final String familyNames, final String givenNames, final String email, final String lang,
  50 + private String getAuthorizeUri(final List<ScopePathType> scopes, final String state, final String orcidId, final String familyNames, final String givenNames, final String email, final String lang,
55 51 final boolean login) throws Exception {
56 52  
57   - if (scopes == null)
  53 + if(scopes == null)
58 54 throw new Exception("Cannot create Authorize Uri - missing scopes");
59 55  
60 56 UriBuilder builder = UriBuilder.fromUri(loginUri);
... ... @@ -106,70 +102,122 @@ public class OrcidOAuthClient extends BaseOrcidOAuthClient {
106 102  
107 103 /* Activities */
108 104 public ActivitiesSummary readActivitiesSummary(final OrcidAccessToken token) throws OrcidClientException {
109   - return get(OrcidConstants.ENDPOINT_ACTIVITIES, token, null).readEntity(ActivitiesSummary.class);
  105 + Response response = null;
  106 + try {
  107 + response = get(OrcidConstants.ENDPOINT_ACTIVITIES, token, null);
  108 +
  109 + return response.readEntity(ActivitiesSummary.class);
  110 + } finally {
  111 + if(response != null)
  112 + response.close();
  113 + }
110 114 }
111 115  
112 116  
113 117 /* Funding */
114 118 public Funding readFunding(final OrcidAccessToken token, final String putCode) throws OrcidClientException {
115   - return get(OrcidConstants.ENDPOINT_FUNDING, token, putCode).readEntity(Funding.class);
  119 + Response response = null;
  120 + try {
  121 + response = get(OrcidConstants.ENDPOINT_FUNDING, token, putCode);
  122 +
  123 + return response.readEntity(Funding.class);
  124 + } finally {
  125 + if(response != null)
  126 + response.close();
  127 + }
116 128 }
117 129  
118   - public String addFunding(final OrcidAccessToken token, final Funding funding)
119   - throws OrcidClientException {
120   - Response response = post(OrcidConstants.ENDPOINT_FUNDING, token, Entity.entity(funding, MediaType.APPLICATION_XML_TYPE));
121   -
122   - String location = response.getLocation().toString();
123   - if (location != null && !"".equals(location)) {
124   - String putCode = location.substring(location.lastIndexOf("/") + 1, location.length());
125   - if (putCode.matches("\\d+")) {
126   - return putCode;
  130 + public String addFunding(final OrcidAccessToken token, final Funding funding) throws OrcidClientException {
  131 + Response response = null;
  132 + try {
  133 + response = post(OrcidConstants.ENDPOINT_FUNDING, token, Entity.entity(funding, MediaType.APPLICATION_XML_TYPE));
  134 +
  135 + String location = response.getLocation().toString();
  136 + if(location != null && !"".equals(location)) {
  137 + String putCode = location.substring(location.lastIndexOf("/") + 1, location.length());
  138 + if(putCode.matches("\\d+"))
  139 + return putCode;
127 140 }
  141 + } finally {
  142 + if(response != null)
  143 + response.close();
128 144 }
129 145  
130 146 return null;
131 147 }
132 148  
133   - public void updateFunding(final OrcidAccessToken token, final String putCode, final Funding funding)
134   - throws OrcidClientException {
135   - put(OrcidConstants.ENDPOINT_FUNDING, token, putCode, Entity.entity(funding, MediaType.APPLICATION_XML_TYPE));
  149 + public void updateFunding(final OrcidAccessToken token, final String putCode, final Funding funding) throws OrcidClientException {
  150 + Response response = null;
  151 + try {
  152 + response = put(OrcidConstants.ENDPOINT_FUNDING, token, putCode, Entity.entity(funding, MediaType.APPLICATION_XML_TYPE));
  153 + } finally {
  154 + if(response != null)
  155 + response.close();
  156 + }
136 157 }
137 158  
138 159 public void deleteFunding(final OrcidAccessToken token, final String putCode) throws OrcidClientException {
139   - delete(OrcidConstants.ENDPOINT_FUNDING, token, putCode);
  160 + Response response = null;
  161 + try {
  162 + response = delete(OrcidConstants.ENDPOINT_FUNDING, token, putCode);
  163 + } finally {
  164 + if(response != null)
  165 + response.close();
  166 + }
140 167 }
141 168  
142 169  
143 170 /* Work */
144 171 public Work readWork(final OrcidAccessToken token, final String putCode) throws OrcidClientException {
145   - return get(OrcidConstants.ENDPOINT_WORK, token, putCode).readEntity(Work.class);
  172 + Response response = null;
  173 + try {
  174 + response = get(OrcidConstants.ENDPOINT_WORK, token, putCode);
  175 +
  176 + return response.readEntity(Work.class);
  177 + } finally {
  178 + if(response != null)
  179 + response.close();
  180 + }
146 181 }
147 182  
148   - public String addWork(final OrcidAccessToken token, final Work work)
149   - throws OrcidClientException {
150   - Response response = post(OrcidConstants.ENDPOINT_WORK, token, Entity.entity(work, MediaType.APPLICATION_XML_TYPE));
151   -
152   - String location = response.getLocation().toString();
153   - if (location != null && !"".equals(location)) {
154   - String putCode = location.substring(location.lastIndexOf("/") + 1, location.length());
155   - if (putCode.matches("\\d+")) {
156   - return putCode;
  183 + public String addWork(final OrcidAccessToken token, final Work work) throws OrcidClientException {
  184 + Response response = null;
  185 + try {
  186 + response = post(OrcidConstants.ENDPOINT_WORK, token, Entity.entity(work, MediaType.APPLICATION_XML_TYPE));
  187 + String location = response.getLocation().toString();
  188 + if (location != null && !"".equals(location)) {
  189 + String putCode = location.substring(location.lastIndexOf("/") + 1, location.length());
  190 + if (putCode.matches("\\d+"))
  191 + return putCode;
157 192 }
  193 + } finally {
  194 + if(response != null)
  195 + response.close();
158 196 }
159 197  
160 198 return null;
161 199 }
162 200  
163   - public void updateWork(final OrcidAccessToken token, final String putCode, final Work work)
164   - throws OrcidClientException {
165   - put(OrcidConstants.ENDPOINT_WORK, token, putCode, Entity.entity(work, MediaType.APPLICATION_XML_TYPE));
  201 + public void updateWork(final OrcidAccessToken token, final String putCode, final Work work) throws OrcidClientException {
  202 + Response response = null;
  203 + try {
  204 + response = put(OrcidConstants.ENDPOINT_WORK, token, putCode, Entity.entity(work, MediaType.APPLICATION_XML_TYPE));
  205 + } finally {
  206 + if(response != null)
  207 + response.close();
  208 + }
166 209 }
167 210  
168 211 public void deleteWork(final OrcidAccessToken token, final String putCode) throws OrcidClientException {
169   - delete(OrcidConstants.ENDPOINT_WORK, token, putCode);
  212 + Response response = null;
  213 + try {
  214 + response = delete(OrcidConstants.ENDPOINT_WORK, token, putCode);
  215 + } finally {
  216 + if(response != null)
  217 + response.close();
  218 + }
170 219 }
171 220  
172   -
173 221 private boolean isValidOrcidId(final String value) {
174 222 return value.matches("([0-9]{4})-([0-9]{4})-([0-9]{4})-([0-9]{3})(?:[0-9X]{1})");
175 223 }
... ...