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

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

Introduction

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

Prototype

public PutMethod(String paramString) 

Source Link

Usage

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Execute the PUT method for the given path.
 *
 * @param path the server relative path to put the data
 * @param is The input stream.//from   w  w  w .j  a v a2s  . c om
 * @return true if the method is succeeded.
 * @exception HttpException
 * @exception IOException
 */
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);
    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    int statusCode = client.executeMethod(method);

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

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Execute the PUT method for the given path.
 *
 * @param path the server relative path to put the data
 * @param data String to send.//from w w  w. j  a va  2 s.  com
 * @return true if the method is succeeded.
 * @exception HttpException
 * @exception IOException
 */
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);
    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    int statusCode = client.executeMethod(method);

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

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Execute the PUT method for the given path.
 *
 * @param path the server relative path to put the given file
 * @param file the filename to get on local.
 * @return true if the method is succeeded.
 * @exception HttpException//w w w  .  j a v a2  s  .c o  m
 * @exception IOException
 */
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));
    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    int statusCode = client.executeMethod(method);

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

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Execute the PUT method for the given path from the given url.
 *
 * @param path the server relative path to put the data
 * @param url The URL to get a resource.
 * @return true if the method is succeeded.
 * @exception HttpException//w ww.  j a  v  a2  s.c om
 * @exception IOException
 */
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());
    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    int statusCode = client.executeMethod(method);

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

From source file:org.apache.wink.itest.addressbook.StringTest.java

/**
 * This will drive a POST, GET, UPDATE, and DELETE on the
 * AddressBookResource//  w w w. ja v  a2s. c o  m
 */
public void testAddressBookResource() {
    PostMethod method = null;
    GetMethod getMethod = null;
    PutMethod put = null;
    DeleteMethod deleteMethod = null;
    try {

        // make sure everything is clear before testing
        HttpClient client = new HttpClient();
        method = new PostMethod(getBaseURI() + "/fromBody");
        String input = "tempAddress&1234 Any Street&AnyTown&90210&TX&US";
        RequestEntity entity = new ByteArrayRequestEntity(input.getBytes(), "text/xml");
        method.setRequestEntity(entity);
        client.executeMethod(method);

        // now let's see if the address we just created is available
        getMethod = new GetMethod(getBaseURI() + "/tempAddress");
        client.executeMethod(getMethod);
        String responseBody = getMethod.getResponseBodyAsString();
        getMethod.releaseConnection();
        assertNotNull(responseBody);
        assertTrue(responseBody, responseBody.contains("tempAddress"));
        assertTrue(responseBody, responseBody.contains("1234 Any Street"));
        assertTrue(responseBody, responseBody.contains("AnyTown"));
        assertTrue(responseBody, responseBody.contains("90210"));
        assertTrue(responseBody, responseBody.contains("TX"));
        assertTrue(responseBody, responseBody.contains("US"));

        // let's update the state
        String query = "entryName=tempAddress&streetAddress=1234+Any+Street&city="
                + "AnyTown&zipCode=90210&state=AL&country=US";
        client = new HttpClient();
        put = new PutMethod(getBaseURI());
        put.setQueryString(query);
        client.executeMethod(put);

        // make sure the state has been updated
        client = new HttpClient();
        client.executeMethod(getMethod);
        responseBody = getMethod.getResponseBodyAsString();
        assertNotNull(responseBody);
        assertTrue(responseBody.contains("tempAddress"));
        assertFalse(responseBody.contains("TX"));
        assertTrue(responseBody.contains("AL"));

        // now let's delete the address
        client = new HttpClient();
        deleteMethod = new DeleteMethod(getBaseURI() + "/tempAddress");
        client.executeMethod(deleteMethod);
        assertEquals(204, deleteMethod.getStatusCode());

        // now try to get the address
        client = new HttpClient();
        client.executeMethod(getMethod);
        assertEquals(404, getMethod.getStatusCode());
        responseBody = getMethod.getResponseBodyAsString();
        ServerContainerAssertions.assertExceptionBodyFromServer(404, getMethod.getResponseBodyAsString());

    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
        if (getMethod != null) {
            getMethod.releaseConnection();
        }
        if (put != null) {
            put.releaseConnection();
        }
        if (deleteMethod != null) {
            deleteMethod.releaseConnection();
        }
    }
}

