summaryrefslogtreecommitdiffstats
path: root/aai-resources
diff options
context:
space:
mode:
Diffstat (limited to 'aai-resources')
-rw-r--r--aai-resources/pom.xml2
-rw-r--r--aai-resources/src/main/java/org/onap/aai/rest/ExceptionHandler.java106
2 files changed, 52 insertions, 56 deletions
diff --git a/aai-resources/pom.xml b/aai-resources/pom.xml
index abfd928..b69b93c 100644
--- a/aai-resources/pom.xml
+++ b/aai-resources/pom.xml
@@ -28,7 +28,7 @@
<parent>
<groupId>org.onap.aai.resources</groupId>
<artifactId>resources</artifactId>
- <version>1.13.5-SNAPSHOT</version>
+ <version>1.13.6-SNAPSHOT</version>
</parent>
<properties>
<java.version>1.8</java.version>
diff --git a/aai-resources/src/main/java/org/onap/aai/rest/ExceptionHandler.java b/aai-resources/src/main/java/org/onap/aai/rest/ExceptionHandler.java
index ed57a7a..81c49b9 100644
--- a/aai-resources/src/main/java/org/onap/aai/rest/ExceptionHandler.java
+++ b/aai-resources/src/main/java/org/onap/aai/rest/ExceptionHandler.java
@@ -45,6 +45,8 @@ import org.onap.aai.logging.ErrorLogHelper;
@Provider
public class ExceptionHandler implements ExceptionMapper<Exception> {
+ private static final String AAI_4007 = "AAI_4007";
+
@Context
private HttpServletRequest request;
@@ -57,70 +59,64 @@ public class ExceptionHandler implements ExceptionMapper<Exception> {
@Override
public Response toResponse(Exception exception) {
- Response response = null;
- ArrayList<String> templateVars = new ArrayList<String>();
-
// the general case is that cxf will give us a WebApplicationException
// with a linked exception
if (exception instanceof WebApplicationException) {
- WebApplicationException e = (WebApplicationException) exception;
- if (e.getCause() != null) {
- if (e.getCause() instanceof SAXParseException2) {
- templateVars.add("UnmarshalException");
- AAIException ex = new AAIException("AAI_4007", exception);
- response = Response
- .status(400).entity(ErrorLogHelper
- .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
- .build();
- }
+ if (exception.getCause() instanceof SAXParseException2) {
+ return buildInputParsingErrorResponse(exception, "UnmarshalException");
+ } else {
+ return ((WebApplicationException)exception).getResponse();
}
+
} else if (exception instanceof JsonParseException) {
// jackson does it differently so we get the direct JsonParseException
- templateVars.add("JsonParseException");
- AAIException ex = new AAIException("AAI_4007", exception);
- response = Response.status(400)
- .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
- .build();
+ return buildInputParsingErrorResponse(exception);
} else if (exception instanceof JsonMappingException) {
- // jackson does it differently so we get the direct JsonParseException
- templateVars.add("JsonMappingException");
- AAIException ex = new AAIException("AAI_4007", exception);
- response = Response.status(400)
- .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
- .build();
+ return buildInputParsingErrorResponse(exception);
+ } else {
+ return defaultException(exception);
}
+ }
- // it didn't get set above, we wrap a general fault here
- if (response == null) {
+ private Response buildInputParsingErrorResponse(Exception exception) {
+ ArrayList<String> templateVars = new ArrayList<>();
+ templateVars.add(exception.getClass().getSimpleName());
+ return buildInputParsingErrorResponse(exception, templateVars);
+ }
- Exception actual_e = exception;
- if (exception instanceof WebApplicationException) {
- WebApplicationException e = (WebApplicationException) exception;
- response = e.getResponse();
- } else {
- templateVars.add(request.getMethod());
- templateVars.add("unknown");
- AAIException ex = new AAIException("AAI_4000", actual_e);
- List<MediaType> mediaTypes = headers.getAcceptableMediaTypes();
- int setError = 0;
-
- for (MediaType mediaType : mediaTypes) {
- if (MediaType.APPLICATION_XML_TYPE.isCompatible(mediaType)) {
- response = Response
- .status(400).type(MediaType.APPLICATION_XML_TYPE).entity(ErrorLogHelper
- .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
- .build();
- setError = 1;
- }
- }
- if (setError == 0) {
- response = Response
- .status(400).type(MediaType.APPLICATION_JSON_TYPE).entity(ErrorLogHelper
- .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
- .build();
- }
- }
- }
- return response;
+ private Response buildInputParsingErrorResponse(Exception exception, String customExceptionName) {
+ ArrayList<String> templateVars = new ArrayList<>();
+ templateVars.add(customExceptionName);
+ return buildInputParsingErrorResponse(exception, templateVars);
+ }
+
+ private Response buildInputParsingErrorResponse(Exception exception, ArrayList<String> templateVars) {
+ AAIException ex = new AAIException(AAI_4007, exception);
+ return Response
+ .status(400).entity(ErrorLogHelper
+ .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
+ .build();
}
+
+ private Response defaultException(Exception exception) {
+ ArrayList<String> templateVars = new ArrayList<>();
+ templateVars.add(request.getMethod());
+ templateVars.add("unknown");
+ AAIException ex = new AAIException("AAI_4000", exception);
+ // prefer xml, use json otherwise
+ return headers.getAcceptableMediaTypes().stream()
+ .filter(MediaType.APPLICATION_ATOM_XML_TYPE::isCompatible)
+ .findAny()
+ .map(xmlType ->
+ Response.status(400).type(MediaType.APPLICATION_XML_TYPE)
+ .entity(ErrorLogHelper.getRESTAPIErrorResponse(
+ headers.getAcceptableMediaTypes(), ex, templateVars))
+ .build())
+ .orElseGet(() ->
+ Response.status(400).type(MediaType.APPLICATION_JSON_TYPE)
+ .entity(ErrorLogHelper.getRESTAPIErrorResponse(
+ headers.getAcceptableMediaTypes(), ex, templateVars))
+ .build());
+ }
+
}