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

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

Introduction

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

Prototype

public abstract String getResponseBodyAsString() throws IOException;

Source Link

Usage

From source file:org.apache.cxf.aegis.jaxws.AegisJaxwsGetTest.java

@Test
public void testGetEchoSimple() throws Exception {
    HttpClient httpClient = createClient();
    String url = "http://localhost:" + PORT + "/SimpleEcho/simpleEcho/string/hello";
    HttpMethod method = null;
    method = new GetMethod(url);
    int status = httpClient.executeMethod(method);
    assertEquals(HttpStatus.SC_OK, status);
    String result = method.getResponseBodyAsString();
    assertTrue(result.contains("hello"));
    method.releaseConnection();/*from   w  w  w. j  a  v  a  2s  .  c  om*/
}

From source file:org.apache.hadoop.fs.swift.exceptions.SwiftInvalidResponseException.java

public SwiftInvalidResponseException(String message, String operation, URI uri, HttpMethod method) {
    super(message);
    this.statusCode = method.getStatusCode();
    this.operation = operation;
    this.uri = uri;
    String bodyAsString;// ww w  . jav  a  2 s.co  m
    try {
        bodyAsString = method.getResponseBodyAsString();
        if (bodyAsString == null) {
            bodyAsString = "";
        }
    } catch (IOException e) {
        bodyAsString = "";
    }
    this.body = bodyAsString;
}

From source file:org.apache.hcatalog.templeton.TestWebHCatE2e.java

/**
 * Does a basic HTTP GET and returns Http Status code + response body
 * Will add the dummy user query string//from w  w  w  .  j a v  a 2 s.  c  o m
 */
private static MethodCallRetVal doHttpCall(String uri, HTTP_METHOD_TYPE type, Map<String, Object> data,
        NameValuePair[] params) throws IOException {
    HttpClient client = new HttpClient();
    HttpMethod method;
    switch (type) {
    case GET:
        method = new GetMethod(uri);
        break;
    case DELETE:
        method = new DeleteMethod(uri);
        break;
    case PUT:
        method = new PutMethod(uri);
        if (data == null) {
            break;
        }
        String msgBody = JsonBuilder.mapToJson(data);
        LOG.info("Msg Body: " + msgBody);
        StringRequestEntity sre = new StringRequestEntity(msgBody, "application/json", charSet);
        ((PutMethod) method).setRequestEntity(sre);
        break;
    default:
        throw new IllegalArgumentException("Unsupported method type: " + type);
    }
    if (params == null) {
        method.setQueryString(new NameValuePair[] { new NameValuePair("user.name", username) });
    } else {
        NameValuePair[] newParams = new NameValuePair[params.length + 1];
        System.arraycopy(params, 0, newParams, 1, params.length);
        newParams[0] = new NameValuePair("user.name", username);
        method.setQueryString(newParams);
    }
    String actualUri = "no URI";
    try {
        actualUri = method.getURI().toString();//should this be escaped string?
        LOG.debug(type + ": " + method.getURI().getEscapedURI());
        int httpStatus = client.executeMethod(method);
        LOG.debug("Http Status Code=" + httpStatus);
        String resp = method.getResponseBodyAsString();
        LOG.debug("response: " + resp);
        return new MethodCallRetVal(httpStatus, resp, actualUri, method.getName());
    } catch (IOException ex) {
        LOG.error("doHttpCall() failed", ex);
    } finally {
        method.releaseConnection();
    }
    return new MethodCallRetVal(-1, "Http " + type + " failed; see log file for details", actualUri,
            method.getName());
}

From source file:org.apache.kylin.engine.mr.common.HadoopStatusGetter.java

