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.util.Map;
021    
022    import org.apache.camel.Endpoint;
023    import org.apache.camel.HeaderFilterStrategyAware;
024    import org.apache.camel.ResolveEndpointFailedException;
025    import org.apache.camel.impl.DefaultComponent;
026    import org.apache.camel.spi.HeaderFilterStrategy;
027    import org.apache.camel.util.CamelContextHelper;
028    import org.apache.camel.util.IntrospectionSupport;
029    import org.apache.camel.util.URISupport;
030    import org.apache.commons.httpclient.HttpConnectionManager;
031    import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
032    import org.apache.commons.httpclient.params.HttpClientParams;
033    
034    /**
035     * Defines the <a href="http://activemq.apache.org/camel/http.html">HTTP
036     * Component</a>
037     *
038     * @version $Revision: 833364 $
039     */
040    public class HttpComponent extends DefaultComponent<HttpExchange> implements HeaderFilterStrategyAware {
041        protected HttpClientConfigurer httpClientConfigurer;
042        protected HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
043        protected HeaderFilterStrategy headerFilterStrategy;
044        protected HttpBinding httpBinding;
045    
046        public HttpComponent() {
047            this.setHeaderFilterStrategy(new HttpHeaderFilterStrategy());
048        }
049        
050        /**
051         * Connects the URL specified on the endpoint to the specified processor.
052         *
053         * @param  consumer the consumer
054         * @throws Exception can be thrown
055         */
056        public void connect(HttpConsumer consumer) throws Exception {
057        }
058    
059        /**
060         * Disconnects the URL specified on the endpoint from the specified processor.
061         *
062         * @param  consumer the consumer
063         * @throws Exception can be thrown
064         */
065        public void disconnect(HttpConsumer consumer) throws Exception {
066        }
067    
068        /** 
069         * Setting http binding and http client configurer according to the parameters
070         * Also setting the BasicAuthenticationHttpClientConfigurer if the username 
071         * and password option are not null.
072         * 
073         * @param parameters the map of parameters 
074         * 
075         */
076        protected void configureParameters(Map parameters) {
077            // lookup http binding in registry if provided
078            String ref = getAndRemoveParameter(parameters, "httpBindingRef", String.class);
079            if (ref != null) {
080                httpBinding = CamelContextHelper.mandatoryLookup(getCamelContext(), ref, HttpBinding.class);
081            }
082            
083            // lookup http client front configurer in the registry if provided
084            ref = getAndRemoveParameter(parameters, "httpClientConfigurerRef", String.class);
085            if (ref != null) {
086                httpClientConfigurer = CamelContextHelper.mandatoryLookup(getCamelContext(), ref, HttpClientConfigurer.class);
087            }
088            
089            // check the user name and password for basic authentication
090            String username = getAndRemoveParameter(parameters, "username", String.class);
091            String password = getAndRemoveParameter(parameters, "password", String.class);
092            String domain = getAndRemoveParameter(parameters, "domain", String.class);
093            String host = getAndRemoveParameter(parameters, "host", String.class);
094            if (username != null && password != null) {
095                
096                httpClientConfigurer = CompositeHttpConfigurer.combineConfigurers(
097                    httpClientConfigurer, 
098                    new BasicAuthenticationHttpClientConfigurer(username, password, domain, host));
099            }
100            
101            // check the proxy details for proxy configuration
102            String proxyHost = getAndRemoveParameter(parameters, "proxyHost", String.class);
103            Integer proxyPort = getAndRemoveParameter(parameters, "proxyPort", Integer.class);
104            if (proxyHost != null && proxyPort != null) {
105                String proxyUsername = getAndRemoveParameter(parameters, "proxyUsername", String.class);
106                String proxyPassword = getAndRemoveParameter(parameters, "proxyPassword", String.class);
107                String proxyDomain = getAndRemoveParameter(parameters, "proxyDomain", String.class);
108                String proxyNtHost = getAndRemoveParameter(parameters, "proxyNtHost", String.class);
109                if (proxyUsername != null && proxyPassword != null) {
110                    httpClientConfigurer = CompositeHttpConfigurer.combineConfigurers(
111                        httpClientConfigurer, new ProxyHttpClientConfigurer(proxyHost, proxyPort, proxyUsername, proxyPassword, proxyDomain, proxyNtHost));
112                } else {
113                    httpClientConfigurer = CompositeHttpConfigurer.combineConfigurers(
114                        httpClientConfigurer, new ProxyHttpClientConfigurer(proxyHost, proxyPort));
115                }
116            }
117        }
118        
119        @Override
120        protected Endpoint createEndpoint(String uri, String remaining, Map parameters)
121            throws Exception {
122    
123            // http client can be configured from URI options
124            HttpClientParams params = new HttpClientParams();
125            IntrospectionSupport.setProperties(params, parameters, "httpClient.");        
126            
127            configureParameters(parameters);
128    
129            // restructure uri to be based on the parameters left as we dont want to include the Camel internal options
130            URI httpUri = URISupport.createRemainingURI(new URI(uri), parameters);
131            uri = httpUri.toString();
132    
133            // validate http uri that end-user did not duplicate the http part that can be a common error
134            String part = httpUri.getSchemeSpecificPart();
135            if (part != null) {
136                part = part.toLowerCase();
137                if (part.startsWith("//http//") || part.startsWith("//https//")) {
138                    throw new ResolveEndpointFailedException(uri,
139                            "The uri part is not configured correctly. You have duplicated the http(s) protocol.");
140                }
141            }
142    
143            HttpEndpoint endpoint = new HttpEndpoint(uri, this, httpUri, params, httpConnectionManager, httpClientConfigurer);
144            if (httpBinding != null) {
145                endpoint.setBinding(httpBinding);
146            }
147            return endpoint;
148        }    
149       
150        @Override
151        protected boolean useIntrospectionOnEndpoint() {
152            return false;
153        }
154    
155        public HttpClientConfigurer getHttpClientConfigurer() {
156            return httpClientConfigurer;
157        }
158    
159        public void setHttpClientConfigurer(HttpClientConfigurer httpClientConfigurer) {
160            this.httpClientConfigurer = httpClientConfigurer;
161        }
162    
163        public HttpConnectionManager getHttpConnectionManager() {
164            return httpConnectionManager;
165        }
166    
167        public void setHttpConnectionManager(HttpConnectionManager httpConnectionManager) {
168            this.httpConnectionManager = httpConnectionManager;
169        }
170    
171        public HeaderFilterStrategy getHeaderFilterStrategy() {
172            return headerFilterStrategy;
173        }
174    
175        public void setHeaderFilterStrategy(HeaderFilterStrategy strategy) {
176            headerFilterStrategy = strategy;
177        }
178    
179        public HttpBinding getHttpBinding() {
180            return httpBinding;
181        }
182    
183        public void setHttpBinding(HttpBinding httpBinding) {
184            this.httpBinding = httpBinding;
185        }
186    }