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.servlet;
018    
019    import java.net.URI;
020    import java.util.LinkedHashSet;
021    import java.util.Map;
022    import java.util.Set;
023    
024    import org.apache.camel.Endpoint;
025    import org.apache.camel.component.http.AuthMethod;
026    import org.apache.camel.component.http.HttpBinding;
027    import org.apache.camel.component.http.HttpClientConfigurer;
028    import org.apache.camel.component.http.HttpComponent;
029    import org.apache.camel.component.http.HttpConsumer;
030    import org.apache.camel.util.CastUtils;
031    import org.apache.camel.util.IntrospectionSupport;
032    import org.apache.camel.util.URISupport;
033    import org.apache.camel.util.UnsafeUriCharactersEncoder;
034    import org.apache.commons.httpclient.HttpConnectionManager;
035    import org.apache.commons.httpclient.params.HttpClientParams;
036    
037    public class ServletComponent extends HttpComponent {
038    
039        private String servletName = "CamelServlet";
040        private HttpRegistry httpRegistry;
041    
042        public String getServletName() {
043            return servletName;
044        }
045    
046        public void setServletName(String servletName) {
047            this.servletName = servletName;
048        }
049    
050        public void setHttpRegistry(HttpRegistry httpRegistry) {
051            this.httpRegistry = httpRegistry;
052        }
053    
054        @Override
055        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
056            if (httpRegistry == null) {
057                httpRegistry = DefaultHttpRegistry.getSingletonHttpRegistry();
058            }
059    
060            HttpClientParams params = new HttpClientParams();
061            IntrospectionSupport.setProperties(params, parameters, "httpClient.");
062    
063            // create the configurer to use for this endpoint
064            final Set<AuthMethod> authMethods = new LinkedHashSet<AuthMethod>();
065            HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, authMethods);
066    
067            // must extract well known parameters before we create the endpoint
068            Boolean throwExceptionOnFailure = getAndRemoveParameter(parameters, "throwExceptionOnFailure", Boolean.class);
069            Boolean transferException = getAndRemoveParameter(parameters, "transferException", Boolean.class);
070            Boolean bridgeEndpoint = getAndRemoveParameter(parameters, "bridgeEndpoint", Boolean.class);
071            HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBindingRef", HttpBinding.class);
072            Boolean matchOnUriPrefix = getAndRemoveParameter(parameters, "matchOnUriPrefix", Boolean.class);
073            String servletName = getAndRemoveParameter(parameters, "servletName", String.class, getServletName());
074    
075            // restructure uri to be based on the parameters left as we dont want to include the Camel internal options
076            URI httpUri = URISupport.createRemainingURI(new URI(UnsafeUriCharactersEncoder.encode(uri)), CastUtils.cast(parameters));
077    
078            ServletEndpoint endpoint = createServletEndpoint(uri, this, httpUri, params, getHttpConnectionManager(), configurer);
079            endpoint.setServletName(servletName);
080            setEndpointHeaderFilterStrategy(endpoint);
081    
082            // prefer to use endpoint configured over component configured
083            if (binding == null) {
084                // fallback to component configured
085                binding = getHttpBinding();
086            }
087            if (binding != null) {
088                endpoint.setBinding(binding);
089            }
090            // should we use an exception for failed error codes?
091            if (throwExceptionOnFailure != null) {
092                endpoint.setThrowExceptionOnFailure(throwExceptionOnFailure);
093            }
094            // should we transfer exception as serialized object
095            if (transferException != null) {
096                endpoint.setTransferException(transferException);
097            }
098            if (bridgeEndpoint != null) {
099                endpoint.setBridgeEndpoint(bridgeEndpoint);
100            }
101            if (matchOnUriPrefix != null) {
102                endpoint.setMatchOnUriPrefix(matchOnUriPrefix);
103            }
104    
105            setProperties(endpoint, parameters);
106            return endpoint;
107        }
108    
109        /**
110         * Strategy to create the servlet endpoint.
111         */
112        protected ServletEndpoint createServletEndpoint(String endpointUri, ServletComponent component, URI httpUri, HttpClientParams params,
113                                                        HttpConnectionManager httpConnectionManager, HttpClientConfigurer clientConfigurer) throws Exception {
114            return new ServletEndpoint(endpointUri, component, httpUri, params, httpConnectionManager, clientConfigurer);
115        }
116    
117        @Override
118        public void connect(HttpConsumer consumer) throws Exception {
119            httpRegistry.register(consumer);
120        }
121    
122        @Override
123        public void disconnect(HttpConsumer consumer) throws Exception {
124            httpRegistry.unregister(consumer);
125        }
126    
127    }