Search in sources :

Example 41 with Header

use of org.apache.commons.httpclient.Header 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 42 with Header

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

the class HttpProducer method populateHttpOperationFailedException.

protected Exception populateHttpOperationFailedException(Exchange exchange, HttpMethod method, int responseCode) throws IOException, ClassNotFoundException, URISyntaxException {
    Exception answer;
    String uri = method.getURI().toString();
    String statusText = method.getStatusLine() != null ? method.getStatusLine().getReasonPhrase() : null;
    Map<String, String> headers = extractResponseHeaders(method.getResponseHeaders());
    // handle cookies
    if (getEndpoint().getCookieHandler() != null) {
        Map<String, List<String>> m = new HashMap<String, List<String>>();
        for (Entry<String, String> e : headers.entrySet()) {
            m.put(e.getKey(), Collections.singletonList(e.getValue()));
        }
        getEndpoint().getCookieHandler().storeCookies(exchange, new URI(method.getURI().getEscapedURI()), m);
    }
    Object responseBody = extractResponseBody(method, exchange, getEndpoint().isIgnoreResponseBody());
    if (transferException && responseBody != null && responseBody instanceof Exception) {
        // if the response was a serialized exception then use that
        return (Exception) responseBody;
    }
    // make a defensive copy of the response body in the exception so its detached from the cache
    String copy = null;
    if (responseBody != null) {
        copy = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, responseBody);
    }
    if (responseCode >= 300 && responseCode < 400) {
        String redirectLocation;
        Header locationHeader = method.getResponseHeader("location");
        if (locationHeader != null) {
            redirectLocation = locationHeader.getValue();
            answer = new HttpOperationFailedException(uri, responseCode, statusText, redirectLocation, headers, copy);
        } else {
            // no redirect location
            answer = new HttpOperationFailedException(uri, responseCode, statusText, null, headers, copy);
        }
    } else {
        // internal server error (error code 500)
        answer = new HttpOperationFailedException(uri, responseCode, statusText, null, headers, copy);
    }
    return answer;
}
Also used : Header(org.apache.commons.httpclient.Header) HashMap(java.util.HashMap) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) ArrayList(java.util.ArrayList) List(java.util.List) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) CamelExchangeException(org.apache.camel.CamelExchangeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 43 with Header

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

the class HttpProducer method populateResponse.

protected void populateResponse(Exchange exchange, HttpMethod method, Message in, HeaderFilterStrategy strategy, int responseCode) throws IOException, ClassNotFoundException, URISyntaxException {
    //We just make the out message is not create when extractResponseBody throws exception,
    Object response = extractResponseBody(method, exchange, getEndpoint().isIgnoreResponseBody());
    Message answer = exchange.getOut();
    answer.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
    answer.setHeader(Exchange.HTTP_RESPONSE_TEXT, method.getStatusText());
    answer.setBody(response);
    // propagate HTTP response headers
    Header[] headers = method.getResponseHeaders();
    Map<String, List<String>> m = new HashMap<String, List<String>>();
    for (Header header : headers) {
        String name = header.getName();
        String value = header.getValue();
        m.put(name, Collections.singletonList(value));
        if (name.toLowerCase().equals("content-type")) {
            name = Exchange.CONTENT_TYPE;
            exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.getCharsetNameFromContentType(value));
        }
        // use http helper to extract parameter value as it may contain multiple values
        Object extracted = HttpHelper.extractHttpParameterValue(value);
        if (strategy != null && !strategy.applyFilterToExternalHeaders(name, extracted, exchange)) {
            HttpHelper.appendHeader(answer.getHeaders(), name, extracted);
        }
    }
    // handle cookies
    if (getEndpoint().getCookieHandler() != null) {
        getEndpoint().getCookieHandler().storeCookies(exchange, new URI(method.getURI().getEscapedURI()), m);
    }
    // filter the http protocol headers
    if (getEndpoint().isCopyHeaders()) {
        MessageHelper.copyHeaders(exchange.getIn(), answer, httpProtocolHeaderFilterStrategy, false);
    }
}
Also used : Message(org.apache.camel.Message) Header(org.apache.commons.httpclient.Header) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) URI(java.net.URI)

Example 44 with Header

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

the class EnableCORSTest method testCORSenabled.

@Test
public void testCORSenabled() throws Exception {
    HttpClient httpclient = new HttpClient();
    HttpMethod httpMethod = new GetMethod("http://localhost:" + getPort2() + "/test2");
    httpMethod.addRequestHeader("Origin", "http://localhost:9000");
    httpMethod.addRequestHeader("Referer", "http://localhost:9000");
    int status = httpclient.executeMethod(httpMethod);
    assertEquals("Get a wrong response status", 200, status);
    Header responseHeader = httpMethod.getResponseHeader("Access-Control-Allow-Credentials");
    assertTrue("CORS not enabled", Boolean.valueOf(responseHeader.getValue()));
}
Also used : Header(org.apache.commons.httpclient.Header) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod) Test(org.junit.Test)

Example 45 with Header

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

the class EnableCORSTest method testCORSdisabled.

@Test
public void testCORSdisabled() throws Exception {
    HttpClient httpclient = new HttpClient();
    HttpMethod httpMethod = new GetMethod("http://localhost:" + getPort() + "/test1");
    httpMethod.addRequestHeader("Origin", "http://localhost:9000");
    httpMethod.addRequestHeader("Referer", "http://localhost:9000");
    int status = httpclient.executeMethod(httpMethod);
    assertEquals("Get a wrong response status", 200, status);
    Header responseHeader = httpMethod.getResponseHeader("Access-Control-Allow-Credentials");
    assertNull("Access-Control-Allow-Credentials HEADER should not be set", responseHeader);
}
Also used : Header(org.apache.commons.httpclient.Header) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod) Test(org.junit.Test)

Aggregations

Header (org.apache.commons.httpclient.Header)93 GetMethod (org.apache.commons.httpclient.methods.GetMethod)23 Test (org.junit.Test)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 IOException (java.io.IOException)20 HttpClient (org.apache.commons.httpclient.HttpClient)20 NameValuePair (org.apache.commons.httpclient.NameValuePair)18 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 ArrayList (java.util.ArrayList)16 InputStream (java.io.InputStream)14 HttpException (org.apache.commons.httpclient.HttpException)13 PutMethod (org.apache.commons.httpclient.methods.PutMethod)10 Account (com.zimbra.cs.account.Account)9 ServiceException (com.zimbra.common.service.ServiceException)6 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)6 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)6 Pair (com.zimbra.common.util.Pair)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 HashMap (java.util.HashMap)5 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)5