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

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

Introduction

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

Prototype

public abstract byte[] getResponseBody() throws IOException;

Source Link

Usage

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

/**
 * Tests that a page which is not storeGzipped is not gzipped when the user agent does not accept gzip encoding
 *///from w w  w . j ava  2s . co  m
public void testNotGzippedWhenNotAcceptEncodingPageFragment() throws Exception {
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(buildUrl("/include/Footer.jsp"));
    httpMethod.setRequestHeader(new Header("Accept-encoding", "gzip"));
    httpClient.executeMethod(httpMethod);
    byte[] responseBody = httpMethod.getResponseBody();
    assertFalse(PageInfo.isGzipped(responseBody));
    assertNotSame("gzip", httpMethod.getResponseHeader("Accept-encoding"));
}

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

/**
 * Tests that a page which is not storeGzipped is not gzipped when the user agent accepts gzip encoding
 *//*  ww  w  . j  a va  2 s . c  o m*/
public void testNotGzippedWhenAcceptEncodingPageFragment() throws Exception {
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(buildUrl("/include/Footer.jsp"));
    //httpMethod.setRequestHeader(new Header("Accept-encoding", "gzip"));
    httpClient.executeMethod(httpMethod);
    byte[] responseBody = httpMethod.getResponseBody();
    assertFalse(PageInfo.isGzipped(responseBody));
    assertNotSame("gzip", httpMethod.getResponseHeader("Accept-encoding"));
}

From source file:io.aino.agents.core.AgentIntegrationTest.java

private JsonNode parseJsonFromResponseBody(HttpMethod method) throws IOException {
    JsonParser jsonParser = new JsonFactory().createJsonParser(method.getResponseBody());
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readTree(jsonParser);
}

From source file:com.cordys.coe.ac.httpconnector.impl.StandardResponseHandler.java

/**
 * This method will log the response as it was received from the web server.
 * // ww  w  .j a  va  2  s.  c om
 * @param httpMethod
 *            The HTTP method.
 * 
 * @return The response body.
 * 
 * @throws IOException
 *             In case the response could not be read for some reason.
 */
protected byte[] getHTTPResponse(HttpMethod httpMethod) throws IOException {
    byte[] returnValue = httpMethod.getResponseBody();

    if (LOG.isDebugEnabled()) {
        if (LOG.isDebugEnabled()) {
            try {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Received response data: " + new String(returnValue, "UTF-8"));
                }
            } catch (Exception ignored) {
            }
        }
    }

    return returnValue;
}

From source file:com.wooki.services.parsers.XHTMLToFormattingObjects.java

public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
    InputSource toReturn = null;//from  www  . j  ava2s  .c o  m
    Element cachedValue = cache.get(systemId);
    if (cachedValue == null) {
        if (systemId.contains("http://")) {
            HttpMethod get = new GetMethod(systemId);
            httpClient.executeMethod(get);
            byte[] body = get.getResponseBody();
            Element element = new Element(systemId, body);
            cache.put(element);
            toReturn = new InputSource(new BufferedInputStream(new ByteArrayInputStream(body)));
            toReturn.setSystemId(systemId);
        } else if (systemId.contains("file://")) {
            String newPath = systemId.substring("file:///".length()).replace('/', File.separatorChar);
            File baseFile = new File(newPath);
            try {
                toReturn = new InputSource(new FileInputStream(baseFile));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                logger.error(e.getLocalizedMessage());
                return null;
            }
        }
    } else {
        toReturn = new InputSource(
                new BufferedInputStream(new ByteArrayInputStream((byte[]) cachedValue.getObjectValue())));
        toReturn.setSystemId(systemId);
    }
    return toReturn;
}

From source file:datasoul.util.OnlineUpdateCheck.java

