Skip to content

Commit

Permalink
avoid NPE when support beans are not defined in the apis #EA-3383
Browse files Browse the repository at this point in the history
  • Loading branch information
gsergiu committed May 15, 2023
1 parent ee3aea0 commit 03be748
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
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
2 changes: 1 addition & 1 deletion commons-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<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 @@ -79,8 +79,7 @@ public String generateETag(Date timestamp, String format, String version) {
serialCode += "0.0.1-SNAPSHOT".hashCode();
}

// see also https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
return "W/\"" + Long.toHexString(serialCode) + "\"";
return "\"" + Long.toHexString(serialCode) + "\"";
}

/**
Expand All @@ -90,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
Expand Up @@ -216,11 +216,23 @@ public ResponseEntity<EuropeanaApiErrorResponse> handleMethodArgNotValidExceptio

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);
if(requestPathMethodService!=null) {
String allowHeaderValue = requestPathMethodService.getMethodsForRequestPattern(httpRequest).orElse(httpRequest.getMethod());

//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;
}
}

0 comments on commit 03be748

Please sign in to comment.