Example usage for org.apache.commons.httpclient.methods PutMethod setRequestEntity

List of usage examples for org.apache.commons.httpclient.methods PutMethod setRequestEntity

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PutMethod setRequestEntity.

Prototype

public void setRequestEntity(RequestEntity paramRequestEntity) 

Source Link

Usage

From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerBookTest.java

@Test
public void testUpdateBook() throws Exception {
    String endpointAddress = "http://localhost:" + PORT + "/bookstore/books";

    File input = new File(getClass().getResource("resources/update_book.txt").toURI());
    PutMethod put = new PutMethod(endpointAddress);
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {//from  w w  w .  j  a  v a  2s. co m
        int result = httpclient.executeMethod(put);
        assertEquals(200, result);
        InputStream expected = getClass().getResourceAsStream("resources/expected_update_book.txt");
        assertEquals(stripXmlInstructionIfNeeded(getStringFromInputStream(expected)),
                stripXmlInstructionIfNeeded(getStringFromInputStream(put.getResponseBodyAsStream())));
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }
}

From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerBookTest.java

@Test
public void testUpdateBookWithDom() throws Exception {
    String endpointAddress = "http://localhost:" + PORT + "/bookstore/bookswithdom";

    File input = new File(getClass().getResource("resources/update_book.txt").toURI());
    PutMethod put = new PutMethod(endpointAddress);
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();
    try {/*from w w  w . ja  v  a 2s . co  m*/
        int result = httpclient.executeMethod(put);
        assertEquals(200, result);
        String resp = put.getResponseBodyAsString();
        InputStream expected = getClass().getResourceAsStream("resources/update_book.txt");
        String s = getStringFromInputStream(expected);
        //System.out.println(resp);
        //System.out.println(s);
        assertTrue(resp.indexOf(s) >= 0);
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }
}

From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerBookTest.java

@Test
public void testUpdateBookWithJSON() throws Exception {
    String endpointAddress = "http://localhost:" + PORT + "/bookstore/bookswithjson";

    File input = new File(getClass().getResource("resources/update_book_json.txt").toURI());
    PutMethod put = new PutMethod(endpointAddress);
    RequestEntity entity = new FileRequestEntity(input, "application/json; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {/*from w  ww .  j  a v  a2 s  .c  om*/
        int result = httpclient.executeMethod(put);
        assertEquals(200, result);
        InputStream expected = getClass().getResourceAsStream("resources/expected_update_book.txt");
        assertEquals(stripXmlInstructionIfNeeded(getStringFromInputStream(expected)),
                stripXmlInstructionIfNeeded(getStringFromInputStream(put.getResponseBodyAsStream())));
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }
}

From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerBookTest.java

@Test
public void testUpdateBookFailed() throws Exception {
    String endpointAddress = "http://localhost:" + PORT + "/bookstore/books";

    File input = new File(getClass().getResource("resources/update_book_not_exist.txt").toURI());
    PutMethod post = new PutMethod(endpointAddress);
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {//from  www  .j ava2  s . c om
        int result = httpclient.executeMethod(post);
        assertEquals(304, result);
    } finally {
        // Release current connection to the connection pool once you are done
        post.releaseConnection();
    }
}

From source file:org.apache.excalibur.source.factories.HTTPClientSource.java

/**
 * Factory method to create a {@link PutMethod} object.
 *
 * @param uri URI to upload <code>uploadFile</code> to
 * @param uploadFile {@link File} to be uploaded
 * @return a {@link PutMethod} instance/*from   w  w w  .  j a v  a2  s  .  co m*/
 * @exception IOException if an error occurs
 */
private PutMethod createPutMethod(final String uri, final File uploadFile) throws IOException {
    final PutMethod put = new PutMethod(uri);
    put.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(uploadFile.getAbsolutePath())));
    return put;
}

From source file:org.apache.hadoop.fs.swift.http.SwiftRestClient.java

