Sonar fixes
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / 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.config;
21
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;
28
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;
37
38 /**
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
42  */
43 @Order(1)
44 @Component
45 public class ErrorHandler extends OncePerRequestFilter {
46
47     private String basePath;
48
49     public ErrorHandler(@Value("${schema.uri.base.path}") String basePath){
50         this.basePath = basePath;
51         if(!basePath.endsWith("/")){
52             this.basePath = basePath + "/";
53         }
54     }
55
56     @Override
57     protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
58
59         String uri = httpServletRequest.getRequestURI();
60
61         if (uri != null && !(uri.startsWith(basePath))) {
62
63             AAIException e = new AAIException("AAI_3012");
64             ArrayList<String> templateVars = new ArrayList<>();
65
66             List<MediaType> mediaTypeList = new ArrayList<>();
67
68             String acceptHeader = httpServletRequest.getHeader("Accept");
69             if (acceptHeader == null) {
70                 mediaTypeList.add(MediaType.APPLICATION_XML_TYPE);
71             } else {
72                 mediaTypeList.add(MediaType.valueOf(acceptHeader));
73             }
74
75             String message = ErrorLogHelper.getRESTAPIErrorResponse(mediaTypeList, e, templateVars);
76
77             httpServletResponse.setStatus(400);
78             httpServletResponse.setContentType(mediaTypeList.get(0).toString());
79             httpServletResponse.getWriter().print(message);
80             httpServletResponse.getWriter().close();
81             return;
82         }
83
84         filterChain.doFilter(httpServletRequest, httpServletResponse);
85     }
86
87 }