diff --git a/hazelcast-wm/src/test/java/com/hazelcast/wm/test/AbstractWebFilterTest.java b/hazelcast-wm/src/test/java/com/hazelcast/wm/test/AbstractWebFilterTest.java index 20d3e8e3b6ad..27a689321484 100644 --- a/hazelcast-wm/src/test/java/com/hazelcast/wm/test/AbstractWebFilterTest.java +++ b/hazelcast-wm/src/test/java/com/hazelcast/wm/test/AbstractWebFilterTest.java @@ -27,6 +27,8 @@ import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.junit.After; @@ -41,6 +43,13 @@ public abstract class AbstractWebFilterTest extends HazelcastTestSupport{ + protected enum RequestType { + + GET_REQUEST, + POST_REQUEST + + } + static { final String logging = "hazelcast.logging.type"; if (System.getProperty(logging) == null) { @@ -64,7 +73,9 @@ public abstract class AbstractWebFilterTest extends HazelcastTestSupport{ } protected static final String HAZELCAST_SESSION_ATTRIBUTE_SEPARATOR = "::hz::"; - + + protected static final RequestType DEFAULT_REQUEST_TYPE = RequestType.GET_REQUEST; + protected String serverXml1; protected String serverXml2; @@ -74,6 +85,10 @@ public abstract class AbstractWebFilterTest extends HazelcastTestSupport{ protected ServletContainer server2; protected HazelcastInstance hz; + protected AbstractWebFilterTest(String serverXml1) { + this.serverXml1 = serverXml1; + } + protected AbstractWebFilterTest(String serverXml1, String serverXml2) { this.serverXml1 = serverXml1; this.serverXml2 = serverXml2; @@ -84,17 +99,22 @@ public void setup() throws Exception { final URL root = new URL(TestServlet.class.getResource("/"), "../test-classes"); final String baseDir = new File(root.getFile().replaceAll("%20", " ")).toString(); final String sourceDir = baseDir + "/../../src/test/webapp"; - hz = Hazelcast.newHazelcastInstance(new FileSystemXmlConfig(new File(sourceDir + "/WEB-INF/", "hazelcast.xml"))); + hz = Hazelcast.newHazelcastInstance( + new FileSystemXmlConfig(new File(sourceDir + "/WEB-INF/", "hazelcast.xml"))); serverPort1 = availablePort(); - serverPort2 = availablePort(); server1 = getServletContainer(serverPort1, sourceDir, serverXml1); - server2 = getServletContainer(serverPort2, sourceDir, serverXml2); + if (serverXml2 != null) { + serverPort2 = availablePort(); + server2 = getServletContainer(serverPort2, sourceDir, serverXml2); + } } @After public void teardown() throws Exception { server1.stop(); - server2.stop(); + if (server2 != null) { + server2.stop(); + } Hazelcast.shutdownAll(); } @@ -120,22 +140,51 @@ protected String findHazelcastSessionId(IMap map) { return null; } - protected String executeRequest(String context, int serverPort, CookieStore cookieStore) throws Exception { - HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build(); - HttpGet request = new HttpGet("http://localhost:" + serverPort + "/" + context); - HttpResponse response = client.execute(request); + protected String responseToString(HttpResponse response) throws IOException { HttpEntity entity = response.getEntity(); return EntityUtils.toString(entity); } + protected String executeRequest(String context, int serverPort, CookieStore cookieStore) throws Exception { + return responseToString(request(context, serverPort, cookieStore)); + } + protected HttpResponse request(String context, int serverPort, CookieStore cookieStore) throws Exception { + return request(DEFAULT_REQUEST_TYPE, context, serverPort, cookieStore); + } + + protected String executeRequest(RequestType reqType, + String context, + int serverPort, + CookieStore cookieStore) throws Exception { + return responseToString(request(reqType, context, serverPort, cookieStore)); + } + + protected HttpResponse request(RequestType reqType, + String context, + int serverPort, + CookieStore cookieStore) throws Exception { + if (reqType == null) { + throw new IllegalArgumentException("Request type paramater cannot be empty !"); + } HttpClient client = HttpClientBuilder.create().disableRedirectHandling().setDefaultCookieStore(cookieStore).build(); - HttpGet request = new HttpGet("http://localhost:" + serverPort + "/" + context); + HttpUriRequest request = null; + switch (reqType) { + case GET_REQUEST: + request = new HttpGet("http://localhost:" + serverPort + "/" + context); + break; + case POST_REQUEST: + request = new HttpPost("http://localhost:" + serverPort + "/" + context); + break; + default: + throw new IllegalArgumentException(reqType + " typed request is not supported"); + } HttpResponse response = client.execute(request); return response; } - protected abstract ServletContainer getServletContainer(int port, String sourceDir, String serverXml) throws Exception; - + protected abstract ServletContainer getServletContainer(int port, + String sourceDir, + String serverXml) throws Exception; } diff --git a/hazelcast-wm/src/test/java/com/hazelcast/wm/test/TestServlet.java b/hazelcast-wm/src/test/java/com/hazelcast/wm/test/TestServlet.java index 31921d8421fe..519a39227787 100644 --- a/hazelcast-wm/src/test/java/com/hazelcast/wm/test/TestServlet.java +++ b/hazelcast-wm/src/test/java/com/hazelcast/wm/test/TestServlet.java @@ -29,10 +29,7 @@ public class TestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - - - if (req.getRequestURI().endsWith("redirect")) - { + if (req.getRequestURI().endsWith("redirect")) { //Don't touch session before redirect resp.sendRedirect("/"); return; diff --git a/hazelcast-wm/src/test/java/com/hazelcast/wm/test/spring/SpringApplicationContextProvider.java b/hazelcast-wm/src/test/java/com/hazelcast/wm/test/spring/SpringApplicationContextProvider.java new file mode 100644 index 000000000000..5ca0ae38fd04 --- /dev/null +++ b/hazelcast-wm/src/test/java/com/hazelcast/wm/test/spring/SpringApplicationContextProvider.java @@ -0,0 +1,25 @@ +package com.hazelcast.wm.test.spring; + +import org.eclipse.jetty.util.ConcurrentHashSet; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +import java.util.Collections; +import java.util.Set; + +public class SpringApplicationContextProvider implements ApplicationContextAware { + + private static Set applicationContextSet = + new ConcurrentHashSet(); + + public static Set getApplicationContextSet() { + return Collections.unmodifiableSet(applicationContextSet); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + applicationContextSet.add(applicationContext); + } + +} diff --git a/hazelcast-wm/src/test/java/com/hazelcast/wm/test/spring/SpringAwareWebFilterTestSupport.java b/hazelcast-wm/src/test/java/com/hazelcast/wm/test/spring/SpringAwareWebFilterTestSupport.java new file mode 100644 index 000000000000..66e2d350adee --- /dev/null +++ b/hazelcast-wm/src/test/java/com/hazelcast/wm/test/spring/SpringAwareWebFilterTestSupport.java @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2008-2014, Hazelcast, Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hazelcast.wm.test.spring; + +import com.hazelcast.wm.test.AbstractWebFilterTest; +import org.apache.http.HttpResponse; +import org.apache.http.client.CookieStore; +import org.apache.http.cookie.Cookie; +import org.apache.http.impl.client.BasicCookieStore; +import org.springframework.web.context.support.WebApplicationContextUtils; + +public abstract class SpringAwareWebFilterTestSupport extends AbstractWebFilterTest { + + protected static final String DEFAULT_SPRING_CONTEXT_FILE_PATH = "spring/hazelcast-spring.xml"; + + protected static final String SPRING_SECURITY_LOGIN_URL = "j_spring_security_check"; + protected static final String SPRING_SECURITY_LOGOUT_URL = "j_spring_security_logout"; + protected static final String SPRING_SECURITY_LOGIN_USERNAME_PARAM = "j_username"; + protected static final String SPRING_SECURITY_LOGIN_PASSWORD_PARAM = "j_password"; + protected static final String SPRING_SECURITY_REMEMBER_ME_PARAM = "_spring_security_remember_me"; + + protected static final String SPRING_SECURITY_DEFAULT_USERNAME = "user"; + protected static final String SPRING_SECURITY_DEFAULT_PASSWORD = "password"; + + protected static final String SESSION_ID_COOKIE_NAME = "JSESSIONID"; + protected static final String HZ_SESSION_ID_COOKIE_NAME = "hazelcast.sessionId"; + protected static final String SPRING_SECURITY_COOKIE_NAME = "SPRING_SECURITY_REMEMBER_ME_COOKIE"; + + public SpringAwareWebFilterTestSupport() { + this(DEFAULT_SPRING_CONTEXT_FILE_PATH, DEFAULT_SPRING_CONTEXT_FILE_PATH); + } + + protected SpringAwareWebFilterTestSupport(String serverXml1, String serverXml2) { + super(serverXml1, serverXml2); + } + + protected static class SpringSecuritySession { + + protected CookieStore cookieStore; + protected HttpResponse lastResponse; + + protected SpringSecuritySession() { + this.cookieStore = new BasicCookieStore(); + } + + protected SpringSecuritySession(CookieStore cookieStore) { + this.cookieStore = cookieStore; + } + + protected String getCookie(String cookieName) { + for (Cookie cookie : cookieStore.getCookies()) { + if (cookie.getName().equals(cookieName)) { + return cookie.getValue(); + } + } + return null; + } + + protected String getSessionId() { + return getCookie(SESSION_ID_COOKIE_NAME); + } + + protected String getHazelcastSessionId() { + return getCookie(HZ_SESSION_ID_COOKIE_NAME); + } + + protected String getSpringSecurityCookie() { + return getCookie(SPRING_SECURITY_COOKIE_NAME); + } + + } + + protected SpringSecuritySession login(SpringSecuritySession springSecuritySession) throws Exception { + return login(springSecuritySession, SPRING_SECURITY_DEFAULT_USERNAME, SPRING_SECURITY_DEFAULT_PASSWORD); + } + + protected SpringSecuritySession login(SpringSecuritySession springSecuritySession, + String username, + String password) throws Exception { + if (springSecuritySession== null) { + springSecuritySession = new SpringSecuritySession(); + } + HttpResponse response = + request( + RequestType.POST_REQUEST, + SPRING_SECURITY_LOGIN_URL + "?" + + SPRING_SECURITY_LOGIN_USERNAME_PARAM + "=" + username + "&" + + SPRING_SECURITY_LOGIN_PASSWORD_PARAM + "=" + password + "&" + + SPRING_SECURITY_REMEMBER_ME_PARAM + "=" + "true", + serverPort1, springSecuritySession.cookieStore); + springSecuritySession.lastResponse = response; + return springSecuritySession; + } + + protected SpringSecuritySession logout(SpringSecuritySession springSecuritySession) throws Exception { + if (springSecuritySession == null) { + throw new IllegalArgumentException("SpringSecuritySession cannot be null !"); + } + HttpResponse response = + request( + RequestType.POST_REQUEST, + SPRING_SECURITY_LOGOUT_URL, + serverPort1, springSecuritySession.cookieStore); + springSecuritySession.lastResponse = response; + return springSecuritySession; + } + +} diff --git a/hazelcast-wm/src/test/webapp/WEB-INF/spring/application-context.xml b/hazelcast-wm/src/test/webapp/WEB-INF/spring/application-context.xml new file mode 100644 index 000000000000..cd61dcb04571 --- /dev/null +++ b/hazelcast-wm/src/test/webapp/WEB-INF/spring/application-context.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + diff --git a/hazelcast-wm/src/test/webapp/WEB-INF/spring/hazelcast-spring.xml b/hazelcast-wm/src/test/webapp/WEB-INF/spring/hazelcast-spring.xml new file mode 100644 index 000000000000..d800deec5a71 --- /dev/null +++ b/hazelcast-wm/src/test/webapp/WEB-INF/spring/hazelcast-spring.xml @@ -0,0 +1,105 @@ + + + + + + Hazelcast-Spring Test + + + hazelcast-filter + com.hazelcast.web.spring.SpringAwareWebFilter + + map-name + default + + + debug + true + + + config-location + /WEB-INF/hazelcast.xml + + + instance-name + hazelcast-spring-node + + + shutdown-on-destroy + true + + + client-config-location + /WEB-INF/hazelcast-client.xml + + + + hazelcast-filter + /* + FORWARD + INCLUDE + REQUEST + + + com.hazelcast.web.SessionListener + + + + contextConfigLocation + + /WEB-INF/spring/application-context.xml + /WEB-INF/spring/security-context.xml + + + + + appServlet + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + /WEB-INF/spring/servlet-context.xml + + 1 + + + appServletz + /* + + + + springSecurityFilterChain + org.springframework.web.filter.DelegatingFilterProxy + + + + springSecurityFilterChain + /* + + + + org.springframework.web.context.ContextLoaderListener + + + + 10 + + + diff --git a/hazelcast-wm/src/test/webapp/WEB-INF/spring/security-context.xml b/hazelcast-wm/src/test/webapp/WEB-INF/spring/security-context.xml new file mode 100644 index 000000000000..ee1db766fdb1 --- /dev/null +++ b/hazelcast-wm/src/test/webapp/WEB-INF/spring/security-context.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hazelcast-wm/src/test/webapp/WEB-INF/spring/servlet-context.xml b/hazelcast-wm/src/test/webapp/WEB-INF/spring/servlet-context.xml new file mode 100644 index 000000000000..a9424e5d0b60 --- /dev/null +++ b/hazelcast-wm/src/test/webapp/WEB-INF/spring/servlet-context.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + +