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:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation10Xml() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation10Xml";
    PutMethod method = new PutMethod(url);
    String param = this.getXmlCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String jsonResponse = new String(responseBody);
    assertTrue(jsonResponse.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>") > -1);
    assertTrue(jsonResponse.indexOf("<title>My Track 1</title>") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation10Json() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation10Json";
    PutMethod method = new PutMethod(url);
    String param = this.getJsonCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/json", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String jsonResponse = new String(responseBody);
    assertTrue(jsonResponse.indexOf("\"title\":\"My Track 1\"") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation11Xml() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation11Xml/41?page=51";
    PutMethod method = new PutMethod(url);
    String param = this.getXmlCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String xmlResponse = new String(responseBody);
    assertTrue(xmlResponse.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>") > -1);
    assertTrue(xmlResponse.indexOf("<title>putOperation11-41-51</title>") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation11Json() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation11Json/41?page=51";
    PutMethod method = new PutMethod(url);
    String param = this.getJsonCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/json", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String jsonResponse = new String(responseBody);
    assertTrue(jsonResponse.indexOf("\"title\":\"putOperation11-41-51\"") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation12Json() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation12Json/artists/madonna/tracks/4";
    PutMethod method = new PutMethod(url);
    String param = this.getJsonCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/json", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String jsonResponse = new String(responseBody);
    assertTrue(jsonResponse.indexOf("\"title\":\"madonna-4\"") > -1);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation2() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation2";
    PutMethod method = new PutMethod(url);
    String param = "this is string parameter!";
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_NO_CONTENT);
    byte[] responseBody = method.getResponseBody();
    assertNull(responseBody);//w  ww.  j  av  a 2  s .  com
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation4() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation4";
    PutMethod method = new PutMethod(url);
    String param = "this is string parameter!";
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK);
    byte[] responseBody = method.getResponseBody();
    String actualResponse = new String(responseBody);
    String expectedResponse = "this is string parameter!";
    assertEquals(expectedResponse, actualResponse);
}

From source file:eu.eco2clouds.scheduler.accounting.client.AccountingClientHC.java

private String putMethod(String url, String payload, String bonfireUserId, String bonfireGroupId,
        Boolean exception) {// w w  w .  j a v a2 s.c o  m
    // Create an instance of HttpClient.
    HttpClient client = getHttpClient();

    logger.debug("Connecting to: " + url);
    // Create a method instance.
    PutMethod method = new PutMethod(url);
    setHeaders(method, bonfireGroupId, bonfireUserId);
    //method.addRequestHeader("Content-Type", SchedulerDictionary.CONTENT_TYPE_ECO2CLOUDS_XML);

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    String response = "";

    try {
        // We set the payload
        StringRequestEntity payloadEntity = new StringRequestEntity(payload,
                SchedulerDictionary.CONTENT_TYPE_ECO2CLOUDS_XML, "UTF-8");
        method.setRequestEntity(payloadEntity);

        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) { //TODO test for this case... 
            logger.warn("Get host information of testbeds: " + url + " failed: " + method.getStatusLine());
        } else {
            // Read the response body.
            byte[] responseBody = method.getResponseBody();
            response = new String(responseBody);
        }

    } catch (HttpException e) {
        logger.warn("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
        exception = true;
    } catch (IOException e) {
        logger.warn("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
        exception = true;
    } finally {
        // Release the connection.
        method.releaseConnection();
    }

    return response;
}

From source file:dk.clarin.tools.create.java

private Document putToPDP(String request) {
    //logger.debug("PDP request: " + request);
    // for downloading...
    org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
    org.apache.commons.httpclient.methods.PutMethod method;
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(500000);

    method = new org.apache.commons.httpclient.methods.PutMethod(ToolsProperties.coreServer + "/aa/pdp");
    try {/*w ww. j  a  v  a2 s  .com*/
        method.setRequestEntity(new org.apache.commons.httpclient.methods.StringRequestEntity(request,
                "application/xml", "UTF-8"));
        //if (userHandle != null && userHandle.trim().length() > 0)
        method.setRequestHeader("Cookie", "escidocCookie=" + ToolsProperties.adminUserHandle /*userHandle*/);
        method.setFollowRedirects(false);
        // Download the item
        httpClient.executeMethod(method);
        if (method.getStatusCode() != 200) {
            if (method.getStatusCode() == 302) {
                logger.error("The userhandle has expired!");
                return null;
            } else {
                logger.warn("Unknown error while trying to use the PDP! Request: " + request + "\nResponse: "
                        + method.getResponseBodyAsString());
                return null;
            }
        }
        //logger.debug("PDP responce: " + method.getResponseBodyAsString() );

        InputStream in = method.getResponseBodyAsStream();
        Document ret = streamToXml(in);
        return ret;
    } catch (UnsupportedEncodingException e) {
        logger.error("An error occured while using the PDP! " + e.getMessage());
        return null;
    } catch (Exception e) {
        logger.error("An error occured while using the PDP!");
        return null;
    } finally {
        method.releaseConnection();
    }
}

From source file:com.sun.faban.driver.transport.hc3.ApacheHC3Transport.java

/**
 * Makes a PUT request to the URL. Reads data back and returns the data
 * read. Note that this method only works with text data as it does the
 * byte-to-char conversion. This method will return null for responses
 * with binary MIME types. The addTextType(String) method is used to
 * register additional MIME types as text types. Use getContentSize() to
 *  obtain the bytes of binary data read.
 *
 * @param url The URL to read from/*w w w.ja  v  a  2  s  .  co  m*/
 * @param buffer containing the PUT data
 * @param contentType the content type, or null
 * @param headers The request headers, or null
 * @return The StringBuilder buffer containing the resulting document
 * @throws java.io.IOException
 */
public StringBuilder putURL(String url, byte[] buffer, String contentType, Map<String, String> headers)
        throws IOException {
    PutMethod method = new PutMethod(url);
    method.setFollowRedirects(followRedirects);
    setHeaders(method, headers);
    method.setRequestEntity(new ByteArrayRequestEntity(buffer, contentType));
    try {
        responseCode = hc.executeMethod(method);
        buildResponseHeaders(method);
        return fetchResponse(method);
    } finally {
        method.releaseConnection();
    }
}