@Override
public void run() {

    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(ONLINE_BASE_URL + "latest-version");
    try {/*from   w w w  . j a va2 s  . co m*/
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            return;
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody();

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        String resp = new String(responseBody);

        // Expected format is: <NUMBER>;<NEW VERSION>;<URL>
        String toks[] = resp.split(";");

        if (toks.length != 3) {
            return;
        }

        int latestversion = Integer.parseInt(toks[0]);

        if (latestversion > VERSION) {
            String msg = java.util.ResourceBundle.getBundle("datasoul/internationalize")
                    .getString("NEW DATASOUL VERSION") + " " + toks[1] + " "
                    + java.util.ResourceBundle.getBundle("datasoul/internationalize")
                            .getString("IS AVAILABLE AT")
                    + " " + toks[2];

            ObjectManager.getInstance().getDatasoulMainForm().setInfoText(msg);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }

}

From source file:com.cordys.coe.ac.httpconnector.impl.StandardResponseHandler.java

/**
 * Converts the HTTP response to XML./*from   w ww  .  j a v  a 2  s  .c  o m*/
 * 
 * @param httpMethod
 *            HTTP method object from which the response data is read.
 * @param doc
 *            NOM document for creating the XML.
 * 
 * @return Converted NOM XML.
 * 
 * @throws IOException
 * @throws XMLException
 */
protected int convertToXml(HttpMethod httpMethod, Document doc) throws IOException, XMLException {
    byte[] responseBody = httpMethod.getResponseBody();

    if (LOG.isDebugEnabled()) {
        try {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Received response data: " + new String(responseBody, "UTF-8"));
            }
        } catch (Exception ignored) {
        }
    }

    if (responseBody == null || responseBody.length == 0) {
        return 0;
    }

    // Convert the response into XML.
    return doc.load(responseBody);
}

From source file:com.wooki.services.parsers.XHTMLToFormattingObjects.java

public Source resolve(String href, String base) throws TransformerException {
    Source toReturn = null;//  ww w. jav  a2  s . c o  m
    if (base != null && base.contains("file://")) {
        String newPath = base.substring("file:///".length()).replace('/', File.separatorChar);
        newPath = newPath.substring(0, newPath.lastIndexOf(File.separatorChar));
        File baseFile = new File(newPath);
        baseFile = new File(
                baseFile.getAbsolutePath() + File.separatorChar + href.replace('/', File.separatorChar));
        try {
            toReturn = new StreamSource(new FileInputStream(baseFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return null;
        }
    } else {
        if (base.contains("http://")) {
            String newUrl = base.substring(0, base.lastIndexOf('/') + 1);
            newUrl += href;
            HttpMethod get = new GetMethod(newUrl);
            try {
                httpClient.executeMethod(get);
                byte[] body = get.getResponseBody();
                Element element = new Element(newUrl, body);
                cache.put(element);
                toReturn = new StreamSource(new BufferedInputStream(new ByteArrayInputStream(body)));
            } catch (HttpException e) {
                e.printStackTrace();
                logger.error(e.getLocalizedMessage());
                return null;
            } catch (IOException e) {
                e.printStackTrace();
                logger.error(e.getLocalizedMessage());
                return null;
            }
            toReturn.setSystemId(newUrl);
        } else {
            toReturn = new StreamSource(XHTMLToFormattingObjects.class.getResourceAsStream(href));
        }
    }
    return toReturn;
}

From source file:com.clarkparsia.sbol.editor.sparql.StardogEndpoint.java

@Override
public void executeSelectQuery(String query, TupleQueryResultHandler handler) throws QueryEvaluationException {
    try {//  w w w  .  j a  v a2  s.co  m
        boolean complete = false;
        HttpMethod response = executeQuery(query);
        try {
            tupleParser.setTupleQueryResultHandler(handler);
            byte[] bytes = response.getResponseBody();
            tupleParser.parse(new ByteArrayInputStream(bytes));
            complete = true;
        } catch (HttpException e) {
            throw new QueryEvaluationException(e);
        } catch (QueryResultParseException e) {
            throw new QueryEvaluationException(e);
        } catch (TupleQueryResultHandlerException e) {
            throw new QueryEvaluationException(e);
        } finally {
            if (!complete) {
                response.abort();
            }
        }
    } catch (IOException e) {
        throw new QueryEvaluationException(e);
    }
}

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

/**
 * JSPs and Servlets can send bodies when the response is SC_NOT_MODIFIED.
 * In this case there should not be a body but there is. Orion seems to kill the body
 * after is has left the Servlet filter chain. To avoid wget going into an inifinite
 * retry loop, and presumably some other web clients, the content length should be 0
 * and the body 0./*from   w  w w  .ja v a2 s . c  o m*/
 * <p/>
 * Manual test: wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_gzip/SC_NO_CONTENT.jsp
 */
public void testNoContentJSPGzipFilter() throws Exception {

    String url = "http://localhost:9080/empty_gzip/SC_NO_CONTENT.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    assertEquals(HttpURLConnection.HTTP_NO_CONTENT, responseCode);
    byte[] responseBody = httpMethod.getResponseBody();
    assertEquals(null, responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
    assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue());
    checkNullOrZeroContentLength(httpMethod);
}