Skip to content

Commit

Permalink
Sonar: fix names, duplicate strings, SuppressWarnings for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
julianladisch committed Oct 22, 2024
1 parent 32f6ea2 commit 06e0030
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -67,18 +67,15 @@ Future<String> 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) {
Expand All @@ -102,10 +99,9 @@ Future<String> 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()) {
Expand All @@ -119,17 +115,20 @@ Future<String> 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) {
return Future.failedFuture(e);
}
}

private String loginFailed(String path) {
return "Login failed. POST " + path
+ " for tenant '" + tenant + "' and username '" + username + "'";
}

@Override
public Future<String> getToken() {
if (cache != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,7 +84,7 @@ String tokenResponse(HttpResponse<Buffer> 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()) {
Expand All @@ -100,14 +101,17 @@ String tokenResponse(HttpResponse<Buffer> 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();
Expand Down

0 comments on commit 06e0030

Please sign in to comment.