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