From source file:org.apache.wink.itest.cachetest.NewsHttpClient.java

public Response updateNewsStory(NewsStory story) throws Exception {
    PutMethod put = new PutMethod(this.baseURI);
    try {//from w ww  .  j a  v a 2s  .c o  m
        HttpClient client = new HttpClient();
        setRequestHeaders(put);
        JAXBContext context = JAXBContext.newInstance(NewsStory.class);
        StringWriter sw = new StringWriter();
        context.createMarshaller().marshal(story, sw);
        RequestEntity entity = new ByteArrayRequestEntity(sw.toString().getBytes(), "text/xml");
        put.setRequestEntity(entity);
        int status = client.executeMethod(put);
        Map<String, List<Object>> headers = getResponseHeaders(put.getResponseHeaders());
        Response resp = Response.status(status).build();
        resp.getMetadata().putAll(headers);
        return resp;
    } catch (Exception e) {
        throw e;
    } finally {
        if (put != null) {
            put.releaseConnection();
        }
    }
}

From source file:org.apache.wink.itest.exceptionmappers.JAXRSExceptionsMappedProvidersTest.java

/**
 * Tests a method that throws a checked exception.
 * //from   ww  w . ja  v a 2s.  c om
 * @throws Exception
 */
public void testCheckExceptionMappedProvider() throws Exception {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/-99999");
    putMethod.setRequestEntity(new StringRequestEntity(
            "<comment><id></id><message></message><author></author></comment>", "text/xml", null));
    client.executeMethod(putMethod);
    assertEquals(454, putMethod.getStatusCode());

    CommentError c = (CommentError) JAXBContext.newInstance(CommentError.class.getPackage().getName())
            .createUnmarshaller().unmarshal(putMethod.getResponseBodyAsStream());
    assertEquals("Unexpected ID.", c.getErrorMessage());
}

From source file:org.apache.wink.itest.exceptionmappers.JAXRSExceptionsNoMapperTest.java

/**
 * Tests a method that throws a checked exception.
 * //from   w  w w . j  ava  2  s.co m
 * @throws Exception
 */
public void testCheckExceptionNoMappingProvider() throws Exception {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/-99999");
    putMethod.setRequestEntity(new StringRequestEntity(
            "<comment><id></id><message></message><author></author></comment>", "text/xml", null));
    client.executeMethod(putMethod);
    assertEquals(500, putMethod.getStatusCode());
    // assertLogContainsException("jaxrs.tests.exceptions.nomapping.server.GuestbookException: Unexpected ID.");
}

From source file:org.apache.wink.itest.exceptionmappers.JAXRSExceptionsNullConditionsTest.java

/**
 * Tests that a <code>WebApplicationException</code> constructed with a
 * cause and response will return the Response entity by default.
 * /*from  ww w  . ja  va2 s  .  c  o m*/
 * @throws Exception
 */
public void testWebExceptionWithCauseAndResponse() throws Exception {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/webappexceptionwithcauseandresponse");
    try {
        client.executeMethod(putMethod);
        assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), putMethod.getStatusCode());
        assertEquals("Entity inside response", putMethod.getResponseBodyAsString());
    } finally {
        putMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.exceptionmappers.JAXRSExceptionsNullConditionsTest.java

/**
 * Tests that a <code>ExceptionMapper</code> can catch a generic Throwable.
 * //ww w . j  a  va  2s .  co m
 * @throws Exception
 */
public void testExceptionMapperForSpecificThrowable() throws Exception {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/throwableexceptionmapper");
    try {
        client.executeMethod(putMethod);
        assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), putMethod.getStatusCode());
        assertEquals("Throwable mapper used", putMethod.getResponseBodyAsString());
    } finally {
        putMethod.releaseConnection();
    }
}