diff --git a/okapi-common/src/main/java/org/folio/okapi/common/refreshtoken/client/impl/LoginClient.java b/okapi-common/src/main/java/org/folio/okapi/common/refreshtoken/client/impl/LoginClient.java index d167ac898..763cc5074 100644 --- a/okapi-common/src/main/java/org/folio/okapi/common/refreshtoken/client/impl/LoginClient.java +++ b/okapi-common/src/main/java/org/folio/okapi/common/refreshtoken/client/impl/LoginClient.java @@ -20,7 +20,7 @@ @java.lang.SuppressWarnings({"squid:S1075"}) // URIs should not be hardcoded public class LoginClient implements Client { - private static final Logger logger = LogManager.getLogger(LoginClient.class); + private static final Logger LOGGER = LogManager.getLogger(LoginClient.class); private static final String LOGIN_EXPIRY_PATH = "/authn/login-with-expiry"; @@ -67,18 +67,15 @@ Future getTokenLegacy(JsonObject payload) { .putHeader(XOkapiHeaders.TENANT, tenant) .sendJsonObject(payload).map(res -> { if (res.statusCode() != 201) { - var msg = "Login failed. POST " + LOGIN_LEGACY_PATH - + " for tenant '" + tenant + "' and username '" + username - + "' returned status " + res.statusCode() + ": " + res.bodyAsString(); - logger.error("{}", msg); + var msg = loginFailed(LOGIN_LEGACY_PATH) + + " returned status " + res.statusCode() + ": " + res.bodyAsString(); + LOGGER.error("{}", msg); throw new ClientException(msg); } String token = res.getHeader(XOkapiHeaders.TOKEN); if (token == null) { - var msg = "Login failed. POST " + LOGIN_LEGACY_PATH - + " for tenant '" + tenant + "' and username '" + username - + "' did not return token."; - logger.error("{}", msg); + var msg = loginFailed(LOGIN_LEGACY_PATH) + " did not return token."; + LOGGER.error("{}", msg); throw new ClientException(msg); } if (cache != null) { @@ -102,10 +99,9 @@ Future getTokenWithExpiry(JsonObject payload) { if (res.statusCode() == 404) { return null; } else if (res.statusCode() != 201) { - var msg = "Login failed. POST " + LOGIN_EXPIRY_PATH - + " for tenant '" + tenant + "' and username '" + username - + "' returned status " + res.statusCode() + ": " + res.bodyAsString(); - logger.error("{}", msg); + var msg = loginFailed(LOGIN_EXPIRY_PATH) + + " returned status " + res.statusCode() + ": " + res.bodyAsString(); + LOGGER.error("{}", msg); throw new ClientException(msg); } for (String v : res.cookies()) { @@ -119,10 +115,8 @@ Future getTokenWithExpiry(JsonObject payload) { return cookie.value(); } } - var msg = "Login failed. POST " + LOGIN_EXPIRY_PATH - + " for tenant '" + tenant + "' and username '" + username - + "' did not return access token"; - logger.error("{}", msg); + var msg = loginFailed(LOGIN_EXPIRY_PATH) + " did not return access token"; + LOGGER.error("{}", msg); throw new ClientException(msg); }); } catch (Exception e) { @@ -130,6 +124,11 @@ Future getTokenWithExpiry(JsonObject payload) { } } + private String loginFailed(String path) { + return "Login failed. POST " + path + + " for tenant '" + tenant + "' and username '" + username + "'"; + } + @Override public Future getToken() { if (cache != null) { diff --git a/okapi-common/src/main/java/org/folio/okapi/common/refreshtoken/client/impl/RefreshClient.java b/okapi-common/src/main/java/org/folio/okapi/common/refreshtoken/client/impl/RefreshClient.java index 21d252b92..1ee51f495 100644 --- a/okapi-common/src/main/java/org/folio/okapi/common/refreshtoken/client/impl/RefreshClient.java +++ b/okapi-common/src/main/java/org/folio/okapi/common/refreshtoken/client/impl/RefreshClient.java @@ -17,11 +17,12 @@ import org.folio.okapi.common.refreshtoken.client.ClientOptions; import org.folio.okapi.common.refreshtoken.tokencache.RefreshTokenCache; -@java.lang.SuppressWarnings({"squid:S1075"}) // URIs should not be hardcoded +@SuppressWarnings({"squid:S1075"}) // URIs should not be hardcoded public class RefreshClient implements Client { - private static final Logger logger = LogManager.getLogger(RefreshClient.class); + private static final Logger LOGGER = LogManager.getLogger(RefreshClient.class); + private static final int PAYLOAD_LOGGING_MAX_LENGTH = 200; private static final String REFRESH_PATH = "/authn/refresh"; private final ClientOptions clientOptions; @@ -83,7 +84,7 @@ String tokenResponse(HttpResponse res) { var msg = "Token refresh failed. POST " + REFRESH_PATH + " for tenant '" + tenant + "' and refreshtoken '" + payload() + "' returned status " + res.statusCode() + ": " + res.bodyAsString(); - logger.error("{}", msg); + LOGGER.error("{}", msg); throw new ClientException(msg); } for (String v: res.cookies()) { @@ -100,14 +101,17 @@ String tokenResponse(HttpResponse res) { var msg = "Token refresh failed. POST " + REFRESH_PATH + " for tenant '" + tenant + "' and refreshtoken '" + payload() + "' did not return access token"; - logger.error("{}", msg); + LOGGER.error("{}", msg); throw new ClientException(msg); } + @SuppressWarnings({ // don't throw exceptions while logging an error + "java:S1166", // suppress "Either log or rethrow this exception." + "java:S2221", // suppress "Catch a list of specific exception subtypes instead." + }) String payload() { - final var maxlength = 200; - if (refreshToken != null && refreshToken.length() > maxlength) { - return "too long: " + refreshToken.substring(0, maxlength) + "..."; + if (refreshToken != null && refreshToken.length() > PAYLOAD_LOGGING_MAX_LENGTH) { + return "too long: " + refreshToken.substring(0, PAYLOAD_LOGGING_MAX_LENGTH) + "..."; } try { return new OkapiToken(refreshToken).getPayloadWithoutValidation().encode();