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:gov.nih.nci.cabio.portal.portlet.RESTProxyServletController.java

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    try {/*from w  w  w. ja  v  a2 s.com*/
        String relativeURL = request.getParameter("relativeURL");
        if (relativeURL == null) {
            log.error("Null relativeURL parameter");
            return null;
        }

        String qs = request.getQueryString();
        if (qs == null) {
            qs = "";
        }

        qs = qs.replaceFirst("relativeURL=(.*?)&", "");
        qs = URLDecoder.decode(qs, "UTF-8");

        String url = caBIORestURL + relativeURL + "?" + qs;
        log.info("Proxying URL: " + url);

        URI uri = new URI(url, false);
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod();
        method.setURI(uri);
        client.executeMethod(method);

        response.setContentType(method.getResponseHeader("Content-Type").getValue());

        CopyUtils.copy(method.getResponseBodyAsStream(), response.getOutputStream());
    } catch (Exception e) {
        throw new ServletException("Unable to connect to caBIO", e);
    }

    return null;
}

From source file:com.comcast.cats.service.util.HttpClientUtil.java

public static synchronized Object getForObject(String uri, Map<String, String> paramMap) {
    Object responseObject = new Object();

    HttpMethod httpMethod = new GetMethod(uri);

    if ((null != paramMap) && (!paramMap.isEmpty())) {
        httpMethod.setQueryString(getNameValuePair(paramMap));
    }//from  www.  ja va2  s .c  o  m

    Yaml yaml = new Yaml();

    HttpClient client = new HttpClient();
    InputStream responseStream = null;
    Reader inputStreamReader = null;

    try {
        int responseCode = client.executeMethod(httpMethod);

        if (HttpStatus.SC_OK != responseCode) {
            logger.error("[ REQUEST  ] " + httpMethod.getURI().toString());
            logger.error("[ METHOD   ] " + httpMethod.getName());
            logger.error("[ STATUS   ] " + responseCode);
        } else {
            logger.trace("[ REQUEST  ] " + httpMethod.getURI().toString());
            logger.trace("[ METHOD   ] " + httpMethod.getName());
            logger.trace("[ STATUS   ] " + responseCode);
        }

        responseStream = httpMethod.getResponseBodyAsStream();
        inputStreamReader = new InputStreamReader(responseStream, VideoRecorderServiceConstants.UTF);
        responseObject = yaml.load(inputStreamReader);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    } finally {
        cleanUp(inputStreamReader, responseStream, httpMethod);
    }

    return responseObject;
}

From source file:com.comcast.cats.service.util.HttpClientUtil.java

public static synchronized Object postForObject(String uri, Map<String, String> paramMap) {
    Object responseObject = new Object();

    HttpMethod httpMethod = new PostMethod(uri);

    if ((null != paramMap) && (!paramMap.isEmpty())) {
        httpMethod.setQueryString(getNameValuePair(paramMap));
    }//from  ww  w.  j a  v  a  2s  .  c  o  m

    Yaml yaml = new Yaml();

    HttpClient client = new HttpClient();
    InputStream responseStream = null;
    Reader inputStreamReader = null;

    try {
        int responseCode = client.executeMethod(httpMethod);

        if (HttpStatus.SC_OK != responseCode) {
            logger.error("[ REQUEST  ] " + httpMethod.getURI().toString());
            logger.error("[ METHOD   ] " + httpMethod.getName());
            logger.error("[ STATUS   ] " + responseCode);
        } else {
            logger.trace("[ REQUEST  ] " + httpMethod.getURI().toString());
            logger.trace("[ METHOD   ] " + httpMethod.getName());
            logger.trace("[ STATUS   ] " + responseCode);
        }

        responseStream = httpMethod.getResponseBodyAsStream();
        inputStreamReader = new InputStreamReader(responseStream, VideoRecorderServiceConstants.UTF);
        responseObject = yaml.load(inputStreamReader);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    } finally {
        cleanUp(inputStreamReader, responseStream, httpMethod);
    }

    return responseObject;
}

