Example usage for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream

List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream.

Prototype

public abstract InputStream getResponseBodyAsStream() throws IOException;

Source Link

Usage

From source file:de.micromata.jira.rest.core.util.RestException.java

public RestException(HttpMethod method) {
    this.statusCode = method.getStatusCode();
    this.reasonPhrase = method.getStatusText();
    try {/*from   ww w  .jav  a2s  . c  o  m*/
        InputStream inputStream = method.getResponseBodyAsStream();
        if (inputStream != null) {
            InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
            JsonReader jsonReader = new JsonReader(reader);
            jsonReader.setLenient(true);
            Gson gson = new Gson();
            restErrorMessage = gson.fromJson(jsonReader, ErrorBean.class);
        }
    } catch (IOException e) {
        // nothing to say
    } finally {
        method.releaseConnection();
    }

}

From source file:com.robonobo.common.io.HttpInputStream.java

protected InputStream createInputStream() throws IOException {
    HttpClient client = new HttpClient();
    HttpMethod method = createMethod(url);

    int status = client.executeMethod(method);
    switch (status) {
    case 200:/*w ww .  j av  a  2s  .  co  m*/
    case 206:
        return method.getResponseBodyAsStream();
    default:
        throw new IOException("Unable to connect to url '" + url + "', status code: " + status);
    }
}

From source file:it.intecs.pisa.toolbox.util.URLReader.java

public static Object getURLContent(String host, String url, int port) throws Exception {
    HttpMethod method;
    try {//ww  w.java  2 s .  c om
        method = new GetMethod(url);
        HttpConnection conn = new HttpConnection(host, port);
        String proxyHost;
        String proxyPort;
        if ((proxyHost = System.getProperty("http.proxyHost")) != null
                && (proxyPort = System.getProperty("http.proxyPort")) != null) {
            conn.setProxyHost(proxyHost);
            conn.setProxyPort(Integer.parseInt(proxyPort));
        }
        conn.open();
        method.execute(new HttpState(), conn);
        String inpTmp = method.getResponseBodyAsString();
        Reader in = new InputStreamReader(method.getResponseBodyAsStream());
        StringBuffer out = new StringBuffer();
        char[] buffer = new char[1024];
        for (int count = in.read(buffer); count >= 0; count = in.read(buffer)) {
            out.append(buffer, 0, count);
        }
        return out.toString();
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return null;
    }
}

From source file:gov.nih.nci.cabio.RESTAPITest.java

/**
 * Queries the REST API with the given query string and parses the resulting
 * XML into a DOM document./*  w  w  w . j  a  va2 s  .c  o m*/
 */
private Document getXML(String query) throws Exception {
    URI uri = new URI(GET_XML_URL + "?" + query, false);
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod();
    method.setURI(uri);
    client.executeMethod(method);
    InputSource inputSource = new InputSource(method.getResponseBodyAsStream());
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    return builder.parse(inputSource);
}

From source file:net.sf.ehcache.constructs.web.filter.SpeedTest.java

/**
 * Time to get 200 Cached Pages/* ww  w  .  j a  v  a2 s . c  o m*/
 * StopWatch time: 947ms
 */
public void testSpeedHttpClientNotCached() throws IOException {
    StopWatch stopWatch = new StopWatch();
    String url = "http://localhost:9080/Login.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    stopWatch.getElapsedTime();
    for (int i = 0; i < 200; i++) {
        httpClient.executeMethod(httpMethod);
        httpMethod.getResponseBodyAsStream();
    }
    long time = stopWatch.getElapsedTime();
    LOG.info("Time for 200 uncached page requests: " + time);
}

From source file:net.sf.ehcache.constructs.web.filter.SpeedTest.java

/**
 * Time to get 200 Cached Pages/*  w ww. ja v a  2  s  . co  m*/
 * StopWatch time: 1021ms
 */
