Example usage for org.apache.commons.httpclient.methods PostMethod toString

List of usage examples for org.apache.commons.httpclient.methods PostMethod toString

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PostMethod toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:net.bioclipse.opentox.api.Dataset.java

public static String createNewDataset(String service, String sdFile, IProgressMonitor monitor)
        throws Exception {
    if (monitor == null)
        monitor = new NullProgressMonitor();

    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(service + "dataset");
    HttpMethodHelper.addMethodHeaders(method, new HashMap<String, String>() {
        {/* w ww  .  ja v  a  2s. c  o m*/
            put("Accept", "text/uri-list");
            put("Content-type", "chemical/x-mdl-sdfile");
        }
    });
    System.out.println("Method: " + method.toString());
    method.setRequestBody(sdFile);
    client.executeMethod(method);
    int status = method.getStatusCode();
    String dataset = "";
    String responseString = method.getResponseBodyAsString();
    logger.debug("Response: " + responseString);
    int tailing = 1;
    if (status == 200 || status == 201 || status == 202) {
        if (responseString.contains("/task/")) {
            logger.debug("Task: " + responseString);
            // OK, we got a task... let's wait until it is done
            String task = method.getResponseBodyAsString();
            Thread.sleep(1000); // let's be friendly, and wait 1 sec
            TaskState state = Task.getState(task);
            while (!state.isFinished() && !monitor.isCanceled()) {
                // let's be friendly, and wait 2 secs and a bit and increase
                // that time after each wait
                int waitingTime = andABit(2000 * tailing);
                logger.debug("Waiting " + waitingTime + "ms.");
                waitUnlessInterrupted(waitingTime, monitor);
                state = Task.getState(task);
                if (state.isRedirected()) {
                    task = state.getResults();
                    logger.debug("  new task, new task!!: " + task);
                }
                // but wait at most 20 secs and a bit
                if (tailing < 10)
                    tailing++;
            }
            if (monitor.isCanceled())
                Task.delete(task);
            // OK, it should be finished now
            dataset = state.getResults();
        } else {
            // OK, that was quick!
            dataset = method.getResponseBodyAsString();
            logger.debug("No Task, Data set: " + dataset);
        }
    }
    method.releaseConnection();
    if (monitor.isCanceled())
        return "";
    logger.debug("Data set: " + dataset);
    dataset = dataset.replaceAll("\n", "");
    return dataset;
}

From source file:com.jackbe.mapreduce.RESTClient.java

public void executeREST(String encodedValue) {
    String result = null;/*from ww w  .j a va  2  s. com*/
    String postPath = protocol + host + ":" + port + path;
    System.out.println("post path = " + postPath);
    PostMethod pm = new PostMethod(postPath);
    System.out.println("10 Encoded value = \n" + encodedValue);
    if (encodedValue != null) {
        //httpMethod = new PostMethod(protocol + host + ":" + port + path);
        //InputStream is = new ByteArrayInputStream(encodedValue.getBytes());
        //pm.setRequestBody(is);
        pm.setRequestBody(encodedValue);
        System.out.println("Validate = " + pm.validate());
        //pm.setQueryString(encodedValue);

        //pm.setHttp11(false);

        log.debug("Invoking REST service: " + protocol + host + ":" + port + path + " " + pm.toString());

    } else {
        throw new RuntimeException("EncodedValue can't be null");
    }

    try {
        client.executeMethod(pm);

        if (pm.getStatusCode() != HttpStatus.SC_OK) {
            log.error("HTTP Error status connecting to Presto: " + pm.getStatusCode());
            log.error("HTTP Error message connecting to Presto: " + pm.getStatusText());
            return;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Status code: " + pm.getStatusCode());
                Header contentTypeHeader = pm.getResponseHeader("content-type");
                log.debug("Mimetype: " + contentTypeHeader.getValue());
            }
        }

        result = pm.getResponseBodyAsString();
        // log.debug(httpMethod.getStatusText());
        if (log.isDebugEnabled())
            log.debug("Response: " + result);
    } catch (Exception e) {
        log.error("Exception executing REST call: " + e, e);
    } finally {
        pm.releaseConnection();
    }
}

From source file:autohit.call.modules.SimpleHttpModule.java

/**
 * Post method. It will set the target address for the client, as well as
 * clearing any state./*from  w ww  .jav  a 2s  .c o  m*/
 * 
 * @param url
 *            the Url path, not to include protocol, address, and port (ie.
 *            "/goats/index.html").
 * @param nv
 *            set of name/value pairs for the post. it can be empty.
 * @return the data from the page as a String
 * @throws CallException
 */
private String post(String url, Hashtable nv) throws CallException {

    if (started == false) {
        throw buildException("Tried to post when a session wasn't started.", CallException.CODE_MODULE_FAULT);
    }

    String result = null;
    String name;
    Object value;

    // Construct our method.
    PostMethod method = new PostMethod(url);
    method.setFollowRedirects(true);
    method.setStrictMode(false);

    //build the rest of the method
    try {
        // Construct the headers
        Enumeration eNV = nv.keys();
        while (eNV.hasMoreElements()) {
            name = (String) eNV.nextElement();
            value = nv.get(name);
            if (value instanceof String) {
                // Only take it if it is a string
                method.addParameter(name, (String) value);
                debug("ADD POST - name=" + name + " value=" + (String) value);
            }
        }
        //DEBUG
        debug("DUMP POST-------------------------------");
        debug(method.toString());
        debug("DUMP POST-------------------------------");

        // Do it
        debug("(post)post=" + url);
        httpClient.executeMethod(method);

        // Process result
        result = method.getResponseBodyAsString();
        log("(post)" + method.getStatusLine().toString() + " size=" + result.length());

    } catch (HttpException he) {
        // Bad but not fatal
        error("(post)Error on connect to url " + url + ".  Error=" + he.getMessage());
    } catch (IOException ioe) {
        // Fatal
        throw buildException("(post)Unable to connect.  Session is invalid.", CallException.CODE_MODULE_FAULT,
                ioe);

    } catch (Exception ex) {
        // Fatal
        throw buildException("(post)Serious general error.", CallException.CODE_MODULE_FAULT, ex);
    } finally {
        try {
            method.releaseConnection();
            method.recycle();
        } catch (Exception e) {
            // Already FUBAR
        }
    }
    return result;
}