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