From 80eff2a68c1451cdee6bc13d3ac1bb801c9abf76 Mon Sep 17 00:00:00 2001
From: Paolo Antinori <pantinor@redhat.com>
Date: Thu, 5 May 2016 14:02:18 +0200
Subject: [PATCH] ENTESB-5389 - Fix issue with Jetty, security and RestDSL

---
 .../camel/component/jetty/JettyHttpComponent.java  | 110 ++++++++++-----------
 1 file changed, 55 insertions(+), 55 deletions(-)

diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
index d079be2..77871fc 100644
--- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
+++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
@@ -24,6 +24,7 @@ import java.lang.reflect.Method;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.security.GeneralSecurityException;
+import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -88,13 +89,13 @@ import org.slf4j.LoggerFactory;
  * An HttpComponent which starts an embedded Jetty for to handle consuming from
  * the http endpoints.
  *
- * @version 
+ * @version
  */
 public abstract class JettyHttpComponent extends HttpComponent implements RestConsumerFactory {
     public static final String TMP_DIR = "CamelJettyTempDir";
-    
+
     protected static final HashMap<String, ConnectorRef> CONNECTORS = new HashMap<String, ConnectorRef>();
-   
+
     private static final Logger LOG = LoggerFactory.getLogger(JettyHttpComponent.class);
     private static final String JETTY_SSL_KEYSTORE = "org.eclipse.jetty.ssl.keystore";
     private static final String JETTY_SSL_KEYPASSWORD = "org.eclipse.jetty.ssl.keypassword";
@@ -150,7 +151,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
         public int decrement() {
             return --refCount;
         }
-        
+
         public int getRefCount() {
             return refCount;
         }
@@ -197,8 +198,8 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
         URI httpUri = URISupport.createRemainingURI(addressUri, parameters);
         // create endpoint after all known parameters have been extracted from parameters
         JettyHttpEndpoint endpoint = createEndpoint(endpointUri, httpUri);
-        
-        
+
+
         if (headerFilterStrategy != null) {
             endpoint.setHeaderFilterStrategy(headerFilterStrategy);
         } else {
@@ -215,7 +216,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
             endpoint.setUrlRewrite(urlRewrite);
         }
         // setup the proxy host and proxy port
-        
+
 
         if (httpClientParameters != null && !httpClientParameters.isEmpty()) {
             endpoint.setHttpClientParameters(httpClientParameters);
@@ -255,11 +256,11 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
         }
         if (enableJmx != null) {
             endpoint.setEnableJmx(enableJmx);
-        } else { 
+        } else {
             // set this option based on setting of JettyHttpComponent
             endpoint.setEnableJmx(isEnableJmx());
         }
-        
+
         endpoint.setEnableMultipartFilter(enableMultipartFilter);
         if (multipartFilter != null) {
             endpoint.setMultipartFilter(multipartFilter);
@@ -332,16 +333,15 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
                     enableSessionSupport(connectorRef.server, connectorKey);
                 }
                 connectorRef.server.start();
-                
+
                 CONNECTORS.put(connectorKey, connectorRef);
-                
+
             } else {
-                
                 if (endpoint.getHandlers() != null && !endpoint.getHandlers().isEmpty()) {
                     // As the server is started, we need to stop the server for a while to add the new handler
-                    connectorRef.server.stop();
+                    //connectorRef.server.stop();
                     addJettyHandlers(connectorRef.server, endpoint.getHandlers());
-                    connectorRef.server.start();
+                    //connectorRef.server.start();
                 }
                 // ref track the connector
                 connectorRef.increment();
@@ -350,11 +350,11 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
             if (endpoint.isSessionSupport()) {
                 enableSessionSupport(connectorRef.server, connectorKey);
             }
-            
+
             if (endpoint.isEnableMultipartFilter()) {
                 enableMultipartFilter(endpoint, connectorRef.server, connectorKey);
             }
-            
+
             if (endpoint.getFilters() != null && endpoint.getFilters().size() > 0) {
                 setFilters(endpoint, connectorRef.server, connectorKey);
             }
@@ -384,7 +384,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
             }
         }
     }
-    
+
     private void setFilters(JettyHttpEndpoint endpoint, Server server, String connectorKey) {
         ServletContextHandler context = server.getChildHandlerByClass(ServletContextHandler.class);
         List<Filter> filters = endpoint.getFilters();
@@ -401,7 +401,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
             addFilter(context, filterHolder, pathSpec);
         }
     }