From source file:com.comcast.cats.service.util.HttpClientUtil.java

public static synchronized Object deleteForObject(String uri, Map<String, String> paramMap) {
    Object responseObject = new Object();

    HttpMethod httpMethod = new DeleteMethod(uri);

    if ((null != paramMap) && (!paramMap.isEmpty())) {
        httpMethod.setQueryString(getNameValuePair(paramMap));
    }/* w ww .  ja  v  a 2  s  .c  om*/

    Yaml yaml = new Yaml();

    HttpClient client = new HttpClient();
    InputStream responseStream = null;
    Reader inputStreamReader = null;

    try {
        int responseCode = client.executeMethod(httpMethod);

        if (HttpStatus.SC_OK != responseCode) {
            logger.error("[ REQUEST  ] " + httpMethod.getURI().toString());
            logger.error("[ METHOD   ] " + httpMethod.getName());
            logger.error("[ STATUS   ] " + responseCode);
        } else {
            logger.trace("[ REQUEST  ] " + httpMethod.getURI().toString());
            logger.trace("[ METHOD   ] " + httpMethod.getName());
            logger.trace("[ STATUS   ] " + responseCode);
        }

        responseStream = httpMethod.getResponseBodyAsStream();
        inputStreamReader = new InputStreamReader(responseStream, VideoRecorderServiceConstants.UTF);
        responseObject = yaml.load(inputStreamReader);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    } finally {
        cleanUp(inputStreamReader, responseStream, httpMethod);
    }

    return responseObject;
}

From source file:com.markwatson.linkeddata.DBpediaLookupClient.java

public DBpediaLookupClient(String query) throws Exception {
    this.query = query;
    HttpClient client = new HttpClient();

    String query2 = query.replaceAll(" ", "+");
    HttpMethod method = new GetMethod(
            "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=" + query2);
    try {//w  ww.  ja v  a 2  s.  c om
        client.executeMethod(method);
        InputStream ins = method.getResponseBodyAsStream();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser sax = factory.newSAXParser();
        sax.parse(ins, this);
    } catch (HttpException he) {
        System.err.println("Http error connecting to lookup.dbpedia.org");
    } catch (IOException ioe) {
        System.err.println("Unable to connect to lookup.dbpedia.org");
    }
    method.releaseConnection();
}

From source file:com.cognifide.actions.msg.push.active.PushClientRunnable.java

private void connect() throws IOException {
    final HttpMethod method = new GetMethod(serverUrl + SERVLET_PATH);
    if (client.executeMethod(method) != 200) {
        return;/*ww w .j a  va 2  s  .  c o m*/
    }
    final InputStreamReader reader = new InputStreamReader(method.getResponseBodyAsStream());
    final BufferedReader bufferedReader = new BufferedReader(reader);

    String msgId;
    while ((msgId = bufferedReader.readLine()) != null) {
        final String topic = bufferedReader.readLine();
        final String msg = bufferedReader.readLine();
        LOG.debug("Got message with id " + msgId);
        for (PushReceiver r : receivers) {
            r.gotMessage(topic, msg);
        }
        try {
            confirm(msgId);
            LOG.debug("Message " + msgId + " confirmed");
        } catch (IOException e) {
            LOG.error("Can't confirm message " + msgId, e);
        }
    }
    method.releaseConnection();
}

From source file:edu.virginia.speclab.juxta.author.view.export.WebServiceClient.java

/**
 * Call after a request is made with the <code>execRequest</code> method. It will return a UTF-8
 * encode string containing the full response body from the service.
 * /* w  ww.ja  va2  s  .  com*/
 * @param httpMethod
 * @return
 * @throws IOException
 */
private final String getResponseString(HttpMethod httpMethod) throws IOException {
    InputStream is = httpMethod.getResponseBodyAsStream();
    return IOUtils.toString(is, "UTF-8");
}

From source file:com.thoughtworks.twist.mingle.core.MingleClient.java

