2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.rest;
22 import java.util.ArrayList;
23 import java.util.List;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.ws.rs.WebApplicationException;
27 import javax.ws.rs.core.Context;
28 import javax.ws.rs.core.HttpHeaders;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.ext.ExceptionMapper;
32 import javax.ws.rs.ext.Provider;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.logging.ErrorLogHelper;
36 import com.fasterxml.jackson.core.JsonParseException;
37 import com.fasterxml.jackson.databind.JsonMappingException;
38 import com.sun.istack.SAXParseException2;
41 * The Class ExceptionHandler.
44 public class ExceptionHandler implements ExceptionMapper<Exception> {
47 private HttpServletRequest request;
50 private HttpHeaders headers;
56 public Response toResponse(Exception exception) {
58 Response response = null;
59 ArrayList<String> templateVars = new ArrayList<String>();
61 //the general case is that cxf will give us a WebApplicationException
62 //with a linked exception
63 if (exception instanceof WebApplicationException) {
64 WebApplicationException e = (WebApplicationException) exception;
65 if (e.getCause() != null) {
66 if (e.getCause() instanceof SAXParseException2) {
67 templateVars.add("UnmarshalException");
68 AAIException ex = new AAIException("AAI_4007", exception);
71 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
75 } else if (exception instanceof JsonParseException) {
76 //jackson does it differently so we get the direct JsonParseException
77 templateVars.add("JsonParseException");
78 AAIException ex = new AAIException("AAI_4007", exception);
81 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
83 } else if (exception instanceof JsonMappingException) {
84 //jackson does it differently so we get the direct JsonParseException
85 templateVars.add("JsonMappingException");
86 AAIException ex = new AAIException("AAI_4007", exception);
89 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
93 // it didn't get set above, we wrap a general fault here
94 if (response == null) {
96 Exception actual_e = exception;
97 if (exception instanceof WebApplicationException) {
98 WebApplicationException e = (WebApplicationException) exception;
99 response = e.getResponse();
101 templateVars.add(request.getMethod());
102 templateVars.add("unknown");
103 AAIException ex = new AAIException("AAI_4000", actual_e);
104 List<MediaType> mediaTypes = headers.getAcceptableMediaTypes();
107 for (MediaType mediaType : mediaTypes) {
108 if (MediaType.APPLICATION_XML_TYPE.isCompatible(mediaType)) {
111 .type(MediaType.APPLICATION_XML_TYPE)
112 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
120 .type(MediaType.APPLICATION_JSON_TYPE)
121 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))