Commit bcd468f51a4e37f0b55e7d78d28d00044ec52128

Authored by Paulo Graça
1 parent e799d06236

Add debug mode

src/main/java/org/um/dsi/gavea/orcid/client/BaseOrcidOAuthClient.java
... ... @@ -21,29 +21,30 @@ import org.um.dsi.gavea.orcid.model.error.Error;
21 21  
22 22 public class BaseOrcidOAuthClient implements Serializable {
23 23 private static final long serialVersionUID = 8455820192048815978L;
24   -
  24 +
25 25 private static final Logger _log = LoggerFactory.getLogger(BaseOrcidOAuthClient.class);
26 26  
27   -
28 27 private Client restClient;
29 28 private WebTarget target;
30   -
  29 +
31 30 protected final String loginUri;
32 31 protected final String apiUri;
33 32 protected final String clientId;
34 33 protected final String clientSecret;
35 34 protected final String redirectUri;
36   -
  35 +
37 36 protected final boolean debugMode;
38   -
39   - public BaseOrcidOAuthClient(Client restClient, String loginUri, String apiUri, String clientId, String clientSecret, String redirectUri) {
  37 +
  38 + public BaseOrcidOAuthClient(Client restClient, String loginUri, String apiUri, String clientId, String clientSecret,
  39 + String redirectUri) {
40 40 this(restClient, loginUri, apiUri, clientId, clientSecret, redirectUri, false);
41 41 }
42   -
43   - public BaseOrcidOAuthClient(Client restClient, String loginUri, String apiUri, String clientId, String clientSecret, String redirectUri, boolean debugMode) {
  42 +
  43 + public BaseOrcidOAuthClient(Client restClient, String loginUri, String apiUri, String clientId, String clientSecret,
  44 + String redirectUri, boolean debugMode) {
44 45 if (loginUri == null || apiUri == null || clientId == null || clientSecret == null || redirectUri == null)
45 46 throw new IllegalArgumentException("Cannot create OrcidOAuthClient - missing init parameters");
46   -
  47 +
47 48 this.restClient = restClient;
48 49 this.loginUri = loginUri;
49 50 this.apiUri = apiUri;
... ... @@ -52,102 +53,108 @@ public class BaseOrcidOAuthClient implements Serializable {
52 53 this.redirectUri = redirectUri;
53 54 this.debugMode = debugMode;
54 55 }
55   -
56   - protected <T> Response get(final String endpoint, final OrcidAccessToken token, final String putCode) throws OrcidClientException {
  56 +
  57 + protected <T> Response get(final String endpoint, final OrcidAccessToken token, final String putCode)
  58 + throws OrcidClientException {
57 59 Response response = null;
58 60 try {
59 61 createConnection(endpoint, token.getOrcid(), putCode);
60   -
61   - response = target.request(OrcidConstants.APPLICATION_ORCID_XML).header("Authorization", "Bearer" + token.getAccess_token()).get();
  62 +
  63 + response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
  64 + .header("Authorization", "Bearer" + token.getAccess_token()).get();
62 65 _log.debug("[get].[Response.Status] " + response.getStatus());
63 66 _log.debug("[get].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
64   -
  67 +
65 68 exceptionBuilder(response);
66   - } catch(ProcessingException e) {
  69 + } catch (ProcessingException e) {
67 70 response = Response.serverError().build();
68 71 exceptionBuilder(response);
69   - }
70   -
  72 + }
  73 +
71 74 return response;
72 75 }
73 76  
74   - protected <T> Response post(final String authorizationCode, final MultivaluedMap<String, String> formData) throws OrcidClientException {
  77 + protected <T> Response post(final String authorizationCode, final MultivaluedMap<String, String> formData)
  78 + throws OrcidClientException {
75 79 createConnection(OrcidConstants.ENDPOINT_OAUTH_TOKEN, null, null);
76 80  
77   - if (debugMode) {
78   - _log.debug("[getAccessTokens][post].[Request.form] " + formData.toString());
  81 + if (debugMode || _log.isTraceEnabled()) {
  82 + _log.trace("[getAccessTokens][post].[Request.form] " + formData.toString());
79 83 }
80 84 Response response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.form(formData));
81 85 _log.debug("[getAccessTokens][post].[Response.Status] " + response.getStatus());
82 86 _log.debug("[getAccessTokens][post].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
83 87  
84 88 exceptionBuilder(response);
85   -
  89 +
86 90 return response;
87 91 }
88 92  
89   - protected <T> Response post(final String endpoint, final OrcidAccessToken token, final Entity<T> entity) throws OrcidClientException {
  93 + protected <T> Response post(final String endpoint, final OrcidAccessToken token, final Entity<T> entity)
  94 + throws OrcidClientException {
90 95 Response response;
91 96 try {
92 97 createConnection(endpoint, token.getOrcid(), null);
93   -
94   - if (debugMode) {
95   - _log.debug("[post].[Request.entity] " + RecordUtil.convertToString(entity.getEntity()));
  98 +
  99 + if (debugMode || _log.isTraceEnabled()) {
  100 + _log.trace("[post].[Request.entity] " + RecordUtil.convertToString(entity.getEntity()));
96 101 }
97   -
  102 +
98 103 response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
99 104 .header("Authorization", "Bearer" + token.getAccess_token()).post(entity);
100 105 _log.debug("[post].[Response.Status] " + response.getStatus());
101 106 _log.debug("[post].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
102   -
  107 +
103 108 exceptionBuilder(response);
104   - } catch(ProcessingException e) {
  109 + } catch (ProcessingException e) {
105 110 response = Response.serverError().build();
106 111 exceptionBuilder(response);
107   - }
  112 + }
108 113  
109 114 return response;
110 115 }
111 116  
112   - protected <T> Response put(final String endpoint, final OrcidAccessToken token, final String putCode, final Entity<T> entity) throws OrcidClientException {
  117 + protected <T> Response put(final String endpoint, final OrcidAccessToken token, final String putCode,
  118 + final Entity<T> entity) throws OrcidClientException {
113 119 Response response;
114 120 try {
115 121 createConnection(endpoint, token.getOrcid(), putCode);
116   -
117   - if (debugMode) {
118   - _log.debug("[put].[Request.entity] " + RecordUtil.convertToString(entity.getEntity()));
  122 +
  123 + if (debugMode || _log.isTraceEnabled()) {
  124 + _log.trace("[put].[Request.entity] " + RecordUtil.convertToString(entity.getEntity()));
119 125 }
120   -
  126 +
121 127 response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
122 128 .header("Authorization", "Bearer" + token.getAccess_token()).put(entity);
123 129 _log.debug("[put].[Response.Status] " + response.getStatus());
124 130 _log.debug("[put].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
125   -
  131 +
126 132 exceptionBuilder(response);
127   - } catch(ProcessingException e) {
  133 + } catch (ProcessingException e) {
128 134 response = Response.serverError().build();
129 135 exceptionBuilder(response);
130   - }
  136 + }
131 137  
132 138 return response;
133 139 }
134 140  
135   - protected <T> Response delete(final String endpoint, final OrcidAccessToken token, final String putCode) throws OrcidClientException {
  141 + protected <T> Response delete(final String endpoint, final OrcidAccessToken token, final String putCode)
  142 + throws OrcidClientException {
136 143 Response response;
137 144 try {
138 145 createConnection(endpoint, token.getOrcid(), putCode);
139   -
  146 +
140 147 response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
141 148 .header("Authorization", "Bearer" + token.getAccess_token()).delete();
142 149 _log.debug("[delete].[Response.Status] " + response.getStatus());
143 150 _log.debug("[delete].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
144   -
  151 +
145 152 exceptionBuilder(response);
146   - } catch(ProcessingException e) {
  153 + } catch (ProcessingException e) {
147 154 response = Response.serverError().build();
148 155 exceptionBuilder(response);
149   - }
150   -
  156 + }
  157 +
151 158 return response;
152 159 }
153 160  
... ... @@ -157,10 +164,10 @@ public class BaseOrcidOAuthClient implements Serializable {
157 164 restClient.property(ClientProperties.CONNECT_TIMEOUT, OrcidConstants.CLIENT_CONNECT_TIMEOUT);
158 165 restClient.property(ClientProperties.READ_TIMEOUT, OrcidConstants.CLIENT_READ_TIMEOUT);
159 166 }
160   -
  167 +
161 168 target = restClient.target(apiUri).path(buildPath(endpoint, orcidId, putCode));
162   - if (debugMode) {
163   - _log.debug("[connection].[Request.path] " + target.getUri().toString());
  169 + if (debugMode || _log.isTraceEnabled()) {
  170 + _log.trace("[connection].[Request.path] " + target.getUri().toString());
164 171 }
165 172 }
166 173  
... ... @@ -180,17 +187,19 @@ public class BaseOrcidOAuthClient implements Serializable {
180 187  
181 188 return sb.toString();
182 189 }
183   -
  190 +
184 191 protected void exceptionBuilder(Response response) throws OrcidClientException {
185 192 if (response.getStatus() > 300) {
186 193 if (response.getStatus() == 400) {
187   - Error error = response.readEntity(Error.class);
188   - throw new OrcidClientException(error.getResponseCode(), error.getUserMessage(), error.getErrorCode(), error.getDeveloperMessage());
189   - } else
190   - throw new OrcidClientException(response.getStatus(), response.getStatus() + " - " + response.getStatusInfo().getReasonPhrase());
  194 + Error error = response.readEntity(Error.class);
  195 + throw new OrcidClientException(error.getResponseCode(), error.getUserMessage(), error.getErrorCode(),
  196 + error.getDeveloperMessage());
  197 + } else
  198 + throw new OrcidClientException(response.getStatus(),
  199 + response.getStatus() + " - " + response.getStatusInfo().getReasonPhrase());
191 200 }
192 201 }
193   -
  202 +
194 203 public String getClientId() {
195 204 return clientId;
196 205 }
... ...