Commit ee6380b55f711d722c186d61e8726416d567df1d

Authored by Paulo Graça
1 parent 109efda989

support debugging mode

src/main/java/org/um/dsi/gavea/orcid/client/BaseOrcidOAuthClient.java
... ... @@ -11,17 +11,18 @@ import javax.ws.rs.core.MediaType;
11 11 import javax.ws.rs.core.MultivaluedMap;
12 12 import javax.ws.rs.core.Response;
13 13  
14   -import org.apache.logging.log4j.LogManager;
15   -import org.apache.logging.log4j.Logger;
  14 +import org.slf4j.Logger;
  15 +import org.slf4j.LoggerFactory;
16 16 import org.glassfish.jersey.client.ClientProperties;
17 17 import org.um.dsi.gavea.orcid.client.constants.OrcidConstants;
18 18 import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
  19 +import org.um.dsi.gavea.orcid.client.util.RecordUtil;
19 20 import org.um.dsi.gavea.orcid.model.error.Error;
20 21  
21 22 public class BaseOrcidOAuthClient implements Serializable {
22 23 private static final long serialVersionUID = 8455820192048815978L;
23 24  
24   - private static final Logger _log = LogManager.getLogger(BaseOrcidOAuthClient.class);
  25 + private static final Logger _log = LoggerFactory.getLogger(BaseOrcidOAuthClient.class);
25 26  
26 27  
27 28 private Client restClient;
... ... @@ -33,8 +34,13 @@ public class BaseOrcidOAuthClient implements Serializable {
33 34 protected final String clientSecret;
34 35 protected final String redirectUri;
35 36  
  37 + protected final boolean debugMode;
36 38  
37 39 public BaseOrcidOAuthClient(Client restClient, String loginUri, String apiUri, String clientId, String clientSecret, String redirectUri) {
  40 + this(restClient, loginUri, apiUri, clientId, clientSecret, redirectUri, false);
  41 + }
  42 +
  43 + public BaseOrcidOAuthClient(Client restClient, String loginUri, String apiUri, String clientId, String clientSecret, String redirectUri, boolean debugMode) {
38 44 if (loginUri == null || apiUri == null || clientId == null || clientSecret == null || redirectUri == null)
39 45 throw new IllegalArgumentException("Cannot create OrcidOAuthClient - missing init parameters");
40 46  
... ... @@ -44,6 +50,7 @@ public class BaseOrcidOAuthClient implements Serializable {
44 50 this.clientId = clientId;
45 51 this.clientSecret = clientSecret;
46 52 this.redirectUri = redirectUri;
  53 + this.debugMode = debugMode;
47 54 }
48 55  
49 56 protected <T> Response get(final String endpoint, final OrcidAccessToken token, final String putCode) throws OrcidClientException {
... ... @@ -67,9 +74,12 @@ public class BaseOrcidOAuthClient implements Serializable {
67 74 protected <T> Response post(final String authorizationCode, final MultivaluedMap<String, String> formData) throws OrcidClientException {
68 75 createConnection(OrcidConstants.ENDPOINT_OAUTH_TOKEN, null, null);
69 76  
  77 + if (debugMode) {
  78 + _log.debug("[getAccessTokens][post].[Request.form] " + formData.toString());
  79 + }
70 80 Response response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.form(formData));
71   - _log.debug("[getAccessTokens].[Response.Status] " + response.getStatus());
72   - _log.debug("[getAccessTokens].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
  81 + _log.debug("[getAccessTokens][post].[Response.Status] " + response.getStatus());
  82 + _log.debug("[getAccessTokens][post].[Response.Reason] " + response.getStatusInfo().getReasonPhrase());
73 83  
74 84 exceptionBuilder(response);
75 85  
... ... @@ -80,7 +90,11 @@ public class BaseOrcidOAuthClient implements Serializable {
80 90 Response response;
81 91 try {
82 92 createConnection(endpoint, token.getOrcid(), null);
83   -
  93 +
  94 + if (debugMode) {
  95 + _log.debug("[post].[Request.entity] " + RecordUtil.convertToString(entity.getEntity()));
  96 + }
  97 +
84 98 response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
85 99 .header("Authorization", "Bearer" + token.getAccess_token()).post(entity);
86 100 _log.debug("[post].[Response.Status] " + response.getStatus());
... ... @@ -100,6 +114,10 @@ public class BaseOrcidOAuthClient implements Serializable {
100 114 try {
101 115 createConnection(endpoint, token.getOrcid(), putCode);
102 116  
  117 + if (debugMode) {
  118 + _log.debug("[put].[Request.entity] " + RecordUtil.convertToString(entity.getEntity()));
  119 + }
  120 +
103 121 response = target.request(OrcidConstants.APPLICATION_ORCID_XML)
104 122 .header("Authorization", "Bearer" + token.getAccess_token()).put(entity);
105 123 _log.debug("[put].[Response.Status] " + response.getStatus());
... ... @@ -141,6 +159,9 @@ public class BaseOrcidOAuthClient implements Serializable {
141 159 }
142 160  
143 161 target = restClient.target(apiUri).path(buildPath(endpoint, orcidId, putCode));
  162 + if (debugMode) {
  163 + _log.debug("[connection].[Request.path] " + target.getUri().toString());
  164 + }
144 165 }
145 166  
146 167 private String buildPath(final String endpoint, final String orcidId, final String putCode) {
... ...
src/main/java/org/um/dsi/gavea/orcid/client/OrcidOAuthClient.java
... ... @@ -10,8 +10,8 @@ import javax.ws.rs.core.MultivaluedMap;
10 10 import javax.ws.rs.core.Response;
11 11 import javax.ws.rs.core.UriBuilder;
12 12  
13   -import org.apache.logging.log4j.LogManager;
14   -import org.apache.logging.log4j.Logger;
  13 +import org.slf4j.Logger;
  14 +import org.slf4j.LoggerFactory;
15 15 import org.um.dsi.gavea.orcid.client.constants.OrcidConstants;
16 16 import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
17 17 import org.um.dsi.gavea.orcid.client.extend.ScopePathTypeExt;
... ... @@ -45,16 +45,24 @@ import org.um.dsi.gavea.orcid.model.work.WorkSummary;
45 45 public class OrcidOAuthClient extends BaseOrcidOAuthClient {
46 46 private static final long serialVersionUID = -6655680509383446710L;
47 47  
48   - private static final Logger _log = LogManager.getLogger(OrcidOAuthClient.class);
  48 + private static final Logger _log = LoggerFactory.getLogger(OrcidOAuthClient.class);
49 49  
50 50 public OrcidOAuthClient(String loginUri, String apiUri, String clientId, String clientSecret, String redirectUri) {
51 51 this(null, loginUri, apiUri, clientId, clientSecret, redirectUri);
52 52 }
53 53  
  54 + public OrcidOAuthClient(String loginUri, String apiUri, String clientId, String clientSecret, String redirectUri, boolean debugMode) {
  55 + this(null, loginUri, apiUri, clientId, clientSecret, redirectUri, debugMode);
  56 + }
  57 +
54 58 public OrcidOAuthClient(Client restClient, String loginUri, String apiUri, String clientId, String clientSecret, String redirectUri) {
55 59 super(restClient, loginUri, apiUri, clientId, clientSecret, redirectUri);
56 60 }
57 61  
  62 + public OrcidOAuthClient(Client restClient, String loginUri, String apiUri, String clientId, String clientSecret, String redirectUri, boolean debugMode) {
  63 + super(restClient, loginUri, apiUri, clientId, clientSecret, redirectUri, debugMode);
  64 + }
  65 +
58 66 /**
59 67 *
60 68 * @param scopes
... ...