/**
 * Uploads file as Input Stream to Swift
 *
 * @param path           path to Swift/*from   w w  w  .  j  ava 2s.co m*/
 * @param data           object data
 * @param length         length of data
 * @param requestHeaders http headers
 * @throws IOException on IO Faults
 */
public void upload(SwiftObjectPath path, final InputStream data, final long length,
        final Header... requestHeaders) throws IOException {
    preRemoteCommand("upload");
    perform(pathToURI(path), new PutMethodProcessor<byte[]>() {
        @Override
        public byte[] extractResult(PutMethod method) throws IOException {
            return method.getResponseBody();
        }

        @Override
        protected void setup(PutMethod method) throws SwiftInternalStateException {
            method.setRequestEntity(new InputStreamRequestEntity(data, length));
            setHeaders(method, requestHeaders);
        }
    });
}

From source file:org.apache.ivy.util.url.HttpClientHandler.java

public void upload(File src, URL dest, CopyProgressListener l) throws IOException {
    HttpClient client = getClient();// w w w.  j a v  a 2  s . c  om

    PutMethod put = new PutMethod(normalizeToString(dest));
    put.setDoAuthentication(useAuthentication(dest) || useProxyAuthentication());
    put.getParams().setBooleanParameter("http.protocol.expect-continue", true);
    try {
        put.setRequestEntity(new FileRequestEntity(src));
        int statusCode = client.executeMethod(put);
        validatePutStatusCode(dest, statusCode, null);
    } finally {
        put.releaseConnection();
    }
}

From source file:org.apache.jackrabbit.webdav.server.BindTest.java

public void testResourceId() throws HttpException, IOException, DavException, URISyntaxException {

    String testcol = this.root + "testResourceId/";
    String testuri1 = testcol + "bindtest1";
    String testuri2 = testcol + "bindtest2";
    int status;/*from ww w  .  j a  v a  2 s  .c  o  m*/
    try {
        MkColMethod mkcol = new MkColMethod(testcol);
        status = this.client.executeMethod(mkcol);
        assertEquals(201, status);

        PutMethod put = new PutMethod(testuri1);
        put.setRequestEntity(new StringRequestEntity("foo", "text/plain", "UTF-8"));
        status = this.client.executeMethod(put);
        assertEquals(201, status);

        // enabling version control always makes the resource referenceable
        VersionControlMethod versioncontrol = new VersionControlMethod(testuri1);
        status = this.client.executeMethod(versioncontrol);
        assertTrue("status: " + status, status == 200 || status == 201);

        URI resourceId = getResourceId(testuri1);

        MoveMethod move = new MoveMethod(testuri1, testuri2, true);
        status = this.client.executeMethod(move);
        move.getResponseBodyAsString();
        assertEquals(201, status);

        URI resourceId2 = getResourceId(testuri2);
        assertEquals(resourceId, resourceId2);
    } finally {
        DeleteMethod delete = new DeleteMethod(testcol);
        status = this.client.executeMethod(delete);
        assertTrue("status: " + status, status == 200 || status == 204);
    }
}

From source file:org.apache.jackrabbit.webdav.server.BindTest.java

