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