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