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;
27 import javax.servlet.FilterChain;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import javax.ws.rs.core.MediaType;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.logging.ErrorLogHelper;
35 import org.onap.aai.logging.ErrorObject;
36 import org.onap.aai.logging.ErrorObjectNotFoundException;
37 import org.onap.aai.logging.LogFormatTools;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.core.annotation.Order;
42 import org.springframework.stereotype.Component;
43 import org.springframework.web.filter.OncePerRequestFilter;
46 * Responsible for dealing with uri that doesn't start with basePath
47 * All of the other interceptors will handle any uri that starts with basePath
48 * So we need this to ensure that these cases are properly handled
52 public class ErrorHandler extends OncePerRequestFilter {
54 private static final Logger LOGGER = LoggerFactory.getLogger(ErrorHandler.class);
55 private String basePath;
57 public ErrorHandler(@Value("${schema.uri.base.path}") String basePath) {
58 this.basePath = basePath;
59 if (!basePath.endsWith("/")) {
60 this.basePath = basePath + "/";
65 protected void doFilterInternal(HttpServletRequest httpServletRequest,
66 HttpServletResponse httpServletResponse, FilterChain filterChain)
67 throws ServletException, IOException {
69 String uri = httpServletRequest.getRequestURI();
71 if (uri != null && !(uri.startsWith(basePath))) {
73 AAIException e = new AAIException("AAI_3012");
74 ArrayList<String> templateVars = new ArrayList<>();
76 List<MediaType> mediaTypeList = new ArrayList<>();
78 String acceptHeader = httpServletRequest.getHeader("Accept");
79 if (acceptHeader == null) {
80 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
82 mediaTypeList.add(MediaType.valueOf(acceptHeader));
85 String message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, e, templateVars);
87 httpServletResponse.setStatus(400);
88 httpServletResponse.setContentType(mediaTypeList.get(0).toString());
89 httpServletResponse.getWriter().print(message);
90 httpServletResponse.getWriter().close();
95 filterChain.doFilter(httpServletRequest, httpServletResponse);
96 } catch (ServletException ex) {
97 Throwable e = ex.getRootCause();
98 if (e instanceof AAIException) {
99 List<MediaType> mediaTypeList = new ArrayList<>();
100 String acceptHeader = httpServletRequest.getHeader("Accept");
101 if (acceptHeader == null) {
102 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
104 mediaTypeList.add(MediaType.valueOf(acceptHeader));
107 ArrayList<String> templateVars = new ArrayList<>();
108 AAIException aaiException = (AAIException) e;
109 String message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, aaiException,
111 ErrorObject object = null;
113 object = ErrorLogHelper.getErrorObject(aaiException.getCode());
114 httpServletResponse.setStatus(object.getHTTPResponseCode().getStatusCode());
115 } catch (ErrorObjectNotFoundException e1) {
116 LOGGER.error("getErrorObject exception {}", LogFormatTools.getStackTop(e1));
118 httpServletResponse.setContentType(mediaTypeList.get(0).toString());
119 httpServletResponse.getWriter().write(message);
120 httpServletResponse.getWriter().close();
123 List<MediaType> mediaTypeList = new ArrayList<>();
124 String acceptHeader = httpServletRequest.getHeader("Accept");
125 if (acceptHeader == null) {
126 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
128 mediaTypeList.add(MediaType.valueOf(acceptHeader));
130 ArrayList<String> templateVars = new ArrayList<>();
131 AAIException aaiException = new AAIException("AAI_4000", e);
132 LOGGER.error("Encountered an internal exception {}", LogFormatTools.getStackTop(e));
133 String message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, aaiException,
135 ErrorObject object = null;
137 object = ErrorLogHelper.getErrorObject(aaiException.getCode());
138 httpServletResponse.setStatus(object.getHTTPResponseCode().getStatusCode());
139 } catch (ErrorObjectNotFoundException e1) {
140 LOGGER.error("getErrorObject exception {}", LogFormatTools.getStackTop(e1));
142 httpServletResponse.setContentType(mediaTypeList.get(0).toString());
143 httpServletResponse.getWriter().write(message);
144 httpServletResponse.getWriter().close();