diff --git a/commons-error/pom.xml b/commons-error/pom.xml index d719a152..6718ae99 100644 --- a/commons-error/pom.xml +++ b/commons-error/pom.xml @@ -10,28 +10,23 @@ commons-error Api Commons - Error - - 2.5.7 - 5.3.18 - - org.springframework.boot spring-boot-autoconfigure - ${spring-boot.version} + ${version.springBoot} provided org.springframework spring-web - ${spring.version} + ${version.spring} provided org.springframework spring-webmvc - ${spring.version} + ${version.spring} provided @@ -61,7 +56,7 @@ jakarta.validation jakarta.validation-api 2.0.2 - provided + io.micrometer diff --git a/commons-error/src/main/java/eu/europeana/api/commons/error/EuropeanaApiErrorAttributes.java b/commons-error/src/main/java/eu/europeana/api/commons/error/EuropeanaApiErrorAttributes.java index b5c626f7..857868cb 100644 --- a/commons-error/src/main/java/eu/europeana/api/commons/error/EuropeanaApiErrorAttributes.java +++ b/commons-error/src/main/java/eu/europeana/api/commons/error/EuropeanaApiErrorAttributes.java @@ -54,7 +54,7 @@ private void addPathRequestParameters(Map errorAttributes, WebRe String paramName = it.next(); s.append(paramName); String paramValue = webRequest.getParameter(paramName); - if (!StringUtils.isEmpty(paramValue)) { + if (StringUtils.hasText(paramValue)) { s.append("=").append(paramValue); } } diff --git a/commons-web/pom.xml b/commons-web/pom.xml index c58126b8..e4248d0c 100644 --- a/commons-web/pom.xml +++ b/commons-web/pom.xml @@ -18,12 +18,18 @@ - - eu.europeana.api.commons + + eu.europeana.api.commons commons-definitions 0.3.21-SNAPSHOT + + eu.europeana.api.commons + commons-error + 0.3.21-SNAPSHOT + + eu.europeana.api.commons commons-net @@ -62,6 +68,13 @@ ${version.spring} + + org.springframework.boot + spring-boot-autoconfigure + ${version.springBoot} + provided + + org.apache.logging.log4j log4j-core diff --git a/commons-web/src/main/java/eu/europeana/api/commons/web/controller/BaseRestController.java b/commons-web/src/main/java/eu/europeana/api/commons/web/controller/BaseRestController.java index 31ed505c..c9f1afcb 100644 --- a/commons-web/src/main/java/eu/europeana/api/commons/web/controller/BaseRestController.java +++ b/commons-web/src/main/java/eu/europeana/api/commons/web/controller/BaseRestController.java @@ -89,10 +89,13 @@ public String generateETag(Date timestamp, String format, String version) { */ protected String getMethodsForRequestPattern(HttpServletRequest request, AbstractRequestPathMethodService requestMethodService) { - Optional methodsForRequestPattern = + if(requestMethodService != null) { + Optional methodsForRequestPattern = requestMethodService.getMethodsForRequestPattern(request); - - return methodsForRequestPattern.orElse(request.getMethod()); + return methodsForRequestPattern.orElse(request.getMethod()); + }else { + return request.getMethod(); + } } } diff --git a/commons-error/src/main/java/eu/europeana/api/commons/error/EuropeanaGlobalExceptionHandler.java b/commons-web/src/main/java/eu/europeana/api/commons/web/exception/EuropeanaGlobalExceptionHandler.java similarity index 85% rename from commons-error/src/main/java/eu/europeana/api/commons/error/EuropeanaGlobalExceptionHandler.java rename to commons-web/src/main/java/eu/europeana/api/commons/web/exception/EuropeanaGlobalExceptionHandler.java index f3b6869d..9a5e4a1e 100644 --- a/commons-error/src/main/java/eu/europeana/api/commons/error/EuropeanaGlobalExceptionHandler.java +++ b/commons-web/src/main/java/eu/europeana/api/commons/web/exception/EuropeanaGlobalExceptionHandler.java @@ -1,4 +1,4 @@ -package eu.europeana.api.commons.error; +package eu.europeana.api.commons.web.exception; import java.util.List; import java.util.Set; @@ -21,6 +21,9 @@ import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; +import eu.europeana.api.commons.error.EuropeanaApiErrorResponse; +import eu.europeana.api.commons.error.EuropeanaApiException; +import eu.europeana.api.commons.web.service.AbstractRequestPathMethodService; /** * Global exception handler that catches all errors and logs the interesting ones @@ -35,6 +38,8 @@ public class EuropeanaGlobalExceptionHandler { private static final Logger LOG = LogManager.getLogger(EuropeanaGlobalExceptionHandler.class); + protected AbstractRequestPathMethodService requestPathMethodService; + /** * Checks if {@link EuropeanaApiException} instances should be logged or not * @@ -76,7 +81,7 @@ public ResponseEntity handleEuropeanaBaseException(Eu return ResponseEntity .status(e.getResponseStatus()) - .contentType(MediaType.APPLICATION_JSON) + .headers(createHttpHeaders(httpRequest)) .body(response); } @@ -96,7 +101,7 @@ public ResponseEntity handleOtherExceptionTypes(Excep return ResponseEntity .status(responseStatus) - .contentType(MediaType.APPLICATION_JSON) + .headers(createHttpHeaders(httpRequest)) .body(response); } @@ -141,7 +146,7 @@ public ResponseEntity handleInputValidationError(Cons return ResponseEntity .status(responseStatus) - .contentType(MediaType.APPLICATION_JSON) + .headers(createHttpHeaders(httpRequest)) .body(response); } @@ -159,7 +164,7 @@ public ResponseEntity handleInputValidationError(Miss return ResponseEntity .status(responseStatus) - .contentType(MediaType.APPLICATION_JSON) + .headers(createHttpHeaders(httpRequest)) .body(response); } @@ -179,7 +184,7 @@ public ResponseEntity handleMediaTypeNotAcceptableExc return ResponseEntity .status(responseStatus) - .contentType(MediaType.APPLICATION_JSON) + .headers(createHttpHeaders(httpRequest)) .body(response); } @@ -205,7 +210,29 @@ public ResponseEntity handleMethodArgNotValidExceptio return ResponseEntity .status(responseStatus) - .contentType(MediaType.APPLICATION_JSON) + .headers(createHttpHeaders(httpRequest)) .body(response); } + + protected HttpHeaders createHttpHeaders(HttpServletRequest httpRequest) { + HttpHeaders headers = new HttpHeaders(); + //enforce application/json as content type, it is the only serialization supported for exceptions + headers.setContentType(MediaType.APPLICATION_JSON); + + //autogenerate allow header if the service is configured + if(getRequestPathMethodService()!=null) { + String allowHeaderValue = getRequestPathMethodService().getMethodsForRequestPattern(httpRequest).orElse(httpRequest.getMethod()); + headers.add(HttpHeaders.ALLOW, allowHeaderValue); + } + return headers; + } + + /** + * The bean needs to be defined in the individual APIs + * + * @return + */ + AbstractRequestPathMethodService getRequestPathMethodService() { + return requestPathMethodService; + } } diff --git a/commons-web/src/main/java/eu/europeana/api/commons/web/service/AbstractRequestPathMethodService.java b/commons-web/src/main/java/eu/europeana/api/commons/web/service/AbstractRequestPathMethodService.java index 949577e9..b3f31f39 100644 --- a/commons-web/src/main/java/eu/europeana/api/commons/web/service/AbstractRequestPathMethodService.java +++ b/commons-web/src/main/java/eu/europeana/api/commons/web/service/AbstractRequestPathMethodService.java @@ -32,7 +32,7 @@ public abstract class AbstractRequestPathMethodService implements InitializingBe */ private final Map> requestPathMethodMap = new HashMap<>(); - private final WebApplicationContext applicationContext; + protected final WebApplicationContext applicationContext; protected AbstractRequestPathMethodService( WebApplicationContext applicationContext) { diff --git a/pom.xml b/pom.xml index 5e313cd4..857cf87d 100644 --- a/pom.xml +++ b/pom.xml @@ -40,7 +40,7 @@ ${java.version} europeana - 5.3.13 + 5.3.18 2.6.1 1.1.1.RELEASE 2.5.1.RELEASE