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:com.mycompany.jaxrsexample.client.Client.java

public static void main(String args[]) throws Exception {
    // Sent HTTP GET request to query all students info

    System.out.println("Invoking server through HTTP GET to query all students info");
    URL url = new URL("http://localhost:9000/studentservice/students");
    InputStream in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP GET request to query student info
    System.out.println("Sent HTTP GET request to query customer info");
    url = new URL("http://localhost:9000/studentservice/students/1");
    in = url.openStream();//from ww  w.  ja v a 2  s  .  c o  m
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP PUT request to update student info
    System.out.println("\n");
    System.out.println("Sent HTTP PUT request to update student info");
    Client client = new Client();
    String inputFile = client.getClass().getResource("/update_student.xml").getFile();
    URIResolver resolver = new URIResolver(inputFile);
    File input = new File(resolver.getURI());
    PutMethod put = new PutMethod("http://localhost:9000/studentservice/students");
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(put);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(put.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }

    // Sent HTTP POST request to add student
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add student");
    inputFile = client.getClass().getResource("/add_student.xml").getFile();
    resolver = new URIResolver(inputFile);
    input = new File(resolver.getURI());
    PostMethod post = new PostMethod("http://localhost:9000/studentservice/students");
    post.addRequestHeader("Accept", "text/xml");
    entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }

    System.out.println("\n");
    System.exit(0);
}

From source file:com.zxm.servicemix.examples.cxf.jaxrs.client.Client.java

public static void main(String args[]) throws Exception {
    // Sent HTTP GET request to query all customer info

    // Sent HTTP GET request to query customer info
    System.out.println("Sent HTTP GET request to query customer info");
    URL url = new URL("http://localhost:8181/cxf/crm/customerservice/customers/123");
    InputStream in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP GET request to query sub resource product info
    System.out.println("\n");
    System.out.println("Sent HTTP GET request to query sub resource product info");
    url = new URL("http://localhost:8181/cxf/crm/customerservice/orders/223/products/323");
    in = url.openStream();/*from w  w  w .  j  a  va2s  .  co  m*/
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP PUT request to update customer info
    System.out.println("\n");
    System.out.println("Sent HTTP PUT request to update customer info");
    Client client = new Client();
    String inputFile = client.getClass().getResource("update_customer.xml").getFile();
    File input = new File(inputFile);
    PutMethod put = new PutMethod("http://localhost:8181/cxf/crm/customerservice/customers");
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(put);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(put.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }

    // Sent HTTP POST request to add customer
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add customer");
    inputFile = client.getClass().getResource("add_customer.xml").getFile();
    input = new File(inputFile);
    PostMethod post = new PostMethod("http://localhost:8181/cxf/crm/customerservice/customers");
    post.addRequestHeader("Accept", "text/xml");
    entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }

    System.out.println("\n");
    System.exit(0);
}

From source file:edu.techseekers.training.client.Client.java

public static void main(String args[]) throws Exception {
    // Sent HTTP GET request to query all customer info
    /*//from  w  w w.  jav  a 2s . c  o m
     * URL url = new URL("http://localhost:9000/customers");
     * System.out.println("Invoking server through HTTP GET to query all
     * customer info"); InputStream in = url.openStream(); StreamSource
     * source = new StreamSource(in); printSource(source);
     */

    // Sent HTTP GET request to query customer info
    System.out.println("Sent HTTP GET request to query customer info");
    URL url = new URL("http://localhost:9000/customerservice/customers/123");
    InputStream in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP GET request to query sub resource product info
    System.out.println("\n");
    System.out.println("Sent HTTP GET request to query sub resource product info");
    url = new URL("http://localhost:9000/customerservice/orders/223/products/323");
    in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP PUT request to update customer info
    System.out.println("\n");
    System.out.println("Sent HTTP PUT request to update customer info");
    Client client = new Client();
    String inputFile = client.getClass().getResource("/update_customer.xml").getFile();
    URIResolver resolver = new URIResolver(inputFile);
    File input = new File(resolver.getURI());
    PutMethod put = new PutMethod("http://localhost:9000/customerservice/customers");
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(put);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(put.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }

    // Sent HTTP POST request to add customer
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add customer");
    inputFile = client.getClass().getResource("/add_customer.xml").getFile();
    resolver = new URIResolver(inputFile);
    input = new File(resolver.getURI());
    PostMethod post = new PostMethod("http://localhost:9000/customerservice/customers");
    post.addRequestHeader("Accept", "text/xml");
    entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }

    System.out.println("\n");
    System.exit(0);
}

