Skip to content

Commit

Permalink
Merge pull request #3 from Tosan/reduce-log
Browse files Browse the repository at this point in the history
[enhancement] reduce logs on certificate loading
  • Loading branch information
tosandev authored Nov 27, 2023
2 parents ecd81e8 + 659e675 commit 6ea66a9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* @since 1/22/2021
*/
public class CertificateLoader {

private static final Logger LOGGER = LoggerFactory.getLogger(CertificateLoader.class);

private CertificateLoader() {
Expand Down Expand Up @@ -76,7 +75,7 @@ public static SSLContext buildSSLContext(HttpClientProperties.SSLConfiguration s
public static TrustManagerFactory getTrustManagerFactory(HttpClientProperties.SSLConfiguration sslConfiguration) {
HttpClientProperties.TruststoreConfiguration truststoreConfiguration = sslConfiguration.getTruststore();
if (StringUtils.isAnyBlank(truststoreConfiguration.getPath(), truststoreConfiguration.getPassword())) {
LOGGER.warn("Truststore Configuration incomplete, skipping");
LOGGER.debug("Truststore Configuration incomplete, skipping");
return null;
}

Expand All @@ -85,7 +84,7 @@ public static TrustManagerFactory getTrustManagerFactory(HttpClientProperties.SS
keyStore.load(is, truststoreConfiguration.getPassword().toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
LOGGER.info("Truststore initialized successfully");
LOGGER.debug("Truststore initialized successfully");
return tmf;
} catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException ex) {
LOGGER.error("Truststore could not be loaded, skipping", ex);
Expand All @@ -100,7 +99,7 @@ public static TrustManagerFactory getTrustManagerFactory(HttpClientProperties.SS
public static KeyManagerFactory getKeyManagerFactory(HttpClientProperties.SSLConfiguration sslConfiguration) {
HttpClientProperties.KeystoreConfiguration keystoreConfiguration = sslConfiguration.getKeystore();
if (StringUtils.isAnyBlank(keystoreConfiguration.getPath(), keystoreConfiguration.getPassword())) {
LOGGER.warn("Keystore Configuration incomplete, skipping");
LOGGER.debug("Keystore Configuration incomplete, skipping");
return null;
}
try (FileInputStream is = new FileInputStream(ResourceUtils.getFile(keystoreConfiguration.getPath()))) {
Expand All @@ -109,7 +108,7 @@ public static KeyManagerFactory getKeyManagerFactory(HttpClientProperties.SSLCon

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, keystoreConfiguration.getPassword().toCharArray());
LOGGER.info("Keystore initialized successfully");
LOGGER.debug("Keystore initialized successfully");
return kmf;
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | UnrecoverableKeyException ex) {
LOGGER.error("Keystore could not be loaded, skipping", ex);
Expand All @@ -129,13 +128,13 @@ public static KeyStore getTrustStore(HttpClientProperties.SSLConfiguration sslCo

private static KeyStore getStore(String path, String password, String type) {
if (StringUtils.isAnyBlank(path, password)) {
LOGGER.warn("Keystore Configuration incomplete, skipping");
LOGGER.debug("Keystore Configuration incomplete, skipping");
return null;
}
try (FileInputStream is = new FileInputStream(ResourceUtils.getFile(path))) {
KeyStore keyStore = KeyStore.getInstance(type);
keyStore.load(is, password.toCharArray());
LOGGER.info("Keystore initialized successfully");
LOGGER.debug("Keystore initialized successfully");
return keyStore;
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException ex) {
LOGGER.error("Keystore could not be loaded, skipping", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public Exception decode(String methodKey, Response response) {
StreamUtils.copy(body.asInputStream(), output);
String responseBody = output.toString();
int status = response.status();
LOGGER.info("ServerErrorResponse :\n ResponseStatus:{}\n ResponseBody:{}", status,
responseBody);
LOGGER.info("ServerErrorResponse :\n ResponseStatus:{}\n ResponseBody:{}", status, responseBody);
Map<String, Class<? extends Exception>> exceptionMap = customErrorDecoderConfig.getExceptionMap();
if (status >= 400 && status < 500) {
return extractBadRequestErrorException(responseBody, exceptionMap);
}
return extractInternalServerErrorException(responseBody, status, exceptionMap);
} catch (Exception e) {
LOGGER.error("ServerInternalRuntimeException", e);
return extractCustomErrorDecoderException(e);
}
}
Expand All @@ -77,7 +77,6 @@ protected Exception extractBadRequestErrorException(String responseBody,
}

protected Exception extractCustomErrorDecoderException(Exception e) {
LOGGER.warn("ServerInternalRuntimeException", e);
InternalServerException internalServerException = new InternalServerException("Internal error", e);
internalServerException.setErrorType("customErrorDecoder");
internalServerException.setErrorCode(e.getClass().getSimpleName());
Expand Down Expand Up @@ -108,7 +107,6 @@ protected Exception extractInternalServerErrorException(String responseBody, int
@Override
public void afterPropertiesSet() {
ExceptionExtractType exceptionExtractType;

if (customErrorDecoderConfig != null) {
if (customErrorDecoderConfig.getObjectMapper() != null) {
objectMapper = customErrorDecoderConfig.getObjectMapper();
Expand Down Expand Up @@ -141,11 +139,11 @@ public void afterPropertiesSet() {
}
}

protected <T> T jsonToObject(String string, Class<T> type) {
protected <T> T jsonToObject(String string, Class<T> type) {
try {
return objectMapper.readValue(string, type);
} catch (Exception e) {
throw new JsonConvertException("error in converting Json to object");
throw new JsonConvertException("error in converting Json to object", e);
}
}

Expand All @@ -168,8 +166,7 @@ private void extractAndFillMap(Class<? extends Exception> type) {
exceptionMap.put(exceptionKey, type);
} catch (IllegalAccessException | NoSuchMethodException | InstantiationException |
InvocationTargetException e) {
LOGGER.warn("error on construction of {}.", type.getSimpleName());
LOGGER.warn("Exception:", e);
LOGGER.error("error on construction of " + type.getSimpleName(), e);
}
} else if (exceptionExtractType.equals(ExceptionExtractType.FULL_NAME_REFLECTION)) {
exceptionMap.put(type.getName(), type);
Expand Down

0 comments on commit 6ea66a9

Please sign in to comment.