List of usage examples for org.apache.commons.httpclient HttpClient getState
public HttpState getState()
From source file:org.jboss.test.cluster.defaultcfg.web.test.PersistentManagerFormAuthTestCase.java
public void testFormAuthentication() throws Exception { String url = baseURLNoAuth + "/http-formauth-persistent/"; // Start by accessing the secured index.html of war1 HttpClient httpConn = new HttpClient(); // Try to access protected resource GetMethod indexGet = new GetMethod(url + "index.jsp"); int responseCode = httpConn.executeMethod(indexGet); String body = indexGet.getResponseBodyAsString(); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0); // Submit the login form PostMethod formPost = new PostMethod(url + "j_security_check"); formPost.addRequestHeader("Referer", url + "login.html"); formPost.addParameter("j_username", "admin"); formPost.addParameter("j_password", "admin"); responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, httpConn.getState()); assertTrue("Saw HTTP_MOVED_TEMP(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_MOVED_TEMP); // Follow the redirect to the index.html page Header location = formPost.getResponseHeader("Location"); String indexURI = location.getValue(); GetMethod warIndex = new GetMethod(indexURI); responseCode = httpConn.executeMethod(warIndex.getHostConfiguration(), warIndex, httpConn.getState()); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); body = warIndex.getResponseBodyAsString(); if (body.indexOf("j_security_check") > 0) fail("get of " + indexURI + " redirected to login page"); }
From source file:org.jboss.test.cluster.multicfg.web.test.ScopedTestCase.java
/** * Test ability for a FORM auth based app to have failover without requiring * a new sign-on//from w w w .j av a 2 s. co m * * This test will not pass until a FormAuthenticator that makes * use of cached usernames and passwords is available. * * @throws Exception */ public void badtestFormAuthFailover() throws Exception { log.info("+++ testFormAuthFailover"); // Start by accessing the war's protected url HttpClient client = new HttpClient(); String body = makeGet(client, baseURL0_ + protectedUrl_); if (body.indexOf("j_security_check") < 0) fail("get of " + protectedUrl_ + " not redirected to login page"); HttpState state = client.getState(); Cookie[] cookies = state.getCookies(); String sessionID = null; for (int c = 0; c < cookies.length; c++) { Cookie k = cookies[c]; if (k.getName().equalsIgnoreCase("JSESSIONID")) sessionID = k.getValue(); } log.debug("Saw JSESSIONID=" + sessionID); // Submit the login form PostMethod formPost = new PostMethod(baseURL0_ + securityCheckUrl_); formPost.addRequestHeader("Referer", baseURL0_ + loginFormUrl_); formPost.addParameter("j_username", "admin"); formPost.addParameter("j_password", "admin"); int responseCode = client.executeMethod(formPost.getHostConfiguration(), formPost, state); String response = formPost.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Saw HTTP_MOVED_TEMP(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_MOVED_TEMP); // Follow the redirect to the index.html page body = makeGet(client, baseURL0_ + protectedUrl_); if (body.indexOf("j_security_check") > 0) fail("get of " + baseURL0_ + protectedUrl_ + " redirected to login page"); // Switch to the second server SessionTestUtil.setCookieDomainToThisServer(client, servers_[1]); sleepThread(DEFAULT_SLEEP); // Now try getting the protected url on the second server body = makeGet(client, baseURL1_ + protectedUrl_); if (body.indexOf("j_security_check") > 0) fail("get of " + baseURL1_ + protectedUrl_ + " redirected to login page"); }
From source file:org.jboss.test.cluster.multicfg.web.test.WebSessionTestCase.java
/** This makes 2 requests to the jbosstest.cluster.node0 /dist-ss/StatefulSessionServlet * followed by 2 requests to the jbosstest.cluster.node1 /dist-ss/StatefulSessionServlet * using the same session ID to validate that the session is replicated * with the current value and updated correctly. The session AccessCount * value is returned via the X-AccessCount header which should be 4 after * the last request.//from w w w. ja va 2 s.c om * * @throws Exception */ public void testServletSessionFailover() throws Exception { getLog().debug("+++ testServletSessionFailover"); String[] servers = super.getServers(); String[] httpURLs = super.getHttpURLs(); // Access the StatefulSessionServlet of dist-ss.war@server0 twice String baseURL0 = httpURLs[0]; HttpClient httpConn = new HttpClient(); GetMethod servletGet0 = new GetMethod(baseURL0 + "/dist-ss/StatefulSessionServlet/"); int responseCode = httpConn.executeMethod(servletGet0); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); Header accessCount = servletGet0.getResponseHeader("X-AccessCount"); int count = Integer.parseInt(accessCount.getValue()); assertEquals("X-AccessCount ", 1, count); // Get the state for the JSESSIONID HttpState state = httpConn.getState(); servletGet0 = new GetMethod(baseURL0 + "/dist-ss/StatefulSessionServlet/"); responseCode = httpConn.executeMethod(servletGet0.getHostConfiguration(), servletGet0, state); accessCount = servletGet0.getResponseHeader("X-AccessCount"); count = Integer.parseInt(accessCount.getValue()); assertEquals("X-AccessCount ", 2, count); // Get the JSESSIONID so we can reset the host Cookie[] cookies = state.getCookies(); Cookie sessionID = null; for (int c = 0; c < cookies.length; c++) { Cookie k = cookies[c]; if (k.getName().equalsIgnoreCase("JSESSIONID")) sessionID = k; } log.info("Saw JSESSIONID=" + sessionID); // Reset the domain so that the cookie will be sent to server1 sessionID.setDomain(servers[1]); state.addCookie(sessionID); _sleep(DEFAULT_SLEEP); // Access the StatefulSessionServlet of dist-ss.war@server1 twice String baseURL1 = httpURLs[1]; GetMethod servletGet1 = new GetMethod(baseURL1 + "/dist-ss/StatefulSessionServlet/"); responseCode = httpConn.executeMethod(servletGet1.getHostConfiguration(), servletGet1, state); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); accessCount = servletGet1.getResponseHeader("X-AccessCount"); count = Integer.parseInt(accessCount.getValue()); assertEquals("X-AccessCount ", 3, count); servletGet1 = new GetMethod(baseURL1 + "/dist-ss/StatefulSessionServlet/"); responseCode = httpConn.executeMethod(servletGet1.getHostConfiguration(), servletGet1, state); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); accessCount = servletGet1.getResponseHeader("X-AccessCount"); count = Integer.parseInt(accessCount.getValue()); assertEquals("X-AccessCount ", 4, count); }
From source file:org.jboss.test.cluster.multicfg.web.test.WebSessionTestCase.java
/** This makes 4 requests alternating between the jbosstest.cluster.node0 * /dist-ss/StatefulSessionServlet and jbosstest.cluster.node1 /dist-ss/StatefulSessionServlet * using the same session ID to validate that the session is replicated * with the current value and updated correctly. The session AccessCount * value is returned via the X-AccessCount header which should be 4 after * the last request./* ww w .ja va 2 s.c o m*/ * * Note: this test is not currently working since current http session * replication assumes sticky session. It does not support random load * balancing. * bwang. * * @throws Exception */ public void testServletSessionLoadBalancing() throws Exception { getLog().debug("+++ testServletSessionLoadBalancing"); String[] servers = getServers(); String[] httpURLs = super.getHttpURLs(); String baseURL0 = httpURLs[0]; String baseURL1 = baseURL0; if (servers.length > 1) { baseURL1 = httpURLs[1]; } // Access the StatefulSessionServlet of dist-ss.war@server0 twice HttpClient httpConn = new HttpClient(); GetMethod servletGet0 = new GetMethod(baseURL0 + "/dist-ss/StatefulSessionServlet/"); int responseCode = httpConn.executeMethod(servletGet0); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); Header accessCount = servletGet0.getResponseHeader("X-AccessCount"); int count = Integer.parseInt(accessCount.getValue()); assertEquals("X-AccessCount ", 1, count); // Get the state for the JSESSIONID HttpState state = httpConn.getState(); // Get the JSESSIONID so we can reset the host Cookie[] cookies = state.getCookies(); Cookie sessionID = null; for (int c = 0; c < cookies.length; c++) { Cookie k = cookies[c]; if (k.getName().equalsIgnoreCase("JSESSIONID")) sessionID = k; } log.info("Saw JSESSIONID=" + sessionID); // Reset the domain so that the cookie will be sent to server1 sessionID.setDomain(servers[1]); state.addCookie(sessionID); _sleep(DEFAULT_SLEEP); // Access the StatefulSessionServlet of dist-ss.war@server1 twice GetMethod servletGet1 = new GetMethod(baseURL1 + "/dist-ss/StatefulSessionServlet/"); responseCode = httpConn.executeMethod(servletGet1.getHostConfiguration(), servletGet1, state); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); accessCount = servletGet1.getResponseHeader("X-AccessCount"); count = Integer.parseInt(accessCount.getValue()); assertEquals("X-AccessCount ", 2, count); _sleep(DEFAULT_SLEEP); // Reset the domain so that the cookie will be sent to server0 sessionID.setDomain(servers[0]); state.addCookie(sessionID); servletGet0 = new GetMethod(baseURL0 + "/dist-ss/StatefulSessionServlet/"); responseCode = httpConn.executeMethod(servletGet0.getHostConfiguration(), servletGet0, state); accessCount = servletGet0.getResponseHeader("X-AccessCount"); count = Integer.parseInt(accessCount.getValue()); assertEquals("X-AccessCount ", 3, count); _sleep(DEFAULT_SLEEP); // Reset the domain so that the cookie will be sent to server1 sessionID.setDomain(servers[1]); state.addCookie(sessionID); servletGet1 = new GetMethod(baseURL1 + "/dist-ss/StatefulSessionServlet/"); responseCode = httpConn.executeMethod(servletGet1.getHostConfiguration(), servletGet1, state); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); accessCount = servletGet1.getResponseHeader("X-AccessCount"); count = Integer.parseInt(accessCount.getValue()); assertEquals("X-AccessCount ", 4, count); }
From source file:org.jboss.test.cluster.test.BaseTest.java
/** * Makes a http call to the jsp that retrieves the attribute stored on the * session. When the attribute values mathes with the one retrieved earlier, * we have HttpSessionReplication./*from www . jav a2s . c o m*/ * Makes use of commons-httpclient library of Apache * * @param client * @param url * @return session attribute */ protected String makeGetWithState(HttpClient client, String url) { GetMethod method = new GetMethod(url); int responseCode = 0; try { HttpState state = client.getState(); responseCode = client.executeMethod(method.getHostConfiguration(), method, state); } catch (IOException e) { e.printStackTrace(); fail("HttpClient executeMethod fails." + e.toString()); } assertTrue("Get OK with url: " + url + " responseCode: " + responseCode, responseCode == HttpURLConnection.HTTP_OK); // Read the response body. byte[] responseBody = method.getResponseBody(); /* Validate that the attribute was actually seen. An absence of the header is treated as true since there are pages used that done't add it. */ Header hdr = method.getResponseHeader("X-SawTestHttpAttribute"); Boolean sawAttr = hdr != null ? Boolean.valueOf(hdr.getValue()) : Boolean.TRUE; String attr = null; if (sawAttr.booleanValue()) attr = new String(responseBody); // Release the connection. // method.releaseConnection(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data return attr; }
From source file:org.jboss.test.cluster.test.BaseTest.java
protected void setCookieDomainToThisServer(HttpClient client, String server) { // Get the session cookie Cookie sessionID = getSessionCookie(client, server); // Reset the domain so that the cookie will be sent to server1 sessionID.setDomain(server);/*from w ww .j av a 2 s .com*/ client.getState().addCookie(sessionID); }
From source file:org.jboss.test.cluster.test.BaseTest.java
protected Cookie getSessionCookie(HttpClient client, String server) { // Get the state for the JSESSIONID HttpState state = client.getState(); // Get the JSESSIONID so we can reset the host Cookie[] cookies = state.getCookies(); Cookie sessionID = null;//from www . jav a 2 s . c o m for (int c = 0; c < cookies.length; c++) { Cookie k = cookies[c]; if (k.getName().equalsIgnoreCase("JSESSIONID")) sessionID = k; } if (sessionID == null) { fail("getSessionCookie(): fail to find session id. Server name: " + server); } log.info("Saw JSESSIONID=" + sessionID); return sessionID; }
From source file:org.jboss.test.cluster.test.CrossContextCallsTestCase.java
/** * Makes a http call to the jsp that retrieves the attribute stored on the * session. When the attribute values mathes with the one retrieved earlier, * we have HttpSessionReplication.//from w w w . ja v a 2s .c o m * Makes use of commons-httpclient library of Apache * * @param client * @param url * @return session attribute */ protected String makeGetWithState(HttpClient client, String url) { getLog().debug("makeGetWithState(): trying to get from url " + url); GetMethod method = new GetMethod(url); int responseCode = 0; try { HttpState state = client.getState(); responseCode = client.executeMethod(method.getHostConfiguration(), method, state); } catch (IOException e) { e.printStackTrace(); fail("HttpClient executeMethod fails." + e.toString()); } assertTrue("Get OK with url: " + url + " responseCode: " + responseCode, responseCode == HttpURLConnection.HTTP_OK); String response = extractResponse(method); // Release the connection. // method.releaseConnection(); return response; }
From source file:org.jboss.test.cluster.test.FormAuthFailoverTestCase.java
/** * Test ability for a FORM auth based app to have failover without requiring * a new sign-on//from ww w .j a va2 s. co m * * @throws Exception */ public void testFormAuthFailover() throws Exception { log.info("+++ testFormAuthFailover"); String[] httpURLs = super.getHttpURLs(); String serverA = httpURLs[0]; String serverB = httpURLs[1]; log.info(System.getProperties()); log.info("serverA: " + serverA); log.info("serverB: " + serverB); // Start by accessing the secured index.html of war1 HttpClient httpConn = new HttpClient(); GetMethod indexGet = new GetMethod(serverA + "/war1/index.html"); int responseCode = httpConn.executeMethod(indexGet); String body = indexGet.getResponseBodyAsString(); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0); HttpState state = httpConn.getState(); Cookie[] cookies = state.getCookies(); String sessionID = null; for (int c = 0; c < cookies.length; c++) { Cookie k = cookies[c]; if (k.getName().equalsIgnoreCase("JSESSIONID")) sessionID = k.getValue(); } log.debug("Saw JSESSIONID=" + sessionID); // Submit the login form PostMethod formPost = new PostMethod(serverA + "/war1/j_security_check"); formPost.addRequestHeader("Referer", serverA + "/war1/login.html"); formPost.addParameter("j_username", "jduke"); formPost.addParameter("j_password", "theduke"); responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, state); String response = formPost.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Saw HTTP_MOVED_TEMP(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_MOVED_TEMP); // Follow the redirect to the index.html page Header location = formPost.getResponseHeader("Location"); String indexURI = location.getValue(); GetMethod war1Index = new GetMethod(indexURI); responseCode = httpConn.executeMethod(war1Index.getHostConfiguration(), war1Index, state); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); body = war1Index.getResponseBodyAsString(); if (body.indexOf("j_security_check") > 0) fail("get of " + indexURI + " redirected to login page"); cookies = state.getCookies(); sessionID = null; for (int c = 0; c < cookies.length; c++) { Cookie k = cookies[c]; if (k.getName().equalsIgnoreCase("JSESSIONID")) { sessionID = k.getValue(); if (serverA.equals(serverB) == false) { // Make a session cookie to send to serverB Cookie copy = copyCookie(k, serverB); state.addCookie(copy); log.debug("Added state cookie: " + copy); } } } assertTrue("Saw JSESSIONID", sessionID != null); log.debug("Saw JSESSIONID=" + sessionID); // Now try getting the war1 index on the second server log.debug("Prepare /war1/index.html get"); GetMethod war2Index = new GetMethod(serverB + "/war2/index.html"); responseCode = httpConn.executeMethod(war2Index.getHostConfiguration(), war2Index, state); response = war2Index.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); body = war2Index.getResponseBodyAsString(); log.debug("body: " + body); if (body.indexOf("j_security_check") > 0) fail("get of /war1/index.html redirected to login page"); /* Access a secured servlet that calls a secured ejb in war2 to test propagation of the identity to the ejb container. */ GetMethod war2Servlet = new GetMethod(serverB + "/war1/EJBServlet"); responseCode = httpConn.executeMethod(war2Servlet.getHostConfiguration(), war2Servlet, state); response = war2Servlet.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); body = war2Servlet.getResponseBodyAsString(); log.debug("body: " + body); if (body.indexOf("j_security_check") > 0) fail("get of /war1/EJBServlet redirected to login page"); }
From source file:org.jboss.test.cluster.testutil.SessionTestUtil.java
public static Cookie getSessionCookie(HttpClient client) { // Get the state for the JSESSIONID HttpState state = client.getState(); // Get the JSESSIONID so we can reset the host Cookie[] cookies = state.getCookies(); Cookie sessionID = null;/*from w w w.ja v a 2 s .c o m*/ for (int c = 0; c < cookies.length; c++) { Cookie k = cookies[c]; if (k.getName().equalsIgnoreCase("JSESSIONID")) sessionID = k; } return sessionID; }