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

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

Introduction

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

Prototype

@Override
public String getResponseBodyAsString() throws IOException 

Source Link

Document

Returns the response body of the HTTP method, if any, as a String .

Usage

From source file:org.jboss.soa.esb.actions.http.HttpAction.java

public Message process(final Message msg) throws ActionProcessingException {

    try {//from w  w  w  .  jav a 2s  .c  o  m
        final HttpClient client = new HttpClient();

        final Properties props = msg.getProperties();

        final String[] names = props.getNames();

        if (METHOD_DELETE.equals(method)) {
            final HttpMethodBase req = new DeleteMethod(uri);

            for (int i = 0; i < names.length; i++) {
                final Matcher headerMatcher = PATTERN_HEADER.matcher(names[i]);

                if (headerMatcher.find())
                    req.addRequestHeader(headerMatcher.group(1),
                            (String) props.getProperty(headerMatcher.group()));

            }

            client.executeMethod(req);

            final Header[] headers = req.getResponseHeaders();

            for (int i = 0; i < headers.length; i++)
                props.setProperty(PREFIX_HEADER + headers[i].getName(), headers[i].getValue());

            proxy.setPayload(msg, req.getResponseBodyAsString());
        } else if (METHOD_GET.equals(method)) {
            final HttpMethodBase req = new GetMethod(uri);

            final List<NameValuePair> paramList = new ArrayList<NameValuePair>();

            for (int i = 0; i < names.length; i++) {
                final Matcher headerMatcher = PATTERN_HEADER.matcher(names[i]);

                if (headerMatcher.find()) {
                    req.addRequestHeader(headerMatcher.group(1),
                            (String) props.getProperty(headerMatcher.group()));

                    continue;
                }

                final Matcher paramMatcher = PATTERN_PARAM.matcher(names[i]);

                if (paramMatcher.find())
                    paramList.add(new NameValuePair(paramMatcher.group(1),
                            (String) props.getProperty(paramMatcher.group())));

            }

            req.setQueryString(paramList.toArray(new NameValuePair[] {}));

            client.executeMethod(req);

            final Header[] headers = req.getResponseHeaders();

            for (int i = 0; i < headers.length; i++)
                props.setProperty(PREFIX_HEADER + headers[i].getName(), headers[i].getValue());

            proxy.setPayload(msg, req.getResponseBodyAsString());
        } else if (METHOD_POST.equals(method)) {
            final PostMethod req = new PostMethod(uri);

            for (int i = 0; i < names.length; i++) {
                final Matcher headerMatcher = PATTERN_HEADER.matcher(names[i]);

                if (headerMatcher.find()) {
                    req.addRequestHeader(headerMatcher.group(1),
                            (String) props.getProperty(headerMatcher.group()));

                    continue;
                }

                final Matcher paramMatcher = PATTERN_PARAM.matcher(names[i]);

                if (paramMatcher.find())
                    req.addParameter(new NameValuePair(paramMatcher.group(1),
                            (String) props.getProperty(paramMatcher.group())));

            }

            req.setRequestEntity(new StringRequestEntity((String) proxy.getPayload(msg), null, null));

            client.executeMethod(req);

            final Header[] headers = req.getResponseHeaders();

            for (int i = 0; i < headers.length; i++)
                props.setProperty(PREFIX_HEADER + headers[i].getName(), headers[i].getValue());

            proxy.setPayload(msg, req.getResponseBodyAsString());
        } else if (METHOD_PUT.equals(method)) {
            final EntityEnclosingMethod req = new PutMethod(uri);

            for (int i = 0; i < names.length; i++) {
                final Matcher headerMatcher = PATTERN_HEADER.matcher(names[i]);

                if (headerMatcher.find())
                    req.addRequestHeader(headerMatcher.group(1),
                            (String) props.getProperty(headerMatcher.group()));

            }

            req.setRequestEntity(new StringRequestEntity((String) proxy.getPayload(msg), null, null));

            client.executeMethod(req);

            final Header[] headers = req.getResponseHeaders();

            for (int i = 0; i < headers.length; i++)
                props.setProperty(PREFIX_HEADER + headers[i].getName(), headers[i].getValue());

            proxy.setPayload(msg, req.getResponseBodyAsString());
        }

    } catch (final Exception e) {
        throw new ActionProcessingException("Can't process message", e);
    }

    return msg;
}

From source file:org.jboss.test.jpa.test.AbstractWebJPATest.java

public String accessURL(String url) throws Exception {
    HttpClient httpConn = new HttpClient();
    HttpMethodBase request = new GetMethod(baseURL + url);
    log.debug("RequestURI: " + request.getURI());
    int responseCode = httpConn.executeMethod(request);
    String response = request.getStatusText();
    log.debug("responseCode=" + responseCode + ", response=" + response);
    String content = request.getResponseBodyAsString();
    log.debug(content);//ww w.j ava 2s . c om
    assertEquals(HttpURLConnection.HTTP_OK, responseCode);
    return content;
}

