Initial commit of the schema service
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / config / ErrorHandler.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.schemaservice.config;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.onap.aai.exceptions.AAIException;
25 import org.onap.aai.logging.ErrorLogHelper;
26 import org.onap.aai.logging.ErrorObject;
27 import org.onap.aai.logging.ErrorObjectNotFoundException;
28 import org.onap.aai.logging.LogFormatTools;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.core.annotation.Order;
31 import org.springframework.stereotype.Component;
32 import org.springframework.web.filter.OncePerRequestFilter;
33
34 import javax.servlet.FilterChain;
35 import javax.servlet.ServletException;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import javax.ws.rs.core.MediaType;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.List;
42
43 /**
44  * Responsible for dealing with uri that doesn't start with basePath
45  * All of the other interceptors will handle any uri that starts with basePath
46  * So we need this to ensure that these cases are properly handled
47  */
48 @Order(1)
49 @Component
50 public class ErrorHandler extends OncePerRequestFilter {
51
52     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ErrorHandler.class);
53     private String basePath;
54
55     public ErrorHandler(@Value("${schema.uri.base.path}") String basePath) {
56         this.basePath = basePath;
57         if (!basePath.endsWith("/")) {
58             this.basePath = basePath + "/";
59         }
60     }
61
62     @Override
63     protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
64
65         String uri = httpServletRequest.getRequestURI();
66
67         if (uri != null && !(uri.startsWith(basePath))) {
68
69             AAIException e = new AAIException("AAI_3012");
70             ArrayList<String> templateVars = new ArrayList<>();
71
72             List<MediaType> mediaTypeList = new ArrayList<>();
73
74             String acceptHeader = httpServletRequest.getHeader("Accept");
75             if (acceptHeader == null) {
76                 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
77             } else {
78                 mediaTypeList.add(MediaType.valueOf(acceptHeader));
79             }
80
81             String message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, e, templateVars);
82
83             httpServletResponse.setStatus(400);
84             httpServletResponse.setContentType(mediaTypeList.get(0).toString());
85             httpServletResponse.getWriter().print(message);
86             httpServletResponse.getWriter().close();
87             return;
88         }
89
90         try {
91             filterChain.doFilter(httpServletRequest, httpServletResponse);
92         } catch (ServletException ex) {
93             Throwable e = ex.getRootCause();
94             if (e instanceof AAIException) {
95                 List<MediaType> mediaTypeList = new ArrayList<>();
96                 String acceptHeader = httpServletRequest.getHeader("Accept");
97                 if (acceptHeader == null) {
98                     mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
99                 } else {
100                     mediaTypeList.add(MediaType.valueOf(acceptHeader));
101                 }
102
103                 ArrayList<String> templateVars = new ArrayList<>();
104                 AAIException aaiException = (AAIException) e;
105                 String message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, aaiException, templateVars);
106                 ErrorObject object = null;
107                 try {
108                     object = ErrorLogHelper.getErrorObject(aaiException.getCode());
109                 } catch (ErrorObjectNotFoundException e1) {
110                     e1.printStackTrace();
111                 }
112                 httpServletResponse.setStatus(object.getHTTPResponseCode().getStatusCode());
113                 httpServletResponse.setContentType(mediaTypeList.get(0).toString());
114                 httpServletResponse.getWriter().write(message);
115                 httpServletResponse.getWriter().close();
116             } else {
117
118                 List<MediaType> mediaTypeList = new ArrayList<>();
119                 String acceptHeader = httpServletRequest.getHeader("Accept");
120                 if (acceptHeader == null) {
121                     mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
122                 } else {
123                     mediaTypeList.add(MediaType.valueOf(acceptHeader));
124                 }
125                 ArrayList<String> templateVars = new ArrayList<>();
126                 AAIException aaiException = new AAIException("AAI_4000", e);
127                 LOGGER.error("Encountered an internal exception {}", LogFormatTools.getStackTop(e));
128                 String message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, aaiException, templateVars);
129                 ErrorObject object = null;
130                 try {
131                     object = ErrorLogHelper.getErrorObject(aaiException.getCode());
132                 } catch (ErrorObjectNotFoundException e1) {
133                     e1.printStackTrace();
134                 }
135                 httpServletResponse.setStatus(object.getHTTPResponseCode().getStatusCode());
136                 httpServletResponse.setContentType(mediaTypeList.get(0).toString());
137                 httpServletResponse.getWriter().write(message);
138                 httpServletResponse.getWriter().close();
139             }
140         }
141     }
142
143 }