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    
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpServletResponse;
024    
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.commons.httpclient.HttpClient;
031    import org.apache.commons.httpclient.HttpConnectionManager;
032    import org.apache.commons.httpclient.params.HttpClientParams;
033    
034    /**
035     * Represents a <a href="http://activemq.apache.org/camel/http.html">HTTP
036     * endpoint</a>
037     *
038     * @version $Revision: 719673 $
039     */
040    public class HttpEndpoint extends DefaultPollingEndpoint<HttpExchange> {
041    
042        private HttpBinding binding;
043        private HttpComponent component;
044        private URI httpUri;
045        private HttpClientParams clientParams;
046        private HttpClientConfigurer httpClientConfigurer;
047        private HttpConnectionManager httpConnectionManager;
048    
049        public HttpEndpoint(String endPointURI, HttpComponent component, URI httpURI, HttpConnectionManager httpConnectionManager) throws URISyntaxException {
050            this(endPointURI, component, httpURI, new HttpClientParams(), httpConnectionManager, null);
051        }
052    
053        public HttpEndpoint(String endPointURI, HttpComponent component, URI httpURI, HttpClientParams clientParams,
054                            HttpConnectionManager httpConnectionManager, HttpClientConfigurer clientConfigurer) throws URISyntaxException {
055            super(endPointURI, component);
056            this.component = component;
057            this.httpUri = httpURI;
058            this.clientParams = clientParams;
059            this.httpClientConfigurer = clientConfigurer;
060            this.httpConnectionManager = httpConnectionManager;
061        }
062    
063        public Producer<HttpExchange> createProducer() throws Exception {
064            return new HttpProducer(this);
065        }
066    
067        @Override
068        public PollingConsumer<HttpExchange> createPollingConsumer() throws Exception {
069            return new HttpPollingConsumer(this);
070        }
071    
072        public HttpExchange createExchange(ExchangePattern pattern) {
073            return new HttpExchange(this, pattern);
074        }
075    
076        public HttpExchange createExchange(HttpServletRequest request, HttpServletResponse response) {
077            return new HttpExchange(this, request, response);
078        }
079    
080        /**
081         * Factory method used by producers and consumers to create a new {@link HttpClient} instance
082         */
083        public HttpClient createHttpClient() {
084            HttpClient answer = new HttpClient(getClientParams());
085            answer.setHttpConnectionManager(httpConnectionManager);
086            HttpClientConfigurer configurer = getHttpClientConfigurer();
087            if (configurer != null) {
088                configurer.configureHttpClient(answer);
089            }
090            return answer;
091        }
092    
093        public void connect(HttpConsumer consumer) throws Exception {
094            component.connect(consumer);
095        }
096    
097        public void disconnect(HttpConsumer consumer) throws Exception {
098            component.disconnect(consumer);
099        }
100    
101        @Override
102        public boolean isLenientProperties() {
103            // true to allow dynamic URI options to be configured and passed to external system for eg. the HttpProducer
104            return true;
105        }
106    
107        // Properties
108        //-------------------------------------------------------------------------
109    
110    
111        /**
112         * Provide access to the client parameters used on new {@link HttpClient} instances
113         * used by producers or consumers of this endpoint.
114         */
115        public HttpClientParams getClientParams() {
116            return clientParams;
117        }
118    
119        /**
120         * Provide access to the client parameters used on new {@link HttpClient} instances
121         * used by producers or consumers of this endpoint.
122         */
123        public void setClientParams(HttpClientParams clientParams) {
124            this.clientParams = clientParams;
125        }
126    
127        public HttpClientConfigurer getHttpClientConfigurer() {
128            return httpClientConfigurer;
129        }
130    
131        /**
132         * Register a custom configuration strategy for new {@link HttpClient} instances
133         * created by producers or consumers such as to configure authentication mechanisms etc
134         *
135         * @param httpClientConfigurer the strategy for configuring new {@link HttpClient} instances
136         */
137        public void setHttpClientConfigurer(HttpClientConfigurer httpClientConfigurer) {
138            this.httpClientConfigurer = httpClientConfigurer;
139        }
140    
141        public HttpBinding getBinding() {
142            if (binding == null) {
143                binding = new DefaultHttpBinding(getHeaderFilterStrategy());
144            }
145            return binding;
146        }
147    
148        public HeaderFilterStrategy getHeaderFilterStrategy() {
149            return component.getHeaderFilterStrategy();
150        }
151    
152        public void setBinding(HttpBinding binding) {
153            this.binding = binding;
154        }
155    
156        public boolean isSingleton() {
157            return true;
158        }
159    
160        public String getPath() {
161            return httpUri.getPath();
162        }
163    
164        public int getPort() {
165            if (httpUri.getPort() == -1) {
166                if ("https".equals(getProtocol())) {
167                    return 443;
168                } else {
169                    return 80;
170                }
171            }
172            return httpUri.getPort();
173        }
174    
175        public String getProtocol() {
176            return httpUri.getScheme();
177        }
178    
179        public URI getHttpUri() {
180            return httpUri;
181        }
182    }