001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.http;
018
019 import java.io.IOException;
020
021 import org.apache.camel.Exchange;
022 import org.apache.camel.Message;
023 import org.apache.camel.RuntimeCamelException;
024 import org.apache.camel.component.http.helper.HttpHelper;
025 import org.apache.camel.impl.PollingConsumerSupport;
026 import org.apache.camel.spi.HeaderFilterStrategy;
027 import org.apache.commons.httpclient.Header;
028 import org.apache.commons.httpclient.HttpClient;
029 import org.apache.commons.httpclient.HttpMethod;
030 import org.apache.commons.httpclient.methods.GetMethod;
031
032 /**
033 * A polling HTTP consumer which by default performs a GET
034 *
035 * @version $Revision: 1053064 $
036 */
037 public class HttpPollingConsumer extends PollingConsumerSupport {
038 private final HttpEndpoint endpoint;
039 private HttpClient httpClient;
040
041 public HttpPollingConsumer(HttpEndpoint endpoint) {
042 super(endpoint);
043 this.endpoint = endpoint;
044 this.httpClient = endpoint.createHttpClient();
045 }
046
047 public Exchange receive() {
048 return doReceive(-1);
049 }
050
051 public Exchange receive(long timeout) {
052 return doReceive((int) timeout);
053 }
054
055 public Exchange receiveNoWait() {
056 return doReceive(-1);
057 }
058
059 protected Exchange doReceive(int timeout) {
060 Exchange exchange = endpoint.createExchange();
061 HttpMethod method = createMethod();
062
063 // set optional timeout in millis
064 if (timeout > 0) {
065 method.getParams().setSoTimeout(timeout);
066 }
067
068 try {
069 // execute request
070 int responseCode = httpClient.executeMethod(method);
071
072 Object body = HttpHelper.readResponseBodyFromInputStream(method.getResponseBodyAsStream(), exchange);
073
074 // lets store the result in the output message.
075 Message message = exchange.getOut();
076 message.setBody(body);
077
078 // lets set the headers
079 Header[] headers = method.getResponseHeaders();
080 HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
081 for (Header header : headers) {
082 String name = header.getName();
083 // mapping the content-type
084 if (name.toLowerCase().equals("content-type")) {
085 name = Exchange.CONTENT_TYPE;
086 }
087 String value = header.getValue();
088 if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
089 message.setHeader(name, value);
090 }
091 }
092 message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
093
094 return exchange;
095 } catch (IOException e) {
096 throw new RuntimeCamelException(e);
097 } finally {
098 method.releaseConnection();
099 }
100 }
101
102 // Properties
103 //-------------------------------------------------------------------------
104 public HttpClient getHttpClient() {
105 return httpClient;
106 }
107
108 public void setHttpClient(HttpClient httpClient) {
109 this.httpClient = httpClient;
110 }
111
112 // Implementation methods
113 //-------------------------------------------------------------------------
114 protected HttpMethod createMethod() {
115 String uri = endpoint.getEndpointUri();
116 return new GetMethod(uri);
117 }
118
119 protected void doStart() throws Exception {
120 }
121
122 protected void doStop() throws Exception {
123 }
124 }