public void testSpeedHttpClientCached() throws IOException {
    StopWatch stopWatch = new StopWatch();
    String url = "http://localhost:9080/CachedPage.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    stopWatch.getElapsedTime();
    for (int i = 0; i < 200; i++) {
        httpClient.executeMethod(httpMethod);
        httpMethod.getResponseBodyAsStream();
    }
    long time = stopWatch.getElapsedTime();
    LOG.info("Time for 200 cached page requests: " + time);
}

From source file:com.utest.webservice.client.rest.RestClient.java

private void processResponse(HttpMethod method) throws IOException {
    method.getRequestHeader("Accept");
    responseStream = method.getResponseBodyAsStream();
}

From source file:hk.hku.cecid.corvus.http.PartnershipSender.java

/**
 * [@EVENT] This method is invoked when receivedas2 the reply HTTP response from the server.
 * <br/><br/>//from  w ww  .  j a  v  a2 s .com
 * Verify the HTTP response (expected a HTML content) by PartnershipOpVerifer to check
 * whether the partnership operation execute successfully or not.
 * 
 * @throws SAXException 
 *          When fail to verify by PartnershipOpVerifer.
 */
protected void onResponse() throws Exception {
    try {
        HttpMethod method = this.getExecutedMethod();
        InputStream ins = method.getResponseBodyAsStream();
        new PartnershipOpVerifer().validate(ins);
        ins.close();
        this.resultStatus = "Operation executed successfully.";
    } catch (Exception ex) {
        this.resultStatus = "ERROR: " + ex.getMessage();
        throw ex; // Re-throw 
    }
}

From source file:hk.hku.cecid.corvus.http.EnvelopQuerySender.java

/**
 * [@EVENT] This method is invoked when received the reply HTTP  response from the server.
 * <br/><br/>//from   w w  w. j  a  va 2s  .c o  m
 * It saves the response body stream and then available to get through by {@link #getEnvelopStream()}
 */
protected void onResponse() throws Exception {
    HttpMethod post = this.getExecutedMethod();
    InputStream ins = post.getResponseBodyAsStream();

    /*
     * We have to pipe the content to either memory or storage because the response stream
     * is directly extracted from socket which is going to close upon the connection 
     * has been closed.
     */
    if (ins.available() < THRESHOLD) {
        byte[] envelop = IOHandler.readBytes(ins);
        this.envelopStream = new ByteArrayInputStream(envelop);
    } else {
        // Create a temporary file at TMP directory.
        File envelopTmp = new File(BASE_PATH + this.hashCode());
        envelopTmp.deleteOnExit();
        // Pipe the content to the TMP file.
        FileChannel fChannel = new FileInputStream(envelopTmp).getChannel();
        fChannel.transferFrom(Channels.newChannel(ins), 0, ins.available());
        fChannel.close();
        // Create an buffered stream to the file.
        this.envelopStream = new BufferedInputStream(new FileInputStream(envelopTmp));
        // InputStream is closed automatically.
    }
}

From source file:com.google.wave.api.robot.HttpRobotConnection.java

/**
 * Fetches the given URL, given a method ({@code GET} or {@code POST}).
 *
 * @param url the URL to be fetched./*from  w ww.  jav  a  2  s  .co  m*/
 * @param method the method to fetch the URL, can be {@code GET} or
 *     {@code POST}.
 * @return the content of the URL.
 *
 * @throws RobotConnectionException if there is a problem fetching the URL,
 *     for example, if the response code is not HTTP OK (200).
 */
private String fetch(String url, HttpMethod method) throws RobotConnectionException {
    try {
        int statusCode = httpClient.executeMethod(method);
        return RobotConnectionUtil.validateAndReadResponse(url, statusCode, method.getResponseBodyAsStream());
    } catch (IOException e) {
        String msg = "Robot fetch http failure: " + url + ".";
        throw new RobotConnectionException(msg, e);
    } finally {
        method.releaseConnection();
    }
}