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