b675ec9a6e5508417eb41e146fd6e7cfc5eee73a
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / filters / GatewayFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.be.filters;
21
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.stream.Stream;
25 import javax.servlet.Filter;
26 import javax.servlet.FilterChain;
27 import javax.servlet.FilterConfig;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import javax.ws.rs.WebApplicationException;
34 import org.apache.http.HttpStatus;
35 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
36 import org.openecomp.sdc.be.config.Configuration;
37 import org.openecomp.sdc.be.servlets.exception.ComponentExceptionMapper;
38 import org.openecomp.sdc.common.api.FilterDecisionEnum;
39 import org.openecomp.sdc.common.log.wrappers.Logger;
40 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
43
44 @Component("gatewayFilter")
45 public class GatewayFilter implements Filter {
46
47     private static final Logger log = Logger.getLogger(GatewayFilter.class);
48     private Configuration.CookieConfig authCookieConf;
49     private Configuration config;
50     @Autowired
51     private ThreadLocalUtils threadLocalUtils;
52     @Autowired
53     private ComponentExceptionMapper componentExceptionMapper;
54
55     public GatewayFilter(org.openecomp.sdc.be.config.Configuration configuration) {
56         this.authCookieConf = configuration.getAuthCookie();
57     }
58
59     @Override
60     public void init(FilterConfig filterConfig) throws ServletException {
61     }
62
63     @Override
64     public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
65         HttpServletRequest httpRequest = (HttpServletRequest) req;
66         HttpServletResponse httpResponse = (HttpServletResponse) res;
67         try {
68             if (isUrlFromWhiteList(httpRequest) || isConsumerBusinessLogic()) {
69                 ThreadLocalsHolder.setApiType(FilterDecisionEnum.NA);
70                 threadLocalUtils.setUserContextFromDB(httpRequest);
71                 filterChain.doFilter(httpRequest, res);
72             }
73         } catch (ComponentException ce) {
74             componentExceptionMapper.writeToResponse(ce, httpResponse);
75         } catch (WebApplicationException we) {
76             httpResponse.setStatus(we.getResponse().getStatus());
77             setDefaultHttpParams(httpResponse);
78             httpResponse.getWriter().write(we.getMessage());
79         } catch (Exception ex) {
80             httpResponse.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
81             setDefaultHttpParams(httpResponse);
82             httpResponse.getWriter().write(ex.getMessage());
83         }
84     }
85
86     private void setDefaultHttpParams(HttpServletResponse httpResponse) {
87         httpResponse.setContentType("application/json");
88         httpResponse.setCharacterEncoding("UTF-8");
89     }
90
91     private boolean isUrlFromWhiteList(HttpServletRequest httpRequest) {
92         String pathInfo;
93         List<String> excludedUrls = authCookieConf.getExcludedUrls();
94         pathInfo = httpRequest.getPathInfo().toLowerCase();
95         log.debug("SessionValidationFilter: white list validation ->  PathInfo: {} ", pathInfo);
96         Stream<String> stream = excludedUrls.stream();
97         pathInfo.getClass();
98         return stream.anyMatch(pathInfo::matches);
99     }
100
101     private Boolean isConsumerBusinessLogic() {
102         return config.getConsumerBusinessLogic();
103     }
104
105     @Override
106     public void destroy() {
107     }
108 }