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.Map; 021 022 import org.apache.camel.Endpoint; 023 import org.apache.camel.component.http.CamelServlet; 024 import org.apache.camel.component.http.HttpComponent; 025 import org.apache.camel.component.http.HttpConsumer; 026 import org.apache.camel.util.IntrospectionSupport; 027 import org.apache.camel.util.ObjectHelper; 028 import org.apache.camel.util.URISupport; 029 import org.apache.camel.util.UnsafeUriCharactersEncoder; 030 import org.apache.commons.httpclient.params.HttpClientParams; 031 032 public class ServletComponent extends HttpComponent { 033 034 private CamelServlet camelServlet; 035 036 public void setCamelServlet(CamelServlet servlet) { 037 camelServlet = servlet; 038 } 039 040 041 public CamelServlet getCamelServlet(String servletName) { 042 CamelServlet answer = null; 043 if (camelServlet == null) { 044 answer = CamelHttpTransportServlet.getCamelServlet(servletName); 045 } else { 046 answer = camelServlet; 047 } 048 if (answer == null) { 049 throw new IllegalArgumentException("Can't find the deployied servlet, please set the ServletComponent with it" 050 + " or delopy a CamelHttpTransportServlet int the web container"); 051 } 052 return answer; 053 } 054 055 @Override 056 protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception { 057 uri = uri.startsWith("servlet:") ? remaining : uri; 058 059 HttpClientParams params = new HttpClientParams(); 060 IntrospectionSupport.setProperties(params, parameters, "httpClient."); 061 062 // configure regular parameters 063 configureParameters(parameters); 064 065 // restructure uri to be based on the parameters left as we dont want to include the Camel internal options 066 URI httpUri = URISupport.createRemainingURI(new URI(UnsafeUriCharactersEncoder.encode(uri)), parameters); 067 uri = httpUri.toString(); 068 069 ServletEndpoint result = new ServletEndpoint(uri, this, httpUri, params, getHttpConnectionManager(), httpClientConfigurer); 070 if (httpBinding != null) { 071 result.setBinding(httpBinding); 072 } 073 setEndpointHeaderFilterStrategy(result); 074 setProperties(result, parameters); 075 return result; 076 } 077 078 079 080 public void connect(HttpConsumer consumer) throws Exception { 081 ServletEndpoint endpoint = (ServletEndpoint) consumer.getEndpoint(); 082 CamelServlet servlet = getCamelServlet(endpoint.getServletName()); 083 ObjectHelper.notNull(servlet, "CamelServlet"); 084 servlet.connect(consumer); 085 } 086 087 public void disconnect(HttpConsumer consumer) throws Exception { 088 ServletEndpoint endpoint = (ServletEndpoint) consumer.getEndpoint(); 089 CamelServlet servlet = getCamelServlet(endpoint.getServletName()); 090 ObjectHelper.notNull(servlet, "CamelServlet"); 091 servlet.disconnect(consumer); 092 } 093 094 }