List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBody
public abstract byte[] getResponseBody() throws IOException;
From source file:org.soasecurity.wso2.mutual.auth.oauth2.client.MutualSSLOAuthClient.java
public static void main(String[] args) throws Exception { File file = new File((new File(".")).getCanonicalPath() + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "keystore" + File.separator + keyStoreName); if (!file.exists()) { throw new Exception("Key Store file can not be found in " + file.getCanonicalPath()); }//from w w w .j a va 2 s . c o m //Set trust store, you need to import server's certificate of CA certificate chain in to this //key store System.setProperty("javax.net.ssl.trustStore", file.getCanonicalPath()); System.setProperty("javax.net.ssl.trustStorePassword", keyStorePassword); //Set key store, this must contain the user private key //here we have use both trust store and key store as the same key store //But you can use a separate key store for key store an trust store. System.setProperty("javax.net.ssl.keyStore", file.getCanonicalPath()); System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword); HttpClient client = new HttpClient(); HttpMethod method = new PostMethod(endPoint); // Base64 encoded client id & secret method.setRequestHeader("Authorization", "Basic T09pN2dpUjUwdDZtUmU1ZkpmWUhVelhVa1QwYTpOOUI2dDZxQ0E2RFp2eTJPQkFIWDhjVlI1eUlh"); method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); NameValuePair pair1 = new NameValuePair(); pair1.setName("grant_type"); pair1.setValue("x509"); NameValuePair pair2 = new NameValuePair(); pair2.setName("username"); pair2.setValue("asela"); NameValuePair pair3 = new NameValuePair(); pair3.setName("password"); pair3.setValue("asela"); method.setQueryString(new NameValuePair[] { pair1, pair2, pair3 }); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.out.println("Failed: " + method.getStatusLine()); } else { byte[] responseBody = method.getResponseBody(); System.out.println(new String(responseBody)); } }
From source file:org.springbyexample.httpclient.HttpClientTemplate.java
/** * Processes <code>HttpMethod</code> by executing the method, * validating the response, and calling the callback. * // ww w.ja va 2 s . c o m * @param httpMethod <code>HttpMethod</code> to process. * @param callback Callback with HTTP method's response. */ protected void processHttpMethod(HttpMethod httpMethod, ResponseCallback<?> callback) { try { client.executeMethod(httpMethod); validateResponse(httpMethod); if (callback instanceof ResponseByteCallback) { ((ResponseByteCallback) callback).doWithResponse(httpMethod.getResponseBody()); } else if (callback instanceof ResponseStreamCallback) { ((ResponseStreamCallback) callback).doWithResponse(httpMethod.getResponseBodyAsStream()); } else if (callback instanceof ResponseStringCallback) { ((ResponseStringCallback) callback).doWithResponse(httpMethod.getResponseBodyAsString()); } } catch (HttpException e) { throw new HttpAccessException(e.getMessage(), e, httpMethod.getStatusCode()); } catch (IOException e) { throw new HttpAccessException(e.getMessage(), e); } finally { httpMethod.releaseConnection(); } }
From source file:org.springside.fi.common.httpclient.HttpClientTemplate.java
/** * Processes <code>HttpMethod</code> by executing the method, * validating the response, and calling the callback. * //from ww w . j av a 2 s. c o m * @param httpMethod <code>HttpMethod</code> to process. * @param callback Callback with HTTP method's response. */ protected byte[] processHttpMethod(HttpMethod httpMethod) { try { client.getHttpConnectionManager().getParams().setConnectionTimeout(60000); client.getHttpConnectionManager().getParams().setSoTimeout(60000); client.executeMethod(httpMethod); validateResponse(httpMethod); return httpMethod.getResponseBody(); } catch (HttpException e) { throw new HttpAccessException(e.getMessage(), e, httpMethod.getStatusCode()); } catch (IOException e) { throw new HttpAccessException(e.getMessage(), e); } finally { httpMethod.releaseConnection(); } }
From source file:org.structr.web.Importer.java
private void copyURLToFile(final String uri, final java.io.File fileOnDisk) throws IOException { final HttpClient client = getHttpClient(); final HttpMethod get = new GetMethod(); get.setURI(new URI(uri, false)); get.addRequestHeader("User-Agent", "curl/7.35.0"); logger.log(Level.INFO, "Downloading from {0}", uri); final int statusCode = client.executeMethod(get); if (statusCode == 200) { try (final InputStream is = get.getResponseBodyAsStream()) { try (final OutputStream os = new FileOutputStream(fileOnDisk)) { IOUtils.copy(is, os);//ww w.j a v a 2 s . c om } } } else { System.out.println("response body: " + new String(get.getResponseBody(), "utf-8")); logger.log(Level.WARNING, "Unable to create file {0}: status code was {1}", new Object[] { uri, statusCode }); } }
From source file:org.wingsource.plugin.impl.gadget.bean.Gadget.java
private Response getResponse(String tokenId, String href, Map<String, String> requestParameters) { logger.finest("Fetching content using HttpClient....user-Id: " + tokenId); HttpClient hc = new HttpClient(); hc.getHttpConnectionManager().getParams().setConnectionTimeout(5000); HttpMethod method = new GetMethod(href); method.addRequestHeader("xx-wings-user-id", tokenId); ArrayList<NameValuePair> nvpList = new ArrayList<NameValuePair>(); Set<String> keys = requestParameters.keySet(); for (String key : keys) { String value = requestParameters.get(key); nvpList.add(new NameValuePair(key, value)); }//from ww w . j a v a2 s . c om String qs = method.getQueryString(); if (qs != null) { String[] nvPairs = qs.split("&"); for (String nvPair : nvPairs) { String[] mapping = nvPair.split("="); nvpList.add(new NameValuePair(mapping[0], mapping[1])); } } method.setFollowRedirects(true); NameValuePair[] nvps = new NameValuePair[nvpList.size()]; nvps = nvpList.toArray(nvps); method.setQueryString(nvps); byte[] content = null; Header[] headers = null; try { hc.executeMethod(method); content = method.getResponseBody(); headers = method.getResponseHeaders(); } catch (HttpException e) { logger.log(Level.SEVERE, e.getMessage(), e); } catch (IOException e) { logger.log(Level.SEVERE, e.getMessage(), e); } return new Response(content, headers); }
From source file:org.xwiki.test.storage.AttachmentTest.java
/** * Tests that XWIKI-5405 remains fixed.//from ww w.j a v a 2s . c o m * This test proves that when an attachment is saved using Document.addAttachment and * then Document.save() the attachment is actually persisted to the database. */ @Test public void testDocumentAddAttachment() throws Exception { final String test = "{{groovy}}\n" // First the attacher. + "doc.addAttachment('" + FILENAME + "', '" + ATTACHMENT_CONTENT + "'.getBytes('UTF-8'));\n" + "doc.saveAsAuthor();" // then the validator. + "println(xwiki.getDocument(doc.getDocumentReference()).getAttachment('" + FILENAME + "').getContentAsString());" + "{{/groovy}}"; // Delete the document if it exists. doPostAsAdmin("Test", "Attachment", null, "delete", "confirm=1", null); // Create a document. doPostAsAdmin("Test", "Attachment", null, "save", null, new HashMap<String, String>() { { put("content", test); } }); HttpMethod ret = null; // Test getAttachment() ret = doPostAsAdmin("Test", "Attachment", null, "view", "xpage=plain", null); Assert.assertEquals("<p>" + ATTACHMENT_CONTENT + "</p>", ret.getResponseBodyAsString()); // Test downloadAction. ret = doPostAsAdmin("Test", "Attachment", FILENAME, "download", null, null); Assert.assertEquals(ATTACHMENT_CONTENT, new String(ret.getResponseBody(), "UTF-8")); Assert.assertEquals(200, ret.getStatusCode()); // Make sure there is exactly 1 version of this attachment. ret = doPostAsAdmin("Test", "Attachment", null, "preview", "xpage=plain", new HashMap<String, String>() { { put("content", "{{velocity}}$doc.getAttachment('" + FILENAME + "').getVersions().size(){{/velocity}}"); } }); Assert.assertEquals("<p>1</p>", ret.getResponseBodyAsString()); // Make sure that version contains the correct content. ret = doPostAsAdmin("Test", "Attachment", null, "preview", "xpage=plain", new HashMap<String, String>() { { put("content", "{{velocity}}$doc.getAttachment('" + FILENAME + "').getAttachmentRevision('1.1').getContentAsString(){{/velocity}}"); } }); Assert.assertEquals("<p>" + ATTACHMENT_CONTENT + "</p>", ret.getResponseBodyAsString()); }
From source file:org.xwiki.test.storage.AttachmentTest.java
@Test public void testRollbackAfterNewAttachment() throws Exception { final String spaceName = "Test"; final String pageName = "testRollbackAfterNewAttachment"; final String attachURL = this.getAddressPrefix() + "download/" + spaceName + "/" + pageName + "/" + FILENAME;//from w w w .j ava2 s .c o m // Delete the document if it exists. doPostAsAdmin(spaceName, pageName, null, "delete", "?confirm=1", null); // Create a document. doPostAsAdmin(spaceName, pageName, null, "save", null, null); HttpMethod ret; // Upload the attachment ret = doUploadAsAdmin(spaceName, pageName, new HashMap<String, byte[]>() { { put(FILENAME, ATTACHMENT_CONTENT.getBytes()); } }); // Make sure it's there. Assert.assertEquals(ATTACHMENT_CONTENT, StoreTestUtils.getPageAsString(attachURL)); // Do a rollback. doPostAsAdmin(spaceName, pageName, null, "rollback", "?rev=1.1&confirm=1", null); // Make sure it's nolonger there. ret = doPostAsAdmin(spaceName, pageName, FILENAME, "download", null, null); Assert.assertFalse(ATTACHMENT_CONTENT.equals(new String(ret.getResponseBody(), "UTF-8"))); Assert.assertEquals(404, ret.getStatusCode()); }
From source file:org.xwiki.test.storage.AttachmentTest.java
/** * If the user saves an attachment, then deletes it, then saves another with the same name, * then rolls the version back, the new attachment should be trashed and the old attachment should be * restored.//w w w . ja v a 2 s.c o m */ @Test // This test flickers. see: http://jira.xwiki.org/jira/browse/XE-934 @Ignore public void testRollbackAfterSaveDeleteSaveAttachment() throws Exception { final String spaceName = "Test"; final String pageName = "testRollbackAfterSaveDeleteSaveAttachment"; final String versionTwo = "This is some different content"; final String attachURL = this.getAddressPrefix() + "download/" + spaceName + "/" + pageName + "/" + FILENAME; // Delete the document if it exists. doPostAsAdmin(spaceName, pageName, null, "delete", "confirm=1", null); // Create a document. v1.1 doPostAsAdmin(spaceName, pageName, null, "save", null, null); HttpMethod ret; // Upload the attachment v2.1 ret = doUploadAsAdmin(spaceName, pageName, new HashMap<String, byte[]>() { { put(FILENAME, ATTACHMENT_CONTENT.getBytes()); } }); // Make sure it's there. Assert.assertEquals(ATTACHMENT_CONTENT, StoreTestUtils.getPageAsString(attachURL)); // Delete it v3.1 doPostAsAdmin(spaceName, pageName, FILENAME, "delattachment", null, null); // Upload again v4.1 ret = doUploadAsAdmin(spaceName, pageName, new HashMap<String, byte[]>() { { put(FILENAME, versionTwo.getBytes()); } }); // Make sure it's there. Assert.assertEquals(versionTwo, StoreTestUtils.getPageAsString(attachURL)); // Do a rollback. v5.1 doPostAsAdmin(spaceName, pageName, null, "rollback", "rev=2.1&confirm=1", null); // Make sure the latest current version is actually v5.1 ret = doPostAsAdmin(spaceName, pageName, null, "preview", "xpage=plain", new HashMap<String, String>() { { put("content", "{{velocity}}$doc.getVersion(){{/velocity}}"); } }); Assert.assertEquals("<p>5.1</p>", new String(ret.getResponseBody(), "UTF-8")); // Make sure it is version1 Assert.assertEquals(ATTACHMENT_CONTENT, StoreTestUtils.getPageAsString(attachURL)); // Do rollback to version2. v6.1 doPostAsAdmin(spaceName, pageName, null, "rollback", "rev=4.1&confirm=1", null); // Make sure it is version2 Assert.assertEquals(versionTwo, StoreTestUtils.getPageAsString(attachURL)); // Make sure the latest current version is actually v6.1 ret = doPostAsAdmin(spaceName, pageName, null, "preview", "xpage=plain", new HashMap<String, String>() { { put("content", "{{velocity}}$doc.getVersion(){{/velocity}}"); } }); Assert.assertEquals("<p>6.1</p>", new String(ret.getResponseBody(), "UTF-8")); }
From source file:org.xwiki.test.storage.DocumentTest.java
@Test public void testRollback() throws Exception { final String spaceName = "DocumentTest"; final String pageName = "testRollback"; final String versionOne = "This is version one"; final String versionTwo = "This is version two"; final String pageURL = this.getAddressPrefix() + "get/" + spaceName + "/" + pageName + "?xpage=plain"; // Delete the document if it exists. doPostAsAdmin(spaceName, pageName, null, "delete", "confirm=1", null); // Create a document. v1.1 doPostAsAdmin(spaceName, pageName, null, "save", null, new HashMap<String, String>() { {/*from w w w . j ava 2 s.co m*/ put("content", versionOne); } }); // Change the document v2.1 doPostAsAdmin(spaceName, pageName, null, "save", null, new HashMap<String, String>() { { put("content", versionTwo); } }); // Make sure it's version 2. Assert.assertEquals("<p>" + versionTwo + "</p>", StoreTestUtils.getPageAsString(pageURL)); // Do a rollback. v3.1 doPostAsAdmin(spaceName, pageName, null, "rollback", "rev=1.1&confirm=1", null); // Make sure it's the same as version 1. Assert.assertEquals("<p>" + versionOne + "</p>", StoreTestUtils.getPageAsString(pageURL)); // Make sure the latest current version is actually v3.1 HttpMethod ret = doPostAsAdmin(spaceName, pageName, null, "preview", "xpage=plain", new HashMap<String, String>() { { put("content", "{{velocity}}$doc.getVersion(){{/velocity}}"); } }); Assert.assertEquals("<p>3.1</p>", new String(ret.getResponseBody(), "UTF-8")); }
From source file:org.xwiki.test.storage.framework.StoreTestUtils.java
public static String getPageAsString(final String address) throws IOException { final HttpMethod ret = doPost(address, null, null); return new String(ret.getResponseBody(), "UTF-8"); }