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,29 +21,30 @@ import org.um.dsi.gavea.orcid.model.error.Error;
21 21
22 public class BaseOrcidOAuthClient implements Serializable { 22 public class BaseOrcidOAuthClient implements Serializable {
23 private static final long serialVersionUID = 8455820192048815978L; 23 private static final long serialVersionUID = 8455820192048815978L;
24 - 24 +
25 private static final Logger _log = LoggerFactory.getLogger(BaseOrcidOAuthClient.class); 25 private static final Logger _log = LoggerFactory.getLogger(BaseOrcidOAuthClient.class);
26 26
27 -  
28 private Client restClient; 27 private Client restClient;
29 private WebTarget target; 28 private WebTarget target;
30 - 29 +
31 protected final String loginUri; 30 protected final String loginUri;
32 protected final String apiUri; 31 protected final String apiUri;
33 protected final String clientId; 32 protected final String clientId;
34 protected final String clientSecret; 33 protected final String clientSecret;
35 protected final String redirectUri; 34 protected final String redirectUri;
36 - 35 +
37 protected final boolean debugMode; 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 this(restClient, loginUri, apiUri, clientId, clientSecret, redirectUri, false); 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 if (loginUri == null || apiUri == null || clientId == null || clientSecret == null || redirectUri == null) 45 if (loginUri == null || apiUri == null || clientId == null || clientSecret == null || redirectUri == null)
45 throw new IllegalArgumentException("Cannot create OrcidOAuthClient - missing init parameters"); 46 throw new IllegalArgumentException("Cannot create OrcidOAuthClient - missing init parameters");
46 - 47 +
47 this.restClient = restClient; 48 this.restClient = restClient;
48 this.loginUri = loginUri; 49 this.loginUri = loginUri;
49 this.apiUri = apiUri; 50 this.apiUri = apiUri;
@@ -52,102 +53,108 @@ public class BaseOrcidOAuthClient implements Serializable { @@ -52,102 +53,108 @@ public class BaseOrcidOAuthClient implements Serializable {
52 this.redirectUri = redirectUri; 53 this.redirectUri = redirectUri;
53 this.debugMode = debugMode; 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 Response response = null; 59 Response response = null;
58 try { 60 try {
59 createConnection(endpoint, token.getOrcid(), putCode); 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 _log.debug("[get].[Response.Status] " + response.getStatus()); 65 _log.debug("[get].[Response.Status] " + response.getStatus());
63 _log.debug("[get].[Response.Reason] " + response.getStatusInfo().getReasonPhrase()); 66 _log.debug("[get].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
64 - 67 +
65 exceptionBuilder(response); 68 exceptionBuilder(response);
66 - } catch(ProcessingException e) { 69 + } catch (ProcessingException e) {
67 response = Response.serverError().build(); 70 response = Response.serverError().build();
68 exceptionBuilder(response); 71 exceptionBuilder(response);
69 - }  
70 - 72 + }
  73 +
71 return response; 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 createConnection(OrcidConstants.ENDPOINT_OAUTH_TOKEN, null, null); 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 Response response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.form(formData)); 84 Response response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.form(formData));
81 _log.debug("[getAccessTokens][post].[Response.Status] " + response.getStatus()); 85 _log.debug("[getAccessTokens][post].[Response.Status] " + response.getStatus());
82 _log.debug("[getAccessTokens][post].[Response.Reason] " + response.getStatusInfo().getReasonPhrase()); 86 _log.debug("[getAccessTokens][post].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
83 87
84 exceptionBuilder(response); 88 exceptionBuilder(response);
85 - 89 +
86 return response; 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 Response response; 95 Response response;
91 try { 96 try {
92 createConnection(endpoint, token.getOrcid(), null); 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 response = target.request(OrcidConstants.APPLICATION_ORCID_XML) 103 response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
99 .header("Authorization", "Bearer" + token.getAccess_token()).post(entity); 104 .header("Authorization", "Bearer" + token.getAccess_token()).post(entity);
100 _log.debug("[post].[Response.Status] " + response.getStatus()); 105 _log.debug("[post].[Response.Status] " + response.getStatus());
101 _log.debug("[post].[Response.Reason] " + response.getStatusInfo().getReasonPhrase()); 106 _log.debug("[post].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
102 - 107 +
103 exceptionBuilder(response); 108 exceptionBuilder(response);
104 - } catch(ProcessingException e) { 109 + } catch (ProcessingException e) {
105 response = Response.serverError().build(); 110 response = Response.serverError().build();
106 exceptionBuilder(response); 111 exceptionBuilder(response);
107 - } 112 + }
108 113
109 return response; 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 Response response; 119 Response response;
114 try { 120 try {
115 createConnection(endpoint, token.getOrcid(), putCode); 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 response = target.request(OrcidConstants.APPLICATION_ORCID_XML) 127 response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
122 .header("Authorization", "Bearer" + token.getAccess_token()).put(entity); 128 .header("Authorization", "Bearer" + token.getAccess_token()).put(entity);
123 _log.debug("[put].[Response.Status] " + response.getStatus()); 129 _log.debug("[put].[Response.Status] " + response.getStatus());
124 _log.debug("[put].[Response.Reason] " + response.getStatusInfo().getReasonPhrase()); 130 _log.debug("[put].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
125 - 131 +
126 exceptionBuilder(response); 132 exceptionBuilder(response);
127 - } catch(ProcessingException e) { 133 + } catch (ProcessingException e) {
128 response = Response.serverError().build(); 134 response = Response.serverError().build();
129 exceptionBuilder(response); 135 exceptionBuilder(response);
130 - } 136 + }
131 137
132 return response; 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 Response response; 143 Response response;
137 try { 144 try {
138 createConnection(endpoint, token.getOrcid(), putCode); 145 createConnection(endpoint, token.getOrcid(), putCode);
139 - 146 +
140 response = target.request(OrcidConstants.APPLICATION_ORCID_XML) 147 response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
141 .header("Authorization", "Bearer" + token.getAccess_token()).delete(); 148 .header("Authorization", "Bearer" + token.getAccess_token()).delete();
142 _log.debug("[delete].[Response.Status] " + response.getStatus()); 149 _log.debug("[delete].[Response.Status] " + response.getStatus());
143 _log.debug("[delete].[Response.Reason] " + response.getStatusInfo().getReasonPhrase()); 150 _log.debug("[delete].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
144 - 151 +
145 exceptionBuilder(response); 152 exceptionBuilder(response);
146 - } catch(ProcessingException e) { 153 + } catch (ProcessingException e) {
147 response = Response.serverError().build(); 154 response = Response.serverError().build();
148 exceptionBuilder(response); 155 exceptionBuilder(response);
149 - }  
150 - 156 + }
  157 +
151 return response; 158 return response;
152 } 159 }
153 160
@@ -157,10 +164,10 @@ public class BaseOrcidOAuthClient implements Serializable { @@ -157,10 +164,10 @@ public class BaseOrcidOAuthClient implements Serializable {
157 restClient.property(ClientProperties.CONNECT_TIMEOUT, OrcidConstants.CLIENT_CONNECT_TIMEOUT); 164 restClient.property(ClientProperties.CONNECT_TIMEOUT, OrcidConstants.CLIENT_CONNECT_TIMEOUT);
158 restClient.property(ClientProperties.READ_TIMEOUT, OrcidConstants.CLIENT_READ_TIMEOUT); 165 restClient.property(ClientProperties.READ_TIMEOUT, OrcidConstants.CLIENT_READ_TIMEOUT);
159 } 166 }
160 - 167 +
161 target = restClient.target(apiUri).path(buildPath(endpoint, orcidId, putCode)); 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,17 +187,19 @@ public class BaseOrcidOAuthClient implements Serializable {
180 187
181 return sb.toString(); 188 return sb.toString();
182 } 189 }
183 - 190 +
184 protected void exceptionBuilder(Response response) throws OrcidClientException { 191 protected void exceptionBuilder(Response response) throws OrcidClientException {
185 if (response.getStatus() > 300) { 192 if (response.getStatus() > 300) {
186 if (response.getStatus() == 400) { 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 public String getClientId() { 203 public String getClientId() {
195 return clientId; 204 return clientId;
196 } 205 }