Skip to content

Commit

Permalink
Clean up loggers, guava usage and instance of usages
Browse files Browse the repository at this point in the history
  • Loading branch information
amanteaux committed Sep 18, 2024
1 parent 16297ac commit fc2b6a4
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* Config includer that forces all includes to resolve or else fail with a ConfigException.
*
*
* See https://github.com/lightbend/config/issues/587
*/
public class RequiredIncluder implements ConfigIncluder, ConfigIncluderFile, ConfigIncluderURL,
Expand Down Expand Up @@ -42,26 +42,26 @@ public ConfigObject include(ConfigIncludeContext context, String what) {
@Override
public ConfigObject includeResources(ConfigIncludeContext context, String what) {
context = verifyDelegateAndStrictContext(context);
if (delegate instanceof ConfigIncluderClasspath) {
return ((ConfigIncluderClasspath) delegate).includeResources(context, what);
if (delegate instanceof ConfigIncluderClasspath configIncluderClasspath) {
return configIncluderClasspath.includeResources(context, what);
}
return ConfigFactory.parseResources(what, context.parseOptions()).root();
}

@Override
public ConfigObject includeFile(ConfigIncludeContext context, File what) {
context = verifyDelegateAndStrictContext(context);
if (delegate instanceof ConfigIncluderFile) {
return ((ConfigIncluderFile) delegate).includeFile(context, what);
if (delegate instanceof ConfigIncluderFile configIncluderFile) {
return configIncluderFile.includeFile(context, what);
}
return ConfigFactory.parseFile(what, context.parseOptions()).root();
}

@Override
public ConfigObject includeURL(ConfigIncludeContext context, URL what) {
context = verifyDelegateAndStrictContext(context);
if (delegate instanceof ConfigIncluderURL) {
return ((ConfigIncluderURL) delegate).includeURL(context, what);
if (delegate instanceof ConfigIncluderURL configIncluderUrl) {
return configIncluderUrl.includeURL(context, what);
}
return ConfigFactory.parseURL(what, context.parseOptions()).root();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.coreoz.plume.mail;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.assertj.core.api.Assertions;
import org.junit.Test;

import com.google.common.collect.ImmutableMap;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.util.Map;

public class MailerProviderTest {

@Test
public void should_generate_well_form_property_file() {
Config config = ConfigFactory.parseMap(ImmutableMap.of(
Config config = ConfigFactory.parseMap(Map.of(
"mail.\"javaxmail.debug\"", "true",
"mail.transportstrategy", "SMTP_SSL"
));
Expand All @@ -22,5 +21,4 @@ public void should_generate_well_form_property_file() {
+ "simplejavamail.transportstrategy=SMTP_SSL"
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import com.codahale.metrics.health.HealthCheck;
import com.coreoz.plume.db.transaction.TransactionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;

@Slf4j
public class DatabaseHealthCheck extends HealthCheck {
private static final Logger logger = LoggerFactory.getLogger(DatabaseHealthCheck.class);
private final TransactionManager transactionManager;

public DatabaseHealthCheck(TransactionManager transactionManager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class MetricsCheckBuilder {
private final MetricRegistry metricRegistry = new MetricRegistry();

public MetricsCheckBuilder registerMetric(String name, Metric metric) {
if (metric instanceof MetricSet) {
this.metricRegistry.registerAll(name, (MetricSet) metric);
if (metric instanceof MetricSet metricSet) {
this.metricRegistry.registerAll(name, metricSet);
} else {
this.metricRegistry.register(name, metric);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package com.coreoz.plume.jersey.async;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

import javax.ws.rs.container.AsyncResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coreoz.plume.jersey.errors.ErrorResponse;
import com.coreoz.plume.jersey.errors.WsError;
import com.google.common.collect.ImmutableList;
import lombok.extern.slf4j.Slf4j;

import javax.ws.rs.container.AsyncResponse;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

/**
* Provides a bridge between JAX-RS asynchronous API and Java 8 asynchronous API
* @see CompletableFuture
* @see AsyncResponse
*/
@Slf4j
public class AsyncJersey {

private static final Logger logger = LoggerFactory.getLogger(AsyncJersey.class);

/**
* Provides a {@link BiConsumer} from an {@link AsyncResponse}
* that should be called when a {@link CompletableFuture} is terminated.<br>
Expand All @@ -41,10 +36,8 @@ public class AsyncJersey {
asyncResponse.resume(responseBody);
} else {
logger.error("An exception was raised during the promise execution", exception);
asyncResponse.resume(new ErrorResponse(WsError.INTERNAL_ERROR, ImmutableList.of()));
asyncResponse.resume(new ErrorResponse(WsError.INTERNAL_ERROR, List.of()));
}
};
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import org.apache.commons.validator.routines.EmailValidator;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
* Provides common validators that will throw {@link WsException}
* when the input data does not pass the validator.
Expand All @@ -21,36 +22,36 @@ public class Validators {

public static @NotNull String checkRequired(@NotNull String fieldName, @Nullable String fieldValue) {
if(Strings.isNullOrEmpty(fieldValue)) {
throw new WsException(WsError.FIELD_REQUIRED, ImmutableList.of(fieldName));
throw new WsException(WsError.FIELD_REQUIRED, List.of(fieldName));
}
return fieldValue;
}

public static<T> @NotNull T checkRequired(@NotNull String fieldName, @Nullable T fieldValue) {
if(fieldValue == null) {
throw new WsException(WsError.FIELD_REQUIRED, ImmutableList.of(fieldName));
throw new WsException(WsError.FIELD_REQUIRED, List.of(fieldName));
}
return fieldValue;
}

public static @Nullable String checkEmail(@NotNull String fieldName, @Nullable String fieldValue) {
if(!EmailValidator.getInstance().isValid(fieldValue)) {
throw new WsException(WsError.EMAIL_INVALID, ImmutableList.of(fieldName));
throw new WsException(WsError.EMAIL_INVALID, List.of(fieldName));
}
return fieldValue;
}

public static @Nullable String checkHexaColor(@NotNull String fieldName, @Nullable String fieldValue) {
if(fieldValue != null && !fieldValue.matches("[0-9a-fA-F]{6}")) {
throw new WsException(WsError.COLOR_INVALID, ImmutableList.of(fieldName));
throw new WsException(WsError.COLOR_INVALID, List.of(fieldName));
}
return fieldValue;
}

public static @Nullable String checkHexaColorWithStartingHash(@NotNull String fieldName, @Nullable String fieldValue) {
if(fieldValue != null) {
if (!fieldValue.startsWith("#")) {
throw new WsException(WsError.COLOR_INVALID, ImmutableList.of(fieldName));
throw new WsException(WsError.COLOR_INVALID, List.of(fieldName));
}
return "#" + checkHexaColor(fieldName, fieldValue.substring(1));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.coreoz.plume.jersey.errors;

import com.google.common.collect.ImmutableList;

import java.io.Serial;
import java.util.List;

/**
* A {@link RuntimeException} that stops the execution of the web-service
Expand All @@ -16,11 +14,11 @@ public class WsException extends RuntimeException {
private final Iterable<String> statusArguments;

public WsException(WsError error) {
this(error, ImmutableList.of());
this(error, List.of());
}

public WsException(WsError error, String... statusArguments) {
this(error, ImmutableList.copyOf(statusArguments));
this(error, List.of(statusArguments));
}

public WsException(WsError error, Iterable<String> statusArguments) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package com.coreoz.plume.jersey.errors;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.cfg.Annotations;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import lombok.extern.slf4j.Slf4j;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.cfg.Annotations;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;

/**
* A Jackson JSON provider that throws a {@link JsonRequestParseException}
* during the parsing.
* It is useful to return proper 400 errors when JSON request input is not valid
* @see JacksonJaxbJsonProvider
*/
@Slf4j
public class WsJacksonJsonProvider extends JacksonJaxbJsonProvider {

private static final Logger logger = LoggerFactory.getLogger(WsJacksonJsonProvider.class);

public WsJacksonJsonProvider() {
super();
}
Expand All @@ -48,5 +43,4 @@ public Object readFrom(Class<Object> type, Type genericType, Annotation[] annota
throw new JsonRequestParseException();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
package com.coreoz.plume.jersey.errors;

import lombok.extern.slf4j.Slf4j;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableList;

@Slf4j
@Provider
public class WsResultExceptionMapper implements ExceptionMapper<Throwable> {

private static final Logger logger = LoggerFactory.getLogger(WsResultExceptionMapper.class);

@Override
public Response toResponse(Throwable e) {
if (e instanceof WsException) {
WsException wsException = (WsException) e;
return Response
if (e instanceof WsException wsException) {
return Response
.status(Status.BAD_REQUEST)
.entity(new ErrorResponse(wsException.getError(), wsException.getStatusArguments()))
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
}
if(e instanceof WebApplicationException) {
return ((WebApplicationException) e).getResponse();
if(e instanceof WebApplicationException webApplicationException) {
return webApplicationException.getResponse();
}
if(e instanceof JsonRequestParseException) {
return Response
.status(Status.BAD_REQUEST)
.entity(new ErrorResponse(WsError.REQUEST_INVALID, ImmutableList.of("JSON object supplied in request is invalid")))
.entity(new ErrorResponse(WsError.REQUEST_INVALID, List.of("JSON object supplied in request is invalid")))
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
}
Expand All @@ -42,9 +37,8 @@ public Response toResponse(Throwable e) {

return Response
.status(Status.INTERNAL_SERVER_ERROR)
.entity(new ErrorResponse(WsError.INTERNAL_ERROR, ImmutableList.of()))
.entity(new ErrorResponse(WsError.INTERNAL_ERROR, List.of()))
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
}

}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.coreoz.plume.jersey.grizzly;

import lombok.extern.slf4j.Slf4j;
import org.glassfish.grizzly.http.server.ErrorPageGenerator;
import org.glassfish.grizzly.http.server.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Replace the default Grizzly error handler to avoid leaking server information
*/
@Slf4j
public class GrizzlyErrorPageHandler implements ErrorPageGenerator {

private static final Logger logger = LoggerFactory.getLogger(GrizzlyErrorPageHandler.class);

@Override
public String generate(Request request, int status, String reasonPhrase, String description,
Throwable exception) {
Expand All @@ -27,5 +24,4 @@ public String generate(Request request, int status, String reasonPhrase, String

return status + " " + reasonPhrase;
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.coreoz.plume.jersey.java8;

import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;

import javax.ws.rs.ext.ParamConverter;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

import javax.ws.rs.ext.ParamConverter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;

@Slf4j
public class InstantConverter implements ParamConverter<Instant> {

private static final Logger logger = LoggerFactory.getLogger(InstantConverter.class);

@Override
public Instant fromString(String value) {
try {
Expand All @@ -37,5 +32,4 @@ public Instant fromString(String value) {
public String toString(Instant value) {
return value == null ? null : value.toString();
}

}
Loading

0 comments on commit fc2b6a4

Please sign in to comment.