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 import org.apache.camel.PollingConsumer; 022 import org.apache.camel.Producer; 023 import org.apache.camel.impl.DefaultPollingEndpoint; 024 import org.apache.camel.spi.HeaderFilterStrategy; 025 import org.apache.camel.spi.HeaderFilterStrategyAware; 026 import org.apache.camel.util.ObjectHelper; 027 import org.apache.commons.httpclient.HttpClient; 028 import org.apache.commons.httpclient.HttpConnectionManager; 029 import org.apache.commons.httpclient.params.HttpClientParams; 030 import org.apache.commons.logging.Log; 031 import org.apache.commons.logging.LogFactory; 032 033 /** 034 * Represents a <a href="http://camel.apache.org/http.html">HTTP endpoint</a> 035 * 036 * @version $Revision: 895572 $ 037 */ 038 public class HttpEndpoint extends DefaultPollingEndpoint implements HeaderFilterStrategyAware { 039 040 private static final transient Log LOG = LogFactory.getLog(HttpEndpoint.class); 041 private HeaderFilterStrategy headerFilterStrategy = new HttpHeaderFilterStrategy(); 042 private HttpBinding binding; 043 private HttpComponent component; 044 private URI httpUri; 045 private HttpClientParams clientParams; 046 private HttpClientConfigurer httpClientConfigurer; 047 private HttpConnectionManager httpConnectionManager; 048 private boolean throwExceptionOnFailure = true; 049 private boolean bridgeEndpoint; 050 private boolean matchOnUriPrefix; 051 private boolean chunked = true; 052 053 public HttpEndpoint() { 054 } 055 056 public HttpEndpoint(String endPointURI, HttpComponent component, URI httpURI) throws URISyntaxException { 057 this(endPointURI, component, httpURI, null); 058 } 059 060 public HttpEndpoint(String endPointURI, HttpComponent component, URI httpURI, HttpConnectionManager httpConnectionManager) throws URISyntaxException { 061 this(endPointURI, component, httpURI, new HttpClientParams(), httpConnectionManager, null); 062 } 063 064 public HttpEndpoint(String endPointURI, HttpComponent component, URI httpURI, HttpClientParams clientParams, 065 HttpConnectionManager httpConnectionManager, HttpClientConfigurer clientConfigurer) throws URISyntaxException { 066 super(endPointURI, component); 067 this.component = component; 068 this.httpUri = httpURI; 069 this.clientParams = clientParams; 070 this.httpClientConfigurer = clientConfigurer; 071 this.httpConnectionManager = httpConnectionManager; 072 } 073 074 public Producer createProducer() throws Exception { 075 return new HttpProducer(this); 076 } 077 078 public PollingConsumer createPollingConsumer() throws Exception { 079 return new HttpPollingConsumer(this); 080 } 081 082 /** 083 * Factory method used by producers and consumers to create a new {@link HttpClient} instance 084 */ 085 public HttpClient createHttpClient() { 086 ObjectHelper.notNull(clientParams, "clientParams"); 087 ObjectHelper.notNull(httpConnectionManager, "httpConnectionManager"); 088 089 HttpClient answer = new HttpClient(getClientParams()); 090 091 // configure http proxy from camelContext 092 if (ObjectHelper.isNotEmpty(getCamelContext().getProperties().get("http.proxyHost")) && ObjectHelper.isNotEmpty(getCamelContext().getProperties().get("http.proxyPort"))) { 093 String host = getCamelContext().getProperties().get("http.proxyHost"); 094 int port = Integer.parseInt(getCamelContext().getProperties().get("http.proxyPort")); 095 if (LOG.isDebugEnabled()) { 096 LOG.debug("CamelContext properties http.proxyHost and http.proxyPort detected. Using http proxy host: " 097 + host + " port: " + port); 098 } 099 answer.getHostConfiguration().setProxy(host, port); 100 } 101 102 answer.setHttpConnectionManager(httpConnectionManager); 103 HttpClientConfigurer configurer = getHttpClientConfigurer(); 104 if (configurer != null) { 105 configurer.configureHttpClient(answer); 106 } 107 return answer; 108 } 109 110 public void connect(HttpConsumer consumer) throws Exception { 111 component.connect(consumer); 112 } 113 114 public void disconnect(HttpConsumer consumer) throws Exception { 115 component.disconnect(consumer); 116 } 117 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 public boolean isSingleton() { 124 return true; 125 } 126 127 128 // Properties 129 //------------------------------------------------------------------------- 130 131 /** 132 * Provide access to the client parameters used on new {@link HttpClient} instances 133 * used by producers or consumers of this endpoint. 134 */ 135 public HttpClientParams getClientParams() { 136 return clientParams; 137 } 138 139 /** 140 * Provide access to the client parameters used on new {@link HttpClient} instances 141 * used by producers or consumers of this endpoint. 142 */ 143 public void setClientParams(HttpClientParams clientParams) { 144 this.clientParams = clientParams; 145 } 146 147 public HttpClientConfigurer getHttpClientConfigurer() { 148 return httpClientConfigurer; 149 } 150 151 /** 152 * Register a custom configuration strategy for new {@link HttpClient} instances 153 * created by producers or consumers such as to configure authentication mechanisms etc 154 * 155 * @param httpClientConfigurer the strategy for configuring new {@link HttpClient} instances 156 */ 157 public void setHttpClientConfigurer(HttpClientConfigurer httpClientConfigurer) { 158 this.httpClientConfigurer = httpClientConfigurer; 159 } 160 161 public HttpBinding getBinding() { 162 if (binding == null) { 163 binding = new DefaultHttpBinding(getHeaderFilterStrategy()); 164 } 165 return binding; 166 } 167 168 public void setBinding(HttpBinding binding) { 169 this.binding = binding; 170 } 171 172 public String getPath() { 173 return httpUri.getPath(); 174 } 175 176 public int getPort() { 177 if (httpUri.getPort() == -1) { 178 if ("https".equals(getProtocol())) { 179 return 443; 180 } else { 181 return 80; 182 } 183 } 184 return httpUri.getPort(); 185 } 186 187 public String getProtocol() { 188 return httpUri.getScheme(); 189 } 190 191 public URI getHttpUri() { 192 return httpUri; 193 } 194 195 public void setHttpUri(URI httpUri) { 196 this.httpUri = httpUri; 197 } 198 199 public HttpConnectionManager getHttpConnectionManager() { 200 return httpConnectionManager; 201 } 202 203 public void setHttpConnectionManager(HttpConnectionManager httpConnectionManager) { 204 this.httpConnectionManager = httpConnectionManager; 205 } 206 207 public HeaderFilterStrategy getHeaderFilterStrategy() { 208 return headerFilterStrategy; 209 } 210 211 public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) { 212 this.headerFilterStrategy = headerFilterStrategy; 213 } 214 215 public boolean isThrowExceptionOnFailure() { 216 return throwExceptionOnFailure; 217 } 218 219 public void setThrowExceptionOnFailure(boolean throwExceptionOnFailure) { 220 this.throwExceptionOnFailure = throwExceptionOnFailure; 221 } 222 223 public boolean isBridgeEndpoint() { 224 return bridgeEndpoint; 225 } 226 227 public void setBridgeEndpoint(boolean bridge) { 228 this.bridgeEndpoint = bridge; 229 } 230 231 public boolean isMatchOnUriPrefix() { 232 return matchOnUriPrefix; 233 } 234 235 public void setMatchOnUriPrefix(boolean match) { 236 this.matchOnUriPrefix = match; 237 } 238 239 public boolean isChunked() { 240 return this.chunked; 241 } 242 243 public void setChunked(boolean chunked) { 244 this.chunked = chunked; 245 } 246 }