aboutsummaryrefslogtreecommitdiffstats
path: root/aai-traversal/src/main/java/org/onap/aai/rest/ExceptionHandler.java
diff options
context:
space:
mode:
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>2023-12-08 16:47:19 +0100
committerFiete Ostkamp <fiete.ostkamp@telekom.de>2023-12-08 16:01:39 +0000
commit33521592f3a466fd5cb6959340c1ec70e412dfa8 (patch)
tree659c25943b5eb35da24f4aea09107b6676446a96 /aai-traversal/src/main/java/org/onap/aai/rest/ExceptionHandler.java
parentefe52a581c4a1217ae9117e2177e157841e75ffa (diff)
Separate entity retrieval from jax-rs Response creation
- move logic to retrieve entity from db into a separate method - do not use runner() from AAI core [1][2] - use global ExceptionHandler to provide a common exception to error response mapping [1] the runner will spawn a separate thread to process the request. In this change there is nothing to replace this functionality. The reason that it is removed is that it tightly couples the app with a) aai-common and b) jax-rs and is also catching all exceptions. Also the timeout mechanism that is implemented is not actually stopping the execution of the thread after the timeout, but rather returning an early response (after 3 minutes(!)). [2] these changes are also done to make a future full migration to spring boot/the removal of jax-rs easier Issue-ID: AAI-3693 Change-Id: I177913c5f6295e1cab476e3c206fecacd7620f69 Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
Diffstat (limited to 'aai-traversal/src/main/java/org/onap/aai/rest/ExceptionHandler.java')
-rw-r--r--aai-traversal/src/main/java/org/onap/aai/rest/ExceptionHandler.java50
1 files changed, 20 insertions, 30 deletions
diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/ExceptionHandler.java b/aai-traversal/src/main/java/org/onap/aai/rest/ExceptionHandler.java
index f15e375..447ecdd 100644
--- a/aai-traversal/src/main/java/org/onap/aai/rest/ExceptionHandler.java
+++ b/aai-traversal/src/main/java/org/onap/aai/rest/ExceptionHandler.java
@@ -26,6 +26,7 @@ import com.sun.istack.SAXParseException2;
import java.util.ArrayList;
+import javax.annotation.Priority;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
@@ -35,6 +36,7 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
+import org.janusgraph.core.SchemaViolationException;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.logging.ErrorLogHelper;
@@ -42,6 +44,7 @@ import org.onap.aai.logging.ErrorLogHelper;
* The Class ExceptionHandler.
*/
@Provider
+@Priority(10000)
public class ExceptionHandler implements ExceptionMapper<Exception> {
private static final String AAI_4007 = "AAI_4007";
@@ -58,47 +61,30 @@ public class ExceptionHandler implements ExceptionMapper<Exception> {
// with a linked exception
if (exception instanceof WebApplicationException) {
if (exception.getCause() instanceof SAXParseException2) {
- return buildInputParsingErrorResponse(exception, "UnmarshalException");
+ return buildAAIExceptionResponse(new AAIException(AAI_4007, exception));
} else {
- return ((WebApplicationException)exception).getResponse();
+ return ((WebApplicationException) exception).getResponse();
}
} else if (exception instanceof JsonParseException) {
// jackson does it differently so we get the direct JsonParseException
- return buildInputParsingErrorResponse(exception);
+ return buildAAIExceptionResponse(new AAIException(AAI_4007, exception));
} else if (exception instanceof JsonMappingException) {
// jackson does it differently so we get the direct JsonParseException
- return buildInputParsingErrorResponse(exception);
+ return buildAAIExceptionResponse(new AAIException(AAI_4007, exception));
+ } else if (exception instanceof SchemaViolationException) {
+ return buildAAIExceptionResponse(new AAIException("AAI_4020", exception));
// it didn't get set above, we wrap a general fault here
+ } else if (exception instanceof AAIException) {
+ return buildAAIExceptionResponse((AAIException) exception);
} else {
return defaultException(exception);
}
}
- private Response buildInputParsingErrorResponse(Exception exception) {
- ArrayList<String> templateVars = new ArrayList<>();
- templateVars.add(exception.getClass().getSimpleName());
- return buildInputParsingErrorResponse(exception, templateVars);
- }
-
- 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) {
+ private Response buildAAIExceptionResponse(AAIException exception) {
ArrayList<String> templateVars = new ArrayList<>();
templateVars.add(request.getMethod());
- templateVars.add("unknown");
- AAIException ex = new AAIException("AAI_4000", exception);
+ templateVars.add(request.getRequestURI());
// prefer xml, use json otherwise
return headers.getAcceptableMediaTypes().stream()
@@ -107,12 +93,16 @@ public class ExceptionHandler implements ExceptionMapper<Exception> {
.map(xmlType ->
Response.status(400).type(MediaType.APPLICATION_XML_TYPE)
.entity(ErrorLogHelper.getRESTAPIErrorResponse(
- headers.getAcceptableMediaTypes(), ex, templateVars))
+ headers.getAcceptableMediaTypes(), exception, templateVars))
.build())
.orElseGet(() ->
Response.status(400).type(MediaType.APPLICATION_JSON_TYPE)
.entity(ErrorLogHelper.getRESTAPIErrorResponse(
- headers.getAcceptableMediaTypes(), ex, templateVars))
+ headers.getAcceptableMediaTypes(), exception, templateVars))
.build());
}
-}
+
+ private Response defaultException(Exception exception) {
+ return buildAAIExceptionResponse(new AAIException("AAI_4000", exception));
+ }
+} \ No newline at end of file