You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/10/16 18:57:47 UTC

svn commit: r585196 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/impl/ components/camel-http/src/main/java/org/apache/camel/component/http/

Author: jstrachan
Date: Tue Oct 16 09:57:16 2007
New Revision: 585196

URL: http://svn.apache.org/viewvc?rev=585196&view=rev
Log:
fixes to make it easier to configure the HttpClient as per this discussion: http://www.nabble.com/Http-endpoint---SSL-and-HTTP-Proxy-t4632491s22882.html

Added:
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpClientConfigurer.java   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java?rev=585196&r1=585195&r2=585196&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java Tue Oct 16 09:57:16 2007
@@ -72,7 +72,9 @@
                 ScheduledPollEndpoint scheduledPollEndpoint = (ScheduledPollEndpoint)endpoint;
                 scheduledPollEndpoint.configureProperties(parameters);
             }
+            if (useIntrospectionOnEndpoint()) {
             setProperties(endpoint, parameters);
+            }
         }
         return endpoint;
     }
@@ -140,4 +142,13 @@
     protected void setProperties(Object bean, Map parameters) throws Exception {
         IntrospectionSupport.setProperties(getCamelContext().getTypeConverter(), bean, parameters);
     }
+
+    /**
+     * Derived classes may wish to overload this to prevent the default introspection of URI parameters
+     * on the created Endpoint instance
+     */
+    protected boolean useIntrospectionOnEndpoint() {
+        return true;
+    }
+
 }

Added: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpClientConfigurer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpClientConfigurer.java?rev=585196&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpClientConfigurer.java (added)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpClientConfigurer.java Tue Oct 16 09:57:16 2007
@@ -0,0 +1,34 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.http;
+
+import org.apache.commons.httpclient.HttpClient;
+
+/**
+ * A plugable strategy for configuring the HttpClient used by this component
+ *
+ * @version $Revision: 1.1 $
+ */
+public interface HttpClientConfigurer {
+    /**
+     * Configure the HttpClient such as setting the authentication or proxying details
+     *
+     * @param client
+     */
+    public void configureHttpClient(HttpClient client);
+}

Propchange: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpClientConfigurer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java?rev=585196&r1=585195&r2=585196&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java Tue Oct 16 09:57:16 2007
@@ -23,7 +23,9 @@
 import org.apache.camel.Endpoint;
 import org.apache.camel.Processor;
 import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.impl.DefaultComponent;
