Author: woonsan
Date: Wed Oct 7 09:19:36 2009
New Revision: 822629
URL: http://svn.apache.org/viewvc?rev=822629&view=rev
Log:
APA-17: Adding default cookies support for remote targets.
Modified:
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/HttpReverseProxyPathMapper.java
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyPathMapperImpl.java
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyPathMapperProviderImpl.java
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-jar/src/main/java/org/apache/portals/applications/webcontent/util/WebResourceUtils.java
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestProxyPathMappings.java
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestReverseProxyLinkRewritingParserAaptor.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/HttpReverseProxyPathMapper.java
URL:
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/HttpReverseProxyPathMapper.java?rev=822629&r1=822628&r2=822629&view=diff
==============================================================================
---
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/HttpReverseProxyPathMapper.java
(original)
+++
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/HttpReverseProxyPathMapper.java
Wed Oct 7 09:19:36 2009
@@ -67,4 +67,10 @@
*/
public Map<String, String> getDefaultRequestHeaders();
+ /**
+ * Returns default request cookies to the remote URL target.
+ * @return
+ */
+ public Map<String, String> getDefaultRequestCookies();
+
}
\ No newline at end of file
Modified:
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyPathMapperImpl.java
URL:
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyPathMapperImpl.java?rev=822629&r1=822628&r2=822629&view=diff
==============================================================================
---
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyPathMapperImpl.java
(original)
+++
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyPathMapperImpl.java
Wed Oct 7 09:19:36 2009
@@ -31,16 +31,25 @@
private String name;
private String localBasePath;
private String remoteBaseURL;
+
private Map<String, String> defaultRequestHeaders;
+ private Map<String, String> defaultRequestCookies;
private int hash;
- public DefaultHttpReverseProxyPathMapperImpl(String name, String
localBasePath, String remoteBaseURL, Map<String, String> defaultRequestHeaders)
+ public DefaultHttpReverseProxyPathMapperImpl(String name, String
localBasePath, String remoteBaseURL)
+ {
+ this(name, localBasePath, remoteBaseURL, null, null);
+ }
+
+ public DefaultHttpReverseProxyPathMapperImpl(String name, String
localBasePath, String remoteBaseURL, Map<String, String> defaultRequestHeaders,
Map<String, String> defaultRequestCookies)
{
this.name = name;
this.localBasePath = localBasePath;
this.remoteBaseURL = remoteBaseURL;
this.defaultRequestHeaders = defaultRequestHeaders;
+ this.defaultRequestCookies = defaultRequestCookies;
+
hash = new
StringBuilder(40).append(name).append(':').append(localBasePath).append(':').append(remoteBaseURL).toString().hashCode();
}
@@ -84,6 +93,11 @@
return defaultRequestHeaders;
}
+ public Map<String, String> getDefaultRequestCookies()
+ {
+ return defaultRequestCookies;
+ }
+
@Override
public int hashCode()
{
Modified:
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyPathMapperProviderImpl.java
URL:
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyPathMapperProviderImpl.java?rev=822629&r1=822628&r2=822629&view=diff
==============================================================================
---
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyPathMapperProviderImpl.java
(original)
+++
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/proxy/impl/DefaultHttpReverseProxyPathMapperProviderImpl.java
Wed Oct 7 09:19:36 2009
@@ -219,7 +219,8 @@
new
DefaultHttpReverseProxyPathMapperImpl(proxyPathMapper.getName() + ":" +
localBasePath,
localBasePath,
remoteBaseURL,
-
proxyPathMapper.getDefaultRequestHeaders());
+
proxyPathMapper.getDefaultRequestHeaders(),
+
proxyPathMapper.getDefaultRequestCookies());
RewriterController rewriterController =
rewriterControllerMap.get(proxyPathMapper);
Ruleset rewriterRules =
rewriterRulesetMap.get(proxyPathMapper);
@@ -314,7 +315,8 @@
new
DefaultHttpReverseProxyPathMapperImpl(proxyPathMapper.getName() + ":" +
localBasePath,
localBasePath,
remoteBaseURL,
-
proxyPathMapper.getDefaultRequestHeaders());
+
proxyPathMapper.getDefaultRequestHeaders(),
+
proxyPathMapper.getDefaultRequestCookies());
RewriterController rewriterController =
rewriterControllerMap.get(proxyPathMapper);
Ruleset rewriterRules =
rewriterRulesetMap.get(proxyPathMapper);
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=822629&r1=822628&r2=822629&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 Oct 7 09:19:36 2009
@@ -186,8 +186,22 @@
}
}
+ Map<String, String> defaultRequestCookies = null;
+ Configuration defaultRequestCookiesConf =
pathPassConf.subset("request.cookie");
+ if (!defaultRequestCookiesConf.isEmpty())
+ {
+ defaultRequestCookies = new HashMap<String, String>();
+
+ for (Iterator it = defaultRequestCookiesConf.getKeys();
it.hasNext(); )
+ {
+ String key = (String) it.next();
+ defaultRequestCookies.put(key,
defaultRequestCookiesConf.getString(key));
+ }
+ }
+
HttpReverseProxyPathMapper proxyPathMapper =
- new DefaultHttpReverseProxyPathMapperImpl(pathName,
localBasePath, remoteBaseURL, defaultRequestHeaders);
+ new DefaultHttpReverseProxyPathMapperImpl(pathName,
localBasePath, remoteBaseURL,
+
defaultRequestHeaders, defaultRequestCookies);
proxyPathMappers.add(proxyPathMapper);
Configuration rewritersConf = pathPassConf.subset("rewriter");
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=822629&r1=822628&r2=822629&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 Oct 7 09:19:36 2009
@@ -44,6 +44,7 @@
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
@@ -62,6 +63,7 @@
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
@@ -334,6 +336,21 @@
}
}
+ CookieStore cookieStore = httpClient.getCookieStore();
+
+ if (cookieStore != null)
+ {
+ Map<String, String> defaultRequestCookies =
proxyPathMapper.getDefaultRequestCookies();
+
+ if (defaultRequestCookies != null)
+ {
+ for (Map.Entry<String, String> entry :
defaultRequestCookies.entrySet())
+ {
+ cookieStore.addCookie(new
BasicClientCookie(entry.getKey(), entry.getValue()));
+ }
+ }
+ }
+
HttpEntity httpEntity = null;
try
Modified:
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/util/WebResourceUtils.java
URL:
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/util/WebResourceUtils.java?rev=822629&r1=822628&r2=822629&view=diff
==============================================================================
---
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/util/WebResourceUtils.java
(original)
+++
portals/applications/webcontent/trunk/webcontent-jar/src/main/java/org/apache/portals/applications/webcontent/util/WebResourceUtils.java
Wed Oct 7 09:19:36 2009
@@ -23,12 +23,16 @@
import java.net.URI;
import java.net.URL;
+import javax.portlet.PortletContext;
import javax.servlet.ServletContext;
import org.apache.commons.lang.StringUtils;
/**
* WebResourceUtils
+ * <P>
+ * This utility provides methods to retrieve web resources from classpath,
servlet/portlet context or file system.
+ * </P>
*
* @version $Id$
*/
@@ -39,6 +43,75 @@
{
}
+ public static URL getResource(String resourcePath)
+ {
+ return getResource(resourcePath, null, null);
+ }
+
+ public static URL getResource(String resourcePath, ClassLoader classloader)
+ {
+ return getResource(resourcePath, classloader, null);
+ }
+
+ public static URL getResource(String resourcePath, Object context)
+ {
+ return getResource(resourcePath, null, context);
+ }
+
+ public static URL getResource(String resourcePath, ClassLoader
classloader, Object context)
+ {
+ if (resourcePath == null)
+ {
+ return null;
+ }
+
+ try
+ {
+ if (StringUtils.startsWith(resourcePath, "file:"))
+ {
+ File file = new File(URI.create(resourcePath));
+
+ if (file.exists())
+ {
+ return file.toURL();
+ }
+ }
+ else if (classloader != null &&
StringUtils.startsWith(resourcePath, "classpath:"))
+ {
+ return classloader.getResource(resourcePath.substring(10));
+ }
+ else if (context != null)
+ {
+ if (context instanceof ServletContext)
+ {
+ return ((ServletContext)
context).getResource(resourcePath);
+ }
+ else if (context instanceof PortletContext)
+ {
+ return ((PortletContext)
context).getResource(resourcePath);
+ }
+ else
+ {
+ throw new IllegalArgumentException("The context should be
either servlet context or portlet context.");
+ }
+ }
+ else
+ {
+ File file = new File(resourcePath);
+
+ if (file.exists())
+ {
+ return file.toURL();
+ }
+ }
+ }
+ catch (Exception ignore)
+ {
+ }
+
+ return null;
+ }
+
public static File getResourceAsFile(String resourcePath)
{
return getResourceAsFile(resourcePath, null, null);
@@ -49,23 +122,25 @@
return getResourceAsFile(resourcePath, classloader, null);
}
- public static File getResourceAsFile(String resourcePath, ServletContext
servletContext)
+ public static File getResourceAsFile(String resourcePath, Object context)
{
- return getResourceAsFile(resourcePath, null, servletContext);
+ return getResourceAsFile(resourcePath, null, context);
}
- public static File getResourceAsFile(String resourcePath, ClassLoader
classloader, ServletContext servletContext)
+ public static File getResourceAsFile(String resourcePath, ClassLoader
classloader, Object context)
{
if (resourcePath == null)
{
return null;
}
+ File file = null;
+
try
{
if (StringUtils.startsWith(resourcePath, "file:"))
{
- return new File(URI.create(resourcePath));
+ file = new File(URI.create(resourcePath));
}
else if (classloader != null &&
StringUtils.startsWith(resourcePath, "classpath:"))
{
@@ -73,23 +148,34 @@
if (resourceURL != null &&
"file".equals(resourceURL.getProtocol()))
{
- return new File(resourceURL.toURI());
+ file = new File(resourceURL.toURI());
}
}
- else if (servletContext != null)
+ else if (context != null)
{
- return new File(servletContext.getRealPath(resourcePath));
+ if (context instanceof ServletContext)
+ {
+ file = new File(((ServletContext)
context).getRealPath(resourcePath));
+ }
+ else if (context instanceof PortletContext)
+ {
+ file = new File(((PortletContext)
context).getRealPath(resourcePath));
+ }
+ else
+ {
+ throw new IllegalArgumentException("The context should be
either servlet context or portlet context.");
+ }
}
else
{
- return new File(resourcePath);
+ file = new File(resourcePath);
}
}
catch (Exception ignore)
{
}
- return null;
+ return ((file != null && file.exists()) ? file : null);
}
public static InputStream getResourceAsStream(String resourcePath) throws
IOException
@@ -102,12 +188,12 @@
return getResourceAsStream(resourcePath, classloader, null);
}
- public static InputStream getResourceAsStream(String resourcePath,
ServletContext servletContext) throws IOException
+ public static InputStream getResourceAsStream(String resourcePath, Object
context) throws IOException
{
- return getResourceAsStream(resourcePath, null, servletContext);
+ return getResourceAsStream(resourcePath, null, context);
}
- public static InputStream getResourceAsStream(String resourcePath,
ClassLoader classloader, ServletContext servletContext) throws IOException
+ public static InputStream getResourceAsStream(String resourcePath,
ClassLoader classloader, Object context) throws IOException
{
if (resourcePath == null)
{
@@ -122,9 +208,20 @@
{
return classloader.getResourceAsStream(resourcePath.substring(10));
}
- else if (servletContext != null)
+ else if (context != null)
{
- return servletContext.getResourceAsStream(resourcePath);
+ if (context instanceof ServletContext)
+ {
+ return ((ServletContext)
context).getResourceAsStream(resourcePath);
+ }
+ else if (context instanceof PortletContext)
+ {
+ return ((PortletContext)
context).getResourceAsStream(resourcePath);
+ }
+ else
+ {
+ throw new IllegalArgumentException("The context should be
either servlet context or portlet context.");
+ }
}
else
{
Modified:
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestProxyPathMappings.java
URL:
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestProxyPathMappings.java?rev=822629&r1=822628&r2=822629&view=diff
==============================================================================
---
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestProxyPathMappings.java
(original)
+++
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestProxyPathMappings.java
Wed Oct 7 09:19:36 2009
@@ -43,13 +43,13 @@
Map<HttpReverseProxyPathMapper, RewriterController>
rewriterControllerMap = new HashMap<HttpReverseProxyPathMapper,
RewriterController>();
Map<HttpReverseProxyPathMapper, Ruleset> rewriterRulesetMap = new
HashMap<HttpReverseProxyPathMapper, Ruleset>();
List<HttpReverseProxyPathMapper> proxyPathMappers = new
ArrayList<HttpReverseProxyPathMapper>();
- proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("localhost", "/localhost/",
"http://www.localhost.com/", null));
- proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("localhost1", "/localhost/path1/",
"http://www.localhost.com/path1/", null));
- proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("localhost2", "/localhost/path1/path2/",
"http://www.localhost.com/path1/path2/", null));
- proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("apache0", "/apache/",
"http://apache.org/", null));
- proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("apache", "/*_apache/",
"http://$1.apache.org/", null));
- proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("secure_apache", "/secure/*_apache/",
"https://$1.apache.org/", null));
- proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("google", "/*.google.*/",
"http://$1.google.$2/", null));
+ proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("localhost", "/localhost/",
"http://www.localhost.com/"));
+ proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("localhost1", "/localhost/path1/",
"http://www.localhost.com/path1/"));
+ proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("localhost2", "/localhost/path1/path2/",
"http://www.localhost.com/path1/path2/"));
+ proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("apache0", "/apache/",
"http://apache.org/"));
+ proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("apache", "/*_apache/",
"http://$1.apache.org/"));
+ proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("secure_apache", "/secure/*_apache/",
"https://$1.apache.org/"));
+ proxyPathMappers.add(new
DefaultHttpReverseProxyPathMapperImpl("google", "/*.google.*/",
"http://$1.google.$2/"));
pathMapperProvider = new
DefaultHttpReverseProxyPathMapperProviderImpl(proxyPathMappers,
rewriterControllerMap, rewriterRulesetMap);
pathMapperProvider.setMaxMatchingPathPartCount(3);
}
Modified:
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestReverseProxyLinkRewritingParserAaptor.java
URL:
http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestReverseProxyLinkRewritingParserAaptor.java?rev=822629&r1=822628&r2=822629&view=diff
==============================================================================
---
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestReverseProxyLinkRewritingParserAaptor.java
(original)
+++
portals/applications/webcontent/trunk/webcontent-jar/src/test/java/org/apache/portals/applications/webcontent/proxy/TestReverseProxyLinkRewritingParserAaptor.java
Wed Oct 7 09:19:36 2009
@@ -70,10 +70,10 @@
@Override
public void setUp()
{
- apacheProxyPathMapper = new
DefaultHttpReverseProxyPathMapperImpl("apache", "/*_apache/",
"http://$1.apache.org/", null);
- apache0ProxyPathMapper = new
DefaultHttpReverseProxyPathMapperImpl("apache0", "/apache/",
"http://apache.org/", null);
- securedApacheProxyPathMapper = new
DefaultHttpReverseProxyPathMapperImpl("secure_apache", "/secure/*_apache/",
"https://$1.apache.org/", null);
- localhostProxyPathMapper = new
DefaultHttpReverseProxyPathMapperImpl("localhost", "/localhost/",
"http://www.localhost.com/", null);
+ apacheProxyPathMapper = new
DefaultHttpReverseProxyPathMapperImpl("apache", "/*_apache/",
"http://$1.apache.org/");
+ apache0ProxyPathMapper = new
DefaultHttpReverseProxyPathMapperImpl("apache0", "/apache/",
"http://apache.org/");
+ securedApacheProxyPathMapper = new
DefaultHttpReverseProxyPathMapperImpl("secure_apache", "/secure/*_apache/",
"https://$1.apache.org/");
+ localhostProxyPathMapper = new
DefaultHttpReverseProxyPathMapperImpl("localhost", "/localhost/",
"http://www.localhost.com/");
Map<HttpReverseProxyPathMapper, RewriterController>
rewriterControllerMap = new HashMap<HttpReverseProxyPathMapper,
RewriterController>();
Map<HttpReverseProxyPathMapper, Ruleset> rewriterRulesetMap = new
HashMap<HttpReverseProxyPathMapper, Ruleset>();
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=822629&r1=822628&r2=822629&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 Oct 7 09:19:36 2009
@@ -126,3 +126,4 @@
proxy.reverse.pass.somewhere.rewriter.rules =
/WEB-INF/conf/default-rewriter-rules.xml
proxy.reverse.pass.somewhere.request.header.Accept-Language = en
proxy.reverse.pass.somewhere.request.header.X-Custom-Header = A custom value
+proxy.reverse.pass.somewhere.request.cookie.CustomCookie = A value