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.http4;
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.http4.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.http.Header;
030    import org.apache.http.HttpEntity;
031    import org.apache.http.HttpResponse;
032    import org.apache.http.client.HttpClient;
033    import org.apache.http.client.methods.HttpGet;
034    import org.apache.http.client.methods.HttpRequestBase;
035    import org.apache.http.params.HttpConnectionParams;
036    
037    /**
038     * A polling HTTP consumer which by default performs a GET
039     *
040     * @version $Revision: 947575 $
041     */
042    public class HttpPollingConsumer extends PollingConsumerSupport {
043        private final HttpEndpoint endpoint;
044        private HttpClient httpClient;
045    
046        public HttpPollingConsumer(HttpEndpoint endpoint) {
047            super(endpoint);
048            this.endpoint = endpoint;
049            this.httpClient = endpoint.getHttpClient();
050        }
051    
052        public Exchange receive() {
053            return doReceive(-1);
054        }
055    
056        public Exchange receive(long timeout) {
057            return doReceive((int) timeout);
058        }
059    
060        public Exchange receiveNoWait() {
061            return doReceive(-1);
062        }
063    
064        protected Exchange doReceive(int timeout) {
065            Exchange exchange = endpoint.createExchange();
066            HttpRequestBase method = createMethod();
067    
068            // set optional timeout in millis
069            if (timeout > 0) {
070                HttpConnectionParams.setSoTimeout(method.getParams(), timeout);
071            }
072    
073            HttpEntity responeEntity = null;
074            try {
075                HttpResponse response = httpClient.execute(method);
076                int responseCode = response.getStatusLine().getStatusCode();
077                responeEntity = response.getEntity();
078                // lets store the result in the output message.
079                LoadingByteArrayOutputStream bos = new LoadingByteArrayOutputStream();
080                InputStream is = responeEntity.getContent();
081    
082                try {
083                    IOHelper.copy(is, bos);
084                    bos.flush();
085                } finally {
086                    IOHelper.close(is, "input stream", null);
087                }
088                Message message = exchange.getIn();
089                message.setBody(bos.createInputStream());
090    
091                // lets set the headers
092                Header[] headers = response.getAllHeaders();
093                HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
094                for (Header header : headers) {
095                    String name = header.getName();
096                    String value = header.getValue();
097                    if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
098                        message.setHeader(name, value);
099                    }
100                }
101    
102                message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
103                return exchange;
104            } catch (IOException e) {
105                throw new RuntimeCamelException(e);
106            } finally {
107                if (responeEntity != null) {
108                    try {
109                        responeEntity.consumeContent();
110                    } catch (IOException e) {
111                        // nothing what we can do
112                    }
113                }
114            }
115        }
116    
117        // Properties
118        //-------------------------------------------------------------------------
119    
120        public HttpClient getHttpClient() {
121            return httpClient;
122        }
123    
124        public void setHttpClient(HttpClient httpClient) {
125            this.httpClient = httpClient;
126        }
127    
128        // Implementation methods
129        //-------------------------------------------------------------------------
130    
131        protected HttpRequestBase createMethod() {
132            String uri = endpoint.getEndpointUri();
133            return new HttpGet(uri);
134        }
135    
136        protected void doStart() throws Exception {
137        }
138    
139        protected void doStop() throws Exception {
140        }
141    }