From source file:main.java.demo.jaxrs.client.Client.java

public static void main(String args[]) throws Exception {
    // Sent HTTP GET request to query all customer info
    /*/* www .  ja  v a2s  .  com*/
     * URL url = new URL("http://localhost:9000/customers");
     * System.out.println("Invoking server through HTTP GET to query all
     * customer info"); InputStream in = url.openStream(); StreamSource
     * source = new StreamSource(in); printSource(source);
     */

    // Sent HTTP GET request to query customer info
    System.out.println("Sent HTTP GET request to query customer info");
    URL url = new URL("http://localhost:8989/RestFull/service1/customerservice/customers/123");
    InputStream in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP GET request to query sub resource product info
    System.out.println("\n");
    System.out.println("Sent HTTP GET request to query sub resource product info");
    url = new URL("http://localhost:9000/customerservice/orders/223/products/323");
    in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP PUT request to update customer info
    System.out.println("\n");
    System.out.println("Sent HTTP PUT request to update customer info");
    Client client = new Client();
    String inputFile = client.getClass().getResource("update_customer.xml").getFile();
    URIResolver resolver = new URIResolver(inputFile);
    File input = new File(resolver.getURI());
    PutMethod put = new PutMethod("http://localhost:9000/customerservice/customers");
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(put);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(put.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }

    // Sent HTTP POST request to add customer
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add customer");
    inputFile = client.getClass().getResource("add_customer.xml").getFile();
    resolver = new URIResolver(inputFile);
    input = new File(resolver.getURI());
    PostMethod post = new PostMethod("http://localhost:9000/customerservice/customers");
    post.addRequestHeader("Accept", "text/xml");
    entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }

    System.out.println("\n");
    System.exit(0);
}

From source file:main.java.client.Client.java

