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.net.URISyntaxException; 021 022 import javax.servlet.http.HttpServletRequest; 023 import javax.servlet.http.HttpServletResponse; 024 025 import org.apache.camel.ExchangePattern; 026 import org.apache.camel.PollingConsumer; 027 import org.apache.camel.Producer; 028 import org.apache.camel.impl.DefaultPollingEndpoint; 029 import org.apache.camel.spi.HeaderFilterStrategy; 030 import org.apache.commons.httpclient.HttpClient; 031 import org.apache.commons.httpclient.HttpConnectionManager; 032 import org.apache.commons.httpclient.params.HttpClientParams; 033 import org.apache.commons.logging.Log; 034 import org.apache.commons.logging.LogFactory; 035 036 /** 037 * Represents a <a href="http://activemq.apache.org/camel/http.html">HTTP 038 * endpoint</a> 039 * 040 * @version $Revision: 833423 $ 041 */ 042 public class HttpEndpoint extends DefaultPollingEndpoint<HttpExchange> { 043 044 private static final transient Log LOG = LogFactory.getLog(HttpEndpoint.class); 045 private HttpBinding binding; 046 private HttpComponent component; 047 private URI httpUri; 048 private HttpClientParams clientParams; 049 private HttpClientConfigurer httpClientConfigurer; 050 private HttpConnectionManager httpConnectionManager; 051 052 public HttpEndpoint(String endPointURI, HttpComponent component, URI httpURI, HttpConnectionManager httpConnectionManager) throws URISyntaxException { 053 this(endPointURI, component, httpURI, new HttpClientParams(), httpConnectionManager, null); 054 } 055 056 public HttpEndpoint(String endPointURI, HttpComponent component, URI httpURI, HttpClientParams clientParams, 057 HttpConnectionManager httpConnectionManager, HttpClientConfigurer clientConfigurer) throws URISyntaxException { 058 super(endPointURI, component); 059 this.component = component; 060 this.httpUri = httpURI; 061 this.clientParams = clientParams; 062 this.httpClientConfigurer = clientConfigurer; 063 this.httpConnectionManager = httpConnectionManager; 064 } 065 066 public Producer<HttpExchange> createProducer() throws Exception { 067 return new HttpProducer(this); 068 } 069 070 @Override 071 public PollingConsumer<HttpExchange> createPollingConsumer() throws Exception { 072 return new HttpPollingConsumer(this); 073 } 074 075 public HttpExchange createExchange(ExchangePattern pattern) { 076 return new HttpExchange(this, pattern); 077 } 078 079 public HttpExchange createExchange(HttpServletRequest request, HttpServletResponse response) { 080 return new HttpExchange(this, request, response); 081 } 082 083 /** 084 * Factory method used by producers and consumers to create a new {@link HttpClient} instance 085 */ 086 public HttpClient createHttpClient() { 087 HttpClient answer = new HttpClient(getClientParams()); 088 089 // configure http proxy if defined as system property 090 // http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html 091 if (System.getProperty("http.proxyHost") != null && System.getProperty("http.proxyPort") != null) { 092 String host = System.getProperty("http.proxyHost"); 093 int port = Integer.parseInt(System.getProperty("http.proxyPort")); 094 if (LOG.isDebugEnabled()) { 095 LOG.debug("Java System Property http.proxyHost and http.proxyPort detected. Using http proxy host: " 096 + host + " port: " + port); 097 } 098 answer.getHostConfiguration().setProxy(host, port); 099 } 100 101 answer.setHttpConnectionManager(httpConnectionManager); 102 HttpClientConfigurer configurer = getHttpClientConfigurer(); 103 if (configurer != null) { 104 configurer.configureHttpClient(answer); 105 } 106 return answer; 107 } 108 109 public void connect(HttpConsumer consumer) throws Exception { 110 component.connect(consumer); 111 } 112 113 public void disconnect(HttpConsumer consumer) throws Exception { 114 component.disconnect(consumer); 115 } 116 117 @Override 118 public boolean isLenientProperties() { 119 // true to allow dynamic URI options to be configured and passed to external system for eg. the HttpProducer 120 return true; 121 } 122 123 // Properties 124 //------------------------------------------------------------------------- 125 126 127 /** 128 * Provide access to the client parameters used on new {@link HttpClient} instances 129 * used by producers or consumers of this endpoint. 130 */ 131 public HttpClientParams getClientParams() { 132 return clientParams; 133 } 134 135 /** 136 * Provide access to the client parameters used on new {@link HttpClient} instances 137 * used by producers or consumers of this endpoint. 138 */ 139 public void setClientParams(HttpClientParams clientParams) { 140 this.clientParams = clientParams; 141 } 142 143 public HttpClientConfigurer getHttpClientConfigurer() { 144 return httpClientConfigurer; 145 } 146 147 /** 148 * Register a custom configuration strategy for new {@link HttpClient} instances 149 * created by producers or consumers such as to configure authentication mechanisms etc 150 * 151 * @param httpClientConfigurer the strategy for configuring new {@link HttpClient} instances 152 */ 153 public void setHttpClientConfigurer(HttpClientConfigurer httpClientConfigurer) { 154 this.httpClientConfigurer = httpClientConfigurer; 155 } 156 157 public HttpBinding getBinding() { 158 if (binding == null) { 159 binding = new DefaultHttpBinding(getHeaderFilterStrategy()); 160 } 161 return binding; 162 } 163 164 public HeaderFilterStrategy getHeaderFilterStrategy() { 165 return component.getHeaderFilterStrategy(); 166 } 167 168 public void setBinding(HttpBinding binding) { 169 this.binding = binding; 170 } 171 172 public boolean isSingleton() { 173 return true; 174 } 175 176 public String getPath() { 177 return httpUri.getPath(); 178 } 179 180 public int getPort() { 181 if (httpUri.getPort() == -1) { 182 if ("https".equals(getProtocol())) { 183 return 443; 184 } else { 185 return 80; 186 } 187 } 188 return httpUri.getPort(); 189 } 190 191 public String getProtocol() { 192 return httpUri.getScheme(); 193 } 194 195 public URI getHttpUri() { 196 return httpUri; 197 } 198 199 public void setHttpUri(URI httpUri) { 200 this.httpUri = httpUri; 201 } 202 }