Search in sources :

Example 86 with HttpMethod

use of org.apache.commons.httpclient.HttpMethod in project camel by apache.

the class HttpPollingConsumer method doReceive.

protected Exchange doReceive(int timeout) {
    Exchange exchange = endpoint.createExchange();
    HttpMethod method = createMethod(exchange);
    // set optional timeout in millis
    if (timeout > 0) {
        method.getParams().setSoTimeout(timeout);
    }
    try {
        // execute request
        int responseCode = httpClient.executeMethod(method);
        Object body = HttpHelper.readResponseBodyFromInputStream(method.getResponseBodyAsStream(), exchange);
        // lets store the result in the output message.
        Message message = exchange.getOut();
        message.setBody(body);
        // lets set the headers
        Header[] headers = method.getResponseHeaders();
        HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
        for (Header header : headers) {
            String name = header.getName();
            // mapping the content-type
            if (name.toLowerCase().equals("content-type")) {
                name = Exchange.CONTENT_TYPE;
            }
            String value = header.getValue();
            if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
                message.setHeader(name, value);
            }
        }
        message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
        message.setHeader(Exchange.HTTP_RESPONSE_TEXT, method.getStatusText());
        return exchange;
    } catch (IOException e) {
        throw new RuntimeCamelException(e);
    } finally {
        method.releaseConnection();
    }
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) Header(org.apache.commons.httpclient.Header) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 87 with HttpMethod

use of org.apache.commons.httpclient.HttpMethod in project camel by apache.

the class NettyHttpHeaderMaxSizeTest method testHttpHeaderMaxSizeFail.

@Test
public void testHttpHeaderMaxSizeFail() throws Exception {
    HttpClient client = new HttpClient();
    HttpMethod method = new PostMethod("http://localhost:" + getPort() + "/myapp/mytest");
    method.setRequestHeader("name", "12345678901234567890123456789012345678901234567890123456789012345678901234567890");
    client.executeMethod(method);
    assertEquals(404, method.getStatusCode());
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) HttpMethod(org.apache.commons.httpclient.HttpMethod) Test(org.junit.Test)

Example 88 with HttpMethod

use of org.apache.commons.httpclient.HttpMethod in project camel by apache.

the class NettyHttpHeaderMaxSizeTest method testHttpHeaderMaxSizeOk.

@Test
public void testHttpHeaderMaxSizeOk() throws Exception {
    HttpClient client = new HttpClient();
    HttpMethod method = new PostMethod("http://localhost:" + getPort() + "/myapp/mytest");
    method.setRequestHeader("name", "you");
    client.executeMethod(method);
    assertEquals(200, method.getStatusCode());
    assertEquals("Bye World", method.getResponseBodyAsString());
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) HttpMethod(org.apache.commons.httpclient.HttpMethod) Test(org.junit.Test)

Example 89 with HttpMethod

use of org.apache.commons.httpclient.HttpMethod in project camel by apache.

the class NettyHttpMapHeadersFalseTest method testHttpHeaderCase.

@Test
public void testHttpHeaderCase() throws Exception {
    HttpClient client = new HttpClient();
    HttpMethod method = new PostMethod("http://localhost:" + getPort() + "/myapp/mytest");
    method.setRequestHeader("clientHeader", "fooBAR");
    method.setRequestHeader("OTHER", "123");
    method.setRequestHeader("beer", "Carlsberg");
    client.executeMethod(method);
    assertEquals("Bye World", method.getResponseBodyAsString());
    assertEquals("aBc123", method.getResponseHeader("MyCaseHeader").getValue());
    assertEquals("456DEf", method.getResponseHeader("otherCaseHeader").getValue());
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) HttpMethod(org.apache.commons.httpclient.HttpMethod) Test(org.junit.Test)

Example 90 with HttpMethod

use of org.apache.commons.httpclient.HttpMethod in project camel by apache.

the class NettyHttpHeaderCaseTest method testHttpHeaderCase.

@Test
public void testHttpHeaderCase() throws Exception {
    HttpClient client = new HttpClient();
    HttpMethod method = new PostMethod("http://localhost:" + getPort() + "/myapp/mytest");
    method.setRequestHeader("clientHeader", "fooBAR");
    method.setRequestHeader("OTHER", "123");
    method.setRequestHeader("beer", "Carlsberg");
    client.executeMethod(method);
    assertEquals("Bye World", method.getResponseBodyAsString());
    assertEquals("aBc123", method.getResponseHeader("MyCaseHeader").getValue());
    assertEquals("456DEf", method.getResponseHeader("otherCaseHeader").getValue());
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) HttpMethod(org.apache.commons.httpclient.HttpMethod) Test(org.junit.Test)

Aggregations

HttpMethod (org.apache.commons.httpclient.HttpMethod)151 HttpClient (org.apache.commons.httpclient.HttpClient)99 GetMethod (org.apache.commons.httpclient.methods.GetMethod)95 InputStream (java.io.InputStream)61 IOException (java.io.IOException)43 ArrayList (java.util.ArrayList)30 HttpException (org.apache.commons.httpclient.HttpException)28 Map (java.util.Map)24 Test (org.junit.Test)23 Element (org.w3c.dom.Element)22 HashMap (java.util.HashMap)20 PostMethod (org.apache.commons.httpclient.methods.PostMethod)19 Header (org.apache.commons.httpclient.Header)17 List (java.util.List)14 NameValuePair (org.apache.commons.httpclient.NameValuePair)13 NodeList (org.w3c.dom.NodeList)12 FileInputStream (java.io.FileInputStream)10 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)10 SAXBuilder (org.jdom.input.SAXBuilder)10 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)9