From 1cfb08779ea0e00be69e072a940b3063e049fe6b Mon Sep 17 00:00:00 2001 From: Ofir Sonsino Date: Wed, 31 Jan 2018 17:19:00 +0200 Subject: org.onap migration Change-Id: I52f0b2851f2c765752b6d21f49b32136d7d72a3d Issue-ID: VID-86 Signed-off-by: Ofir Sonsino --- .../vid/controller/filter/TempFilterForCORS.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 vid-app-common/src/main/java/org/onap/vid/controller/filter/TempFilterForCORS.java (limited to 'vid-app-common/src/main/java/org/onap/vid/controller/filter/TempFilterForCORS.java') diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/filter/TempFilterForCORS.java b/vid-app-common/src/main/java/org/onap/vid/controller/filter/TempFilterForCORS.java new file mode 100644 index 000000000..02dcde095 --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/controller/filter/TempFilterForCORS.java @@ -0,0 +1,48 @@ +package org.onap.vid.controller.filter; + +import org.apache.commons.lang3.StringUtils; +import org.openecomp.portalsdk.core.util.SystemProperties; +import org.springframework.web.filter.GenericFilterBean; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebFilter(urlPatterns = "/*") +public class TempFilterForCORS extends GenericFilterBean { + + private static final String ENV_MODE = "env.mode"; + private Boolean devMode = null; + + //dev mode is initialized here since @WebFilter doesn't support @Autowired + //So we are sure that SystemProperties bean was initialed only after the first call to doFilter + private boolean isDevMode() { + if (devMode!=null) { + return devMode; + } + else { + if (!SystemProperties.containsProperty(ENV_MODE)) { + devMode = Boolean.FALSE; + return devMode; + } + + String envMode = SystemProperties.getProperty(ENV_MODE); + devMode = StringUtils.equalsIgnoreCase(envMode, "dev") ; + } + return devMode; + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + + if (isDevMode() && response instanceof HttpServletResponse) { + ((HttpServletResponse) response).addHeader("Access-Control-Allow-Origin", "http://localhost:3000"); + ((HttpServletResponse) response).addHeader("Access-Control-Allow-Credentials", "true"); + } + chain.doFilter(request, response); + } +} -- cgit 1.2.3-korg