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=========================================================
21 package org.onap.aai.schemaservice.config;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import javax.servlet.FilterChain;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import javax.ws.rs.core.MediaType;
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.logging.ErrorLogHelper;
33 import org.onap.aai.logging.ErrorObject;
34 import org.onap.aai.logging.ErrorObjectNotFoundException;
35 import org.onap.aai.logging.LogFormatTools;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.core.annotation.Order;
40 import org.springframework.stereotype.Component;
41 import org.springframework.web.filter.OncePerRequestFilter;
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,
64 HttpServletResponse httpServletResponse,
65 FilterChain filterChain) throws ServletException, IOException {
67 String uri = httpServletRequest.getRequestURI();
69 if (uri != null && !(uri.startsWith(basePath))) {
71 AAIException e = new AAIException("AAI_3012");
72 ArrayList<String> templateVars = new ArrayList<>();
74 List<MediaType> mediaTypeList = new ArrayList<>();
76 String acceptHeader = httpServletRequest.getHeader("Accept");
77 if (acceptHeader == null) {
78 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
80 mediaTypeList.add(MediaType.valueOf(acceptHeader));
83 String message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, e, templateVars);
85 httpServletResponse.setStatus(400);
86 httpServletResponse.setContentType(mediaTypeList.get(0).toString());
87 httpServletResponse.getWriter().print(message);
88 httpServletResponse.getWriter().close();
93 filterChain.doFilter(httpServletRequest, httpServletResponse);
94 } catch (ServletException ex) {
95 Throwable e = ex.getRootCause();
96 if (e instanceof AAIException) {
97 List<MediaType> mediaTypeList = new ArrayList<>();
98 String acceptHeader = httpServletRequest.getHeader("Accept");
99 if (acceptHeader == null) {
100 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
102 mediaTypeList.add(MediaType.valueOf(acceptHeader));
105 ArrayList<String> templateVars = new ArrayList<>();
106 AAIException aaiException = (AAIException) e;
107 String message = ErrorLogHelper
108 .getRESTAPIErrorResponse(mediaTypeList, aaiException, templateVars);
109 ErrorObject object = null;
111 object = ErrorLogHelper.getErrorObject(aaiException.getCode());
112 httpServletResponse.setStatus(object.getHTTPResponseCode().getStatusCode());
113 } catch (ErrorObjectNotFoundException e1) {
114 LOGGER.error("getErrorObject exception {}", LogFormatTools.getStackTop(e1));
116 httpServletResponse.setContentType(mediaTypeList.get(0).toString());
117 httpServletResponse.getWriter().write(message);
118 httpServletResponse.getWriter().close();
121 List<MediaType> mediaTypeList = new ArrayList<>();
122 String acceptHeader = httpServletRequest.getHeader("Accept");
123 if (acceptHeader == null) {
124 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
126 mediaTypeList.add(MediaType.valueOf(acceptHeader));
128 ArrayList<String> templateVars = new ArrayList<>();
129 AAIException aaiException = new AAIException("AAI_4000", e);
130 LOGGER.error("Encountered an internal exception {}", LogFormatTools.getStackTop(e));
131 String message = ErrorLogHelper
132 .getRESTAPIErrorResponse(mediaTypeList, aaiException, templateVars);
133 ErrorObject object = null;
135 object = ErrorLogHelper.getErrorObject(aaiException.getCode());
136 httpServletResponse.setStatus(object.getHTTPResponseCode().getStatusCode());
137 } catch (ErrorObjectNotFoundException e1) {
138 LOGGER.error("getErrorObject exception {}", LogFormatTools.getStackTop(e1));
140 httpServletResponse.setContentType(mediaTypeList.get(0).toString());
141 httpServletResponse.getWriter().write(message);
142 httpServletResponse.getWriter().close();