public static void main(String args[]) throws Exception {
    // Sent HTTP GET request to query all customer info
    /*/*from  ww  w .  ja v  a  2s . com*/
     * URL url = new URL("http://localhost:9000/customers");
     * System.out.println("Invoking server through HTTP GET to query all
     * customer info"); InputStream in = url.openStream(); StreamSource
     * source = new StreamSource(in); printSource(source);
     */

    // Sent HTTP GET request to query customer info
    System.out.println("Sent HTTP GET request to query customer info");
    URL url = new URL("http://localhost:8080/authenticate/customers/123");
    InputStream in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP GET request to query sub resource product info
    System.out.println("\n");
    System.out.println("Sent HTTP GET request to query sub resource product info");
    url = new URL("http://localhost:9000/authenticate/orders/223/products/323");
    in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP PUT request to update customer info
    System.out.println("\n");
    System.out.println("Sent HTTP PUT request to update customer info");
    Client client = new Client();
    String inputFile = client.getClass().getResource("/update_customer.xml").getFile();
    URIResolver resolver = new URIResolver(inputFile);
    File input = new File(resolver.getURI());
    PutMethod put = new PutMethod("http://localhost:9000/authenticate/customers");
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(put);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(put.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }

    // Sent HTTP POST request to add customer
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add customer");
    inputFile = client.getClass().getResource("/add_customer.xml").getFile();
    resolver = new URIResolver(inputFile);
    input = new File(resolver.getURI());
    PostMethod post = new PostMethod("http://localhost:9000/authenticate/customers");
    post.addRequestHeader("Accept", "text/xml");
    entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }

    // Sent HTTP POST request to add json customer
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add json customer");
    String inputFilej = client.getClass().getResource("/add_customer.json").getFile();
    URIResolver resolverj = new URIResolver(inputFilej);
    File inputj = new File(resolverj.getURI());
    PostMethod postj = new PostMethod("http://localhost:9000/authenticate/customersjson");
    postj.addRequestHeader("Accept", "application/json");
    RequestEntity entityj = new FileRequestEntity(inputj, "application/json; charset=ISO-8859-1");
    postj.setRequestEntity(entityj);
    HttpClient httpclientj = new HttpClient();

    try {
        int resultj = httpclientj.executeMethod(postj);
        System.out.println("Response status code json: " + resultj);
        System.out.println("Response body json: ");
        System.out.println(postj.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        postj.releaseConnection();
    }
    System.out.println("\n");
    System.exit(0);
}

From source file:de.mpg.escidoc.services.edoc.BatchUpdate.java

/**
 * @param args/*from  w ww .ja  v a 2  s  .  co  m*/
 */
public static void main(String[] args) throws Exception {
    CORESERVICES_URL = PropertyReader.getProperty("escidoc.framework_access.framework.url");

    String userHandle = AdminHelper.loginUser("import_user", "");

    logger.info("Querying core-services...");
    HttpClient httpClient = new HttpClient();
    String filter = "<param><filter name=\"http://escidoc.de/core/01/structural-relations/context\">"
            + IMPORT_CONTEXT
            + "</filter><order-by>http://escidoc.de/core/01/properties/creation-date</order-by><limit>0</limit></param>";

    logger.info("Filter: " + filter);

    PostMethod postMethod = new PostMethod(CORESERVICES_URL + "/ir/items/filter");

    postMethod.setRequestBody(filter);

    ProxyHelper.executeMethod(httpClient, postMethod);

    //        GetMethod getMethod = new GetMethod(CORESERVICES_URL + "/ir/item/escidoc:100220");
    //        getMethod.setRequestHeader("Cookie", "escidocCookie=" + userHandle)ProxyHelper.executeMethod(httpClient, getMethod)hod(httpClient, getMethod);

    String response = postMethod.getResponseBodyAsString();
    logger.info("...done!");

    System.out.println(response);

    while (response.contains("<escidocItem:item")) {

        int startPos = response.indexOf("<escidocItem:item");
        int endPos = response.indexOf("</escidocItem:item>");

        String item = response.substring(startPos, endPos + 19);

        response = response.substring(endPos + 19);

        startPos = item.indexOf("xlink:href=\"");
        endPos = item.indexOf("\"", startPos + 12);

        String objId = item.substring(startPos + 12, endPos);

        System.out.print(objId);

        if (item.contains("escidoc:22019")) {
            item = item.replaceAll("escidoc:22019", "escidoc:55222");

            PutMethod putMethod = new PutMethod(CORESERVICES_URL + objId);

            putMethod.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
            putMethod.setRequestEntity(new StringRequestEntity(item));
            ProxyHelper.executeMethod(httpClient, putMethod);

            String result = putMethod.getResponseBodyAsString();

            //System.out.println(item);

            startPos = result.indexOf("last-modification-date=\"");
            endPos = result.indexOf("\"", startPos + 24);
            String modDate = result.substring(startPos + 24, endPos);
            //System.out.println("modDate: " + modDate);
            String param = "<param last-modification-date=\"" + modDate
                    + "\"><url>http://pubman.mpdl.mpg.de/pubman/item/" + objId.substring(4) + "</url></param>";
            postMethod = new PostMethod(CORESERVICES_URL + objId + "/assign-version-pid");
            postMethod.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
            postMethod.setRequestEntity(new StringRequestEntity(param));
            ProxyHelper.executeMethod(httpClient, postMethod);
            result = postMethod.getResponseBodyAsString();
            //System.out.println("Result: " + result);

            startPos = result.indexOf("last-modification-date=\"");
            endPos = result.indexOf("\"", startPos + 24);
            modDate = result.substring(startPos + 24, endPos);
            //System.out.println("modDate: " + modDate);
            param = "<param last-modification-date=\"" + modDate
                    + "\"><comment>Batch repair of organizational unit identifier</comment></param>";
            postMethod = new PostMethod(CORESERVICES_URL + objId + "/submit");
            postMethod.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
            postMethod.setRequestEntity(new StringRequestEntity(param));
            ProxyHelper.executeMethod(httpClient, postMethod);
            result = postMethod.getResponseBodyAsString();
            //System.out.println("Result: " + result);

            startPos = result.indexOf("last-modification-date=\"");
            endPos = result.indexOf("\"", startPos + 24);
            modDate = result.substring(startPos + 24, endPos);
            //System.out.println("modDate: " + modDate);
            param = "<param last-modification-date=\"" + modDate
                    + "\"><comment>Batch repair of organizational unit identifier</comment></param>";
            postMethod = new PostMethod(CORESERVICES_URL + objId + "/release");
            postMethod.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
            postMethod.setRequestEntity(new StringRequestEntity(param));
            ProxyHelper.executeMethod(httpClient, postMethod);
            result = postMethod.getResponseBodyAsString();
            //System.out.println("Result: " + result);
            System.out.println("...changed");
        } else {
            System.out.println("...not affected");
        }
    }

}

From source file:com.mindquarry.desktop.util.HttpUtilities.java

public static String putAsXML(String login, String pwd, String address, byte[] content)
        throws NotAuthorizedException {
    try {/*from   w  w  w. j a v  a2  s . c o  m*/
        HttpClient client = createHttpClient(login, pwd, address);
        PutMethod put = new PutMethod(address);
        put.setDoAuthentication(true);
        put.addRequestHeader("accept", "text/xml"); //$NON-NLS-1$ //$NON-NLS-2$
        put.setRequestEntity(new ByteArrayRequestEntity(content, "text/xml; charset=utf-8"));

        log.info("Executing HTTP PUT on " + address); //$NON-NLS-1$
        client.executeMethod(put);
        log.info("Finished HTTP PUT with status code: "//$NON-NLS-1$
                + put.getStatusCode());

        String id = null;
        if (put.getStatusCode() == 401) {
            throw new NotAuthorizedException(AUTH_REFUSED, address, login, pwd);
        } else if (put.getStatusCode() == 302) {
            Header locationHeader = put.getResponseHeader("location");
            if (locationHeader != null) {
                // we received a redirect to the URL of the putted document,
                // so
                // everything seems right and we just use the new ID:
                id = locationHeader.getValue();
            } else {
                throw new RuntimeException(CONNECTION_ERROR + put.getStatusCode());
            }
        } else {
            throw new RuntimeException(CONNECTION_ERROR + put.getStatusCode());
        }
        put.releaseConnection();
        return id;
    } catch (NotAuthorizedException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e.toString(), e);
    }
}

