Package org.apache.commons.httpclient.methods

Examples of org.apache.commons.httpclient.methods.PutMethod


     */
    public boolean putMethod(String path, URL url)
        throws HttpException, IOException {

        setClient();
        PutMethod method = new PutMethod(URIUtil.encodePathQuery(path));
        generateIfHeader(method);
        if (getGetContentType() != null && !getGetContentType().equals(""))
            method.setRequestHeader("Content-Type", getGetContentType());
        method.setRequestBody(url.openStream());
        int statusCode = client.executeMethod(method);

        setStatusCode(statusCode);
        return (statusCode >= 200 && statusCode < 300) ? true : false;
    }
View Full Code Here


     */
    public boolean putMethod(String path, byte[] data)
        throws HttpException, IOException {

        setClient();
        PutMethod method = new PutMethod(URIUtil.encodePathQuery(path));
        generateIfHeader(method);
        if (getGetContentType() != null && !getGetContentType().equals(""))
            method.setRequestHeader("Content-Type", getGetContentType());

        method.setRequestHeader("Content-Length", String.valueOf(data.length));
        method.setRequestBody(new ByteArrayInputStream(data));

        int statusCode = client.executeMethod(method);

        setStatusCode(statusCode);
        return (statusCode >= 200 && statusCode < 300) ? true : false;
View Full Code Here

     */
    public boolean putMethod(String path, InputStream is)
        throws HttpException, IOException {

        setClient();
        PutMethod method = new PutMethod(URIUtil.encodePathQuery(path));
        generateIfHeader(method);
        if (getGetContentType() != null && !getGetContentType().equals(""))
            method.setRequestHeader("Content-Type", getGetContentType());
        method.setRequestContentLength(PutMethod.CONTENT_LENGTH_CHUNKED);
        method.setRequestBody(is);
        int statusCode = client.executeMethod(method);

        setStatusCode(statusCode);
        return (statusCode >= 200 && statusCode < 300) ? true : false;
    }
View Full Code Here

     */
    public boolean putMethod(String path, String data)
        throws HttpException, IOException {

        setClient();
        PutMethod method = new PutMethod(URIUtil.encodePathQuery(path));
        generateIfHeader(method);
        if (getGetContentType() != null && !getGetContentType().equals(""))
            method.setRequestHeader("Content-Type", getGetContentType());
        method.setRequestBody(data);
        int statusCode = client.executeMethod(method);

        setStatusCode(statusCode);
        return (statusCode >= 200 && statusCode < 300) ? true : false;
    }
View Full Code Here

     */
    public boolean putMethod(String path, File file)
        throws HttpException, IOException {

        setClient();
        PutMethod method = new PutMethod(URIUtil.encodePathQuery(path));
        generateIfHeader(method);
        if (getGetContentType() != null && !getGetContentType().equals(""))
            method.setRequestHeader("Content-Type", getGetContentType());
        long fileLength = file.length();
        method.setRequestContentLength(fileLength <= Integer.MAX_VALUE
                                       ? (int) fileLength
                                       : PutMethod.CONTENT_LENGTH_CHUNKED);
        method.setRequestBody(new FileInputStream(file));
        int statusCode = client.executeMethod(method);

        setStatusCode(statusCode);
        return (statusCode >= 200 && statusCode < 300) ? true : false;
    }
View Full Code Here

    @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 {
            int result = httpclient.executeMethod(put);
            assertEquals(200, result);
        } finally {
            // Release current connection to the connection pool once you are
            // done
            put.releaseConnection();
        }

        // Verify result
        endpointAddress = "http://localhost:" + PORT + "/bookstore/books/123";
        URL url = new URL(endpointAddress);
        URLConnection connect = url.openConnection();
        connect.addRequestProperty("Accept", "application/xml");
        InputStream in = connect.getInputStream();
        assertNotNull(in);

        InputStream expected = getClass().getResourceAsStream("resources/expected_update_book.txt");

        assertEquals(getStringFromInputStream(expected), getStringFromInputStream(in));

        // Roll back changes:
        File input1 = new File(getClass().getResource("resources/expected_get_book123.txt").toURI());
        PutMethod put1 = new PutMethod(endpointAddress);
        RequestEntity entity1 = new FileRequestEntity(input1, "text/xml; charset=ISO-8859-1");
        put1.setRequestEntity(entity1);
        HttpClient httpclient1 = new HttpClient();

        try {
            int result = httpclient1.executeMethod(put);
            assertEquals(200, result);
        } finally {
            // Release current connection to the connection pool once you are
            // done
            put1.releaseConnection();
        }
    } 
View Full Code Here

    @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 {
            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();
        }
    }
View Full Code Here

    @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 {
            int result = httpclient.executeMethod(put);
            assertEquals(200, result);
        } finally {
            // Release current connection to the connection pool once you are
            // done
            put.releaseConnection();
        }

        // Verify result
        endpointAddress = "http://localhost:" + PORT + "/bookstore/books/123";
        URL url = new URL(endpointAddress);
        URLConnection connection = url.openConnection();
        connection.addRequestProperty("Accept", "application/xml");
        InputStream in = connection.getInputStream();
        assertNotNull(in);

        InputStream expected = getClass().getResourceAsStream("resources/expected_update_book.txt");

        assertEquals(getStringFromInputStream(expected), getStringFromInputStream(in));

        // Roll back changes:
        File input1 = new File(getClass().getResource("resources/expected_get_book123.txt").toURI());
        PutMethod put1 = new PutMethod(endpointAddress);
        RequestEntity entity1 = new FileRequestEntity(input1, "text/xml; charset=ISO-8859-1");
        put1.setRequestEntity(entity1);
        HttpClient httpclient1 = new HttpClient();

        try {
            int result = httpclient1.executeMethod(put);
            assertEquals(200, result);
        } finally {
            // Release current connection to the connection pool once you are
            // done
            put1.releaseConnection();
        }
    }
View Full Code Here

    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 {
            int result = httpclient.executeMethod(post);
            assertEquals(304, result);
        } finally {
            // Release current connection to the connection pool once you are done
            post.releaseConnection();
        }              
    }
View Full Code Here

    }

    public void upload(File src, URL dest, CopyProgressListener l) throws IOException {
        HttpClient client = getClient();

        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();
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.httpclient.methods.PutMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.