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.CamelServlet;
027 import org.apache.camel.component.http.HttpBinding;
028 import org.apache.camel.component.http.HttpClientConfigurer;
029 import org.apache.camel.component.http.HttpComponent;
030 import org.apache.camel.component.http.HttpConsumer;
031 import org.apache.camel.util.CastUtils;
032 import org.apache.camel.util.IntrospectionSupport;
033 import org.apache.camel.util.ObjectHelper;
034 import org.apache.camel.util.URISupport;
035 import org.apache.camel.util.UnsafeUriCharactersEncoder;
036 import org.apache.commons.httpclient.HttpConnectionManager;
037 import org.apache.commons.httpclient.params.HttpClientParams;
038
039 public class ServletComponent extends HttpComponent {
040
041 private CamelServlet camelServlet;
042
043 public void setCamelServlet(CamelServlet servlet) {
044 camelServlet = servlet;
045 }
046
047 public CamelServlet getCamelServlet(String servletName) {
048 CamelServlet answer;
049 if (camelServlet == null) {
050 answer = CamelHttpTransportServlet.getCamelServlet(servletName);
051 } else {
052 answer = camelServlet;
053 }
054 if (answer == null) {
055 throw new IllegalArgumentException("Cannot find the deployed servlet, please configure the ServletComponent"
056 + " or configure a org.apache.camel.component.servlet.CamelHttpTransportServlet servlet in web.xml ");
057 }
058 return answer;
059 }
060
061 @Override
062 protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
063 uri = uri.startsWith("servlet:") ? remaining : uri;
064
065 HttpClientParams params = new HttpClientParams();
066 IntrospectionSupport.setProperties(params, parameters, "httpClient.");
067
068 // create the configurer to use for this endpoint
069 final Set<AuthMethod> authMethods = new LinkedHashSet<AuthMethod>();
070 HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, authMethods);
071
072 // must extract well known parameters before we create the endpoint
073 HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBindingRef", HttpBinding.class);
074 Boolean matchOnUriPrefix = getAndRemoveParameter(parameters, "matchOnUriPrefix", Boolean.class);
075
076 // restructure uri to be based on the parameters left as we dont want to include the Camel internal options
077 URI httpUri = URISupport.createRemainingURI(new URI(UnsafeUriCharactersEncoder.encode(uri)), CastUtils.cast(parameters));
078 uri = httpUri.toString();
079
080 ServletEndpoint endpoint = createServletEndpoint(uri, this, httpUri, params, getHttpConnectionManager(), configurer);
081 setEndpointHeaderFilterStrategy(endpoint);
082
083 // prefer to use endpoint configured over component configured
084 if (binding == null) {
085 // fallback to component configured
086 binding = getHttpBinding();
087 }
088 if (binding != null) {
089 endpoint.setBinding(binding);
090 }
091 if (matchOnUriPrefix != null) {
092 endpoint.setMatchOnUriPrefix(matchOnUriPrefix);
093 }
094
095 setProperties(endpoint, parameters);
096 return endpoint;
097 }
098
099 protected ServletEndpoint createServletEndpoint(String endpointUri,
100 ServletComponent component, URI httpUri, HttpClientParams params,
101 HttpConnectionManager httpConnectionManager,
102 HttpClientConfigurer clientConfigurer) throws Exception {
103 return new ServletEndpoint(endpointUri, component, httpUri, params,
104 httpConnectionManager, clientConfigurer);
105 }
106
107 public void connect(HttpConsumer consumer) throws Exception {
108 ServletEndpoint endpoint = (ServletEndpoint) consumer.getEndpoint();
109 CamelServlet servlet = getCamelServlet(endpoint.getServletName());
110 ObjectHelper.notNull(servlet, "CamelServlet");
111 servlet.connect(consumer);
112 }
113
114 public void disconnect(HttpConsumer consumer) throws Exception {
115 ServletEndpoint endpoint = (ServletEndpoint) consumer.getEndpoint();
116 CamelServlet servlet = getCamelServlet(endpoint.getServletName());
117 ObjectHelper.notNull(servlet, "CamelServlet");
118 servlet.disconnect(consumer);
119 }
120
121 }