Commit f69f51226dff4c4444c8cc62f85f16984b96df3b

Authored by Luis Pedro
1 parent b4f5b380e8
Exists in master and in 1 other branch API_v3.0

C5079

src/main/java/org/um/dsi/gavea/orcid/client/BaseOrcidOAuthClient.java
... ... @@ -7,12 +7,15 @@ import javax.ws.rs.client.Client;
7 7 import javax.ws.rs.client.ClientBuilder;
8 8 import javax.ws.rs.client.Entity;
9 9 import javax.ws.rs.client.WebTarget;
  10 +import javax.ws.rs.core.MediaType;
10 11 import javax.ws.rs.core.MultivaluedMap;
11 12 import javax.ws.rs.core.Response;
12 13  
13 14 import org.glassfish.jersey.client.ClientProperties;
  15 +import org.glassfish.jersey.jackson.JacksonFeature;
14 16 import org.slf4j.Logger;
15 17 import org.slf4j.LoggerFactory;
  18 +import org.um.dsi.gavea.orcid.client.configurator.JacksonConfigurator;
16 19 import org.um.dsi.gavea.orcid.client.constants.OrcidConstants;
17 20 import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
18 21 import org.um.dsi.gavea.orcid.client.util.RecordUtil;
... ... @@ -79,10 +82,10 @@ public class BaseOrcidOAuthClient implements Serializable {
79 82 if (debugMode || _log.isTraceEnabled()) {
80 83 _log.debug("[getAccessTokens][post].[Request.form] " + formData.toString());
81 84 }
82   - Response response = target.request(OrcidConstants.APPLICATION_ORCID_XML).post(Entity.form(formData));
  85 + Response response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.form(formData));
83 86 _log.debug("[getAccessTokens][post].[Response.Status] " + response.getStatus());
84 87 _log.debug("[getAccessTokens][post].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
85   -
  88 +
86 89 exceptionBuilder(response);
87 90  
88 91 return response;
... ... @@ -98,8 +101,7 @@ public class BaseOrcidOAuthClient implements Serializable {
98 101 _log.debug("[post].[Request.entity] " + RecordUtil.convertToString(entity.getEntity()));
99 102 }
100 103  
101   - response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
102   - .header("Authorization", "Bearer" + token.getAccess_token()).post(entity);
  104 + response = target.request(OrcidConstants.APPLICATION_ORCID_XML).header("Authorization", "Bearer" + token.getAccess_token()).post(entity);
103 105 _log.debug("[post].[Response.Status] " + response.getStatus());
104 106 _log.debug("[post].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
105 107  
... ... @@ -122,8 +124,7 @@ public class BaseOrcidOAuthClient implements Serializable {
122 124 _log.debug("[put].[Request.entity] " + RecordUtil.convertToString(entity.getEntity()));
123 125 }
124 126  
125   - response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
126   - .header("Authorization", "Bearer" + token.getAccess_token()).put(entity);
  127 + response = target.request(OrcidConstants.APPLICATION_ORCID_XML).header("Authorization", "Bearer" + token.getAccess_token()).put(entity);
127 128 _log.debug("[put].[Response.Status] " + response.getStatus());
128 129 _log.debug("[put].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
129 130  
... ... @@ -142,8 +143,7 @@ public class BaseOrcidOAuthClient implements Serializable {
142 143 try {
143 144 createConnection(endpoint, token.getOrcid(), putCode);
144 145  
145   - response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
146   - .header("Authorization", "Bearer" + token.getAccess_token()).delete();
  146 + response = target.request(OrcidConstants.APPLICATION_ORCID_XML).header("Authorization", "Bearer" + token.getAccess_token()).delete();
147 147 _log.debug("[delete].[Response.Status] " + response.getStatus());
148 148 _log.debug("[delete].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
149 149  
... ... @@ -161,6 +161,8 @@ public class BaseOrcidOAuthClient implements Serializable {
161 161 restClient = ClientBuilder.newClient();
162 162 restClient.property(ClientProperties.CONNECT_TIMEOUT, OrcidConstants.CLIENT_CONNECT_TIMEOUT);
163 163 restClient.property(ClientProperties.READ_TIMEOUT, OrcidConstants.CLIENT_READ_TIMEOUT);
  164 + restClient.register(JacksonFeature.class);
  165 + restClient.register(JacksonConfigurator.class);
164 166 }
165 167  
166 168 target = restClient.target(apiUri).path(buildPath(endpoint, orcidId, putCode));
... ... @@ -190,11 +192,12 @@ public class BaseOrcidOAuthClient implements Serializable {
190 192 if (response.getStatus() > 300) {
191 193 if (response.getStatus() == 400) {
192 194 Error error = response.readEntity(Error.class);
193   - throw new OrcidClientException(error.getResponseCode(), error.getUserMessage(), error.getErrorCode(),
194   - error.getDeveloperMessage());
  195 + if(error.getUserMessage() != null || error.getErrorCode() != null || error.getDeveloperMessage() != null)
  196 + throw new OrcidClientException(error.getResponseCode(), error.getUserMessage(), error.getErrorCode(), error.getDeveloperMessage());
  197 + else
  198 + throw new OrcidClientException(response.getStatus(), response.getStatus() + " - " + response.getStatusInfo().getReasonPhrase());
195 199 } else
196   - throw new OrcidClientException(response.getStatus(),
197   - response.getStatus() + " - " + response.getStatusInfo().getReasonPhrase());
  200 + throw new OrcidClientException(response.getStatus(), response.getStatus() + " - " + response.getStatusInfo().getReasonPhrase());
198 201 }
199 202 }
200 203  
... ...