-    
+
     private void addFilter(ServletContextHandler context, FilterHolder filterHolder, String pathSpec) {
         context.getServletHandler().addFilterWithMapping(filterHolder, pathSpec, 0);
     }
@@ -446,7 +446,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
         // If the connector is not needed anymore then stop it
         HttpEndpoint endpoint = consumer.getEndpoint();
         String connectorKey = getConnectorKey(endpoint);
-        
+
         synchronized (CONNECTORS) {
             ConnectorRef connectorRef = CONNECTORS.get(connectorKey);
             if (connectorRef != null) {
@@ -466,14 +466,14 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
             }
         }
     }
-    
+
     private String getConnectorKey(HttpEndpoint endpoint) {
         return endpoint.getProtocol() + ":" + endpoint.getHttpUri().getHost() + ":" + endpoint.getPort();
     }
 
     // Properties
     // -------------------------------------------------------------------------
-       
+
     public String getSslKeyPassword() {
         return sslKeyPassword;
     }
@@ -514,7 +514,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
             connector = getSocketConnector(server, endpoint);
         }
         return connector;
-    }   
+    }
     protected Connector getSocketConnector(Server server, JettyHttpEndpoint endpoint) {
         Connector answer = null;
         if (socketConnectors != null) {
@@ -536,18 +536,18 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
         }
         return answer;
     }
