Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / filter / TempFilterForCORS.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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
21 package org.onap.vid.controller.filter;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.portalsdk.core.util.SystemProperties;
25 import org.springframework.web.filter.GenericFilterBean;
26
27 import javax.servlet.FilterChain;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.annotation.WebFilter;
32 import javax.servlet.http.HttpServletResponse;
33 import java.io.IOException;
34
35 @WebFilter(urlPatterns = "/*")
36 public class TempFilterForCORS extends GenericFilterBean {
37
38     private static final String ENV_MODE = "env.mode";
39     private Boolean devMode = null;
40
41      //dev mode is initialized here since @WebFilter doesn't support @Autowired
42     //So we are sure that SystemProperties bean was initialed only after the first call to doFilter
43     private boolean isDevMode() {
44         if (devMode!=null) {
45             return devMode;
46         }
47         else {
48             if (!SystemProperties.containsProperty(ENV_MODE)) {
49                 devMode = Boolean.FALSE;
50                 return devMode;
51             }
52
53             String envMode = SystemProperties.getProperty(ENV_MODE);
54             devMode = StringUtils.equalsIgnoreCase(envMode, "dev") ;
55         }
56         return devMode;
57     }
58
59     @Override
60     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
61
62         if (isDevMode() && response instanceof HttpServletResponse) {
63             ((HttpServletResponse) response).addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
64             ((HttpServletResponse) response).addHeader("Access-Control-Allow-Credentials", "true");
65         }
66         chain.doFilter(request, response);
67     }
68 }