From source file:com.buglabs.dragonfly.util.WSHelper.java

/**
 * @param url/*  ww w. jav a 2s .  c  om*/
 * @param payload
 * @return
 * @throws HttpException
 * @throws IOException
 */
protected static String put(String url, String payload) throws HttpException, IOException {
    HttpClient c = new HttpClient();

    PutMethod m = new PutMethod(url);
    m.setRequestEntity(new StringRequestEntity(payload));
    c.executeMethod(m);

    return m.getResponseBodyAsString();
}

From source file:eu.eco2clouds.api.bonfire.client.rest.RestClient.java

/**
 * Performs a POST method against the API
 * @param url -> This URL is going to be converted to API_URL:BONFIRE_PORT/url
 * @param payload message sent in the post method
 * @return Response object encapsulation all the HTTP information, it returns <code>null</code> if there was an error.
 *///from w  ww .ja  va 2  s . c  o  m
public static Response executePutMethod(String url, String payload, String username, String password) {
    Response response = null;

    try {
        PutMethod put = new PutMethod(url);

        // We define the request entity
        RequestEntity requestEntity = new StringRequestEntity(payload, BONFIRE_XML, null);
        put.setRequestEntity(requestEntity);

        response = executeMethod(put, BONFIRE_XML, username, password, url);

    } catch (UnsupportedEncodingException exception) {
        System.out.println("THE PAYLOAD ENCODING IS NOT SUPPORTED");
        System.out.println("ERROR: " + exception.getMessage());
        System.out.println("ERROR: " + exception.getStackTrace());
    }

    return response;
}

From source file:com.linkedin.pinot.controller.helix.ControllerTest.java

public static PutMethod sendMultipartPutRequest(String url, String body) throws IOException {
    HttpClient httpClient = new HttpClient();
    PutMethod putMethod = new PutMethod(url);
    // our handlers ignore key...so we can put anything here
    Part[] parts = { new StringPart("body", body) };
    putMethod.setRequestEntity(new MultipartRequestEntity(parts, putMethod.getParams()));
    httpClient.executeMethod(putMethod);
    return putMethod;

}