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:com.manning.blogapps.chapter10.examples.AuthPut.java

public static void main(String[] args) throws Exception {
    if (args.length < 4) {
        System.out.println("USAGE: authput <username> <password> <filepath> <url>");
        System.exit(-1);//  w w  w.  j a  v  a  2s  .c  o m
    }
    String credentials = args[0] + ":" + args[1];
    String filepath = args[2];
    String url = args[3];

    HttpClient httpClient = new HttpClient();
    EntityEnclosingMethod method = new PutMethod(url);
    method.setRequestHeader("Authorization",
            "Basic " + new String(Base64.encodeBase64(credentials.getBytes())));

    File upload = new File(filepath);
    method.setRequestHeader("name", upload.getName());

    String contentType = "application/atom+xml; charset=utf8";
    if (filepath.endsWith(".gif"))
        contentType = "image/gif";
    else if (filepath.endsWith(".jpg"))
        contentType = "image/jpg";
    method.setRequestHeader("Content-type", contentType);

    method.setRequestBody(new FileInputStream(filepath));
    httpClient.executeMethod(method);

    System.out.println(method.getResponseBodyAsString());
}

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 ww  w .j  a v a  2 s  . 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: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();/*ww w  .  j  a 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: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 ww .j ava2s.  co 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
    /*/*from   ww w  .  j  a v  a  2s. c om*/
     * 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
    /*// www  . j a  v a2  s.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: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/* w w w.  ja  v a 2 s.  c o  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:au.org.ala.spatial.util.UploadSpatialResource.java

public static String loadResource(String url, String extra, String username, String password,
        String resourcepath) {/*from   w  ww  . j av  a  2 s . co m*/
    String output = "";

    HttpClient client = new HttpClient();
    client.setConnectionTimeout(10000);
    client.setTimeout(60000);

    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

    File input = new File(resourcepath);

    PutMethod put = new PutMethod(url);
    put.setDoAuthentication(true);

    //put.addRequestHeader("Content-type", "application/zip");

    // Request content will be retrieved directly 
    // from the input stream 
    RequestEntity entity = new FileRequestEntity(input, "application/zip");
    put.setRequestEntity(entity);

    // Execute the request 
    try {
        int result = client.executeMethod(put);

        output = result + ": " + put.getResponseBodyAsString();

    } catch (Exception e) {
        e.printStackTrace(System.out);
        output = "0: " + e.getMessage();
    } finally {
        // Release current connection to the connection pool once you are done 
        put.releaseConnection();
    }

    return output;

}

From source file:com.thoughtworks.go.remote.work.RemoteConsoleAppender.java

public void append(String content) throws IOException {
    PutMethod putMethod = new PutMethod(consoleUri);
    try {//from   w ww  .j av a 2  s . c  om
        HttpClient httpClient = httpService.httpClient();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Appending console to URL -> " + consoleUri);
        }
        putMethod.setRequestEntity(new StringRequestEntity(content));

        HttpClientParams clientParams = new HttpClientParams();
        clientParams.setParameter("agentId", agentIdentifier.getUuid());
        HttpService.setSizeHeader(putMethod, content.getBytes().length);
        putMethod.setParams(clientParams);
        httpClient.executeMethod(putMethod);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Got " + putMethod.getStatusCode());
        }
    } finally {
        putMethod.releaseConnection();
    }
}

From source file:net.sf.j2ep.requesthandlers.EntityEnclosingRequestHandler.java

/**
 * Will set the input stream and the Content-Type header to match this request.
 * Will also set the other headers send in the request.
 * //from  w  w  w  .  j  a  va  2 s  .  c om
 * @throws IOException An exception is throws when there is a problem getting the input stream
 * @see net.sf.j2ep.model.RequestHandler#process(javax.servlet.http.HttpServletRequest, java.lang.String)
 */
public HttpMethod process(HttpServletRequest request, String url) throws IOException {

    EntityEnclosingMethod method = null;

    if (request.getMethod().equalsIgnoreCase("POST")) {
        method = new PostMethod(url);
    } else if (request.getMethod().equalsIgnoreCase("PUT")) {
        method = new PutMethod(url);
    }

    setHeaders(method, request);

    InputStreamRequestEntity stream;
    stream = new InputStreamRequestEntity(request.getInputStream());
    method.setRequestEntity(stream);
    method.setRequestHeader("Content-type", request.getContentType());

    return method;

}