private String getHttpResponse(String url) throws IOException {
    HttpClient client = new HttpClient();

    String response = null;//from ww w.j  a v  a 2s. c o  m
    while (response == null) { // follow redirects via 'refresh'
        if (url.startsWith("https://")) {
            registerEasyHttps();
        }
        if (url.contains("anonymous=true") == false) {
            url += url.contains("?") ? "&" : "?";
            url += "anonymous=true";
        }

        HttpMethod get = new GetMethod(url);
        get.addRequestHeader("accept", "application/json");

        try {
            client.executeMethod(get);

            String redirect = null;
            Header h = get.getResponseHeader("Location");
            if (h != null) {
                redirect = h.getValue();
                if (isValidURL(redirect) == false) {
                    logger.info("Get invalid redirect url, skip it: " + redirect);
                    Thread.sleep(1000L);
                    continue;
                }
            } else {
                h = get.getResponseHeader("Refresh");
                if (h != null) {
                    String s = h.getValue();
                    int cut = s.indexOf("url=");
                    if (cut >= 0) {
                        redirect = s.substring(cut + 4);

                        if (isValidURL(redirect) == false) {
                            logger.info("Get invalid redirect url, skip it: " + redirect);
                            Thread.sleep(1000L);
                            continue;
                        }
                    }
                }
            }

            if (redirect == null) {
                response = get.getResponseBodyAsString();
                logger.debug("Job " + mrJobId + " get status check result.\n");
            } else {
                url = redirect;
                logger.debug("Job " + mrJobId + " check redirect url " + url + ".\n");
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            logger.error(e.getMessage());
        } finally {
            get.releaseConnection();
        }
    }

    return response;
}

From source file:org.apache.kylin.job.tools.HadoopStatusGetter.java

private String getHttpResponse(String url) throws IOException {
    HttpClient client = new HttpClient();

    String response = null;/*from   ww  w.  j  av a2 s.  c  o m*/
    while (response == null) { // follow redirects via 'refresh'
        if (url.startsWith("https://")) {
            registerEasyHttps();
        }
        if (url.contains("anonymous=true") == false) {
            url += url.contains("?") ? "&" : "?";
            url += "anonymous=true";
        }

        HttpMethod get = new GetMethod(url);
        try {
            client.executeMethod(get);

            String redirect = null;
            Header h = get.getResponseHeader("Refresh");
            if (h != null) {
                String s = h.getValue();
                int cut = s.indexOf("url=");
                if (cut >= 0) {
                    redirect = s.substring(cut + 4);
                }
            }

            if (redirect == null) {
                response = get.getResponseBodyAsString();
                log.debug("Job " + mrJobId + " get status check result.\n");
            } else {
                url = redirect;
                log.debug("Job " + mrJobId + " check redirect url " + url + ".\n");
            }
        } finally {
            get.releaseConnection();
        }
    }

    return response;
}

From source file:org.apache.kylin.tool.JobTaskCounterExtractor.java

private String getHttpResponse(String url) {
    HttpClient client = new HttpClient();
    String response = null;/*from w w  w. j  av a  2  s.co m*/
    while (response == null) {
        HttpMethod get = new GetMethod(url);
        try {
            get.addRequestHeader("accept", "application/json");
            client.executeMethod(get);
            response = get.getResponseBodyAsString();
        } catch (Exception e) {
            logger.warn("Failed to fetch http response" + e);
        } finally {
            get.releaseConnection();
        }
    }
    return response;
}

From source file:org.apache.maven.doxia.linkcheck.validation.OnlineHTTPLinkValidator.java

/** {@inheritDoc} */
public LinkValidationResult validateLink(LinkValidationItem lvi) {
    if (this.cl == null) {
        initHttpClient();/*  ww  w  .j  a  v  a  2  s .c  o  m*/
    }

    if (this.http.getHttpClientParameters() != null) {
        for (Map.Entry<Object, Object> entry : this.http.getHttpClientParameters().entrySet()) {
            if (entry.getValue() != null) {
                System.setProperty(entry.getKey().toString(), entry.getValue().toString());
            }
        }
    }

    // Some web servers don't allow the default user-agent sent by httpClient
    System.setProperty(HttpMethodParams.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    this.cl.getParams().setParameter(HttpMethodParams.USER_AGENT,
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");

    String link = lvi.getLink();
    String anchor = "";
    int idx = link.indexOf('#');
    if (idx != -1) {
        anchor = link.substring(idx + 1);
        link = link.substring(0, idx);
    }

    try {
        if (link.startsWith("/")) {
            if (getBaseURL() == null) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn("Cannot check link [" + link + "] in page [" + lvi.getSource()
                            + "], as no base URL has been set!");
                }

                return new LinkValidationResult(LinkcheckFileResult.WARNING_LEVEL, false,
                        "No base URL specified");
            }

            link = getBaseURL() + link;
        }

        HttpMethod hm = null;
        try {
            hm = checkLink(link, 0);
        } catch (Throwable t) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Received: [" + t + "] for [" + link + "] in page [" + lvi.getSource() + "]", t);
            }

            return new LinkValidationResult(LinkcheckFileResult.ERROR_LEVEL, false,
                    t.getClass().getName() + " : " + t.getMessage());
        }

        if (hm == null) {
            return new LinkValidationResult(LinkcheckFileResult.ERROR_LEVEL, false,
                    "Cannot retreive HTTP Status");
        }

        if (hm.getStatusCode() == HttpStatus.SC_OK) {
            // lets check if the anchor is present
            if (anchor.length() > 0) {
                String content = hm.getResponseBodyAsString();

                if (!Anchors.matchesAnchor(content, anchor)) {
                    return new HTTPLinkValidationResult(LinkcheckFileResult.VALID_LEVEL, false,
                            "Missing anchor '" + anchor + "'");
                }
            }
            return new HTTPLinkValidationResult(LinkcheckFileResult.VALID_LEVEL, true, hm.getStatusCode(),
                    hm.getStatusText());
        }

        String msg = "Received: [" + hm.getStatusCode() + "] for [" + link + "] in page [" + lvi.getSource()
                + "]";
        // If there's a redirection ... add a warning
        if (hm.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY
                || hm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY
                || hm.getStatusCode() == HttpStatus.SC_TEMPORARY_REDIRECT) {
            LOG.warn(msg);

            return new HTTPLinkValidationResult(LinkcheckFileResult.WARNING_LEVEL, true, hm.getStatusCode(),
                    hm.getStatusText());
        }

        LOG.debug(msg);

        return new HTTPLinkValidationResult(LinkcheckFileResult.ERROR_LEVEL, false, hm.getStatusCode(),
                hm.getStatusText());
    } catch (Throwable t) {
        String msg = "Received: [" + t + "] for [" + link + "] in page [" + lvi.getSource() + "]";
        if (LOG.isDebugEnabled()) {
            LOG.debug(msg, t);
        } else {
            LOG.error(msg);
        }

        return new LinkValidationResult(LinkcheckFileResult.ERROR_LEVEL, false, t.getMessage());
    } finally {
        System.getProperties().remove(HttpMethodParams.USER_AGENT);

        if (this.http.getHttpClientParameters() != null) {
            for (Map.Entry<Object, Object> entry : this.http.getHttpClientParameters().entrySet()) {
                if (entry.getValue() != null) {
                    System.getProperties().remove(entry.getKey().toString());
                }
            }
        }
    }
}

From source file:org.apache.ode.axis2.httpbinding.HttpClientHelper.java

/**
 * @param method/*w  w  w . j a  va 2  s  .  c  o  m*/
 * @param bodyIsXml if true the body will be parsed as xml else the body will be inserted as string
 * @return
 * @throws IOException
 */
public static Element prepareDetailsElement(HttpMethod method, boolean bodyIsXml) throws IOException {
    Document doc = DOMUtils.newDocument();
    Element detailsEl = doc.createElementNS(null, "details");
    Element statusLineEl = statusLineToElement(doc, method.getStatusLine());
    detailsEl.appendChild(statusLineEl);

    // set the body if any
    final InputStream bodyAsStream = method.getResponseBodyAsStream();
    if (bodyAsStream != null) {
        Element bodyEl = doc.createElementNS(null, "responseBody");
        detailsEl.appendChild(bodyEl);
        // first, try to parse the body as xml
        // if it fails, put it as string in the body element
        boolean exceptionDuringParsing = false;
        if (bodyIsXml) {
            try {
                Element parsedBodyEl = DOMUtils.parse(bodyAsStream).getDocumentElement();
                bodyEl.appendChild(parsedBodyEl);
            } catch (Exception e) {
                String errmsg = "Unable to parse the response body as xml. Body will be inserted as string.";
                if (log.isDebugEnabled())
                    log.debug(errmsg, e);
                exceptionDuringParsing = true;
            }
        }
        if (!bodyIsXml || exceptionDuringParsing) {
            bodyEl.setTextContent(method.getResponseBodyAsString());
        }
    }
    return detailsEl;
}

From source file:org.apache.ode.axis2.httpbinding.HttpHelper.java

/**
 * Build a "details" element that looks like this:
 *
 * @param method/*ww w  . j a va  2  s.  co m*/
 * @return
 * @throws IOException
 */
public static Element prepareDetailsElement(HttpMethod method) {
    Header h = method.getResponseHeader("Content-Type");
    String receivedType = h != null ? h.getValue() : null;
    boolean bodyIsXml = receivedType != null && HttpUtils.isXml(receivedType);

    Document doc = DOMUtils.newDocument();
    Element detailsEl = doc.createElementNS(null, "details");
    Element statusLineEl = statusLineToElement(doc, method.getStatusLine());
    detailsEl.appendChild(statusLineEl);

    // set the body if any
    try {
        final String body = method.getResponseBodyAsString();
        if (StringUtils.isNotEmpty(body)) {
            Element bodyEl = doc.createElementNS(null, "responseBody");
            detailsEl.appendChild(bodyEl);
            // first, try to parse the body as xml
            // if it fails, put it as string in the body element
            boolean exceptionDuringParsing = false;
            if (bodyIsXml) {
                try {
                    Element parsedBodyEl = DOMUtils.stringToDOM(body);
                    bodyEl.appendChild(doc.importNode(parsedBodyEl, true));
                } catch (Exception e) {
                    String errmsg = "Unable to parse the response body as xml. Body will be inserted as string.";
                    if (log.isDebugEnabled())
                        log.debug(errmsg, e);
                    exceptionDuringParsing = true;
                }
            }
            if (!bodyIsXml || exceptionDuringParsing) {
                bodyEl.setTextContent(body);
            }
        }
    } catch (IOException e) {
        if (log.isWarnEnabled())
            log.warn("Exception while loading response body", e);
    }
    return detailsEl;
}

From source file:org.apache.ode.axis2.httpbinding.HttpHelper.java

public static String responseToString(HttpMethod m) {
    StringBuilder sb = new StringBuilder(256);
    try {/*from ww w .  j  a va 2s. c  o  m*/
        sb.append("HTTP Response Details: \n").append(m.getName()).append(" ").append(m.getURI());
    } catch (URIException e) {
        // not that important
        if (log.isDebugEnabled())
            log.debug(e);
    }
    sb.append("\nStatus-Line: ").append(m.getStatusLine());
    Header[] headers = m.getResponseHeaders();
    if (headers.length != 0)
        sb.append("\nResponse Headers: ");
    for (int i = 0; i < headers.length; i++) {
        Header h = headers[i];
        sb.append("\n\t").append(h.getName()).append(": ").append(h.getValue());
    }
    try {
        if (StringUtils.isNotEmpty(m.getResponseBodyAsString())) {
            sb.append("\nResponse Entity:\n").append(m.getResponseBodyAsString());
        }
    } catch (IOException e) {
        log.error(e);
    }
    Header[] footers = m.getResponseFooters();
    if (footers.length != 0)
        sb.append("\nResponse Footers: ");
    for (int i = 0; i < footers.length; i++) {
        Header h = footers[i];
        sb.append("\n\t").append(h.getName()).append(": ").append(h.getValue());
    }
    return sb.toString();
}