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.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);//from  w  ww.  j a  v  a2s.co m
    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();
    }
}

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/* w  w  w.  jav a 2s  . c o  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.hcatalog.templeton.TestWebHCatE2e.java

/**
 * Does a basic HTTP GET and returns Http Status code + response body
 * Will add the dummy user query string//from ww w .jav a  2  s .  c o  m
 */
private static MethodCallRetVal doHttpCall(String uri, HTTP_METHOD_TYPE type, Map<String, Object> data,
        NameValuePair[] params) throws IOException {
    HttpClient client = new HttpClient();
    HttpMethod method;
    switch (type) {
    case GET:
        method = new GetMethod(uri);
        break;
    case DELETE:
        method = new DeleteMethod(uri);
        break;
    case PUT:
        method = new PutMethod(uri);
        if (data == null) {
            break;
        }
        String msgBody = JsonBuilder.mapToJson(data);
        LOG.info("Msg Body: " + msgBody);
        StringRequestEntity sre = new StringRequestEntity(msgBody, "application/json", charSet);
        ((PutMethod) method).setRequestEntity(sre);
        break;
    default:
        throw new IllegalArgumentException("Unsupported method type: " + type);
    }
    if (params == null) {
        method.setQueryString(new NameValuePair[] { new NameValuePair("user.name", username) });
    } else {
        NameValuePair[] newParams = new NameValuePair[params.length + 1];
        System.arraycopy(params, 0, newParams, 1, params.length);
        newParams[0] = new NameValuePair("user.name", username);
        method.setQueryString(newParams);
    }
    String actualUri = "no URI";
    try {
        actualUri = method.getURI().toString();//should this be escaped string?
        LOG.debug(type + ": " + method.getURI().getEscapedURI());
        int httpStatus = client.executeMethod(method);
        LOG.debug("Http Status Code=" + httpStatus);
        String resp = method.getResponseBodyAsString();
        LOG.debug("response: " + resp);
        return new MethodCallRetVal(httpStatus, resp, actualUri, method.getName());
    } catch (IOException ex) {
        LOG.error("doHttpCall() failed", ex);
    } finally {
        method.releaseConnection();
    }
    return new MethodCallRetVal(-1, "Http " + type + " failed; see log file for details", actualUri,
            method.getName());
}

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

public void upload(File src, URL dest, CopyProgressListener l) throws IOException {
    HttpClient client = getClient();//from   w w  w.  j ava2s  . com

    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;/*www  .  j a va  2 s .co  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;//w  w  w  .  j ava  2s  .c  o 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);

        //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;/*from 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);
    }
}

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

public void testBindOverwrite() 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;/*  w w  w.j a v  a  2 s .  c  om*/
    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 resource R' with path bindtest2/res2
        put = new PutMethod(testres2);
        put.setRequestEntity(new StringRequestEntity("bar", "text/plain", "UTF-8"));
        status = this.client.executeMethod(put);
        assertEquals(201, status);

        //try to create new binding of R with path bindtest2/res2 and Overwrite:F
        DavMethodBase bind = new BindMethod(subcol2, new BindInfo(testres1, "res2"));
        bind.addRequestHeader(new Header("Overwrite", "F"));
        status = this.client.executeMethod(bind);
        assertEquals(412, status);

        //verify that bindtest2/res2 still points to R'
        GetMethod get = new GetMethod(testres2);
        status = this.client.executeMethod(get);
        assertEquals(200, status);
        assertEquals("bar", get.getResponseBodyAsString());

        //create new binding of R with path bindtest2/res2
        bind = new BindMethod(subcol2, new BindInfo(testres1, "res2"));
        status = this.client.executeMethod(bind);
        assertTrue("status: " + status, status == 200 || status == 204);

        //verify that bindtest2/res2 now points to R
        get = new GetMethod(testres2);
        status = this.client.executeMethod(get);
        assertEquals(200, status);
        assertEquals("foo", get.getResponseBodyAsString());

        //verify that the initial binding is still there
        HeadMethod head = new HeadMethod(testres1);
        status = this.client.executeMethod(head);
        assertEquals(200, status);
    } 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 testRebindOverwrite() 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  ww  w .  jav a2s  . c  o 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 testSimpleBind/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);

        //create new resource R' with path testSimpleBind/bindtest2/res2
        put = new PutMethod(testres2);
        put.setRequestEntity(new StringRequestEntity("bar", "text/plain", "UTF-8"));
        status = this.client.executeMethod(put);
        assertEquals(201, status);

        //try rebind R with path testSimpleBind/bindtest2/res2 and Overwrite:F
        RebindMethod rebind = new RebindMethod(subcol2, new RebindInfo(testres1, "res2"));
        rebind.addRequestHeader(new Header("Overwrite", "F"));
        status = this.client.executeMethod(rebind);
        assertEquals(412, status);

        //verify that testSimpleBind/bindtest2/res2 still points to R'
        GetMethod get = new GetMethod(testres2);
        status = this.client.executeMethod(get);
        assertEquals(200, status);
        assertEquals("bar", get.getResponseBodyAsString());

        //rebind R with path testSimpleBind/bindtest2/res2
        rebind = new RebindMethod(subcol2, new RebindInfo(testres1, "res2"));
        status = this.client.executeMethod(rebind);
        assertTrue("status: " + status, status == 200 || status == 204);

        //verify that testSimpleBind/bindtest2/res2 now points to R
        get = new GetMethod(testres2);
        status = this.client.executeMethod(get);
        assertEquals(200, status);
        assertEquals("foo", get.getResponseBodyAsString());

        //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);
    }
}

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

public void testParentSet() throws Exception {
    String testcol = this.root + "testParentSet/";
    String subcol1 = testcol + "bindtest1/";
    String testres1 = subcol1 + "res1";
    String subcol2 = testcol + "bindtest2/";
    String testres2 = subcol2 + "res2";
    int status;//  ww  w.jav  a  2s  . 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 testSimpleBind/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 testSimpleBind/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));

        //verify values of parent-set properties
        List hrefs1 = new ArrayList();
        List segments1 = new ArrayList();
        List hrefs2 = new ArrayList();
        List segments2 = new ArrayList();
        Object ps1 = this.getParentSet(testres1).getValue();
        Object ps2 = this.getParentSet(testres2).getValue();
        assertTrue(ps1 instanceof List);
        assertTrue(ps2 instanceof List);
        List plist1 = (List) ps1;
        List plist2 = (List) ps2;
        assertEquals(2, plist1.size());
        assertEquals(2, plist2.size());
        for (int k = 0; k < 2; k++) {
            Object pObj1 = plist1.get(k);
            Object pObj2 = plist2.get(k);
            assertTrue(pObj1 instanceof Element);
            assertTrue(pObj2 instanceof Element);
            ParentElement p1 = ParentElement.createFromXml((Element) pObj1);
            ParentElement p2 = ParentElement.createFromXml((Element) pObj2);
            hrefs1.add(p1.getHref());
            hrefs2.add(p2.getHref());
            segments1.add(p1.getSegment());
            segments2.add(p2.getSegment());
        }
        Collections.sort(hrefs1);
        Collections.sort(hrefs2);
        Collections.sort(segments1);
        Collections.sort(segments2);
        assertEquals(hrefs1, hrefs2);
        assertEquals(segments1, segments2);
    } finally {
        DeleteMethod delete = new DeleteMethod(testcol);
        status = this.client.executeMethod(delete);
        assertTrue("status: " + status, status == 200 || status == 204);
    }
}