List of usage examples for org.apache.commons.httpclient HttpClient getState
public HttpState getState()
From source file:org.jboss.test.web.test.FormAuthUnitTestCase.java
/** Test that a post from an unsecured form to a secured servlet does not * loose its data during the redirct to the form login. * /*from w w w . ja va 2 s . c om*/ * @throws Exception */ public void testPostDataFormAuth() throws Exception { log.info("+++ testPostDataFormAuth"); // Start by accessing the secured index.html of war1 HttpClient httpConn = new HttpClient(); GetMethod indexGet = new GetMethod(baseURLNoAuth + "form-auth/unsecure_form.html"); int responseCode = httpConn.executeMethod(indexGet); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); // Submit the form to /restricted/SecuredPostServlet PostMethod servletPost = new PostMethod(baseURLNoAuth + "form-auth/restricted/SecuredPostServlet"); servletPost.addParameter("checkParam", "123456"); responseCode = httpConn.executeMethod(servletPost); String body = servletPost.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(); } getLog().debug("Saw JSESSIONID=" + sessionID); // Submit the login form PostMethod formPost = new PostMethod(baseURLNoAuth + "form-auth/j_security_check"); formPost.addRequestHeader("Referer", baseURLNoAuth + "form-auth/unsecure_form.html"); formPost.addParameter("j_username", "jduke"); formPost.addParameter("j_password", "theduke"); responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, state); String response = formPost.getStatusText(); getLog().debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP); // Follow the redirect to the SecureServlet 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"); }
From source file:org.jboss.test.web.test.SSOBaseCase.java
/** Test single sign-on across two web apps using form based auth * // ww w. ja va2s. c om * @throws Exception */ protected static void executeFormAuthSingleSignOnTest(String serverA, String serverB, Category log) throws Exception { String warA1 = serverA + "/war1/"; String warB2 = serverB + "/war2/"; // Start by accessing the secured index.html of war1 HttpClient httpConn = new HttpClient(); checkAccessDenied(httpConn, warA1 + "index.html"); HttpState state = httpConn.getState(); String sessionID = getSessionIdFromState(state); log.debug("Saw JSESSIONID=" + sessionID); // Submit the login form executeFormLogin(httpConn, warA1); String ssoID = processSSOCookie(state, serverA, serverB); log.debug("Saw JSESSIONIDSSO=" + ssoID); // Pause a moment before switching wars to better simulate real life // use cases. Otherwise, the test case can "outrun" the async // replication in the TreeCache used by the clustered SSO // 500 ms is a long time, but this isn't a test of replication speed // and we don't want spurious failures. if (!serverA.equals(serverB)) Thread.sleep(500); // Now try getting the war2 index using the JSESSIONIDSSO cookie log.debug("Prepare /war2/index.html get"); checkAccessAllowed(httpConn, warB2 + "index.html"); /* Access a secured servlet that calls a secured ejb in war2 to test propagation of the SSO identity to the ejb container. */ checkAccessAllowed(httpConn, warB2 + "EJBServlet"); // Now try logging out of war2 executeLogout(httpConn, warB2); // Again, pause before switching wars if (!serverA.equals(serverB)) Thread.sleep(500); // Try accessing war1 again checkAccessDenied(httpConn, warA1 + "index.html"); // Try accessing war2 again checkAccessDenied(httpConn, warB2 + "index.html"); }
From source file:org.jboss.test.web.test.SSOBaseCase.java
public static void executeLogout(HttpClient httpConn, String warURL) throws IOException, HttpException { GetMethod logout = new GetMethod(warURL + "Logout"); logout.setFollowRedirects(false);//from w w w . ja v a 2 s .c om int responseCode = httpConn.executeMethod(logout.getHostConfiguration(), logout, httpConn.getState()); assertTrue("Logout: Saw HTTP_MOVED_TEMP(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_MOVED_TEMP); Header location = logout.getResponseHeader("Location"); String indexURI = location.getValue(); if (indexURI.indexOf("index.html") < 0) fail("get of " + warURL + "Logout not redirected to login page"); }
From source file:org.jboss.test.web.test.SSOBaseCase.java
public static void checkAccessAllowed(HttpClient httpConn, String url) throws IOException, HttpException { GetMethod war2Index = new GetMethod(url); int responseCode = httpConn.executeMethod(war2Index.getHostConfiguration(), war2Index, httpConn.getState()); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); String body = war2Index.getResponseBodyAsString(); if (body.indexOf("j_security_check") > 0) fail("get of " + url + " redirected to login page"); }
From source file:org.jboss.test.web.test.SSOBaseCase.java
public static void executeFormLogin(HttpClient httpConn, String warURL) throws IOException, HttpException { PostMethod formPost = new PostMethod(warURL + "j_security_check"); formPost.addRequestHeader("Referer", warURL + "login.html"); formPost.addParameter("j_username", "jduke"); formPost.addParameter("j_password", "theduke"); int 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); String body = warIndex.getResponseBodyAsString(); if (body.indexOf("j_security_check") > 0) fail("get of " + indexURI + " redirected to login page"); }
From source file:org.jboss.tools.common.util.HttpUtil.java
private static HttpClient createHttpClient(String url, IProxyService proxyService) throws IOException { HttpClient httpClient = new HttpClient(); if (proxyService.isProxiesEnabled()) { IProxyData[] proxyData = proxyService.getProxyData(); URL netUrl = new URL(url); String hostName = netUrl.getHost(); String[] nonProxiedHosts = proxyService.getNonProxiedHosts(); boolean nonProxiedHost = false; for (int i = 0; i < nonProxiedHosts.length; i++) { String nonProxiedHostName = nonProxiedHosts[i]; if (nonProxiedHostName.equalsIgnoreCase(hostName)) { nonProxiedHost = true;//from w w w . j a v a 2 s. c om break; } } if (!nonProxiedHost) { for (int i = 0; i < proxyData.length; i++) { IProxyData proxy = proxyData[i]; if (IProxyData.HTTP_PROXY_TYPE.equals(proxy.getType())) { String proxyHostName = proxy.getHost(); if (proxyHostName == null) { break; } int portNumber = proxy.getPort(); if (portNumber == -1) { portNumber = 80; } httpClient.getHostConfiguration().setProxy(proxyHostName, portNumber); if (proxy.isRequiresAuthentication()) { String userName = proxy.getUserId(); if (userName != null) { String password = proxy.getPassword(); httpClient.getState().setProxyCredentials( new AuthScope(null, AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME), new UsernamePasswordCredentials(userName, password)); } } break; // Use HTTP proxy only. } } } } httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000); return httpClient; }
From source file:org.jboss.web.loadbalancer.Loadbalancer.java
protected HttpClient prepareServerRequest(HttpServletRequest request, HttpServletResponse response, HttpMethod method) {// w w w .j ava 2s. c o m // clear state HttpClient client = new HttpClient(connectionManager); client.setStrictMode(false); client.setTimeout(connectionTimeout); method.setFollowRedirects(false); method.setDoAuthentication(false); client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY); Enumeration reqHeaders = request.getHeaderNames(); while (reqHeaders.hasMoreElements()) { String headerName = (String) reqHeaders.nextElement(); String headerValue = request.getHeader(headerName); if (!ignorableHeader.contains(headerName.toLowerCase())) { method.setRequestHeader(headerName, headerValue); } } //Cookies Cookie[] cookies = request.getCookies(); HttpState state = client.getState(); for (int i = 0; cookies != null && i < cookies.length; ++i) { Cookie cookie = cookies[i]; org.apache.commons.httpclient.Cookie reqCookie = new org.apache.commons.httpclient.Cookie(); reqCookie.setName(cookie.getName()); reqCookie.setValue(cookie.getValue()); if (cookie.getPath() != null) { reqCookie.setPath(cookie.getPath()); } else { reqCookie.setPath("/"); } reqCookie.setSecure(cookie.getSecure()); reqCookie.setDomain(method.getHostConfiguration().getHost()); state.addCookie(reqCookie); } return client; }
From source file:org.jboss.web.loadbalancer.Loadbalancer.java
protected void parseServerResponse(HttpServletRequest request, HttpServletResponse response, HttpClient client, HttpMethod method) throws ServletException, IOException { response.setStatus(method.getStatusCode()); //Cookies/*from ww w .jav a 2s . c o m*/ org.apache.commons.httpclient.Cookie[] respCookies = client.getState().getCookies(); for (int i = 0; i < respCookies.length; ++i) { Cookie cookie = new Cookie(respCookies[i].getName(), respCookies[i].getValue()); if (respCookies[i].getPath() != null) { cookie.setPath(respCookies[i].getPath()); } response.addCookie(cookie); } Header[] header = method.getResponseHeaders(); for (int i = 0; i < header.length; ++i) { if (!ignorableHeader.contains(header[i].getName().toLowerCase())) { response.setHeader(header[i].getName(), header[i].getValue()); } } copyServerResponse(response, method); }
From source file:org.jbpm.formbuilder.server.GuvnorHelper.java
public void setAuth(HttpClient client, HttpMethod method) { if (notEmpty(this.user) && notEmpty(this.password)) { client.getParams().setAuthenticationPreemptive(true); UsernamePasswordCredentials defaultcreds = new UsernamePasswordCredentials(this.user, this.password); AuthScope authScope = new AuthScope(this.domainName, this.portNumber, AuthScope.ANY_REALM); client.getState().setCredentials(authScope, defaultcreds); }/* w w w. j av a 2 s . com*/ }
From source file:org.jenkinsci.plugins.testrail.TestRailClient.java
private HttpClient setUpHttpClient(HttpMethod method) { HttpClient httpclient = new HttpClient(); httpclient.getParams().setAuthenticationPreemptive(true); httpclient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(this.user, this.password)); method.setDoAuthentication(true);//from w w w.j a v a 2 s. c o m method.addRequestHeader("Content-Type", "application/json"); return httpclient; }