Skip to content

Instantly share code, notes, and snippets.

View daniel-shuy's full-sized avatar

Daniel Shuy daniel-shuy

  • SICPA
  • Malaysia
View GitHub Profile
@daniel-shuy
daniel-shuy / EntityManagerProducer.java
Last active February 25, 2020 06:42
JPA + CDI : @Inject EntityManager as a @RequestScoped bean
import javax.enterprise.context.Dependent;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Dependent
public class EntityManagerProducer {
@PersistenceContext(name = "persistence-unit") // name is Persistence Unit Name configured in persistence.xml
private EntityManager entityManager;
@daniel-shuy
daniel-shuy / ConstraintViolationExceptionMapper.java
Last active August 9, 2019 07:14
JAX-RS ExceptionMapper to handle Bean Validation's ConstraintViolationException
import com.google.common.collect.Iterables;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@daniel-shuy
daniel-shuy / KeycloakAuthorization.scala
Last active February 11, 2022 17:56
Akka HTTP (Scala) Keycloak token verifier
import akka.actor.ActorSystem
import akka.dispatch.MessageDispatcher
import akka.event.LoggingAdapter
import akka.http.scaladsl.model.headers.{Authorization, OAuth2BearerToken}
import akka.http.scaladsl.server.Directives.{optionalCookie, optionalHeaderValueByType, provide, reject}
import akka.http.scaladsl.server.{AuthorizationFailedRejection, Directive1, Directives}
import org.keycloak.adapters.KeycloakDeployment
import org.keycloak.adapters.rotation.AdapterTokenVerifier
import org.keycloak.representations.AccessToken
@daniel-shuy
daniel-shuy / README.md
Last active January 3, 2020 15:30
jib-maven-plugin configuration template

jib-maven-plugin's default example runs the build goal during the package phase. The build goal will push the docker image to the remote registry, which may be too frequent to run in the package phase for some projects.

This template instead configures the jib-maven-plugin to run the following goals in the following phases:

Phase Goal Goal Description
package buildTar Build docker image as tar file
install dockerBuild Build docker image and install in Docker daemon
@daniel-shuy
daniel-shuy / EmptyOptionalResponseBodyControllerAdvice.java
Last active February 7, 2020 03:56
Spring MVC ResponseBodyAdvice to return HTTP 404 if Response Body is an empty Optional
@ControllerAdvice
public class EmptyOptionalResponseBodyControllerAdvice implements ResponseBodyAdvice<Optional<?>> {
@Override
public boolean supports(MethodParameter returnType, @Nullable Class<? extends HttpMessageConverter<?>> converterType) {
return Optional.class.isAssignableFrom(returnType.getParameterType());
}
@Override
public Optional<?> beforeBodyWrite(Optional<?> body, @Nullable MethodParameter returnType, @Nullable MediaType selectedContentType,
@Nullable Class<? extends HttpMessageConverter<?>> selectedConverterType,
@daniel-shuy
daniel-shuy / README.md
Last active May 21, 2020 13:07
Spring Boot: Map Hibernate Validator ConstraintValidator to `@Constraint` annotation without having both in the same project
@daniel-shuy
daniel-shuy / enum-composition.ts
Last active March 31, 2021 07:14
Examples of TypeScript's powerful type system
enum Weekday {
MONDAY = 'MONDAY',
TUESDAY = 'TUESDAY',
WEDNESDAY = 'WEDNESDAY',
THURSDAY = 'THURSDAY',
FRIDAY = 'FRIDAY',
}
enum Weekend {
SATURDAY = 'SATURDAY',
@daniel-shuy
daniel-shuy / ErrorLoggingInterceptor.java
Created August 17, 2020 15:55
Spring HTTP Client ErrorLoggingInterceptor
@Component
@Slf4j
public class ErrorLoggingInterceptor implements ClientHttpRequestInterceptor {
@Nonnull
@Override
public ClientHttpResponse intercept(@Nonnull HttpRequest request, @Nonnull byte[] body, ClientHttpRequestExecution execution)
throws IOException {
var response = execution.execute(request, body);
var statusCode = response.getStatusCode();
@daniel-shuy
daniel-shuy / ValidationUtils.java
Created July 16, 2021 14:08
Get error messages from Spring BindingResult
public List<String> getErrorMessages(BindingResult bindingResult) {
return bindingResult.getAllErrors()
.stream()
.map(error -> {
var defaultMessage = error.getDefaultMessage();
if (error instanceof FieldError) {
var fieldError = (FieldError) error;
return String.format("%s %s", fieldError.getField(), defaultMessage);
} else {
return defaultMessage;
@daniel-shuy
daniel-shuy / journal.md
Last active November 14, 2024 07:59
Dev Journal