Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / rest / ExceptionHandler.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.rest;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
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;
33
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;
39
40 /**
41  * The Class ExceptionHandler.
42  */
43 @Provider
44 public class ExceptionHandler implements ExceptionMapper<Exception> {
45
46     @Context
47     private HttpServletRequest request;
48     
49     @Context
50     private HttpHeaders headers;
51     
52     /**
53          * @{inheritDoc}
54          */
55     @Override
56     public Response toResponse(Exception exception) {
57
58         Response response = null;
59         ArrayList<String> templateVars = new ArrayList<String>();
60
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);
69                                 response = Response
70                                                 .status(400)
71                                                 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
72                                                 .build();
73                         }
74                 }
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);
79                 response = Response
80                                 .status(400)
81                                 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
82                                 .build();
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);
87                 response = Response
88                                 .status(400)
89                                 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
90                                 .build();
91         } 
92         
93         // it didn't get set above, we wrap a general fault here
94         if (response == null) { 
95                 
96                 Exception actual_e = exception;
97                 if (exception instanceof WebApplicationException) { 
98                         WebApplicationException e = (WebApplicationException) exception;
99                         response = e.getResponse();
100                 } else { 
101                         templateVars.add(request.getMethod());
102                         templateVars.add("unknown");
103                         AAIException ex = new AAIException("AAI_4000", actual_e);
104                         List<MediaType> mediaTypes = headers.getAcceptableMediaTypes();
105                         int setError = 0;
106
107                         for (MediaType mediaType : mediaTypes) { 
108                                 if (MediaType.APPLICATION_XML_TYPE.isCompatible(mediaType)) {
109                                         response = Response
110                                                         .status(400)
111                                                         .type(MediaType.APPLICATION_XML_TYPE)
112                                                         .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
113                                                         .build();       
114                                         setError = 1;
115                                 } 
116                         }
117                         if (setError == 0) { 
118                                 response = Response
119                                                 .status(400)
120                                                 .type(MediaType.APPLICATION_JSON_TYPE)
121                                                 .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
122                                                 .build();       
123                         }
124                 }
125         }               
126         return response;
127     }
128 }