From source file:org.jboss.test.util.web.HttpUtils.java

public static HttpMethodBase accessURL(URL url, String realm, int expectedHttpCode, Header[] hdrs, int type)
        throws Exception {
    HttpClient httpConn = new HttpClient();
    HttpMethodBase request = createMethod(url, type);
    int hdrCount = hdrs != null ? hdrs.length : 0;
    for (int n = 0; n < hdrCount; n++)
        request.addRequestHeader(hdrs[n]);
    try {/*w ww. j a  v a  2 s.  c om*/
        log.debug("Connecting to: " + url);
        String userInfo = url.getUserInfo();
        if (userInfo != null) {
            UsernamePasswordCredentials auth = new UsernamePasswordCredentials(userInfo);
            httpConn.getState().setCredentials(realm, url.getHost(), auth);
        }
        log.debug("RequestURI: " + request.getURI());
        int responseCode = httpConn.executeMethod(request);
        String response = request.getStatusText();
        log.debug("responseCode=" + responseCode + ", response=" + response);
        String content = request.getResponseBodyAsString();
        log.debug(content);
        // Validate that we are seeing the requested response code
        if (responseCode != expectedHttpCode) {
            throw new IOException("Expected reply code:" + expectedHttpCode + ", actual=" + responseCode);
        }
    } catch (IOException e) {
        throw e;
    }
    return request;
}

From source file:org.jboss.test.web.test.RootContextUnitTestCase.java

/**
 *  Access http://localhost//*from ww  w  .jav a 2  s  .c  o m*/
 */
private String hitRootContext(String deploymentUnit) throws Exception {
    // We need to suspend the default root context when running these tests
    ObjectName root = new ObjectName("jboss.web.deployment:war=/ROOT");
    MBeanServerConnection connection = getServer();
    connection.invoke(root, "stop", null, null);
    try {
        deploy(deploymentUnit);
        try {
            HttpMethodBase request = new GetMethod(baseURL);
            client.executeMethod(request);

            String responseBody = request.getResponseBodyAsString();
            if (responseBody == null) {
                throw new Exception("Unable to get response from server.");
            }

            return responseBody;
        } finally {
            undeploy(deploymentUnit);
        }
    } finally {
        connection.invoke(root, "start", null, null);
    }
}

From source file:org.mule.transport.http.reliability.InboundMessageLossTestCase.java

@Test
public void testNoException() throws Exception {
    HttpMethodBase request = createRequest(getBaseUri() + "/noException");
    int status = httpClient.executeMethod(request);
    assertEquals(HttpConstants.SC_OK, status);
    assertEquals("Here you go", request.getResponseBodyAsString());
}

From source file:org.mule.transport.http.reliability.InboundMessageLossTestCase.java

@Test
public void testTransformerException() throws Exception {
    HttpMethodBase request = createRequest(getBaseUri() + "/transformerException");
    int status = httpClient.executeMethod(request);
    assertEquals(HttpConstants.SC_INTERNAL_SERVER_ERROR, status);
    assertTrue(request.getResponseBodyAsString().contains("Failure"));
}

From source file:org.mule.transport.http.reliability.InboundMessageLossTestCase.java

@Test
public void testHandledTransformerException() throws Exception {
    HttpMethodBase request = createRequest(getBaseUri() + "/handledTransformerException");
    int status = httpClient.executeMethod(request);
    assertEquals(HttpConstants.SC_OK, status);
    assertTrue(request.getResponseBodyAsString().contains("Success"));
}

From source file:org.mule.transport.http.reliability.InboundMessageLossTestCase.java

@Test
public void testNotHandledTransformerException() throws Exception {
    HttpMethodBase request = createRequest(getBaseUri() + "/notHandledTransformerException");
    int status = httpClient.executeMethod(request);
    assertEquals(HttpConstants.SC_INTERNAL_SERVER_ERROR, status);
    assertTrue(request.getResponseBodyAsString().contains("Bad news"));
}

From source file:org.mule.transport.http.reliability.InboundMessageLossTestCase.java

@Test
public void testRouterException() throws Exception {
    HttpMethodBase request = createRequest(getBaseUri() + "/routerException");
    int status = httpClient.executeMethod(request);
    assertEquals(HttpConstants.SC_INTERNAL_SERVER_ERROR, status);
    assertTrue(request.getResponseBodyAsString().contains("Failure"));
}

From source file:org.mule.transport.http.reliability.InboundMessageLossTestCase.java

@Test
public void testComponentException() throws Exception {
    HttpMethodBase request = createRequest(getBaseUri() + "/componentException");
    int status = httpClient.executeMethod(request);
    // Component exception occurs after the SEDA queue for an asynchronous request, but since 
    // this request is synchronous, the failure propagates back to the client.
    assertEquals(HttpConstants.SC_INTERNAL_SERVER_ERROR, status);
    assertTrue(request.getResponseBodyAsString().contains("exception"));
}