diff --git a/pom.xml b/pom.xml index 9cb1c65adf505a34488e6952f4c6114f9b782afd..46efeb18846040fcc08f461d076db419a85e1a02 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,7 @@ 1.5 1.6.8 + 1.7.21 true @@ -92,11 +93,11 @@ scribejava-core 4.2.0 - - log4j - log4j - 1.2.17 - + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + commons-lang commons-lang diff --git a/src/main/java/org/genesys2/client/oauth/CLI.java b/src/main/java/org/genesys2/client/oauth/CLI.java index 3e6df19f1d78c54df1a9b42b7a48e7e0e0254cc8..1c10757adacecbdc79bf4d61fdad922104e59869 100644 --- a/src/main/java/org/genesys2/client/oauth/CLI.java +++ b/src/main/java/org/genesys2/client/oauth/CLI.java @@ -41,8 +41,8 @@ import com.github.scribejava.core.model.Verb; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; -import org.apache.log4j.LogManager; -import org.apache.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Simple command line interface to Genesys. @@ -51,8 +51,8 @@ import org.apache.log4j.Logger; */ public class CLI { - /** The Constant _log. */ - private static final Logger _log = LogManager.getLogger(CLI.class); + /** The Constant LOG. */ + private static final Logger LOG = LoggerFactory.getLogger(CLI.class); /** The properties file. */ private File propertiesFile; @@ -88,7 +88,7 @@ public class CLI { * @param args the arguments */ public static void main(final String[] args) { - _log.info("Hello World!"); + LOG.info("Hello World!"); final CLI cli = new CLI(); cli.genesysClient = GenesysClient.build(cli.loadProperties("client.properties")); @@ -108,22 +108,22 @@ public class CLI { saveProperties(); } catch (final OAuthAuthenticationException e) { - _log.error("Failed to fetch /me", e); + LOG.error("Failed to fetch /me", e); try { authenticate(); } catch (OAuthAuthenticationException e1) { - _log.error("Failed to authenticate", e1); + LOG.error("Failed to authenticate", e1); return; } } catch (final Throwable e) { - _log.error(e.getMessage(), e); + LOG.error(e.getMessage(), e); return; } try { doWork(); } catch (final Throwable e) { - _log.error(e.getMessage(), e); + LOG.error(e.getMessage(), e); } } @@ -555,7 +555,7 @@ public class CLI { if (restart) { saveProperties(); - _log.warn("Properties udpated, please restart CLI application"); + LOG.warn("Properties udpated, please restart CLI application"); System.exit(-1); } } @@ -592,7 +592,7 @@ public class CLI { fis = new FileOutputStream(propertiesFile); properties.store(fis, "OAuth client properties"); } catch (final IOException e) { - _log.error(e); + LOG.error(e.getMessage(), e); } finally { IOUtils.closeQuietly(fis); } @@ -612,15 +612,15 @@ public class CLI { FileInputStream fis = null; try { - _log.info("Loading " + propertiesFile.getAbsolutePath()); + LOG.info("Loading {}", propertiesFile.getAbsolutePath()); fis = new FileInputStream(propertiesFile); properties.load(fis); // Keep properties this.properties = properties; } catch (final FileNotFoundException e) { - _log.warn(e, e); + LOG.warn(e.getMessage(), e); } catch (final IOException e) { - _log.error(e, e); + LOG.error(e.getMessage(), e); } finally { IOUtils.closeQuietly(fis); } diff --git a/src/main/java/org/genesys2/client/oauth/GenesysClient.java b/src/main/java/org/genesys2/client/oauth/GenesysClient.java index 81c8614880280d6a138124ebe3a7563e509f8d55..dcff217f5be36ff4cbf92b6fa96a48385a5fbfcb 100644 --- a/src/main/java/org/genesys2/client/oauth/GenesysClient.java +++ b/src/main/java/org/genesys2/client/oauth/GenesysClient.java @@ -50,19 +50,19 @@ import com.github.scribejava.core.oauth.OAuth20Service; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; -import org.apache.log4j.LogManager; -import org.apache.log4j.Logger; import org.genesys2.client.oauth.api.GenesysApi; import org.genesys2.client.oauth.api.accession.AccessionJson; import org.genesys2.client.oauth.api.images.RepositoryImage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Genesys API client using Scribe. */ public class GenesysClient { - /** The Constant _log. */ - private static final Logger _log = LogManager.getLogger(GenesysClient.class); + /** The Constant LOG. */ + private static final Logger LOG = LoggerFactory.getLogger(GenesysClient.class); /** The mapper. */ private static ObjectMapper objectMapper; @@ -215,8 +215,8 @@ public class GenesysClient { } } - if (_log.isDebugEnabled()) { - _log.debug(method + " " + request.getCompleteUrl()); + if (LOG.isDebugEnabled()) { + LOG.debug("{} {}", method, request.getCompleteUrl()); } if (StringUtils.isNotBlank(contentType)) { @@ -225,8 +225,8 @@ public class GenesysClient { if (postBody != null) { // System.err.println("Adding data: " + data); - if (_log.isTraceEnabled()) { - _log.trace("Body: " + new String(postBody)); + if (LOG.isTraceEnabled()) { + LOG.trace("Body: {}", new String(postBody)); } request.setPayload(postBody); } @@ -240,18 +240,18 @@ public class GenesysClient { final String responseBody = response.getBody(); - _log.debug("HTTP status code " + response.getCode()); + LOG.debug("HTTP status code {}", response.getCode()); if (response.getCode() >= 200 && response.getCode() < 300) { - _log.debug("Returning response body"); + LOG.debug("Returning response body"); return responseBody; } else { if (response.getCode() == 301 || response.getCode() == 302) { - _log.debug("Redirect: " + response.getHeader(HttpConstants.LOCATION)); + LOG.debug("Redirect: {}", response.getHeader(HttpConstants.LOCATION)); throw new HttpRedirectException(response.getHeader(HttpConstants.LOCATION)); } if (response.getCode() == 401) { - _log.warn("Response error: " + response.getCode()); + LOG.warn("Response error: {}", response.getCode()); System.err.println(responseBody); if (i == 0) { refreshAccessToken(); @@ -259,10 +259,10 @@ public class GenesysClient { throw new OAuthAuthenticationException("Unauthorized"); } } else { - _log.error(method + " " + request.getCompleteUrl()); - _log.error(new String(postBody)); - _log.error("HTTP response code: " + response.getCode()); - _log.error("Response: " + responseBody); + LOG.error("{} {}", method, request.getCompleteUrl()); + LOG.error(new String(postBody)); + LOG.error("HTTP response code: {}", response.getCode()); + LOG.error("Response: {}", responseBody); if (responseBody.contains("Deadlock found when trying to get lock; try restarting transaction") || responseBody.contains("nested exception is org.hibernate.exception.LockAcquisitionException: could not execute statement") || responseBody.contains("nested exception is org.hibernate.exception.LockTimeoutException: could not execute statement")) { @@ -298,14 +298,14 @@ public class GenesysClient { */ public void refreshAccessToken() throws GenesysApiException { if (tokens.hasRefreshToken()) { - _log.info("Using Refresh Token to get new access token"); + LOG.info("Using Refresh Token to get new access token"); try { OAuth2AccessToken accessToken = service.refreshAccessToken(tokens.getRefreshToken()); tokens.setAccessToken(accessToken.getAccessToken()); - _log.info("Got new Access Token!"); + LOG.info("Got new Access Token!"); } catch (Throwable e) { - _log.info("Refresh token didn't work: " + e.getMessage()); + LOG.info("Refresh token didn't work: {}", e.getMessage()); throw new OAuthAuthenticationException("Refresh token not valid, please re-authenticate"); } } else { @@ -370,7 +370,7 @@ public class GenesysClient { return null; } - _log.debug("Sending: " + accns); + LOG.debug("Sending: {}", accns); return query(Verb.PUT, "/acn/" + instCode + "/update", null, objectMapper.writeValueAsString(accns)); } @@ -394,7 +394,7 @@ public class GenesysClient { return query(Verb.PUT, "/acn/" + instCode + "/upsert", null, jsonAccessionList); } catch (final PleaseRetryException e) { final long sleepTime = (long) (Math.pow(2, retry) * 100 + Math.pow(2, retry) * 2500 * Math.random()); - _log.warn("Retrying PUT after " + sleepTime + " ms."); + LOG.warn("Retrying PUT after {} ms.", sleepTime); Thread.sleep(sleepTime); } } @@ -424,10 +424,10 @@ public class GenesysClient { return query(Verb.PUT, "/acn/" + instCode + "/upsert", null, data); } catch (final PleaseRetryException e) { final long sleepTime = (long) (Math.pow(2, retry) * 100 + Math.pow(2, retry) * 2500 * Math.random()); - _log.warn("Retrying PUT after " + sleepTime + " ms."); + LOG.warn("Retrying PUT after {} ms.", sleepTime); Thread.sleep(sleepTime); } catch (final GenesysApiException e) { - _log.error("Failed to upload data: " + data); + LOG.error("Failed to upload data: {}", data); throw e; } } @@ -443,11 +443,11 @@ public class GenesysClient { * @throws GenesysApiException the genesys api exception */ public String updateOrganizationMembers(final String organizationSlug, final ArrayNode institutes) throws GenesysApiException { - _log.debug("Sending: " + institutes); + LOG.debug("Sending: {}", institutes); try { return query(Verb.PUT, "/org/" + organizationSlug + "/set-institutes", null, institutes.toString()); } catch (final PleaseRetryException e) { - _log.warn("Retrying PUT after some time..."); + LOG.warn("Retrying PUT after some time..."); try { Thread.sleep((long) (1000 * Math.random())); } catch (final InterruptedException e1) { @@ -466,17 +466,17 @@ public class GenesysClient { * @throws GenesysApiException the genesys api exception */ public String updateAccessionNames(final String instCode, final Collection batch) throws GenesysApiException { - _log.debug("Sending: " + batch); + LOG.debug("Sending: {}", batch); try { return query(Verb.PUT, "/acn/" + instCode + "/names", null, batch.toString()); } catch (final PleaseRetryException e) { - _log.warn("Retrying PUT after some time..."); + LOG.warn("Retrying PUT after some time..."); try { Thread.sleep((long) (1000 * Math.random())); } catch (final InterruptedException e1) { e1.printStackTrace(); } - _log.warn("Retrying PUT"); + LOG.warn("Retrying PUT"); return query(Verb.PUT, "/acn/" + instCode + "/names", null, batch.toString()); } } @@ -550,16 +550,16 @@ public class GenesysClient { try { OAuth2AccessToken accessToken = service.getAccessToken(verifierCode); - _log.info("ACCESS TOKEN: " + accessToken.getAccessToken() + " scope=" + accessToken.getScope() + " raw=" + accessToken.getRawResponse()); + LOG.info("ACCESS TOKEN: {} scope={} raw={}", accessToken.getAccessToken(), accessToken.getScope(), accessToken.getRawResponse()); final String refreshToken = accessToken.getRefreshToken(); - _log.info("REFRESH TOKEN: " + refreshToken); + LOG.info("REFRESH TOKEN: {}", refreshToken); tokens.setAccessToken(accessToken.getAccessToken()); tokens.setRefreshToken(refreshToken); } catch (IOException | InterruptedException | ExecutionException e) { - _log.error("Auth error", e); + LOG.error("Auth error", e); throw new OAuthAuthenticationException(e.getMessage()); } } @@ -1046,7 +1046,7 @@ public class GenesysClient { throw new GenesysApiException("Content-Type must be provided for the file " + file.getAbsolutePath()); } - _log.debug("Image content type: " + contentType); + LOG.debug("Image content type: {}", contentType); // PUT file on server final String json = query(Verb.PUT, String.format("/img/%1$s/acn/%2$s/", instCode, acceNumb), Collections.singletonMap("originalFilename", file.getName()), contentType,