List of usage examples for org.apache.commons.httpclient.methods PutMethod setRequestEntity
public void setRequestEntity(RequestEntity paramRequestEntity)
From source file:org.xwiki.tool.provision.InstallMojo.java
private PutMethod executePutXml(String uri, Object object, Marshaller marshaller, HttpClient httpClient) throws Exception { PutMethod putMethod = new PutMethod(uri); putMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString()); StringWriter writer = new StringWriter(); marshaller.marshal(object, writer);/*from ww w .jav a 2 s. c om*/ RequestEntity entity = new StringRequestEntity(writer.toString(), MediaType.APPLICATION_XML.toString(), "UTF-8"); putMethod.setRequestEntity(entity); httpClient.executeMethod(putMethod); return putMethod; }
From source file:org.xwiki.webdav.test.AbstractWebDAVTest.java
/** * Tests the PUT method on the given url. * /* w ww . j a v a 2 s . c om*/ * @param url the target url. * @param content the content for the {@link PutMethod}. * @param expect the return status expected. * @return the {@link HttpMethod} which contains the response. */ protected HttpMethod put(String url, String content, int expect) throws Exception { PutMethod putMethod = new PutMethod(); putMethod.setDoAuthentication(true); putMethod.setPath(url); putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(content.getBytes()))); testMethod(putMethod, expect); return putMethod; }
From source file:org.xwiki.webdav.test.DefaultWebDAVTest.java
/** * Test update page content./* w ww.ja v a 2 s . c om*/ */ @Test public void testUpdatePageWikiContent() throws Exception { String spaceUrl = SPACES + "/TestSpace"; String pageUrl = spaceUrl + "/TestPage"; String wikiTextFileUrl = pageUrl + "/wiki.txt"; String wikiXMLFileUrl = pageUrl + "/wiki.xml"; String newContent = "New Content"; DeleteMethod deleteMethod = new DeleteMethod(); deleteMethod.setDoAuthentication(true); MkcolMethod mkColMethod = new MkcolMethod(); mkColMethod.setDoAuthentication(true); PutMethod putMethod = new PutMethod(); putMethod.setDoAuthentication(true); GetMethod getMethod = new GetMethod(); getMethod.setDoAuthentication(true); deleteMethod.setPath(spaceUrl); assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod)); mkColMethod.setPath(spaceUrl); assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod)); mkColMethod.setPath(pageUrl); assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod)); putMethod.setPath(wikiTextFileUrl); putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(newContent.getBytes()))); // Already existing resource, in which case SC_NO_CONTENT will be the return status. assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(putMethod)); getMethod.setPath(wikiTextFileUrl); assertEquals(DavServletResponse.SC_OK, getHttpClient().executeMethod(getMethod)); assertEquals(newContent, getMethod.getResponseBodyAsString()); putMethod.setPath(wikiXMLFileUrl); putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(newContent.getBytes()))); // XML saving was disabled recently. See https://jira.xwiki.org/browse/XWIKI-2910 assertEquals(DavServletResponse.SC_METHOD_NOT_ALLOWED, getHttpClient().executeMethod(putMethod)); deleteMethod.setPath(pageUrl); assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod)); deleteMethod.setPath(spaceUrl); assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod)); }
From source file:org.xwiki.webdav.test.DefaultWebDAVTest.java
/** * Test making attachment.//from w ww.j a v a 2s . c o m */ @Test public void testMakingAttachment() throws Exception { String spaceUrl = SPACES + "/TestSpace"; String pageUrl = spaceUrl + "/TestPage"; String attachmentUrl = pageUrl + "/attachment.txt"; String attachmentContent = "Attachment Content"; DeleteMethod deleteMethod = new DeleteMethod(); deleteMethod.setDoAuthentication(true); MkcolMethod mkColMethod = new MkcolMethod(); mkColMethod.setDoAuthentication(true); PutMethod putMethod = new PutMethod(); putMethod.setDoAuthentication(true); GetMethod getMethod = new GetMethod(); getMethod.setDoAuthentication(true); deleteMethod.setPath(spaceUrl); assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod)); mkColMethod.setPath(spaceUrl); assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod)); mkColMethod.setPath(pageUrl); assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(mkColMethod)); getMethod.setPath(attachmentUrl); assertEquals(DavServletResponse.SC_NOT_FOUND, getHttpClient().executeMethod(getMethod)); putMethod.setPath(attachmentUrl); putMethod.setRequestEntity( new InputStreamRequestEntity(new ByteArrayInputStream(attachmentContent.getBytes()))); assertEquals(DavServletResponse.SC_CREATED, getHttpClient().executeMethod(putMethod)); getMethod.setPath(attachmentUrl); assertEquals(DavServletResponse.SC_OK, getHttpClient().executeMethod(getMethod)); assertEquals(attachmentContent, getMethod.getResponseBodyAsString()); deleteMethod.setPath(attachmentUrl); assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod)); deleteMethod.setPath(pageUrl); assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod)); deleteMethod.setPath(spaceUrl); assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod)); }
From source file:se.sperber.cryson.apitest.CrysonAPITest.java
@Test public void shouldCreateEntities() throws Exception { String serializedEntity = "{\"id\":null, \"name\":\"created\", \"childEntities_cryson_ids\":[100]}"; PutMethod entityPutMethod = new PutMethod("http://localhost:8789/cryson/CrysonTestEntity"); entityPutMethod.setRequestEntity(new StringRequestEntity(serializedEntity, "application/json", "UTF-8")); assertEquals(200, httpClient.executeMethod(entityPutMethod)); CrysonTestEntity testEntity = crysonSerializer.deserialize(entityPutMethod.getResponseBodyAsString(), CrysonTestEntity.class, null); String serializedChildEntity = "{\"id\":null,\"parent_cryson_id\":" + testEntity.getId() + "}"; PutMethod childEntityPutMethod = new PutMethod("http://localhost:8789/cryson/CrysonTestChildEntity"); childEntityPutMethod//from www .j a va 2 s . c om .setRequestEntity(new StringRequestEntity(serializedChildEntity, "application/json", "UTF-8")); assertEquals(200, httpClient.executeMethod(childEntityPutMethod)); }
From source file:simulation.util.Neo4JManageTool.java
public void addProperty(String nodeURI, String propertyName, String propertyValue) { try {/*w w w.j av a 2s. com*/ String nodePointUrl = nodeURI + "/properties/" + propertyName; System.out.println(nodePointUrl); HttpClient client = new HttpClient(); PutMethod mPut = new PutMethod(nodePointUrl); Header mtHeader = new Header(); mtHeader.setName("content-type"); mtHeader.setValue("application/json"); mtHeader.setName("accept"); mtHeader.setValue("application/json"); mPut.addRequestHeader(mtHeader); String jsonString = "\"" + propertyValue + "\""; StringRequestEntity requestEntity = new StringRequestEntity(jsonString, "application/json", "UTF-8"); mPut.setRequestEntity(requestEntity); client.executeMethod(mPut); mPut.getResponseBodyAsString(); mPut.releaseConnection(); } catch (Exception e) { System.out.println("Exception in creating node in neo4j : " + e); } }
From source file:terrastore.integration.IntegrationTest.java
@Test public void testCorrectlyCreateBucketOnPutThenRemoveAndCreateBucketAgain() throws Exception { // Do not change the bucket name: it is required to ensure operations will be executed on the expected node! String bucket = "bucket1"; ////w w w .j ava 2 s . co m TestValue value = new TestValue("value", 1); // This will be executed on node 1: PutMethod putValue = makePutMethod(NODE1_PORT, bucket + "/value"); putValue.setRequestEntity(new StringRequestEntity(fromObjectToJson(value), "application/json", null)); HTTP_CLIENT.executeMethod(putValue); assertEquals(HttpStatus.SC_NO_CONTENT, putValue.getStatusCode()); putValue.releaseConnection(); // This will be executed on node 2: DeleteMethod deleteBucket = makeDeleteMethod(NODE2_PORT, bucket); HTTP_CLIENT.executeMethod(deleteBucket); assertEquals(HttpStatus.SC_NO_CONTENT, putValue.getStatusCode()); deleteBucket.releaseConnection(); // This will be executed on node 1: GetMethod getBuckets = makeGetMethod(NODE1_PORT, ""); HTTP_CLIENT.executeMethod(getBuckets); assertEquals(HttpStatus.SC_OK, getBuckets.getStatusCode()); assertTrue(getBuckets.getResponseBodyAsString().indexOf(bucket) == -1); getBuckets.releaseConnection(); // This will be executed on node 1: putValue = makePutMethod(NODE1_PORT, bucket + "/value"); putValue.setRequestEntity(new StringRequestEntity(fromObjectToJson(value), "application/json", null)); HTTP_CLIENT.executeMethod(putValue); assertEquals(HttpStatus.SC_NO_CONTENT, putValue.getStatusCode()); putValue.releaseConnection(); // This will be executed on node 1: getBuckets = makeGetMethod(NODE1_PORT, ""); HTTP_CLIENT.executeMethod(getBuckets); assertEquals(HttpStatus.SC_OK, getBuckets.getStatusCode()); assertTrue(getBuckets.getResponseBodyAsString().indexOf(bucket) > -1); getBuckets.releaseConnection(); }
From source file:terrastore.integration.IntegrationTest.java
@Test public void testPutValueAndGetOnOtherNode() throws Exception { String bucket = UUID.randomUUID().toString(); TestValue value = new TestValue("value", 1); PutMethod putValue = makePutMethod(NODE1_PORT, bucket + "/value"); putValue.setRequestEntity(new StringRequestEntity(fromObjectToJson(value), "application/json", null)); HTTP_CLIENT.executeMethod(putValue); assertEquals(HttpStatus.SC_NO_CONTENT, putValue.getStatusCode()); putValue.releaseConnection();//www . jav a 2s .co m GetMethod getValue = makeGetMethod(NODE2_PORT, bucket + "/value"); HTTP_CLIENT.executeMethod(getValue); assertEquals(HttpStatus.SC_OK, getValue.getStatusCode()); TestValue returned = fromJsonToObject(getValue.getResponseBodyAsString()); assertEquals(value, returned); getValue.releaseConnection(); }
From source file:terrastore.integration.IntegrationTest.java
@Test public void testPutValueAndConditionallyGetWithSuccess() throws Exception { String bucket = UUID.randomUUID().toString(); TestValue value = new TestValue("value", 1); PutMethod putValue = makePutMethod(NODE1_PORT, bucket + "/value"); putValue.setRequestEntity(new StringRequestEntity(fromObjectToJson(value), "application/json", null)); HTTP_CLIENT.executeMethod(putValue); assertEquals(HttpStatus.SC_NO_CONTENT, putValue.getStatusCode()); putValue.releaseConnection();/*from ww w . j a v a2 s . co m*/ GetMethod getValue = makeGetMethodWithPredicate(NODE2_PORT, bucket + "/value", "jxpath:/stringField[.='value']"); HTTP_CLIENT.executeMethod(getValue); assertEquals(HttpStatus.SC_OK, getValue.getStatusCode()); TestValue returned = fromJsonToObject(getValue.getResponseBodyAsString()); assertEquals(value, returned); getValue.releaseConnection(); }
From source file:terrastore.integration.IntegrationTest.java
@Test public void testPutValueAndConditionallyGetNotFound() throws Exception { String bucket = UUID.randomUUID().toString(); TestValue value = new TestValue("value", 1); PutMethod putValue = makePutMethod(NODE1_PORT, bucket + "/value"); putValue.setRequestEntity(new StringRequestEntity(fromObjectToJson(value), "application/json", null)); HTTP_CLIENT.executeMethod(putValue); assertEquals(HttpStatus.SC_NO_CONTENT, putValue.getStatusCode()); putValue.releaseConnection();/*from w w w .ja v a2 s . com*/ GetMethod getValue = makeGetMethodWithPredicate(NODE2_PORT, bucket + "/value", "jxpath:/stringField[.='wrong']"); HTTP_CLIENT.executeMethod(getValue); assertEquals(HttpStatus.SC_NOT_FOUND, getValue.getStatusCode()); getValue.releaseConnection(); }