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.wink.itest.standard.JAXRSStreamingOutputTest.java

/**
 * Tests receiving a StreamingOutput with a non-standard content-type.
 * //from   ww w  .j  ava 2  s.co m
 * @throws HttpException
 * @throws IOException
 */
public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/streamingoutput");
    byte[] barr = new byte[100000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "any/type"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/streamingoutput");
    getMethod.addRequestHeader("Accept", "mytype/subtype");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[barr.length];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue());
        Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
        assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader);
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.subresources.JAXRSExceptionsSubresourcesTest.java

/**
 * Test that a checked exception is processed properly.
 * //from   ww w  . ja  v  a  2s  .c om
 * @throws Exception
 */
public void testCheckedException() throws Exception {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/commentdata");
    try {
        putMethod.setRequestEntity(new StringRequestEntity("<comment></comment>", "text/xml", null));
        client.executeMethod(putMethod);
        assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), putMethod.getStatusCode());
        // assertLogContainsException("jaxrs.tests.exceptions.subresources.server.GuestbookException: Unexpected ID.");
    } finally {
        putMethod.releaseConnection();
    }
}

From source file:org.apache.wookie.tests.functional.PropertiesControllerTest.java

@Test
public void updateProperty() {
    try {//from w  ww  .jav  a2  s  .  c  o  m
        HttpClient client = new HttpClient();
        PutMethod put = new PutMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
        put.setQueryString("api_key=" + API_KEY_VALID + "&widgetid=" + WIDGET_ID_VALID
                + "&userid=test&is_public=true&shareddatakey=propstest&propertyname=cat&propertyvalue=felix");
        client.executeMethod(put);
        int code = put.getStatusCode();
        assertEquals(200, code);
        put.releaseConnection();
    } catch (Exception e) {
        fail("set shared data failed");
    }

    try {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
        get.setQueryString("api_key=" + API_KEY_VALID + "&widgetid=" + WIDGET_ID_VALID
                + "&userid=test&shareddatakey=propstest&propertyname=cat");
        client.executeMethod(get);
        int code = get.getStatusCode();
        assertEquals(200, code);
        String resp = get.getResponseBodyAsString();
        assertEquals("felix", resp);
        get.releaseConnection();
    } catch (Exception e) {
        fail("get property failed");
    }

}

From source file:org.apache.wookie.tests.functional.WidgetAccessRequestPolicyControllerTest.java

@Test
public void grantPolicy() {
    try {//from   www  . ja v a 2  s .  c om
        HttpClient client = new HttpClient();
        setAuthenticationCredentials(client);
        PutMethod put = new PutMethod(TEST_WARP_SERVICE_URL_VALID + "/" + id + "?granted=true");
        put.setDoAuthentication(true);
        client.executeMethod(put);
        int code = put.getStatusCode();
        assertEquals(200, code);
        put.releaseConnection();
    } catch (Exception e) {
        e.printStackTrace();
        fail("put failed");
    }
    // Now lets GET it to make sure it was modded OK
    Element[] policies = getPolicies();
    for (Element policy : policies) {
        if (policy.getAttribute("origin").getValue().equals("http://www.9128.org")) {
            try {
                assertTrue(policy.getAttribute("granted").getBooleanValue());
            } catch (DataConversionException e) {
                e.printStackTrace();
                fail("bad return value for granted");
            }
        }
    }
}

From source file:org.apache.wookie.tests.functional.WidgetAccessRequestPolicyControllerTest.java

@Test
public void revokePolicy() {
    try {//from w w  w . j a  v a 2 s .c om
        HttpClient client = new HttpClient();
        setAuthenticationCredentials(client);
        PutMethod put = new PutMethod(TEST_WARP_SERVICE_URL_VALID + "/" + id + "?granted=false");
        put.setDoAuthentication(true);
        client.executeMethod(put);
        int code = put.getStatusCode();
        assertEquals(200, code);
        put.releaseConnection();
    } catch (Exception e) {
        e.printStackTrace();
        fail("put failed");
    }
    // Now lets GET it to make sure it was modded OK
    Element[] policies = getPolicies();
    for (Element policy : policies) {
        if (policy.getAttribute("origin").getValue().equals("http://www.9128.org")) {
            try {
                assertFalse(policy.getAttribute("granted").getBooleanValue());
            } catch (DataConversionException e) {
                e.printStackTrace();
                fail("bad return value for granted");
            }
        }
    }
}

From source file:org.apache.wookie.tests.functional.WidgetAccessRequestPolicyControllerTest.java

@Test
public void testGrantNonExistingPolicy() {
    try {/*from   ww w  .ja va  2  s.  c  om*/
        HttpClient client = new HttpClient();
        setAuthenticationCredentials(client);
        PutMethod put = new PutMethod(TEST_WARP_SERVICE_URL_VALID + "/9999?granted=true");
        put.setDoAuthentication(true);
        client.executeMethod(put);
        int code = put.getStatusCode();
        assertEquals(404, code);
        put.releaseConnection();
    } catch (Exception e) {
        e.printStackTrace();
        fail("put failed");
    }
}

