From 91514ee83f2b44f1a4c63f602a280649ef84ad2b Mon Sep 17 00:00:00 2001 From: "Castigliego, Marco" Date: Tue, 5 May 2015 17:13:24 +0100 Subject: [PATCH] Adding cross domain filter to make swagger-ui working --- .../server/web/filter/CrossDomainFilter.java | 42 +++++++++++++++++ .../server/servers/CrossDomainFilterTest.java | 47 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 micro-core/src/main/java/com/aol/micro/server/web/filter/CrossDomainFilter.java create mode 100644 micro-core/src/test/java/com/aol/micro/server/servers/CrossDomainFilterTest.java diff --git a/micro-core/src/main/java/com/aol/micro/server/web/filter/CrossDomainFilter.java b/micro-core/src/main/java/com/aol/micro/server/web/filter/CrossDomainFilter.java new file mode 100644 index 000000000..653b18821 --- /dev/null +++ b/micro-core/src/main/java/com/aol/micro/server/web/filter/CrossDomainFilter.java @@ -0,0 +1,42 @@ +package com.aol.micro.server.web.filter; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.stereotype.Component; + +import com.aol.micro.server.auto.discovery.FilterConfiguration; + +@Component(value = "crossDomainFilter") +public class CrossDomainFilter implements Filter, FilterConfiguration { + + @Override + public String[] getMapping() { + return new String[] { "/*" }; + } + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletResponse resp = (HttpServletResponse) response; + resp.addHeader("Access-Control-Allow-Origin", "*"); + resp.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); + resp.addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia"); + chain.doFilter(request, response); + } + + @Override + public void destroy() { + } +} diff --git a/micro-core/src/test/java/com/aol/micro/server/servers/CrossDomainFilterTest.java b/micro-core/src/test/java/com/aol/micro/server/servers/CrossDomainFilterTest.java new file mode 100644 index 000000000..b2ab0da1b --- /dev/null +++ b/micro-core/src/test/java/com/aol/micro/server/servers/CrossDomainFilterTest.java @@ -0,0 +1,47 @@ +package com.aol.micro.server.servers; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; + +import java.io.IOException; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InOrder; +import org.mockito.Mockito; + +import com.aol.micro.server.web.filter.CrossDomainFilter; + +public class CrossDomainFilterTest { + + private CrossDomainFilter crossDomainFilter; + + @Before + public void init() { + this.crossDomainFilter = new CrossDomainFilter(); + } + + @Test + public void testFilter() throws IOException, ServletException { + + ServletRequest request = mock(ServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + FilterChain filterChain = mock(FilterChain.class); + + crossDomainFilter.doFilter(request, response, filterChain); + + InOrder inOrder = Mockito.inOrder(response, filterChain); + + inOrder.verify(response, times(1)).addHeader("Access-Control-Allow-Origin", "*"); + inOrder.verify(response, times(1)).addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); + inOrder.verify(response, times(1)).addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia"); + + inOrder.verify(filterChain, times(1)).doFilter(request, response); + + } +}