public void testSimpleBind() throws Exception {
    String testcol = this.root + "testSimpleBind/";
    String subcol1 = testcol + "bindtest1/";
    String testres1 = subcol1 + "res1";
    String subcol2 = testcol + "bindtest2/";
    String testres2 = subcol2 + "res2";
    int status;//from w  ww. j a va2s .  com
    try {
        MkColMethod mkcol = new MkColMethod(testcol);
        status = this.client.executeMethod(mkcol);
        assertEquals(201, status);
        mkcol = new MkColMethod(subcol1);
        status = this.client.executeMethod(mkcol);
        assertEquals(201, status);
        mkcol = new MkColMethod(subcol2);
        status = this.client.executeMethod(mkcol);
        assertEquals(201, status);

        //create new resource R with path bindtest1/res1
        PutMethod put = new PutMethod(testres1);
        put.setRequestEntity(new StringRequestEntity("foo", "text/plain", "UTF-8"));
        status = this.client.executeMethod(put);
        assertEquals(201, status);

        //create new binding of R with path bindtest2/res2
        DavMethodBase bind = new BindMethod(subcol2, new BindInfo(testres1, "res2"));
        status = this.client.executeMethod(bind);
        assertEquals(201, status);
        //check if both bindings report the same DAV:resource-id
        assertEquals(this.getResourceId(testres1), this.getResourceId(testres2));

        //compare representations retrieved with both paths
        GetMethod get = new GetMethod(testres1);
        status = this.client.executeMethod(get);
        assertEquals(200, status);
        assertEquals("foo", get.getResponseBodyAsString());
        get = new GetMethod(testres2);
        status = this.client.executeMethod(get);
        assertEquals(200, status);
        assertEquals("foo", get.getResponseBodyAsString());

        //modify R using the new path
        put = new PutMethod(testres2);
        put.setRequestEntity(new StringRequestEntity("bar", "text/plain", "UTF-8"));
        status = this.client.executeMethod(put);
        assertTrue("status: " + status, status == 200 || status == 204);

        //compare representations retrieved with both paths
        get = new GetMethod(testres1);
        status = this.client.executeMethod(get);
        assertEquals(200, status);
        assertEquals("bar", get.getResponseBodyAsString());
        get = new GetMethod(testres2);
        status = this.client.executeMethod(get);
        assertEquals(200, status);
        assertEquals("bar", get.getResponseBodyAsString());
    } finally {
        DeleteMethod delete = new DeleteMethod(testcol);
        status = this.client.executeMethod(delete);
        assertTrue("status: " + status, status == 200 || status == 204);
    }
}

From source file:org.apache.jackrabbit.webdav.server.BindTest.java

public void testRebind() throws Exception {
    String testcol = this.root + "testRebind/";
    String subcol1 = testcol + "bindtest1/";
    String testres1 = subcol1 + "res1";
    String subcol2 = testcol + "bindtest2/";
    String testres2 = subcol2 + "res2";
    int status;/*w  w  w . j a  v a 2 s . co  m*/
    try {
        MkColMethod mkcol = new MkColMethod(testcol);
        status = this.client.executeMethod(mkcol);
        assertEquals(201, status);
        mkcol = new MkColMethod(subcol1);
        status = this.client.executeMethod(mkcol);
        assertEquals(201, status);
        mkcol = new MkColMethod(subcol2);
        status = this.client.executeMethod(mkcol);
        assertEquals(201, status);

        //create new resource R with path bindtest1/res1
        PutMethod put = new PutMethod(testres1);
        put.setRequestEntity(new StringRequestEntity("foo", "text/plain", "UTF-8"));
        status = this.client.executeMethod(put);
        assertEquals(201, status);

        // enabling version control always makes the resource referenceable
        VersionControlMethod versioncontrol = new VersionControlMethod(testres1);
        status = this.client.executeMethod(versioncontrol);
        assertTrue("status: " + status, status == 200 || status == 201);

        URI r1 = this.getResourceId(testres1);

        GetMethod get = new GetMethod(testres1);
        status = this.client.executeMethod(get);
        assertEquals(200, status);
        assertEquals("foo", get.getResponseBodyAsString());

        //rebind R with path bindtest2/res2
        DavMethodBase rebind = new RebindMethod(subcol2, new RebindInfo(testres1, "res2"));
        status = this.client.executeMethod(rebind);
        assertEquals(201, status);

        URI r2 = this.getResourceId(testres2);

        get = new GetMethod(testres2);
        status = this.client.executeMethod(get);
        assertEquals(200, status);
        assertEquals("foo", get.getResponseBodyAsString());

        //make sure that rebind did not change the resource-id
        assertEquals(r1, r2);

        //verify that the initial binding is gone
        HeadMethod head = new HeadMethod(testres1);
        status = this.client.executeMethod(head);
        assertEquals(404, status);
    } finally {
        DeleteMethod delete = new DeleteMethod(testcol);
        status = this.client.executeMethod(delete);
        assertTrue("status: " + status, status == 200 || status == 204);
    }
}