-        
+
     protected Connector createConnector(Server server, JettyHttpEndpoint endpoint) {
 
         // now we just use the SelectChannelConnector as the default connector
         SslContextFactory sslcf = null;
-        
+
         // Note that this was set on the endpoint when it was constructed.  It was
         // either explicitly set at the component or on the endpoint, but either way,
         // the value is already set.  We therefore do not need to look at the component
         // level SSLContextParameters again in this method.
-        SSLContextParameters endpointSslContextParameters = endpoint.getSslContextParameters();        
-        
+        SSLContextParameters endpointSslContextParameters = endpoint.getSslContextParameters();
+
         if (endpointSslContextParameters != null) {
             try {
                 sslcf = createSslContextFactory(endpointSslContextParameters);
@@ -555,21 +555,21 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
                 throw new RuntimeCamelException(e);
             }
         } else if ("https".equals(endpoint.getProtocol())) {
-            sslcf = new SslContextFactory();  
+            sslcf = new SslContextFactory();
             String keystoreProperty = System.getProperty(JETTY_SSL_KEYSTORE);
             if (keystoreProperty != null) {
                 sslcf.setKeyStorePath(keystoreProperty);
             } else if (sslKeystore != null) {
                 sslcf.setKeyStorePath(sslKeystore);
             }
-    
+
             String keystorePassword = System.getProperty(JETTY_SSL_KEYPASSWORD);
             if (keystorePassword != null) {
                 sslcf.setKeyManagerPassword(keystorePassword);
             } else if (sslKeyPassword != null) {
                 sslcf.setKeyManagerPassword(sslKeyPassword);
             }
-    
+
             String password = System.getProperty(JETTY_SSL_PASSWORD);
             if (password != null) {
                 sslcf.setKeyStorePassword(password);
@@ -580,9 +580,9 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
 
         return createConnectorJettyInternal(server, endpoint, sslcf);
     }
-    
+
     protected abstract AbstractConnector createConnectorJettyInternal(Server server, JettyHttpEndpoint endpoint, SslContextFactory sslcf);
-    
+
     private SslContextFactory createSslContextFactory(SSLContextParameters ssl) throws GeneralSecurityException, IOException {
         SslContextFactory answer = new SslContextFactory();
         if (ssl != null) {
@@ -590,7 +590,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
         }
         return answer;
     }
-    
+
     protected boolean checkSSLContextFactoryConfig(Object instance) {
         try {
             Method method = instance.getClass().getMethod("checkConfig");
@@ -631,10 +631,10 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
     public CamelHttpClient createHttpClient(JettyHttpEndpoint endpoint, Integer minThreads, Integer maxThreads, SSLContextParameters ssl) throws Exception {
         SslContextFactory sslContextFactory = createSslContextFactory(ssl);
         CamelHttpClient httpClient = createCamelHttpClient(sslContextFactory);
-        
+
         CamelContext context = endpoint.getCamelContext();
 
-        if (context != null 
+        if (context != null
             && ObjectHelper.isNotEmpty(context.getProperty("http.proxyHost"))
             && ObjectHelper.isNotEmpty(context.getProperty("http.proxyPort"))) {
             String host = context.getProperty("http.proxyHost");
@@ -649,7 +649,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
             LOG.debug("proxyHost and proxyPort options detected. Using http proxy host: {} port: {}", host, port);
             httpClient.setProxy(host, port);
         }
-        
+
         // must have both min and max
         if (minThreads != null || maxThreads != null) {
 
@@ -668,7 +668,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
             qtp.setName("CamelJettyClient(" + ObjectHelper.getIdentityHashCode(httpClient) + ")");
             httpClient.setThreadPoolOrExecutor(qtp);
         }
-        
+
         if (LOG.isDebugEnabled()) {
             if (minThreads != null) {
                 LOG.debug("Created HttpClient with thread pool {}-{} -> {}", new Object[]{minThreads, maxThreads, httpClient});
@@ -676,7 +676,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
                 LOG.debug("Created HttpClient with default thread pool size -> {}", httpClient);
             }
         }
-        
+
         return httpClient;
     }
 
@@ -729,7 +729,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
     public boolean isEnableJmx() {
         return enableJmx;
     }
-    
+
     public JettyHttpBinding getJettyHttpBinding() {
         return jettyHttpBinding;
     }
@@ -742,13 +742,13 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
         // If null, provide the default implementation.
         if (mbContainer == null) {
             MBeanServer mbs = null;
-            
+
             final ManagementStrategy mStrategy = this.getCamelContext().getManagementStrategy();
             final ManagementAgent mAgent = mStrategy.getManagementAgent();
             if (mAgent != null) {
                 mbs = mAgent.getMBeanServer();
             }
-            
+
             if (mbs != null) {
                 mbContainer = new MBeanContainer(mbs);
                 startMbContainer();
@@ -756,7 +756,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
                 LOG.warn("JMX disabled in CamelContext. Jetty JMX extensions will remain disabled.");
             }
         }
-        
+
         return this.mbContainer;
     }
 
@@ -809,7 +809,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
     public void setUseContinuation(boolean useContinuation) {
         this.useContinuation = useContinuation;
     }
-    
+
     public SSLContextParameters getSslContextParameters() {
         return sslContextParameters;
     }
@@ -932,7 +932,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
         if (!query.isEmpty()) {
             url = url + "&" + query;
         }
-        
+
         JettyHttpEndpoint endpoint = camelContext.getEndpoint(url, JettyHttpEndpoint.class);
         setProperties(endpoint, parameters);
 
@@ -991,7 +991,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
 
         return camelServlet;
     }
-    
+
     protected void addJettyHandlers(Server server, List<Handler> handlers) {
         if (handlers != null && !handlers.isEmpty()) {
             for (Handler handler : handlers) {
@@ -1010,7 +1010,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
             }
         }
     }
-    
+
     protected Server createServer() {
         Server s = null;
         ThreadPool tp = threadPool;
@@ -1033,7 +1033,7 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
             try {
                 if (!Server.getVersion().startsWith("8")) {
                     s = Server.class.getConstructor(ThreadPool.class).newInstance(tp);
-                    
+
                 } else {
                     s = new Server();
                     Server.class.getMethod("setThreadPool", ThreadPool.class).invoke(s, tp);
@@ -1060,11 +1060,11 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
         if (getErrorHandler() != null) {
             s.addBean(getErrorHandler());
         } else if (!Server.getVersion().startsWith("8")) {
-            //need an error handler that won't leak information about the exception 
+            //need an error handler that won't leak information about the exception
             //back to the client.
             ErrorHandler eh = new ErrorHandler() {
-                public void handle(String target, Request baseRequest, 
-                                   HttpServletRequest request, HttpServletResponse response) 
+                public void handle(String target, Request baseRequest,
+                                   HttpServletRequest request, HttpServletResponse response)
                     throws IOException {
                     String msg = HttpStatus.getMessage(response.getStatus());
                     request.setAttribute(RequestDispatcher.ERROR_MESSAGE, msg);
@@ -1084,8 +1084,8 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
         }
         return s;
     }
-    
-    
+
+
     /**
      * Starts {@link #mbContainer} and registers the container with itself as a managed bean
      * logging an error if there is a problem starting the container.
@@ -1144,12 +1144,12 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
             mbContainer = null;
         }
     }
-    
+
     private void addServerMBean(Server server) {
         if (mbContainer == null) {
             return;
-        }        
-        
+        }
+
         try {
             Object o = getContainer(server);
             o.getClass().getMethod("addEventListener", Container.Listener.class).invoke(o, mbContainer);
@@ -1193,5 +1193,5 @@ public abstract class JettyHttpComponent extends HttpComponent implements RestCo
             throw new RuntimeException(t);
         }
     }
-    
+
 }
-- 
2.5.5

