camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1422249 - in /camel/branches/camel-2.9.x: ./ components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
Date Sat, 15 Dec 2012 13:37:53 GMT
Author: davsclaus
Date: Sat Dec 15 13:37:51 2012
New Revision: 1422249

URL: http://svn.apache.org/viewvc?rev=1422249&view=rev
Log:
CAMEL-5867: Jetty producer creates a new http client and uses any httpClient.xxx options configured.
This avoids issue with handling proper lifecycle of these clients.

Modified:
    camel/branches/camel-2.9.x/   (props changed)
    camel/branches/camel-2.9.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
    camel/branches/camel-2.9.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
  Merged /camel/trunk:r1422241
  Merged /camel/branches/camel-2.10.x:r1422247

Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.9.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java?rev=1422249&r1=1422248&r2=1422249&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
(original)
+++ camel/branches/camel-2.9.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
Sat Dec 15 13:37:51 2012
@@ -132,8 +132,7 @@ public class JettyHttpComponent extends 
 
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
-        Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
-        
+
         // must extract well known parameters before we create the endpoint
         List<Handler> handlerList = resolveAndRemoveReferenceListParameter(parameters,
"handlers", Handler.class);
         HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBindingRef",
HttpBinding.class);
@@ -150,57 +149,21 @@ public class JettyHttpComponent extends 
         Long continuationTimeout = getAndRemoveParameter(parameters, "continuationTimeout",
Long.class);
         Boolean useContinuation = getAndRemoveParameter(parameters, "useContinuation", Boolean.class);
         SSLContextParameters sslContextParameters = resolveAndRemoveReferenceParameter(parameters,
"sslContextParametersRef", SSLContextParameters.class);
-        
-        
-        // configure http client if we have url configuration for it
-        // http client is only used for jetty http producer (hence not very commonly used)
-        HttpClient client = null;
-        if (IntrospectionSupport.hasProperties(parameters, "httpClient.") || sslContextParameters
!= null) {
-            client = getNewHttpClient();
-            
-            if (IntrospectionSupport.hasProperties(parameters, "httpClient.")) {
-                if (isExplicitHttpClient) {
-                    LOG.warn("The user explicitly set an HttpClient instance on the component,
"
-                             + "but this endpoint provides HttpClient configuration.  Are
you sure that "
-                             + "this is what was intended?  Applying endpoint configuration
to a new HttpClient instance "
-                             + "to avoid altering existing HttpClient instances.");
-                }
-            
-                // set additional parameters on http client
-                IntrospectionSupport.setProperties(client, parameters, "httpClient.");
-                // validate that we could resolve all httpClient. parameters as this component
is lenient
-                validateParameters(uri, parameters, "httpClient.");
-            }
-            
-            // Note that the component level instance is already configured in getNewHttpClient.
-            // We replace it here for endpoint level config.
-            if (sslContextParameters != null) {
-                if (isExplicitHttpClient) {
-                    LOG.warn("The user explicitly set an HttpClient instance on the component,
"
-                             + "but this endpoint provides SSLContextParameters configuration.
 Are you sure that "
-                             + "this is what was intended?  Applying endpoint configuration
to a new HttpClient instance "
-                             + "to avoid altering existing HttpClient instances.");
-                }
-                
-                ((CamelHttpClient) client).setSSLContext(sslContextParameters.createSSLContext());
-            }
-        }
-        // keep the configure parameters for the http client
-        for (String key : parameters.keySet()) {
-            httpClientParameters.remove(key);
-        }
+        SSLContextParameters ssl = sslContextParameters != null ? sslContextParameters :
this.sslContextParameters;
+        // extract httpClient. parameters
+        Map<String, Object> httpClientParameters = IntrospectionSupport.extractProperties(parameters,
"httpClient.");
 
         String address = remaining;
         URI addressUri = new URI(UnsafeUriCharactersEncoder.encode(address));
-        URI endpointUri = URISupport.createRemainingURI(addressUri, CastUtils.cast(httpClientParameters));
+        URI endpointUri = URISupport.createRemainingURI(addressUri, CastUtils.cast(parameters));
         // restructure uri to be based on the parameters left as we dont want to include
the Camel internal options
         URI httpUri = URISupport.createRemainingURI(addressUri, CastUtils.cast(parameters));
         // create endpoint after all known parameters have been extracted from parameters
         JettyHttpEndpoint endpoint = new JettyHttpEndpoint(this, endpointUri.toString(),
httpUri);
         setEndpointHeaderFilterStrategy(endpoint);
 
