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    import java.io.InputStream;
021    
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Message;
024    import org.apache.camel.RuntimeCamelException;
025    import org.apache.camel.component.http.helper.LoadingByteArrayOutputStream;
026    import org.apache.camel.impl.PollingConsumerSupport;
027    import org.apache.camel.spi.HeaderFilterStrategy;
028    import org.apache.commons.httpclient.Header;
029    import org.apache.commons.httpclient.HttpClient;
030    import org.apache.commons.httpclient.HttpMethod;
031    import org.apache.commons.httpclient.methods.GetMethod;
032    import org.apache.commons.io.IOUtils;
033    
034    /**
035     * A polling HTTP consumer which by default performs a GET
036     *
037     * @version $Revision: 774740 $
038     */
039    public class HttpPollingConsumer extends PollingConsumerSupport {
040        private final HttpEndpoint endpoint;
041        private HttpClient httpClient;
042    
043        public HttpPollingConsumer(HttpEndpoint endpoint) {
044            super(endpoint);
045            this.endpoint = endpoint;
046            httpClient = endpoint.createHttpClient();
047        }
048    
049        public Exchange receive() {
050            return receiveNoWait();
051        }
052    
053        public Exchange receive(long timeout) {
054            return receiveNoWait();
055        }
056    
057        public Exchange receiveNoWait() {
058            HttpExchange exchange = (HttpExchange) endpoint.createExchange();
059            HttpMethod method = createMethod();
060    
061            try {
062                int responseCode = httpClient.executeMethod(method);
063                // lets store the result in the output message.
064                LoadingByteArrayOutputStream bos = new LoadingByteArrayOutputStream();
065                InputStream is = method.getResponseBodyAsStream();
066                try {
067                    IOUtils.copy(is, bos);
068                    bos.flush();
069                } finally {
070                    is.close();
071                }
072                Message message = exchange.getIn();
073                message.setBody(bos.createInputStream());
074    
075                // lets set the headers
076                Header[] headers = method.getResponseHeaders();
077                HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
078                for (Header header : headers) {
079                    String name = header.getName();
080                    String value = header.getValue();
081                    if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
082                        message.setHeader(name, value);
083                    }
084                }
085            
086                message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
087                return exchange;
088            } catch (IOException e) {
089                throw new RuntimeCamelException(e);
090            } finally {
091                method.releaseConnection();
092            }
093        }
094    
095        // Properties
096        //-------------------------------------------------------------------------
097        public HttpClient getHttpClient() {
098            return httpClient;
099        }
100    
101        public void setHttpClient(HttpClient httpClient) {
102            this.httpClient = httpClient;
103        }
104    
105        // Implementation methods
106        //-------------------------------------------------------------------------
107        protected HttpMethod createMethod() {
108            String uri = endpoint.getEndpointUri();
109            return new GetMethod(uri);
110        }
111    
112        protected void doStart() throws Exception {
113        }
114    
115        protected void doStop() throws Exception {
116        }
117    }