From source file:org.apache.wookie.tests.functional.WidgetInstancesControllerTest.java

@Test
public void cloneSharedData() {
    // Create an instance
    try {/*from ww  w  .  ja  va 2 s. c  o  m*/
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
        post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid=" + WIDGET_ID_VALID
                + "&userid=test&shareddatakey=clonetestsrc");
        client.executeMethod(post);
        int code = post.getStatusCode();
        assertEquals(201, code);
        post.releaseConnection();
    } catch (Exception e) {
        fail("create instance failed");
    }

    // Set some shared data
    try {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
        post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid=" + WIDGET_ID_VALID
                + "&userid=test&is_public=true&shareddatakey=clonetestsrc&propertyname=cat&propertyvalue=garfield");
        client.executeMethod(post);
        int code = post.getStatusCode();
        assertEquals(201, code);
        post.releaseConnection();
    } catch (Exception e) {
        fail("set shared data failed");
    }

    // Clone it
    try {
        HttpClient client = new HttpClient();
        PutMethod post = new PutMethod(TEST_INSTANCES_SERVICE_URL_VALID);
        post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid=" + WIDGET_ID_VALID
                + "&userid=test&shareddatakey=clonetestsrc&requestid=clone&cloneshareddatakey=clonetestsync");
        client.executeMethod(post);
        int code = post.getStatusCode();
        assertEquals(200, code);
        post.releaseConnection();
    } catch (Exception e) {
        fail("clone shared data failed");
    }

    // Check it
    // Create an instance for the clone
    try {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
        post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid=" + WIDGET_ID_VALID
                + "&userid=test&shareddatakey=clonetestsync");
        client.executeMethod(post);
        int code = post.getStatusCode();
        assertEquals(201, code);
        post.releaseConnection();
    } catch (Exception e) {
        fail("create instance failed");
    }
    // Get the data for the clone
    try {
        HttpClient client = new HttpClient();
        GetMethod post = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
        post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid=" + WIDGET_ID_VALID
                + "&userid=test&shareddatakey=clonetestsync&propertyname=cat");
        client.executeMethod(post);
        int code = post.getStatusCode();
        assertEquals(200, code);
        String resp = post.getResponseBodyAsString();
        assertEquals("garfield", resp);
        post.releaseConnection();
    } catch (Exception e) {
        fail("create instance failed");
    }
}

From source file:org.apache.wookie.tests.functional.WidgetServicesControllerTest.java

@Test
public void modifyService() {
    try {//from  ww  w . jav  a2s  . c  om
        HttpClient client = new HttpClient();
        setAuthenticationCredentials(client);
        PutMethod put = new PutMethod(TEST_SERVICES_SERVICE_URL_VALID + "/test");
        put.setQueryString("name=test2");
        put.setDoAuthentication(true);
        client.executeMethod(put);
        int code = put.getStatusCode();
        assertEquals(200, code);
        put.releaseConnection();
    } catch (Exception e) {
        e.printStackTrace();
        fail("put failed");
    }
    // Now lets GET it to make sure it was added OK
    try {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(TEST_SERVICES_SERVICE_URL_VALID + "/test2");
        client.executeMethod(get);
        int code = get.getStatusCode();
        assertEquals(200, code);
        String response = get.getResponseBodyAsString();
        assertTrue(response.contains("<service name=\"test2\">"));
        get.releaseConnection();
    } catch (Exception e) {
        e.printStackTrace();
        fail("get failed");
    }

}

From source file:org.apache.wookie.tests.functional.WidgetServicesControllerTest.java

@Test
public void modifyServiceNoAuth() {
    try {//from w ww .j av  a 2 s .  c o m
        HttpClient client = new HttpClient();
        PutMethod put = new PutMethod(TEST_SERVICES_SERVICE_URL_VALID + "/test");
        put.setQueryString("name=test2");
        client.executeMethod(put);
        int code = put.getStatusCode();
        assertEquals(401, code);
        put.releaseConnection();
    } catch (Exception e) {
        e.printStackTrace();
        fail("put failed");
    }
}

From source file:org.apache.wookie.tests.functional.WidgetServicesControllerTest.java

@Test
public void modifyServiceNonExists() {
    try {/*from w  ww  . j a  va2s .  c  o m*/
        HttpClient client = new HttpClient();
        setAuthenticationCredentials(client);
        PutMethod put = new PutMethod(TEST_SERVICES_SERVICE_URL_VALID + "/test999");
        put.setQueryString("name=test2");
        put.setDoAuthentication(true);
        client.executeMethod(put);
        int code = put.getStatusCode();
        assertEquals(404, code);
        put.releaseConnection();
    } catch (Exception e) {
        e.printStackTrace();
        fail("put failed");
    }
}