FELIX-4908 : X-Forward Header support when running behind a proxy/load balancer. Apply patch from Antonio Sanso

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1685752 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConfigMetaTypeProvider.java b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConfigMetaTypeProvider.java
index e96bd2f..a0d4ee9 100644
--- a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConfigMetaTypeProvider.java
+++ b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/ConfigMetaTypeProvider.java
@@ -242,6 +242,12 @@
                 2147483647,
                 null, null,
                 bundle.getBundleContext().getProperty(JettyConfig.FELIX_JETTY_EXCLUDED_PROTOCOLS)));
+        
+        adList.add(new AttributeDefinitionImpl(JettyConfig.FELIX_PROXY_LOAD_BALANCER_CONNECTION_ENABLE,
+                "Enable Proxy/Load Balancer Connection",
+                "Whether or not the Proxy/Load Balancer Connection is enabled. Defaults to false thus disabled.",
+                false,
+                bundle.getBundleContext().getProperty(JettyConfig.FELIX_PROXY_LOAD_BALANCER_CONNECTION_ENABLE)));
 
         return new ObjectClassDefinition()
         {
diff --git a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
index daafbef..14cce15 100644
--- a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
+++ b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
@@ -127,6 +127,9 @@
     /** Felix specific property to configure the excluded protocols */
     public static final String FELIX_JETTY_EXCLUDED_PROTOCOLS = "org.apache.felix.https.jetty.protocols.excluded";
 
+    /** Felix specific property to control whether to enable Proxy/Load Balancer Connection */
+    public static final String FELIX_PROXY_LOAD_BALANCER_CONNECTION_ENABLE = "org.apache.felix.proxy.load.balancer.connection.enable";
+    
     private static String validateContextPath(String ctxPath)
     {
         // undefined, empty, or root context path
@@ -359,6 +362,11 @@
         boolean useHttps = getBooleanProperty(FELIX_HTTPS_ENABLE, getBooleanProperty(OSCAR_HTTPS_ENABLE, false));
         return useHttps && getHttpsPort() > 0;
     }
+    
+    public boolean isProxyLoadBalancerConnection()
+    {
+        return getBooleanProperty(FELIX_PROXY_LOAD_BALANCER_CONNECTION_ENABLE, false);
+    }
 
     public void reset()
     {
diff --git a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
index 30fe550..b6db3a5 100644
--- a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
+++ b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
@@ -47,6 +47,7 @@
 import org.eclipse.jetty.security.HashLoginService;
 import org.eclipse.jetty.server.Connector;
 import org.eclipse.jetty.server.ConnectorStatistics;
+import org.eclipse.jetty.server.ForwardedRequestCustomizer;
 import org.eclipse.jetty.server.HttpConfiguration;
 import org.eclipse.jetty.server.HttpConnectionFactory;
 import org.eclipse.jetty.server.SecureRequestCustomizer;
@@ -317,6 +318,12 @@
         configureHttpConnectionFactory(connFactory);
         ServerConnector connector = new ServerConnector(server, connFactory);
         configureConnector(connector, this.config.getHttpPort());
+        
+        if (this.config.isProxyLoadBalancerConnection())
+        {
+            connFactory.getHttpConfiguration().addCustomizer(new ForwardedRequestCustomizer());
+        }
+        
         return startConnector(connector);
     }
 
@@ -329,7 +336,14 @@
         configureSslContextFactory(sslContextFactory);
 
         ServerConnector connector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.toString()), connFactory);
-        connFactory.getHttpConfiguration().addCustomizer(new SecureRequestCustomizer());
+        HttpConfiguration httpConfiguration = connFactory.getHttpConfiguration();
+        httpConfiguration.addCustomizer(new SecureRequestCustomizer());
+        
+        if (this.config.isProxyLoadBalancerConnection())
+        {
+            httpConfiguration.addCustomizer(new ForwardedRequestCustomizer());
+        }
+        
         configureConnector(connector, this.config.getHttpsPort());
         return startConnector(connector);
     }