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.net.URI;
020    import java.net.URISyntaxException;
021    import javax.servlet.http.HttpServletRequest;
022    import javax.servlet.http.HttpServletResponse;
023    
024    import org.apache.camel.Exchange;
025    import org.apache.camel.ExchangePattern;
026    import org.apache.camel.PollingConsumer;
027    import org.apache.camel.Producer;
028    import org.apache.camel.impl.DefaultPollingEndpoint;
029    import org.apache.camel.spi.HeaderFilterStrategy;
030    import org.apache.camel.spi.HeaderFilterStrategyAware;
031    import org.apache.camel.util.ObjectHelper;
032    import org.apache.commons.httpclient.HttpClient;
033    import org.apache.commons.httpclient.HttpConnectionManager;
034    import org.apache.commons.httpclient.params.HttpClientParams;
035    import org.apache.commons.logging.Log;
036    import org.apache.commons.logging.LogFactory;
037    
038    /**
039     * Represents a <a href="http://camel.apache.org/http.html">HTTP endpoint</a>
040     *
041     * @version $Revision: 782923 $
042     */
043    public class HttpEndpoint extends DefaultPollingEndpoint implements HeaderFilterStrategyAware {
044    
045        private static final transient Log LOG = LogFactory.getLog(HttpEndpoint.class);
046        private HeaderFilterStrategy headerFilterStrategy = new HttpHeaderFilterStrategy();
047        private HttpBinding binding;
048        private HttpComponent component;
049        private URI httpUri;
050        private HttpClientParams clientParams;
051        private HttpClientConfigurer httpClientConfigurer;
052        private HttpConnectionManager httpConnectionManager;
053        private boolean throwExceptionOnFailure = true;
054    
055        public HttpEndpoint() {
056        }
057    
058        public HttpEndpoint(String endPointURI, HttpComponent component, URI httpURI, HttpConnectionManager httpConnectionManager) throws URISyntaxException {
059            this(endPointURI, component, httpURI, new HttpClientParams(), httpConnectionManager, null);
060        }
061    
062        public HttpEndpoint(String endPointURI, HttpComponent component, URI httpURI, HttpClientParams clientParams,
063                            HttpConnectionManager httpConnectionManager, HttpClientConfigurer clientConfigurer) throws URISyntaxException {
064            super(endPointURI, component);
065            this.component = component;
066            this.httpUri = httpURI;
067            this.clientParams = clientParams;
068            this.httpClientConfigurer = clientConfigurer;
069            this.httpConnectionManager = httpConnectionManager;
070        }
071    
072        public Producer createProducer() throws Exception {
073            return new HttpProducer(this);
074        }
075    
076        public PollingConsumer createPollingConsumer() throws Exception {
077            return new HttpPollingConsumer(this);
078        }
079    
080        public Exchange createExchange(ExchangePattern pattern) {
081            return new HttpExchange(this, pattern);
082        }
083    
084        public HttpExchange createExchange(HttpServletRequest request, HttpServletResponse response) {
085            return new HttpExchange(this, request, response);
086        }
087    
088        /**
089         * Factory method used by producers and consumers to create a new {@link HttpClient} instance
090         */
091        public HttpClient createHttpClient() {
092            ObjectHelper.notNull(clientParams, "clientParams");
093            ObjectHelper.notNull(httpConnectionManager, "httpConnectionManager");
094    
095            HttpClient answer = new HttpClient(getClientParams());
096    
097            // configure http proxy if defined as system property
098            // http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html
099            if (System.getProperty("http.proxyHost") != null && System.getProperty("http.proxyPort") != null) {
100                String host = System.getProperty("http.proxyHost");
101                int port = Integer.parseInt(System.getProperty("http.proxyPort"));
102                if (LOG.isDebugEnabled()) {
103                    LOG.debug("Java System Property http.proxyHost and http.proxyPort detected. Using http proxy host: "
104                            + host + " port: " + port);
105                }
106                answer.getHostConfiguration().setProxy(host, port);
107            }
108    
109            answer.setHttpConnectionManager(httpConnectionManager);
110            HttpClientConfigurer configurer = getHttpClientConfigurer();
111            if (configurer != null) {
112                configurer.configureHttpClient(answer);
113            }
114            return answer;
115        }
116    
117        public void connect(HttpConsumer consumer) throws Exception {
118            component.connect(consumer);
119        }
120    
121        public void disconnect(HttpConsumer consumer) throws Exception {
122            component.disconnect(consumer);
123        }
124    
125        public boolean isLenientProperties() {
126            // true to allow dynamic URI options to be configured and passed to external system for eg. the HttpProducer
127            return true;
128        }
129    
130        public boolean isSingleton() {
131            return true;
132        }
133    
134    
135        // Properties
136        //-------------------------------------------------------------------------
137    
138        /**
139         * Provide access to the client parameters used on new {@link HttpClient} instances
140         * used by producers or consumers of this endpoint.
141         */
142        public HttpClientParams getClientParams() {
143            return clientParams;
144        }
145    
146        /**
147         * Provide access to the client parameters used on new {@link HttpClient} instances
148         * used by producers or consumers of this endpoint.
149         */
150        public void setClientParams(HttpClientParams clientParams) {
151            this.clientParams = clientParams;
152        }
153    
154        public HttpClientConfigurer getHttpClientConfigurer() {
155            return httpClientConfigurer;
156        }
157    
158        /**
159         * Register a custom configuration strategy for new {@link HttpClient} instances
160         * created by producers or consumers such as to configure authentication mechanisms etc
161         *
162         * @param httpClientConfigurer the strategy for configuring new {@link HttpClient} instances
163         */
164        public void setHttpClientConfigurer(HttpClientConfigurer httpClientConfigurer) {
165            this.httpClientConfigurer = httpClientConfigurer;
166        }
167    
168        public HttpBinding getBinding() {
169            if (binding == null) {
170                binding = new DefaultHttpBinding(getHeaderFilterStrategy());
171            }
172            return binding;
173        }
174    
175        public void setBinding(HttpBinding binding) {
176            this.binding = binding;
177        }
178    
179        public String getPath() {
180            return httpUri.getPath();
181        }
182    
183        public int getPort() {
184            if (httpUri.getPort() == -1) {
185                if ("https".equals(getProtocol())) {
186                    return 443;
187                } else {
188                    return 80;
189                }
190            }
191            return httpUri.getPort();
192        }
193    
194        public String getProtocol() {
195            return httpUri.getScheme();
196        }
197    
198        public URI getHttpUri() {
199            return httpUri;
200        }
201    
202        public void setHttpUri(URI httpUri) {
203            this.httpUri = httpUri;
204        }
205    
206        public HttpConnectionManager getHttpConnectionManager() {
207            return httpConnectionManager;
208        }
209    
210        public void setHttpConnectionManager(HttpConnectionManager httpConnectionManager) {
211            this.httpConnectionManager = httpConnectionManager;
212        }
213    
214        public HeaderFilterStrategy getHeaderFilterStrategy() {
215            return headerFilterStrategy;
216        }
217    
218        public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
219            this.headerFilterStrategy = headerFilterStrategy;
220        }
221    
222        public boolean isThrowExceptionOnFailure() {
223            return throwExceptionOnFailure;
224        }
225    
226        public void setThrowExceptionOnFailure(boolean throwExceptionOnFailure) {
227            this.throwExceptionOnFailure = throwExceptionOnFailure;
228        }
229    }