+import org.apache.commons.httpclient.params.HttpClientParams;
 
 /**
  * Defines the <a href="http://activemq.apache.org/camel/http.html">HTTP
@@ -33,6 +35,8 @@
  */
 public class HttpComponent extends DefaultComponent<HttpExchange> {
 
+    private HttpClientConfigurer httpClientConfigurer;
+
     /**
      * Connects the URL specified on the endpoint to the specified processor.
      * 
@@ -50,10 +54,23 @@
     public void disconnect(HttpConsumer consumer) throws Exception {
     }
 
+    public HttpClientConfigurer getHttpClientConfigurer() {
+        return httpClientConfigurer;
+    }
+
+    public void setHttpClientConfigurer(HttpClientConfigurer httpClientConfigurer) {
+        this.httpClientConfigurer = httpClientConfigurer;
+    }
 
     @Override
     protected Endpoint<HttpExchange> createEndpoint(String uri, String remaining, Map parameters) throws Exception {
-        return new HttpEndpoint(uri, this, new URI(uri));
+        HttpClientParams params = new HttpClientParams();
+        IntrospectionSupport.setProperties(params, parameters, "httpClient.");
+        return new HttpEndpoint(uri, this, new URI(uri), params);
     }
 
+    @Override
+    protected boolean useIntrospectionOnEndpoint() {
+        return false;
+    }
 }

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java?rev=585196&r1=585195&r2=585196&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java Tue Oct 16 09:57:16 2007
@@ -20,6 +20,8 @@
 import org.apache.camel.Producer;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.impl.DefaultPollingEndpoint;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.params.HttpClientParams;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -37,11 +39,18 @@
     private HttpBinding binding;
     private HttpComponent component;
     private URI httpUri;
+    private HttpClientParams clientParams;
+    private HttpClientConfigurer httpClientConfigurer;
 
     public HttpEndpoint(String endPointURI, HttpComponent component, URI httpURI) throws URISyntaxException {
+        this(endPointURI, component, httpURI, new HttpClientParams());
+    }
+
+    public HttpEndpoint(String endPointURI, HttpComponent component, URI httpURI, HttpClientParams clientParams) throws URISyntaxException {
         super(endPointURI, component);
         this.component = component;
         this.httpUri = httpURI;
+        this.clientParams = clientParams;
     }
 
     public Producer<HttpExchange> createProducer() throws Exception {
@@ -61,6 +70,63 @@
         return new HttpExchange(this, request, response);
     }
 
+    /**
+     * Factory method used by producers and consumers to create a new {@link HttpClient} instance
+     */
+    public HttpClient createHttpClient() {
+        HttpClient answer = new HttpClient(getClientParams());
+        HttpClientConfigurer configurer = getHttpClientConfigurer();
+        if (configurer != null) {
+            configurer.configureHttpClient(answer);
+        }
+        return answer;
+    }
+
+    public void connect(HttpConsumer consumer) throws Exception {
+        component.connect(consumer);
+    }
+
+    public void disconnect(HttpConsumer consumer) throws Exception {
+        component.disconnect(consumer);
+    }
+
+
+    // Properties
+    //-------------------------------------------------------------------------
+
+
+    /**
+     * Provide access to the client parameters used on new {@link HttpClient} instances
+     * used by producers or consumers of this endpoint.
+     */
+    public HttpClientParams getClientParams() {
+        return clientParams;
+    }
+
+    /**
+     * Provide access to the client parameters used on new {@link HttpClient} instances
+     * used by producers or consumers of this endpoint.
+     *
+     * @param clientParams
+     */
+    public void setClientParams(HttpClientParams clientParams) {
+        this.clientParams = clientParams;
+    }
+
+    public HttpClientConfigurer getHttpClientConfigurer() {
+        return httpClientConfigurer;
+    }
+
+    /**
+     * Register a custom configuration strategy for new {@ilnk HttpClient} instances
+     * created by producers or consumers such as to configure authentication mechanisms etc
+     *
+     * @param httpClientConfigurer the strategy for configuring new {@ilnk HttpClient} instances
+     */
+    public void setHttpClientConfigurer(HttpClientConfigurer httpClientConfigurer) {
+        this.httpClientConfigurer = httpClientConfigurer;
+    }
+
     public HttpBinding getBinding() {
         if (binding == null) {
             binding = new HttpBinding();
@@ -74,14 +140,6 @@
 
     public boolean isSingleton() {
         return true;
-    }
-
-    public void connect(HttpConsumer consumer) throws Exception {
-        component.connect(consumer);
-    }
-
-    public void disconnect(HttpConsumer consumer) throws Exception {
-        component.disconnect(consumer);
     }
 
     public String getPath() {

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java?rev=585196&r1=585195&r2=585196&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java Tue Oct 16 09:57:16 2007
@@ -34,11 +34,12 @@
  */
 public class HttpPollingConsumer extends PollingConsumerSupport<HttpExchange> {
     private final HttpEndpoint endpoint;
-    private HttpClient httpClient = new HttpClient();
+    private HttpClient httpClient;
 
     public HttpPollingConsumer(HttpEndpoint endpoint) {
         super(endpoint);
         this.endpoint = endpoint;
+        httpClient = endpoint.createHttpClient();
     }
 
     public HttpExchange receive() {

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java?rev=585196&r1=585195&r2=585196&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java Tue Oct 16 09:57:16 2007
@@ -35,10 +35,11 @@
  */
 public class HttpProducer extends DefaultProducer<HttpExchange> implements Producer<HttpExchange> {
     public static final String QUERY = "org.apache.camel.component.http.query";
-    private HttpClient httpClient = new HttpClient();
+    private HttpClient httpClient;
 
     public HttpProducer(HttpEndpoint endpoint) {
         super(endpoint);
+        httpClient = endpoint.createHttpClient();
     }
 
     public void process(Exchange exchange) throws Exception {
@@ -59,6 +60,14 @@
         }
 
         out.setHeader("http.responseCode", responseCode);
+    }
+
+    public HttpClient getHttpClient() {
+        return httpClient;
+    }
+
+    public void setHttpClient(HttpClient httpClient) {
+        this.httpClient = httpClient;
     }
 
     protected HttpMethod createMethod(Exchange exchange) {