List of usage examples for org.apache.commons.httpclient.methods PostMethod addParameter
public void addParameter(String paramString1, String paramString2) throws IllegalArgumentException
From source file:org.jahia.test.bin.RenderTest.java
@Test public void testRestAPI() throws RepositoryException, IOException, JSONException { JCRPublicationService jcrService = ServicesRegistry.getInstance().getJCRPublicationService(); Locale englishLocale = LanguageCodeConverters.languageCodeToLocale("en"); JCRSessionWrapper editSession = jcrService.getSessionFactory() .getCurrentUserSession(Constants.EDIT_WORKSPACE, englishLocale); jcrService.getSessionFactory().getCurrentUserSession(Constants.LIVE_WORKSPACE, englishLocale); JCRNodeWrapper stageRootNode = editSession.getNode(SITECONTENT_ROOT_NODE); JCRNodeWrapper stageNode = stageRootNode.getNode("home"); JCRNodeWrapper stagedPageContent = stageNode.getNode("listA"); JCRNodeWrapper mainContent = stagedPageContent.addNode("mainContent", "jnt:mainContent"); mainContent.setProperty("jcr:title", MAIN_CONTENT_TITLE + "0"); mainContent.setProperty("body", MAIN_CONTENT_BODY + "0"); editSession.save();//from ww w . j a v a 2s .c o m PostMethod createPost = new PostMethod(getBaseServerURL() + Jahia.getContextPath() + "/cms/render/default/en" + SITECONTENT_ROOT_NODE + "/home/listA/*"); createPost.addRequestHeader("x-requested-with", "XMLHttpRequest"); createPost.addRequestHeader("accept", "application/json"); // here we voluntarily don't set the node name to test automatic name creation. createPost.addParameter("jcrNodeType", "jnt:mainContent"); createPost.addParameter("jcr:title", MAIN_CONTENT_TITLE + "1"); createPost.addParameter("body", MAIN_CONTENT_BODY + "1"); String responseBody = ""; try { int responseCode = getHttpClient().executeMethod(createPost); assertEquals("Error in response, code=" + responseCode, 201, responseCode); responseBody = createPost.getResponseBodyAsString(); } finally { createPost.releaseConnection(); } JSONObject jsonResults = new JSONObject(responseBody); assertNotNull("A proper JSONObject instance was expected, got null instead", jsonResults); assertTrue("body property should be " + MAIN_CONTENT_BODY + "1", jsonResults.get("body").equals(MAIN_CONTENT_BODY + "1")); }
From source file:org.jahia.test.JahiaTestCase.java
protected PostResult post(String url, String[]... params) throws IOException { PostMethod method = new PostMethod(url); for (String[] param : params) { method.addParameter(param[0], param[1]); }//from w ww .ja v a 2 s .c om // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); int statusCode = 0; String statusLine = null; String responseBody = null; try { // Execute the method. statusCode = getHttpClient().executeMethod(method); statusLine = method.getStatusLine().toString(); if (statusCode != HttpStatus.SC_OK) { logger.warn("Method failed: {}", statusLine); } // Read the response body. responseBody = method.getResponseBodyAsString(); } finally { method.releaseConnection(); } return new PostResult(statusCode, statusLine, responseBody); }
From source file:org.jboss.mod_cluster.TestBaseJgroups.java
public void NotestBase() { boolean clienterror = false; StandardServer server = new StandardServer(); ModClusterService cluster = null;//w ww . jav a 2 s. c o m JBossWeb service = null; System.out.println("TestBaseJgroups Started"); System.setProperty("org.apache.catalina.core.StandardService.DELAY_CONNECTOR_STARTUP", "false"); try { service = new JBossWeb("node1", "localhost"); service.addConnector(8011); server.addService(service); cluster = Maintest.createClusterListener(server, "224.0.1.105", 23364, false, null, true, false, true, "secret"); } catch (Exception ex) { ex.printStackTrace(); fail("can't start service"); } // start the server thread. ServerThread wait = new ServerThread(3000, server); wait.start(); // Wait until httpd we know about httpd. String[] nodes = new String[1]; nodes[0] = "node1"; int countinfo = 0; while ((!Maintest.checkProxyInfo(cluster, nodes)) && countinfo < 20) { try { Thread.sleep(3000); } catch (InterruptedException ex) { ex.printStackTrace(); } countinfo++; } // Create 2 JGroups ID and query the result and remove them. String proxy = Maintest.getProxyAddress(cluster); String URL = "http://" + proxy + "/"; HttpClient httpClient = new HttpClient(); PostMethod pm = null; pm = (PostMethod) new AddIdMethod(URL); pm.addParameter("JGroupUuid", "ID1"); pm.addParameter("JGroupData", "DATA1"); String response = processrequest(pm, httpClient); if (response == null) fail("ADDID(1) failed on: " + URL); pm = (PostMethod) new AddIdMethod(URL); pm.addParameter("JGroupUuid", "ID2"); pm.addParameter("JGroupData", "DATA2"); response = processrequest(pm, httpClient); if (response == null) fail("ADDID(2) failed"); pm = (PostMethod) new QueryMethod(URL); pm.addParameter("JGroupUuid", "*"); response = processrequest(pm, httpClient); if (response == null) fail("QUERY failed"); System.out.println("Response:\n" + response); String[] records = response.split("\n"); if (records.length != 2) fail("QUERY return " + records.length + " JGroupUuid instead 2"); pm = (PostMethod) new RemoveIdMethod(URL); pm.addParameter("JGroupUuid", "ID2"); response = processrequest(pm, httpClient); if (response == null) fail("REMOVE(ID2) failed"); pm = (PostMethod) new RemoveIdMethod(URL); pm.addParameter("JGroupUuid", "ID1"); response = processrequest(pm, httpClient); if (response == null) fail("REMOVE(ID1) failed"); /* See MODCLUSTER-282 it doesn't work on all hudson boxes. pm = (PostMethod) new QueryMethod(URL); pm.addParameter("JGroupUuid", "*"); response = processrequest(pm, httpClient); if (response == null) fail("QUERY failed"); System.out.println("Response:\n" + response); if (response.length() == 0) System.out.println("AddId + Remove OK"); else fail("QUERY returns " + response + " instead nothing"); */ // Stop the jboss and remove the services. try { wait.stopit(); wait.join(); server.removeService(service); } catch (InterruptedException ex) { ex.printStackTrace(); fail("can't stop service"); } if (clienterror) fail("Client error"); // Wait until httpd as received the stop messages. countinfo = 0; while ((!Maintest.checkProxyInfo(cluster, null)) && countinfo < 20) { try { Thread.sleep(3000); } catch (InterruptedException ex) { ex.printStackTrace(); } countinfo++; } Maintest.StopClusterListener(); System.gc(); System.out.println("TestBaseJgroups Done"); }
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//www. j a va 2 s . c om * * 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.test.FormAuthFailoverTestCase.java
/** * Test ability for a FORM auth based app to have failover without requiring * a new sign-on// w w w . ja v a2 s . c o 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.jmx.test.JMXConsoleUnitTestCase.java
/** Test an mbean invocation via a post to the HtmlAdaptor * @throws Exception//from w ww . j a va2s. c o m */ public void testMBeanOperation() throws Exception { // The jboss.system:type=Server mbean view URL url = new URL( baseURLNoAuth + "jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss.system%3Atype%3DServer"); HttpUtils.accessURL(url); // Submit the op invocation form for op=runGarbageCollector PostMethod formPost = new PostMethod(baseURLNoAuth + "jmx-console/HtmlAdaptor"); formPost.addRequestHeader("Referer", baseURLNoAuth + "jmx-console/HtmlAdaptor"); formPost.addParameter("action", "invokeOpByName"); formPost.addParameter("name", "jboss.system:type=Server"); formPost.addParameter("methodName", "runGarbageCollector"); HttpClient httpConn = new HttpClient(); int responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost); assertTrue("HTTP_OK", responseCode == HttpURLConnection.HTTP_OK); }
From source file:org.jboss.test.security.test.CustomPrincipalPropagationUnitTestCase.java
/** * A web-app has a welcome jsp (called as index.jsp). Inside this jsp, * there is a call made out to an ejb//www. j a v a 2 s .c om * * @throws Exception */ public void testCustomPrincipalTransmissionInVM() throws Exception { String baseURLNoAuth = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + "/"; HttpClient httpConn = new HttpClient(); GetMethod indexGet = new GetMethod(baseURLNoAuth + "custom-principal/"); int responseCode = httpConn.executeMethod(indexGet); String body = indexGet.getResponseBodyAsString(); assertTrue("Get OK(" + responseCode + ")", 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 + "custom-principal/j_security_check"); formPost.addRequestHeader("Referer", baseURLNoAuth + "custom-principal/login.jsp"); formPost.addParameter("j_username", this.username); formPost.addParameter("j_password", new String(password)); responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, state); String loginResult = formPost.getResponseBodyAsString(); if (loginResult.indexOf("Encountered a login error") > 0) fail("Login Failed"); String response = formPost.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP); // Follow the redirect to the index.jsp Header location = formPost.getResponseHeader("Location"); String indexURI = location.getValue(); GetMethod war1Index = new GetMethod(indexURI); responseCode = httpConn.executeMethod(war1Index.getHostConfiguration(), war1Index, state); response = war1Index.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); body = war1Index.getResponseBodyAsString(); log.debug("Final result obtained:" + body); if (body.indexOf("j_security_check") > 0) fail("get of " + indexURI + " redirected to login page"); if (body.indexOf("Propagation Success") < 0) fail("Propagation of custom principal within VM failed"); }
From source file:org.jboss.test.security.test.mapping.RoleMappingWebTestCase.java
/** * Test a FORM auth simple webapp. A role of "testRole" will * be mapped to Authorized User via the role mapping logic *///ww w . j a v a2 s.c o m public void testWebAccess() throws Exception { baseURLNoAuth = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + "/"; GetMethod indexGet = new GetMethod(baseURLNoAuth + "web-role-map/Secured.jsp"); int responseCode = httpConn.executeMethod(indexGet); String body = indexGet.getResponseBodyAsString(); assertTrue("Get OK(" + responseCode + ")", 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 + "web-role-map/j_security_check"); formPost.addRequestHeader("Referer", baseURLNoAuth + "web-role-map/login.html"); formPost.addParameter("j_username", "user"); formPost.addParameter("j_password", "pass"); responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, state); String response = formPost.getStatusText(); log.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); response = war1Index.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); 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.security.test.SecurityDomainTolerateUnitTestCase.java
public void testWeb() throws Exception { String baseURLNoAuth = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + "/"; HttpClient httpConn = new HttpClient(); GetMethod indexGet = new GetMethod(baseURLNoAuth + "sdtolerate/"); int responseCode = httpConn.executeMethod(indexGet); String body = indexGet.getResponseBodyAsString(); assertTrue("Get OK(" + responseCode + ")", 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;/* w w w .j ava 2s . c o m*/ 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 + "sdtolerate/j_security_check"); formPost.addRequestHeader("Referer", baseURLNoAuth + "sdtolerate/login.jsp"); formPost.addParameter("j_username", this.username); formPost.addParameter("j_password", new String(password)); responseCode = httpConn.executeMethod(formPost); String loginResult = formPost.getResponseBodyAsString(); if (loginResult.indexOf("Encountered a login error") > 0) fail("Login Failed"); String response = formPost.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP); // Follow the redirect to the index.jsp Header location = formPost.getResponseHeader("Location"); String indexURI = location.getValue(); GetMethod war1Index = new GetMethod(indexURI); responseCode = httpConn.executeMethod(war1Index); response = war1Index.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); }