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.config;
22 import org.onap.aai.exceptions.AAIException;
23 import org.onap.aai.logging.ErrorLogHelper;
24 import org.springframework.beans.factory.annotation.Value;
25 import org.springframework.core.annotation.Order;
26 import org.springframework.stereotype.Component;
27 import org.springframework.web.filter.OncePerRequestFilter;
29 import javax.servlet.FilterChain;
30 import javax.servlet.ServletException;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import javax.ws.rs.core.MediaType;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.List;
39 * Responsible for dealing with uri that doesn't start with basePath
40 * All of the other interceptors will handle any uri that starts with basePath
41 * So we need this to ensure that these cases are properly handled
45 public class ErrorHandler extends OncePerRequestFilter {
47 private String basePath;
49 public ErrorHandler(@Value("${schema.uri.base.path}") String basePath){
50 this.basePath = basePath;
51 if(!basePath.endsWith("/")){
52 this.basePath = basePath + "/";
57 protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
59 String uri = httpServletRequest.getRequestURI();
61 if (uri != null && !(uri.startsWith(basePath))) {
63 AAIException e = new AAIException("AAI_3012");
64 ArrayList<String> templateVars = new ArrayList<>();
66 List<MediaType> mediaTypeList = new ArrayList<>();
68 String acceptHeader = httpServletRequest.getHeader("Accept");
69 if (acceptHeader == null) {
70 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
72 mediaTypeList.add(MediaType.valueOf(acceptHeader));
75 String message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, e, templateVars);
77 httpServletResponse.setStatus(400);
78 httpServletResponse.setContentType(mediaTypeList.get(0).toString());
79 httpServletResponse.getWriter().print(message);
80 httpServletResponse.getWriter().close();
84 filterChain.doFilter(httpServletRequest, httpServletResponse);