Commit 21cde930 authored by Matija Obreza's avatar Matija Obreza

Merge branch '5-logging' into 'master'

Logging

Closes #5

See merge request !5
parents e568b535 4aeb3013
Pipeline #3005 passed with stage
in 21 seconds
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
<maven-gpg-plugin.version>1.5</maven-gpg-plugin.version> <maven-gpg-plugin.version>1.5</maven-gpg-plugin.version>
<nexus-staging-maven-plugin.version>1.6.8</nexus-staging-maven-plugin.version> <nexus-staging-maven-plugin.version>1.6.8</nexus-staging-maven-plugin.version>
<slf4j.version>1.7.21</slf4j.version>
<show.deprecations>true</show.deprecations> <show.deprecations>true</show.deprecations>
</properties> </properties>
...@@ -92,11 +93,11 @@ ...@@ -92,11 +93,11 @@
<artifactId>scribejava-core</artifactId> <artifactId>scribejava-core</artifactId>
<version>4.2.0</version> <version>4.2.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>log4j</artifactId> <artifactId>jcl-over-slf4j</artifactId>
<version>1.2.17</version> <version>${slf4j.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId> <artifactId>commons-lang</artifactId>
......
...@@ -41,8 +41,8 @@ import com.github.scribejava.core.model.Verb; ...@@ -41,8 +41,8 @@ import com.github.scribejava.core.model.Verb;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager; import org.slf4j.Logger;
import org.apache.log4j.Logger; import org.slf4j.LoggerFactory;
/** /**
* Simple command line interface to Genesys. * Simple command line interface to Genesys.
...@@ -51,8 +51,8 @@ import org.apache.log4j.Logger; ...@@ -51,8 +51,8 @@ import org.apache.log4j.Logger;
*/ */
public class CLI { public class CLI {
/** The Constant _log. */ /** The Constant LOG. */
private static final Logger _log = LogManager.getLogger(CLI.class); private static final Logger LOG = LoggerFactory.getLogger(CLI.class);
/** The properties file. */ /** The properties file. */
private File propertiesFile; private File propertiesFile;
...@@ -88,7 +88,7 @@ public class CLI { ...@@ -88,7 +88,7 @@ public class CLI {
* @param args the arguments * @param args the arguments
*/ */
public static void main(final String[] args) { public static void main(final String[] args) {
_log.info("Hello World!"); LOG.info("Hello World!");
final CLI cli = new CLI(); final CLI cli = new CLI();
cli.genesysClient = GenesysClient.build(cli.loadProperties("client.properties")); cli.genesysClient = GenesysClient.build(cli.loadProperties("client.properties"));
...@@ -108,22 +108,22 @@ public class CLI { ...@@ -108,22 +108,22 @@ public class CLI {
saveProperties(); saveProperties();
} catch (final OAuthAuthenticationException e) { } catch (final OAuthAuthenticationException e) {
_log.error("Failed to fetch /me", e); LOG.error("Failed to fetch /me", e);
try { try {
authenticate(); authenticate();
} catch (OAuthAuthenticationException e1) { } catch (OAuthAuthenticationException e1) {
_log.error("Failed to authenticate", e1); LOG.error("Failed to authenticate", e1);
return; return;
} }
} catch (final Throwable e) { } catch (final Throwable e) {
_log.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
return; return;
} }
try { try {
doWork(); doWork();
} catch (final Throwable e) { } catch (final Throwable e) {
_log.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
} }
} }
...@@ -555,7 +555,7 @@ public class CLI { ...@@ -555,7 +555,7 @@ public class CLI {
if (restart) { if (restart) {
saveProperties(); saveProperties();
_log.warn("Properties udpated, please restart CLI application"); LOG.warn("Properties udpated, please restart CLI application");
System.exit(-1); System.exit(-1);
} }
} }
...@@ -592,7 +592,7 @@ public class CLI { ...@@ -592,7 +592,7 @@ public class CLI {
fis = new FileOutputStream(propertiesFile); fis = new FileOutputStream(propertiesFile);
properties.store(fis, "OAuth client properties"); properties.store(fis, "OAuth client properties");
} catch (final IOException e) { } catch (final IOException e) {
_log.error(e); LOG.error(e.getMessage(), e);
} finally { } finally {
IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fis);
} }
...@@ -612,15 +612,15 @@ public class CLI { ...@@ -612,15 +612,15 @@ public class CLI {
FileInputStream fis = null; FileInputStream fis = null;
try { try {
_log.info("Loading " + propertiesFile.getAbsolutePath()); LOG.info("Loading {}", propertiesFile.getAbsolutePath());
fis = new FileInputStream(propertiesFile); fis = new FileInputStream(propertiesFile);
properties.load(fis); properties.load(fis);
// Keep properties // Keep properties
this.properties = properties; this.properties = properties;
} catch (final FileNotFoundException e) { } catch (final FileNotFoundException e) {
_log.warn(e, e); LOG.warn(e.getMessage(), e);
} catch (final IOException e) { } catch (final IOException e) {
_log.error(e, e); LOG.error(e.getMessage(), e);
} finally { } finally {
IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fis);
} }
......
...@@ -50,19 +50,19 @@ import com.github.scribejava.core.oauth.OAuth20Service; ...@@ -50,19 +50,19 @@ import com.github.scribejava.core.oauth.OAuth20Service;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils; 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.GenesysApi;
import org.genesys2.client.oauth.api.accession.AccessionJson; import org.genesys2.client.oauth.api.accession.AccessionJson;
import org.genesys2.client.oauth.api.images.RepositoryImage; import org.genesys2.client.oauth.api.images.RepositoryImage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Genesys API client using Scribe. * Genesys API client using Scribe.
*/ */
public class GenesysClient { public class GenesysClient {
/** The Constant _log. */ /** The Constant LOG. */
private static final Logger _log = LogManager.getLogger(GenesysClient.class); private static final Logger LOG = LoggerFactory.getLogger(GenesysClient.class);
/** The mapper. */ /** The mapper. */
private static ObjectMapper objectMapper; private static ObjectMapper objectMapper;
...@@ -215,8 +215,8 @@ public class GenesysClient { ...@@ -215,8 +215,8 @@ public class GenesysClient {
} }
} }
if (_log.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
_log.debug(method + " " + request.getCompleteUrl()); LOG.debug("{} {}", method, request.getCompleteUrl());
} }
if (StringUtils.isNotBlank(contentType)) { if (StringUtils.isNotBlank(contentType)) {
...@@ -225,8 +225,8 @@ public class GenesysClient { ...@@ -225,8 +225,8 @@ public class GenesysClient {
if (postBody != null) { if (postBody != null) {
// System.err.println("Adding data: " + data); // System.err.println("Adding data: " + data);
if (_log.isTraceEnabled()) { if (LOG.isTraceEnabled()) {
_log.trace("Body: " + new String(postBody)); LOG.trace("Body: {}", new String(postBody));
} }
request.setPayload(postBody); request.setPayload(postBody);
} }
...@@ -240,18 +240,18 @@ public class GenesysClient { ...@@ -240,18 +240,18 @@ public class GenesysClient {
final String responseBody = response.getBody(); 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) { if (response.getCode() >= 200 && response.getCode() < 300) {
_log.debug("Returning response body"); LOG.debug("Returning response body");
return responseBody; return responseBody;
} else { } else {
if (response.getCode() == 301 || response.getCode() == 302) { 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)); throw new HttpRedirectException(response.getHeader(HttpConstants.LOCATION));
} }
if (response.getCode() == 401) { if (response.getCode() == 401) {
_log.warn("Response error: " + response.getCode()); LOG.warn("Response error: {}", response.getCode());
System.err.println(responseBody); System.err.println(responseBody);
if (i == 0) { if (i == 0) {
refreshAccessToken(); refreshAccessToken();
...@@ -259,10 +259,10 @@ public class GenesysClient { ...@@ -259,10 +259,10 @@ public class GenesysClient {
throw new OAuthAuthenticationException("Unauthorized"); throw new OAuthAuthenticationException("Unauthorized");
} }
} else { } else {
_log.error(method + " " + request.getCompleteUrl()); LOG.error("{} {}", method, request.getCompleteUrl());
_log.error(new String(postBody)); LOG.error(new String(postBody));
_log.error("HTTP response code: " + response.getCode()); LOG.error("HTTP response code: {}", response.getCode());
_log.error("Response: " + responseBody); LOG.error("Response: {}", responseBody);
if (responseBody.contains("Deadlock found when trying to get lock; try restarting transaction") 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.LockAcquisitionException: could not execute statement")
|| responseBody.contains("nested exception is org.hibernate.exception.LockTimeoutException: could not execute statement")) { || responseBody.contains("nested exception is org.hibernate.exception.LockTimeoutException: could not execute statement")) {
...@@ -298,14 +298,14 @@ public class GenesysClient { ...@@ -298,14 +298,14 @@ public class GenesysClient {
*/ */
public void refreshAccessToken() throws GenesysApiException { public void refreshAccessToken() throws GenesysApiException {
if (tokens.hasRefreshToken()) { if (tokens.hasRefreshToken()) {
_log.info("Using Refresh Token to get new access token"); LOG.info("Using Refresh Token to get new access token");
try { try {
OAuth2AccessToken accessToken = service.refreshAccessToken(tokens.getRefreshToken()); OAuth2AccessToken accessToken = service.refreshAccessToken(tokens.getRefreshToken());
tokens.setAccessToken(accessToken.getAccessToken()); tokens.setAccessToken(accessToken.getAccessToken());
_log.info("Got new Access Token!"); LOG.info("Got new Access Token!");
} catch (Throwable e) { } 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"); throw new OAuthAuthenticationException("Refresh token not valid, please re-authenticate");
} }
} else { } else {
...@@ -370,7 +370,7 @@ public class GenesysClient { ...@@ -370,7 +370,7 @@ public class GenesysClient {
return null; return null;
} }
_log.debug("Sending: " + accns); LOG.debug("Sending: {}", accns);
return query(Verb.PUT, "/acn/" + instCode + "/update", null, objectMapper.writeValueAsString(accns)); return query(Verb.PUT, "/acn/" + instCode + "/update", null, objectMapper.writeValueAsString(accns));
} }
...@@ -394,7 +394,7 @@ public class GenesysClient { ...@@ -394,7 +394,7 @@ public class GenesysClient {
return query(Verb.PUT, "/acn/" + instCode + "/upsert", null, jsonAccessionList); return query(Verb.PUT, "/acn/" + instCode + "/upsert", null, jsonAccessionList);
} catch (final PleaseRetryException e) { } catch (final PleaseRetryException e) {
final long sleepTime = (long) (Math.pow(2, retry) * 100 + Math.pow(2, retry) * 2500 * Math.random()); 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); Thread.sleep(sleepTime);
} }
} }
...@@ -424,10 +424,10 @@ public class GenesysClient { ...@@ -424,10 +424,10 @@ public class GenesysClient {
return query(Verb.PUT, "/acn/" + instCode + "/upsert", null, data); return query(Verb.PUT, "/acn/" + instCode + "/upsert", null, data);
} catch (final PleaseRetryException e) { } catch (final PleaseRetryException e) {
final long sleepTime = (long) (Math.pow(2, retry) * 100 + Math.pow(2, retry) * 2500 * Math.random()); 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); Thread.sleep(sleepTime);
} catch (final GenesysApiException e) { } catch (final GenesysApiException e) {
_log.error("Failed to upload data: " + data); LOG.error("Failed to upload data: {}", data);
throw e; throw e;
} }
} }
...@@ -443,11 +443,11 @@ public class GenesysClient { ...@@ -443,11 +443,11 @@ public class GenesysClient {
* @throws GenesysApiException the genesys api exception * @throws GenesysApiException the genesys api exception
*/ */
public String updateOrganizationMembers(final String organizationSlug, final ArrayNode institutes) throws GenesysApiException { public String updateOrganizationMembers(final String organizationSlug, final ArrayNode institutes) throws GenesysApiException {
_log.debug("Sending: " + institutes); LOG.debug("Sending: {}", institutes);
try { try {
return query(Verb.PUT, "/org/" + organizationSlug + "/set-institutes", null, institutes.toString()); return query(Verb.PUT, "/org/" + organizationSlug + "/set-institutes", null, institutes.toString());
} catch (final PleaseRetryException e) { } catch (final PleaseRetryException e) {
_log.warn("Retrying PUT after some time..."); LOG.warn("Retrying PUT after some time...");
try { try {
Thread.sleep((long) (1000 * Math.random())); Thread.sleep((long) (1000 * Math.random()));
} catch (final InterruptedException e1) { } catch (final InterruptedException e1) {
...@@ -466,17 +466,17 @@ public class GenesysClient { ...@@ -466,17 +466,17 @@ public class GenesysClient {
* @throws GenesysApiException the genesys api exception * @throws GenesysApiException the genesys api exception
*/ */
public String updateAccessionNames(final String instCode, final Collection<ObjectNode> batch) throws GenesysApiException { public String updateAccessionNames(final String instCode, final Collection<ObjectNode> batch) throws GenesysApiException {
_log.debug("Sending: " + batch); LOG.debug("Sending: {}", batch);
try { try {
return query(Verb.PUT, "/acn/" + instCode + "/names", null, batch.toString()); return query(Verb.PUT, "/acn/" + instCode + "/names", null, batch.toString());
} catch (final PleaseRetryException e) { } catch (final PleaseRetryException e) {
_log.warn("Retrying PUT after some time..."); LOG.warn("Retrying PUT after some time...");
try { try {
Thread.sleep((long) (1000 * Math.random())); Thread.sleep((long) (1000 * Math.random()));
} catch (final InterruptedException e1) { } catch (final InterruptedException e1) {
e1.printStackTrace(); e1.printStackTrace();
} }
_log.warn("Retrying PUT"); LOG.warn("Retrying PUT");
return query(Verb.PUT, "/acn/" + instCode + "/names", null, batch.toString()); return query(Verb.PUT, "/acn/" + instCode + "/names", null, batch.toString());
} }
} }
...@@ -550,16 +550,16 @@ public class GenesysClient { ...@@ -550,16 +550,16 @@ public class GenesysClient {
try { try {
OAuth2AccessToken accessToken = service.getAccessToken(verifierCode); 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(); final String refreshToken = accessToken.getRefreshToken();
_log.info("REFRESH TOKEN: " + refreshToken); LOG.info("REFRESH TOKEN: {}", refreshToken);
tokens.setAccessToken(accessToken.getAccessToken()); tokens.setAccessToken(accessToken.getAccessToken());
tokens.setRefreshToken(refreshToken); tokens.setRefreshToken(refreshToken);
} catch (IOException | InterruptedException | ExecutionException e) { } catch (IOException | InterruptedException | ExecutionException e) {
_log.error("Auth error", e); LOG.error("Auth error", e);
throw new OAuthAuthenticationException(e.getMessage()); throw new OAuthAuthenticationException(e.getMessage());
} }
} }
...@@ -1046,7 +1046,7 @@ public class GenesysClient { ...@@ -1046,7 +1046,7 @@ public class GenesysClient {
throw new GenesysApiException("Content-Type must be provided for the file " + file.getAbsolutePath()); 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 // 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, final String json = query(Verb.PUT, String.format("/img/%1$s/acn/%2$s/", instCode, acceNumb), Collections.singletonMap("originalFilename", file.getName()), contentType,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment