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.zdevra.guice.mvc.security.webprincipal.WebPrincipalControllerTest.java

@Test
public void shouldShowAnnonymousUserName() throws IOException, ServletException {

    HttpMethod method = doRequest("http://localhost:9191/auth/invalidate");
    method = doRequest("http://localhost:9191/secure/anonPrincipal");

    int code = method.getStatusCode();
    System.out.println("code:" + code);
    assertThat(code, is(HttpServletResponse.SC_OK));

    String responseBodyAsString = method.getResponseBodyAsString();
    assertThat(responseBodyAsString, is("Annonymous"));
}

From source file:org.zdevra.guice.mvc.security.webprincipal.WebPrincipalControllerTest.java

@Test
public void shouldShowAuthenticatedUserName() throws IOException, ServletException {

    HttpMethod method = doRequest("http://localhost:9191/auth/invalidate");
    method = doRequest("http://localhost:9191/auth/authenticate");
    method = doRequest("http://localhost:9191/secure/authPrincipal");

    int code = method.getStatusCode();
    System.out.println("code:" + code);
    assertThat(code, is(HttpServletResponse.SC_OK));

    String responseBodyAsString = method.getResponseBodyAsString();
    assertThat(responseBodyAsString, is("SimpleName"));
}

From source file:org.zdevra.guice.mvc.security.webprincipal.WebPrincipalControllerTest.java

@Test
public void shouldProviderWebPrincipalAsRequestParameter() throws IOException, ServletException {

    HttpMethod method = doRequest("http://localhost:9191/auth/invalidate");
    method = doRequest("http://localhost:9191/auth/authenticate");
    method = doRequest("http://localhost:9191/secure/authPrincipalParam");

    int code = method.getStatusCode();
    System.out.println("code:" + code);
    assertThat(code, is(HttpServletResponse.SC_OK));

    String responseBodyAsString = method.getResponseBodyAsString();
    assertThat(responseBodyAsString, is("SimpleName"));
}

From source file:smartrics.rest.client.RestClientImpl.java

/**
 * See {@link smartrics.rest.client.RestClient#execute(java.lang.String, smartrics.rest.client.RestRequest)}
 */// w ww  .java  2 s  . c o  m
public RestResponse execute(String hostAddr, final RestRequest request) {
    if (request == null || !request.isValid())
        throw new IllegalArgumentException("Invalid request " + request);
    if (request.getTransactionId() == null)
        request.setTransactionId(Long.valueOf(System.currentTimeMillis()));
    LOG.debug("request: {}", request);
    HttpMethod m = createHttpClientMethod(request);
    configureHttpMethod(m, hostAddr, request);
    RestResponse resp = new RestResponse();
    resp.setTransactionId(request.getTransactionId());
    resp.setResource(request.getResource());
    try {
        client.executeMethod(m);
        for (Header h : m.getResponseHeaders()) {
            resp.addHeader(h.getName(), h.getValue());
        }
        resp.setStatusCode(m.getStatusCode());
        resp.setStatusText(m.getStatusText());
        resp.setBody(m.getResponseBodyAsString());
        resp.setRawBody(m.getResponseBody());
    } catch (HttpException e) {
        String message = "Http call failed for protocol failure";
        throw new IllegalStateException(message, e);
    } catch (IOException e) {
        String message = "Http call failed for IO failure";
        throw new IllegalStateException(message, e);
    } finally {
        m.releaseConnection();
    }
    LOG.debug("response: {}", resp);
    return resp;
}

From source file:uk.co.firstzero.webdav.Common.java

/**
 * Runs a WEBDAV command//from w  w w. j a  v a 2  s .co  m
 * @param client The HTTP client transport to use
 * @param method The method to be executed
 * @return True/False based on execution
 * @throws IOException
 */
public static boolean executeMethod(HttpClient client, HttpMethod method) throws IOException {
    client.executeMethod(method);
    int statusCode = method.getStatusCode();
    logger.trace("executeMethod - statusCode - " + statusCode);

    boolean result = method.getStatusCode() == HttpURLConnection.HTTP_OK;
    logger.debug("executeMethod - result - " + result);
    logger.debug(method.getStatusCode() + " " + method.getStatusText() + " " + method.getResponseBodyAsString()
            + " " + Arrays.toString(method.getResponseHeaders()));

    return result;
}

From source file:uk.co.firstzero.webdav.Common.java

/**
 * Used for checking if a file exists//from  w w  w  .j  a v a 2s  .com
 * @param client    The client to execute the method
 * @param method    The method to be executed
 * @param ignoreHTTPNOTFOUND  Used to flag if the HTTP_NOT_FOUND error has to be ignored, if not the IOException raised will be thrown
 * @return  Returns if the execution has been successful
 * @throws IOException
 */
public static boolean executeMethod(HttpClient client, HttpMethod method, boolean ignoreHTTPNOTFOUND)
        throws IOException {
    try {
        client.executeMethod(method);
    } catch (IOException e) {
        //If it is not 404 - throw exception, otherwise
        if (method.getStatusCode() != HttpURLConnection.HTTP_NOT_FOUND)
            throw e;
    }
    int statusCode = method.getStatusCode();
    logger.trace("executeMethod - statusCode - " + statusCode);

    boolean result = (method.getStatusCode() == HttpURLConnection.HTTP_OK);
    logger.debug("executeMethod - result - " + result);
    logger.debug(method.getStatusCode() + " " + method.getStatusText() + " " + method.getResponseBodyAsString()
            + " " + Arrays.toString(method.getResponseHeaders()));

    return result;
}

From source file:velo.resource.operationControllers.GenericHttpClientSpmlController.java

private List<Response> invokeHttpMethods(HttpMethods<HttpMethod> hmList) throws AdapterException {
    List<Response> responses = new ArrayList<Response>();
    for (HttpMethod currMethod : hmList) {
        Response r = new Response();
        try {// w  ww . ja va 2s  . com
            r.setDescription(currMethod.getResponseBodyAsString());
            String responseCode = String.valueOf(getAdapter().executeMethod(currMethod));
            String responseText = currMethod.getResponseBodyAsString();
            r.setCode(responseCode);
            r.setText(responseText);

            responses.add(r);
            log.trace("Printing response per HTTP METHOD execution: " + currMethod.getResponseBodyAsString());
        } catch (AdapterException e) {
            log.error("An error has occured while trying to invoke an HttpMethod: " + e.toString());
            throw e;
        } catch (IOException ie) {
            log.error("An IO Exeption has occured: " + ie.toString());
        }
    }

    return responses;
}