-        if (client != null) {
-            endpoint.setClient(client);
+        if (httpClientParameters != null && !httpClientParameters.isEmpty()) {
+            endpoint.setHttpClientParameters(httpClientParameters);
         }
         if (handlerList.size() > 0) {
             endpoint.setHandlers(handlerList);
@@ -243,12 +206,10 @@ public class JettyHttpComponent extends 
         }
         
         endpoint.setEnableMultipartFilter(enableMultipartFilter);
-        
         if (multipartFilter != null) {
             endpoint.setMultipartFilter(multipartFilter);
             endpoint.setEnableMultipartFilter(true);
         }
-        
         if (filters != null) {
             endpoint.setFilters(filters);
         }
@@ -259,11 +220,9 @@ public class JettyHttpComponent extends 
         if (useContinuation != null) {
             endpoint.setUseContinuation(useContinuation);
         }
-        
-        if (sslContextParameters == null) {
-            sslContextParameters = this.sslContextParameters;
+        if (ssl != null) {
+            endpoint.setSslContextParameters(ssl);
         }
-        endpoint.setSslContextParameters(sslContextParameters);
 
         setProperties(endpoint, parameters);
         return endpoint;

Modified: camel/branches/camel-2.9.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java?rev=1422249&r1=1422248&r2=1422249&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
(original)
+++ camel/branches/camel-2.9.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
Sat Dec 15 13:37:51 2012
@@ -18,16 +18,19 @@ package org.apache.camel.component.jetty
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.HashMap;
 import java.util.List;
-
+import java.util.Map;
 import javax.servlet.Filter;
 
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
+import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.component.http.HttpConsumer;
 import org.apache.camel.component.http.HttpEndpoint;
 import org.apache.camel.impl.SynchronousDelegateProducer;
+import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.util.jsse.SSLContextParameters;
 import org.eclipse.jetty.client.HttpClient;
 import org.eclipse.jetty.server.Handler;
@@ -48,6 +51,7 @@ public class JettyHttpEndpoint extends H
     private Long continuationTimeout;
     private Boolean useContinuation;
     private SSLContextParameters sslContextParameters;
+    private Map<String, Object> httpClientParameters;
 
     public JettyHttpEndpoint(JettyHttpComponent component, String uri, URI httpURL) throws
URISyntaxException {
         super(uri, component, httpURL);
@@ -62,15 +66,32 @@ public class JettyHttpEndpoint extends H
     public Producer createProducer() throws Exception {
         JettyHttpProducer answer = new JettyHttpProducer(this);
         if (client != null) {
-            // use shared client
+            // use shared client, and ensure its started so we can use it
+            client.start();
             answer.setSharedClient(client);
         } else {
             // create a new client
             // thread pool min/max from endpoint take precedence over from component
             Integer min = getComponent().getHttpClientMinThreads();
             Integer max = getComponent().getHttpClientMaxThreads();
-            answer.setClient(JettyHttpComponent.createHttpClient(min, max, sslContextParameters));
+            HttpClient httpClient = JettyHttpComponent.createHttpClient(min, max, sslContextParameters);
+
+            // set optional http client parameters
+            if (httpClientParameters != null) {
+                // copy parameters as we need to re-use them again if creating a new producer
later
+                Map<String, Object> params = new HashMap<String, Object>(httpClientParameters);
+                IntrospectionSupport.setProperties(httpClient, params);
+                // validate we could set all parameters
+                if (params.size() > 0) {
+                    throw new ResolveEndpointFailedException(getEndpointUri(), "There are
" + params.size()
+                            + " parameters that couldn't be set on the endpoint."
+                            + " Check the uri if the parameters are spelt correctly and that
they are properties of the endpoint."
+                            + " Unknown parameters=[" + params + "]");
+                }
+            }
+            answer.setClient(httpClient);
         }
+
         answer.setBinding(getJettyBinding());
         if (isSynchronous()) {
             return new SynchronousDelegateProducer(answer);
@@ -101,12 +122,21 @@ public class JettyHttpEndpoint extends H
     }
 
     public HttpClient getClient() throws Exception {
-        if (client == null) {
-            return getComponent().getHttpClient();
-        }
         return client;
     }
 
+    /**
+     * Sets a shared {@link HttpClient} to use for all producers
+     * created by this endpoint. By default each producer will
+     * use a new http client, and not share.
+     * <p/>
+     * <b>Important: </b> Make sure to handle the lifecycle of the shared
+     * client, such as stopping the client, when it is no longer in use.
+     * Camel will call the <tt>start</tt> method on the client to ensure
+     * its started when this endpoint creates a producer.
+     * <p/>
+     * This options should only be used in special circumstances.
+     */
     public void setClient(HttpClient client) {
         this.client = client;
     }
@@ -181,20 +211,12 @@ public class JettyHttpEndpoint extends H
         this.sslContextParameters = sslContextParameters;
     }
 
-    @Override
-    protected void doStart() throws Exception {
-        if (client != null) {
-            client.start();
-        }
-        super.doStart();
+    public Map<String, Object> getHttpClientParameters() {
+        return httpClientParameters;
     }
 
-    @Override
-    protected void doStop() throws Exception {
-        super.doStop();
-        if (client != null) {
-            client.stop();
-        }
+    public void setHttpClientParameters(Map<String, Object> httpClientParameters) {
+        this.httpClientParameters = httpClientParameters;
     }
 
 }



Mime
View raw message