Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gsergiu committed May 16, 2023
2 parents 310db17 + 0b9d9b5 commit b48c87e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 24 deletions.
13 changes: 4 additions & 9 deletions commons-error/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,23 @@
<artifactId>commons-error</artifactId>
<name>Api Commons - Error</name>

<properties>
<spring-boot.version>2.5.7</spring-boot.version>
<spring.version>5.3.18</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot.version}</version>
<version>${version.springBoot}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<version>${version.spring}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<version>${version.spring}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -61,7 +56,7 @@
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
<scope>provided</scope>
<!-- <scope>provided</scope> -->
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void addPathRequestParameters(Map<String, Object> 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);
}
}
Expand Down
17 changes: 15 additions & 2 deletions commons-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@

<dependencies>

<dependency>
<groupId>eu.europeana.api.commons</groupId>
<dependency>
<groupId>eu.europeana.api.commons</groupId>
<artifactId>commons-definitions</artifactId>
<version>0.3.21-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>eu.europeana.api.commons</groupId>
<artifactId>commons-error</artifactId>
<version>0.3.21-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>eu.europeana.api.commons</groupId>
<artifactId>commons-net</artifactId>
Expand Down Expand Up @@ -62,6 +68,13 @@
<version>${version.spring}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${version.springBoot}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ public String generateETag(Date timestamp, String format, String version) {
*/
protected String getMethodsForRequestPattern(HttpServletRequest request,
AbstractRequestPathMethodService requestMethodService) {
Optional<String> methodsForRequestPattern =
if(requestMethodService != null) {
Optional<String> methodsForRequestPattern =
requestMethodService.getMethodsForRequestPattern(request);

return methodsForRequestPattern.orElse(request.getMethod());
return methodsForRequestPattern.orElse(request.getMethod());
}else {
return request.getMethod();
}
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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
*
Expand Down Expand Up @@ -76,7 +81,7 @@ public ResponseEntity<EuropeanaApiErrorResponse> handleEuropeanaBaseException(Eu

return ResponseEntity
.status(e.getResponseStatus())
.contentType(MediaType.APPLICATION_JSON)
.headers(createHttpHeaders(httpRequest))
.body(response);
}

Expand All @@ -96,7 +101,7 @@ public ResponseEntity<EuropeanaApiErrorResponse> handleOtherExceptionTypes(Excep

return ResponseEntity
.status(responseStatus)
.contentType(MediaType.APPLICATION_JSON)
.headers(createHttpHeaders(httpRequest))
.body(response);
}

Expand Down Expand Up @@ -141,7 +146,7 @@ public ResponseEntity<EuropeanaApiErrorResponse> handleInputValidationError(Cons

return ResponseEntity
.status(responseStatus)
.contentType(MediaType.APPLICATION_JSON)
.headers(createHttpHeaders(httpRequest))
.body(response);
}

Expand All @@ -159,7 +164,7 @@ public ResponseEntity<EuropeanaApiErrorResponse> handleInputValidationError(Miss

return ResponseEntity
.status(responseStatus)
.contentType(MediaType.APPLICATION_JSON)
.headers(createHttpHeaders(httpRequest))
.body(response);
}

Expand All @@ -179,7 +184,7 @@ public ResponseEntity<EuropeanaApiErrorResponse> handleMediaTypeNotAcceptableExc

return ResponseEntity
.status(responseStatus)
.contentType(MediaType.APPLICATION_JSON)
.headers(createHttpHeaders(httpRequest))
.body(response);
}

Expand All @@ -205,7 +210,29 @@ public ResponseEntity<EuropeanaApiErrorResponse> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public abstract class AbstractRequestPathMethodService implements InitializingBe
*/
private final Map<String, Set<String>> requestPathMethodMap = new HashMap<>();

private final WebApplicationContext applicationContext;
protected final WebApplicationContext applicationContext;

protected AbstractRequestPathMethodService(
WebApplicationContext applicationContext) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<maven.compiler.target>${java.version}</maven.compiler.target>
<sonar.organization>europeana</sonar.organization>

<version.spring>5.3.13</version.spring>
<version.spring>5.3.18</version.spring>
<version.springBoot>2.6.1</version.springBoot>
<version.springSecurityJwt>1.1.1.RELEASE</version.springSecurityJwt>
<version.springSecurityOauth>2.5.1.RELEASE</version.springSecurityOauth>
Expand Down

0 comments on commit b48c87e

Please sign in to comment.