protected Reader getResponse(HttpMethod method) {
    try {//from  w w w.  ja  va2s. com
        return new InputStreamReader(method.getResponseBodyAsStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cloud.test.longrun.VirtualMachine.java

public void deployVM(long zoneId, long serviceOfferingId, long templateId, String server, String apiKey,
        String secretKey) throws IOException {

    String encodedZoneId = URLEncoder.encode("" + zoneId, "UTF-8");
    String encodedServiceOfferingId = URLEncoder.encode("" + serviceOfferingId, "UTF-8");
    String encodedTemplateId = URLEncoder.encode("" + templateId, "UTF-8");
    String encodedApiKey = URLEncoder.encode(apiKey, "UTF-8");
    String requestToSign = "apiKey=" + encodedApiKey + "&command=deployVirtualMachine&serviceOfferingId="
            + encodedServiceOfferingId + "&templateId=" + encodedTemplateId + "&zoneId=" + encodedZoneId;

    requestToSign = requestToSign.toLowerCase();
    String signature = TestClientWithAPI.signRequest(requestToSign, secretKey);
    String encodedSignature = URLEncoder.encode(signature, "UTF-8");
    String url = server + "?command=deployVirtualMachine" + "&zoneId=" + encodedZoneId + "&serviceOfferingId="
            + encodedServiceOfferingId + "&templateId=" + encodedTemplateId + "&apiKey=" + encodedApiKey
            + "&signature=" + encodedSignature;

    s_logger.info("Sending this request to deploy a VM: " + url);
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    int responseCode = client.executeMethod(method);
    s_logger.info("deploy linux vm response code: " + responseCode);
    if (responseCode == 200) {
        InputStream is = method.getResponseBodyAsStream();
        Map<String, String> values = TestClientWithAPI.getSingleValueFromXML(is,
                new String[] { "id", "ipaddress" });
        long linuxVMId = Long.parseLong(values.get("id"));
        s_logger.info("got linux virtual machine id: " + linuxVMId);
        this.setPrivateIp(values.get("ipaddress"));

    } else if (responseCode == 500) {
        InputStream is = method.getResponseBodyAsStream();
        Map<String, String> errorInfo = TestClientWithAPI.getSingleValueFromXML(is,
                new String[] { "errorcode", "description" });
        s_logger.error("deploy linux vm test failed with errorCode: " + errorInfo.get("errorCode")
                + " and description: " + errorInfo.get("description"));
    } else {//from   w  w w . jav  a2 s .co  m
        s_logger.error("internal error processing request: " + method.getStatusText());
    }
}

From source file:com.dlecan.agreg.AgregResultsBot.java

public boolean isResultatsDisponibles(String type) throws Exception {
    boolean resultatsDisponibles = false;

    String urlAppelee = URL_PUBLINET_PREFIX + type + URL_PUBLINET_SUFFIX;

    HttpMethod getUrlPublinet = new GetMethod(urlAppelee);
    try {//w w w.j  ava 2  s  .  c om
        int status = client.executeMethod(getUrlPublinet);

        if (status == HttpStatus.SC_OK) {
            InputStream streamPage = getUrlPublinet.getResponseBodyAsStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(streamPage));

            String line;
            while ((line = reader.readLine()) != null) {

                if (line.toUpperCase().contains("AUCUN CANDIDAT ADMIS")) {
                    resultatsDisponibles = false;
                    break;
                } else if (line.toUpperCase()
                        .contains("Cliquez sur une des lettres de l'alphabet".toUpperCase())) {
                    resultatsDisponibles = true;
                    break;
                } else {
                    // Le systme dconne
                }
            }
            if (resultatsDisponibles) {
                while ((line = reader.readLine()) != null) {

                    if (line.toUpperCase().contains("VALADE")) {
                        recue = true;
                        messageAEnvoyer = urlAppelee + "\n\n" + line;
                        break;
                    } else {
                        // Le systme dconne
                    }
                }
            }
        } else {
            logger.error("Method failed: {}", getUrlPublinet.getStatusLine());
        }

    } finally {
        getUrlPublinet.releaseConnection();
    }
    return resultatsDisponibles;
}