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.schemaservice.config;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
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;
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;
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
50 public class ErrorHandler extends OncePerRequestFilter {
52 private static final Logger LOGGER = LoggerFactory.getLogger(ErrorHandler.class);
53 private String basePath;
55 public ErrorHandler(@Value("${schema.uri.base.path}") String basePath) {
56 this.basePath = basePath;
57 if (!basePath.endsWith("/")) {
58 this.basePath = basePath + "/";
63 protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
65 String uri = httpServletRequest.getRequestURI();
67 if (uri != null && !(uri.startsWith(basePath))) {
69 AAIException e = new AAIException("AAI_3012");
70 ArrayList<String> templateVars = new ArrayList<>();
72 List<MediaType> mediaTypeList = new ArrayList<>();
74 String acceptHeader = httpServletRequest.getHeader("Accept");
75 if (acceptHeader == null) {
76 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
78 mediaTypeList.add(MediaType.valueOf(acceptHeader));
81 String message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, e, templateVars);
83 httpServletResponse.setStatus(400);
84 httpServletResponse.setContentType(mediaTypeList.get(0).toString());
85 httpServletResponse.getWriter().print(message);
86 httpServletResponse.getWriter().close();
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);
100 mediaTypeList.add(MediaType.valueOf(acceptHeader));
103 ArrayList<String> templateVars = new ArrayList<>();
104 AAIException aaiException = (AAIException) e;
105 String message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, aaiException, templateVars);
106 ErrorObject object = null;
108 object = ErrorLogHelper.getErrorObject(aaiException.getCode());
109 } catch (ErrorObjectNotFoundException e1) {
110 e1.printStackTrace();
112 httpServletResponse.setStatus(object.getHTTPResponseCode().getStatusCode());
113 httpServletResponse.setContentType(mediaTypeList.get(0).toString());
114 httpServletResponse.getWriter().write(message);
115 httpServletResponse.getWriter().close();
118 List<MediaType> mediaTypeList = new ArrayList<>();
119 String acceptHeader = httpServletRequest.getHeader("Accept");
120 if (acceptHeader == null) {
121 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
123 mediaTypeList.add(MediaType.valueOf(acceptHeader));
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;
131 object = ErrorLogHelper.getErrorObject(aaiException.getCode());
132 } catch (ErrorObjectNotFoundException e1) {
133 e1.printStackTrace();
135 httpServletResponse.setStatus(object.getHTTPResponseCode().getStatusCode());
136 httpServletResponse.setContentType(mediaTypeList.get(0).toString());
137 httpServletResponse.getWriter().write(message);
138 httpServletResponse.getWriter().close();