Author: woonsan
Date: Wed Nov 25 14:53:03 2009
New Revision: 884123
URL: http://svn.apache.org/viewvc?rev=884123&view=rev
Log:
APA-23: Fixing proxy configuration problems.
Added:
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/ProxyHttpRoutePlanner.java
(with props)
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestHttpClientThroughProxy.java
(with props)
Modified:
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyServlet.java
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java
portals/applications/webcontent/trunk/webcontent-war/src/main/webapp/WEB-INF/conf/reverseproxy.properties
Modified:
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyServlet.java
URL:
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyServlet.java?rev=884123&r1=884122&r2=884123&view=diff
==============================================================================
---
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyServlet.java
(original)
+++
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyServlet.java
Wed Nov 25 14:53:03 2009
@@ -57,6 +57,10 @@
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.routing.RouteInfo;
+import org.apache.http.conn.scheme.PlainSocketFactory;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import
org.apache.portals.applications.webcontent.proxy.HttpReverseProxyException;
@@ -284,18 +288,44 @@
proxyPathMapperProvider.setMaxMatchingPathPartCount(maxMatchingPathPartCount);
- HttpReverseProxyService tempProxyService = new
RewritableHttpReverseProxyServiceImpl(proxyPathMapperProvider);
+ SchemeRegistry schemeRegistry = new SchemeRegistry();
+ schemeRegistry.register(new Scheme("http",
PlainSocketFactory.getSocketFactory(), 80));
+ schemeRegistry.register(new Scheme("https",
SSLSocketFactory.getSocketFactory(), 443));
+
+ RewritableHttpReverseProxyServiceImpl tempProxyService = new
RewritableHttpReverseProxyServiceImpl(proxyPathMapperProvider);
+ tempProxyService.setSchemeRegistry(schemeRegistry);
+ ProxyHttpRoutePlanner httpRoutePlanner = new
ProxyHttpRoutePlanner(schemeRegistry);
+ tempProxyService.setHttpRoutePlanner(httpRoutePlanner);
+
+ Configuration defaultProxiesConf =
configuration.subset("proxy.http.client.default.proxy");
+
+ if (!defaultProxiesConf.isEmpty())
+ {
+ String [] proxyNames = defaultProxiesConf.getStringArray("");
+
+ if (proxyNames.length > 0)
+ {
+ HttpHost [] defaultProxyHosts = new
HttpHost[proxyNames.length];
+
+ for (int i = 0; i < proxyNames.length; i++)
+ {
+ defaultProxyHosts[i] =
buildHttpHost(defaultProxiesConf.subset(proxyNames[i]));
+ }
+
+ httpRoutePlanner.setDefaultProxyHosts(defaultProxyHosts);
+ }
+ }
Configuration serverConf = configuration.subset("proxy.server");
if (!StringUtils.isBlank(serverConf.getString("hostname")))
{
- ((RewritableHttpReverseProxyServiceImpl)
tempProxyService).setHostHeaderValue(serverConf.getString("hostname"));
+
tempProxyService.setHostHeaderValue(serverConf.getString("hostname"));
}
if (!StringUtils.isBlank(serverConf.getString("baseurl")))
{
- ((RewritableHttpReverseProxyServiceImpl)
tempProxyService).setLocalBaseURL(serverConf.getString("baseurl"));
+ tempProxyService.setLocalBaseURL(serverConf.getString("baseurl"));
}
Configuration clientParamsConf =
configuration.subset("proxy.http.client.param");
@@ -304,7 +334,7 @@
{
HttpParams clientParams = new BasicHttpParams();
setBeanPropertiesByConfiguration(new
ClientParamBean(clientParams), clientParamsConf);
- ((RewritableHttpReverseProxyServiceImpl)
tempProxyService).setClientParams(clientParams);
+ tempProxyService.setClientParams(clientParams);
}
Configuration connManagerParamsConf =
configuration.subset("proxy.http.connManager.param");
@@ -316,6 +346,8 @@
ConnManagerParamBean connManagerParamBean = new
ConnManagerParamBean(connManagerParams);
setBeanPropertiesByConfiguration(connManagerParamBean,
connManagerParamsConf);
+ Map<HttpHost, HttpRoute> proxyRouteMap = new HashMap<HttpHost,
HttpRoute>();
+
if (!routesConf.isEmpty())
{
ConnPerRouteBean connPerRouteBean = new ConnPerRouteBean();
@@ -332,12 +364,18 @@
try
{
HttpRoute httpRoute = buildHttpRoute(routeConf);
+
int maxConnections =
routeConf.getInt("maxConnections", -1);
if (maxConnections >= 0)
{
maxForRoutes.put(httpRoute, maxConnections);
}
+
+ if (httpRoute.getProxyHost() != null)
+ {
+ proxyRouteMap.put(httpRoute.getTargetHost(),
httpRoute);
+ }
}
catch (Exception e)
{
@@ -349,11 +387,14 @@
connManagerParamBean.setConnectionsPerRoute(connPerRouteBean);
}
- ((RewritableHttpReverseProxyServiceImpl)
tempProxyService).setConnectionManagerParams(connManagerParams);
+ tempProxyService.setConnectionManagerParams(connManagerParams);
+
+ if (!proxyRouteMap.isEmpty())
+ {
+ httpRoutePlanner.setProxyRouteMap(proxyRouteMap);
+ }
}
- Configuration ssoUserConf = configuration.subset("proxy.sso.user");
-
tempProxyService.initialize();
HttpReverseProxyService oldProxyService = proxyService;
@@ -623,17 +664,13 @@
{
}
- if (localAddress == null)
+ if (proxyHosts != null && proxyHosts.length > 0)
{
- httpRoute = new HttpRoute(targetHost);
- }
- else if (proxyHosts == null)
- {
- httpRoute = new HttpRoute(targetHost, localAddress, secure);
+ httpRoute = new HttpRoute(targetHost, localAddress, proxyHosts,
secure, tunnelType, layerType);
}
else
{
- httpRoute = new HttpRoute(targetHost, localAddress, proxyHosts,
secure, tunnelType, layerType);
+ httpRoute = new HttpRoute(targetHost, localAddress, secure);
}
return httpRoute;
Added:
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/ProxyHttpRoutePlanner.java
URL:
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/ProxyHttpRoutePlanner.java?rev=884123&view=auto
==============================================================================
---
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/ProxyHttpRoutePlanner.java
(added)
+++
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/ProxyHttpRoutePlanner.java
Wed Nov 25 14:53:03 2009
@@ -0,0 +1,85 @@
+/*
+ * 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.portals.applications.webcontent.proxy.impl;
+
+import java.net.InetAddress;
+import java.util.Map;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.conn.routing.HttpRoute;
+import org.apache.http.conn.routing.RouteInfo.LayerType;
+import org.apache.http.conn.routing.RouteInfo.TunnelType;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.impl.conn.DefaultHttpRoutePlanner;
+import org.apache.http.protocol.HttpContext;
+
+/**
+ * CustomHttpRoutePlanner
+ *
+ * @version $Id$
+ */
+public class ProxyHttpRoutePlanner extends DefaultHttpRoutePlanner
+{
+
+ private HttpHost [] defaultProxyHosts;
+ private Map<HttpHost, HttpRoute> proxyRouteMap;
+
+ public ProxyHttpRoutePlanner(SchemeRegistry schreg)
+ {
+ super(schreg);
+ }
+
+ public void setDefaultProxyHosts(HttpHost [] defaultProxyHosts)
+ {
+ this.defaultProxyHosts = defaultProxyHosts;
+ }
+
+ public void setProxyRouteMap(Map<HttpHost, HttpRoute> proxyRouteMap)
+ {
+ this.proxyRouteMap = proxyRouteMap;
+ }
+
+ public HttpRoute determineRoute(HttpHost target, HttpRequest request,
HttpContext context) throws HttpException
+ {
+ if (proxyRouteMap != null)
+ {
+ HttpRoute targetRoute = proxyRouteMap.get(target);
+
+ if (targetRoute != null)
+ {
+ return targetRoute;
+ }
+ }
+
+ HttpRoute route = super.determineRoute(target, request, context);
+
+ if (defaultProxyHosts != null && defaultProxyHosts.length > 0)
+ {
+ HttpHost targetHost = route.getTargetHost();
+ InetAddress local = route.getLocalAddress();
+ boolean secure = route.isSecure();
+ TunnelType tunnelType = route.getTunnelType();
+ LayerType layerType = route.getLayerType();
+ route = new HttpRoute(targetHost, local, defaultProxyHosts,
secure, tunnelType, layerType);
+ }
+
+ return route;
+ }
+
+}
Propchange:
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/ProxyHttpRoutePlanner.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/ProxyHttpRoutePlanner.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange:
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/ProxyHttpRoutePlanner.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java
URL:
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java?rev=884123&r1=884122&r2=884123&view=diff
==============================================================================
---
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java
(original)
+++
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/RewritableHttpReverseProxyServiceImpl.java
Wed Nov 25 14:53:03 2009
@@ -59,6 +59,7 @@
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
@@ -116,6 +117,11 @@
private String localBaseURL;
/**
+ * SchemeRegistry
+ */
+ private SchemeRegistry schemeRegistry;
+
+ /**
* The multithreaded connection manager for performance.
*/
private ClientConnectionManager connectionManager;
@@ -130,6 +136,11 @@
*/
private HttpParams clientParams;
+ /**
+ * HTTP Route Planner
+ */
+ private HttpRoutePlanner httpRoutePlanner;
+
public
RewritableHttpReverseProxyServiceImpl(HttpReverseProxyPathMapperProvider
proxyPathMapperProvider)
{
this.proxyPathMapperProvider = proxyPathMapperProvider;
@@ -150,11 +161,21 @@
this.clientParams = clientParams;
}
+ public void setSchemeRegistry(SchemeRegistry schemeRegistry)
+ {
+ this.schemeRegistry = schemeRegistry;
+ }
+
public void setConnectionManagerParams(HttpParams connectionManagerParams)
{
this.connectionManagerParams = connectionManagerParams;
}
+ public void setHttpRoutePlanner(HttpRoutePlanner httpRoutePlanner)
+ {
+ this.httpRoutePlanner = httpRoutePlanner;
+ }
+
public void initialize()
{
if (clientParams == null)
@@ -167,10 +188,13 @@
connectionManagerParams = new BasicHttpParams();
}
- SchemeRegistry schemeRegistry = new SchemeRegistry();
- schemeRegistry.register(new Scheme("http",
PlainSocketFactory.getSocketFactory(), 80));
- schemeRegistry.register(new Scheme("https",
SSLSocketFactory.getSocketFactory(), 443));
-
+ if (schemeRegistry == null)
+ {
+ schemeRegistry = new SchemeRegistry();
+ schemeRegistry.register(new Scheme("http",
PlainSocketFactory.getSocketFactory(), 80));
+ schemeRegistry.register(new Scheme("https",
SSLSocketFactory.getSocketFactory(), 443));
+ }
+
connectionManager = new
ThreadSafeClientConnManager(connectionManagerParams, schemeRegistry);
}
@@ -248,6 +272,11 @@
}
};
+ if (httpRoutePlanner != null)
+ {
+ httpClient.setRoutePlanner(httpRoutePlanner);
+ }
+
// redirection should be adjusted with local host header...
httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS,
false);
Added:
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestHttpClientThroughProxy.java
URL:
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestHttpClientThroughProxy.java?rev=884123&view=auto
==============================================================================
---
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestHttpClientThroughProxy.java
(added)
+++
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestHttpClientThroughProxy.java
Wed Nov 25 14:53:03 2009
@@ -0,0 +1,84 @@
+/*
+ * 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.portals.applications.webcontent.proxy;
+
+import java.io.InputStream;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.conn.params.ConnRouteParamBean;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
+
+/**
+ * Example test case to show how to implement http client invocations with
proxy configuration
+ *
+ * @version $Id$
+ */
+public class TestHttpClientThroughProxy extends TestCase
+{
+
+ private HttpClient httpClient;
+ private HttpParams clientParams;
+ private HttpHost defaultProxy = new HttpHost("localhost", 8000);
+
+ @Override
+ public void setUp()
+ {
+ clientParams = new BasicHttpParams();
+ httpClient = new DefaultHttpClient(clientParams);
+ }
+
+ public void testClientThroughProxy()
+ {
+ String targetURL = "http://portals.apache.org/issue-tracking.html";
+
+ try
+ {
+ HttpRequestBase httpRequest = new HttpGet(targetURL);
+
+ ConnRouteParamBean connRouteParam = new
ConnRouteParamBean(httpRequest.getParams());
+ connRouteParam.setDefaultProxy(defaultProxy);
+
+ HttpResponse httpResponse = httpClient.execute(httpRequest);
+ HttpEntity httpEntity = httpResponse.getEntity();
+
+ InputStream input = httpEntity.getContent();
+
+ if (input != null)
+ {
+ IOUtils.copy(input, System.out);
+ System.out.flush();
+ IOUtils.closeQuietly(input);
+ }
+ }
+ catch (Exception e)
+ {
+ System.out.println("WARNING: Connection through proxy has failed.
" + e);
+ System.out.println("WARNING: Proxy: " + defaultProxy + ", target:
" + targetURL);
+ }
+ }
+
+}
Propchange:
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestHttpClientThroughProxy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestHttpClientThroughProxy.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange:
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestHttpClientThroughProxy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
portals/applications/webcontent/trunk/webcontent-war/src/main/webapp/WEB-INF/conf/reverseproxy.properties
URL:
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-war/src/main/webapp/WEB-INF/conf/reverseproxy.properties?rev=884123&r1=884122&r2=884123&view=diff
==============================================================================
---
portals/applications/webcontent/trunk/webcontent-war/src/main/webapp/WEB-INF/conf/reverseproxy.properties
(original)
+++
portals/applications/webcontent/trunk/webcontent-war/src/main/webapp/WEB-INF/conf/reverseproxy.properties
Wed Nov 25 14:53:03 2009
@@ -32,6 +32,11 @@
# ... Refer to the javadoc of org.apache.http.client.params.ClientParamBean.
proxy.http.client.param.allowCircularRedirects = false
+# Default http client proxy configuration (If a specific route does not define
proxy configuration, this applies.)
+#proxy.http.client.default.proxy = proxyserver1
+#proxy.http.client.default.proxy.proxyserver1.hostname = proxyserver1
+#proxy.http.client.default.proxy.proxyserver1.port = 10080
+
# Global HTTP ClientConnectionManager parameters
# ... You can set various http client connection manager parameters with
prefix, 'proxy.http.connManager.param.'.
@@ -50,6 +55,13 @@
proxy.http.route.apache.target.hostname = www.apache.org
proxy.http.route.apache.maxConnections = 40
+# ... Sets proxy attributes for portals.apache.org route
+#proxy.http.route = portals
+#proxy.http.route.portals.target.hostname = portals.apache.org
+#proxy.http.route.portals.proxy = proxyserver1
+#proxy.http.route.portals.proxy.proxyserver1.hostname = proxyserver1
+#proxy.http.route.portals.proxy.proxyserver1.port = 10080
+
# ... 'somewhere' is just an example to show the full configurable items...
# ... Sets detail attirbutes for 'somewhere' route
proxy.http.route = somewhere