|
From: <ath...@us...> - 2014-03-18 13:51:14
|
Revision: 1576
http://sourceforge.net/p/webassembletool/code/1576
Author: athaveau
Date: 2014-03-18 13:51:06 +0000 (Tue, 18 Mar 2014)
Log Message:
-----------
#289 : new parameter stripMappingPath to perform the mapping path removal
Modified Paths:
--------------
trunk/esigate-app-aggregator/src/main/resources/esigate.properties
trunk/esigate-app-casified-aggregator/src/main/resources/esigate.properties
trunk/esigate-app-master/src/main/resources/esigate.properties
trunk/esigate-app-master/src/test/java/org/esigate/test/cases/MasterTest.java
trunk/esigate-core/src/main/java/org/esigate/DriverConfiguration.java
trunk/esigate-core/src/main/java/org/esigate/Parameters.java
trunk/esigate-core/src/test/java/org/esigate/DriverConfigurationTest.java
trunk/esigate-servlet/src/main/java/org/esigate/servlet/ProxyFilter.java
trunk/esigate-servlet/src/main/java/org/esigate/servlet/impl/DriverSelector.java
trunk/esigate-servlet/src/main/java/org/esigate/servlet/impl/RequestUrl.java
trunk/esigate-servlet/src/test/java/org/esigate/servlet/ProxyFilterTest.java
trunk/src/site/xdoc/reference.xml
Modified: trunk/esigate-app-aggregator/src/main/resources/esigate.properties
===================================================================
--- trunk/esigate-app-aggregator/src/main/resources/esigate.properties 2014-02-15 23:26:55 UTC (rev 1575)
+++ trunk/esigate-app-aggregator/src/main/resources/esigate.properties 2014-03-18 13:51:06 UTC (rev 1576)
@@ -18,6 +18,7 @@
aggregated2.storeCookiesInSession=test0
aggregated2.maxConnectionsPerHost=60
aggregated2.mappings=/ag2/*
+aggregated2.stripMappingPath=true
nocacheaggregated1.remoteUrlBase=http://localhost:8080/esigate-app-aggregated1/
nocacheaggregated1.extensions=org.esigate.extension.Esi,org.esigate.extension.Aggregate
@@ -30,10 +31,12 @@
nocacheaggregated1.maxConnectionsPerHost=50
nocacheaggregated1.mappings=/nocache/ag1/*
nocacheaggregated1.preserveHost=false
+nocacheaggregated1.stripMappingPath=true
preservehostaggregated1.remoteUrlBase=http://localhost:8080/esigate-app-aggregated1/
preservehostaggregated1.preserveHost=true
preservehostaggregated1.mappings=/preservehost/*
+preservehostaggregated1.stripMappingPath=true
local.extensions=org.esigate.servlet.ServletExtension,org.esigate.extension.Esi
local.mappings=/local/*
@@ -43,3 +46,4 @@
crosscontextaggregated2.extensions=org.esigate.servlet.ServletExtension,org.esigate.extension.Esi
crosscontextaggregated2.context=/esigate-app-aggregated2
crosscontextaggregated2.mappings=/crosscontext/*
+crosscontextaggregated2.stripMappingPath=true
\ No newline at end of file
Modified: trunk/esigate-app-casified-aggregator/src/main/resources/esigate.properties
===================================================================
--- trunk/esigate-app-casified-aggregator/src/main/resources/esigate.properties 2014-02-15 23:26:55 UTC (rev 1575)
+++ trunk/esigate-app-casified-aggregator/src/main/resources/esigate.properties 2014-03-18 13:51:06 UTC (rev 1576)
@@ -1,6 +1,8 @@
default.remoteUrlBase=http://localhost:8080/esigate-app-casified-aggregated1/
default.extensions=org.esigate.cas.CasAuthenticationHandler,org.esigate.extension.Aggregate
default.mappings=/aggregated1/*
+default.stripMappingPath=true
aggregated2.remoteUrlBase=http://localhost:8080/esigate-app-casified-aggregated2/
aggregated2.extensions=org.esigate.cas.CasAuthenticationHandler,org.esigate.extension.Aggregate
+aggregated2.stripMappingPath=true
\ No newline at end of file
Modified: trunk/esigate-app-master/src/main/resources/esigate.properties
===================================================================
--- trunk/esigate-app-master/src/main/resources/esigate.properties 2014-02-15 23:26:55 UTC (rev 1575)
+++ trunk/esigate-app-master/src/main/resources/esigate.properties 2014-03-18 13:51:06 UTC (rev 1576)
@@ -18,3 +18,12 @@
proxy2.preserveHost=true
proxy2.ttl=5
proxy2.mappings=/images/*
+proxy2.stripMappingPath=true
+
+
+proxy3.remoteUrlBase=http://localhost:8080/esigate-app-provider/
+proxy3.extensions=
+proxy3.preserveHost=true
+proxy3.ttl=5
+proxy3.mappings=/css/*
+proxy3.stripMappingPath=false
\ No newline at end of file
Modified: trunk/esigate-app-master/src/test/java/org/esigate/test/cases/MasterTest.java
===================================================================
--- trunk/esigate-app-master/src/test/java/org/esigate/test/cases/MasterTest.java 2014-02-15 23:26:55 UTC (rev 1575)
+++ trunk/esigate-app-master/src/test/java/org/esigate/test/cases/MasterTest.java 2014-03-18 13:51:06 UTC (rev 1576)
@@ -89,13 +89,21 @@
"You should see here the same hostname and port as in the browser=>127.0.0.1:8080", resp.getText());
}
- public void testProxy() throws Exception {
+ public void testProxyWithStripMappingPath() throws Exception {
WebRequest req = new GetMethodWebRequest(APPLICATION_PATH + "images/smile.jpg");
WebResponse resp = webConversation.getResponse(req);
assertEquals("Status should be 200", HttpServletResponse.SC_OK, resp.getResponseCode());
assertEquals("image/jpeg", resp.getContentType().toLowerCase());
}
+ public void testProxyWithoutStripMappingPath() throws Exception {
+ WebRequest req = new GetMethodWebRequest(APPLICATION_PATH + "css/sample.css");
+ WebResponse resp = webConversation.getResponse(req);
+ assertEquals("Status should be 200", HttpServletResponse.SC_OK, resp.getResponseCode());
+ assertEquals("text/css", resp.getContentType().toLowerCase());
+ }
+
+
public void testProxyIfmodifiedsince() throws Exception {
WebRequest req = new GetMethodWebRequest(APPLICATION_PATH + "images/smile.jpg");
WebResponse resp = webConversation.getResponse(req);
Modified: trunk/esigate-core/src/main/java/org/esigate/DriverConfiguration.java
===================================================================
--- trunk/esigate-core/src/main/java/org/esigate/DriverConfiguration.java 2014-02-15 23:26:55 UTC (rev 1575)
+++ trunk/esigate-core/src/main/java/org/esigate/DriverConfiguration.java 2014-03-18 13:51:06 UTC (rev 1576)
@@ -44,14 +44,16 @@
private final BaseUrlRetrieveStrategy baseUrlRetrieveStrategy;
private final boolean isVisibleBaseURLEmpty;
private final List<UriMapping> uriMappings;
+ private boolean stripMappingPath;
public DriverConfiguration(String instanceName, Properties props) {
this.instanceName = instanceName;
- baseUrlRetrieveStrategy = getBaseUrlRetrieveSession(props);
- uriEncoding = Parameters.URI_ENCODING.getValueString(props);
- preserveHost = Parameters.PRESERVE_HOST.getValueBoolean(props);
- visibleBaseURL = Parameters.VISIBLE_URL_BASE.getValueString(props);
- isVisibleBaseURLEmpty = StringUtils.isEmpty(visibleBaseURL);
+ this.baseUrlRetrieveStrategy = getBaseUrlRetrieveSession(props);
+ this.uriEncoding = Parameters.URI_ENCODING.getValueString(props);
+ this.preserveHost = Parameters.PRESERVE_HOST.getValueBoolean(props);
+ this.visibleBaseURL = Parameters.VISIBLE_URL_BASE.getValueString(props);
+ this.isVisibleBaseURLEmpty = StringUtils.isEmpty(visibleBaseURL);
+ this.stripMappingPath= Parameters.STRIP_MAPPING_PATH.getValueBoolean(props);
this.uriMappings = parseMappings(props);
properties = props;
}
@@ -129,4 +131,11 @@
return this.uriMappings;
}
+ /**
+ *
+ * @return stripMappingPath
+ */
+ public boolean isStripMappingPath() {
+ return stripMappingPath;
+ }
}
Modified: trunk/esigate-core/src/main/java/org/esigate/Parameters.java
===================================================================
--- trunk/esigate-core/src/main/java/org/esigate/Parameters.java 2014-02-15 23:26:55 UTC (rev 1575)
+++ trunk/esigate-core/src/main/java/org/esigate/Parameters.java 2014-03-18 13:51:06 UTC (rev 1576)
@@ -42,6 +42,7 @@
// Core parameters
public static final Parameter REMOTE_URL_BASE = new Parameter("remoteUrlBase", null);
public static final Parameter MAPPINGS = new Parameter("mappings", null);
+ public static final Parameter STRIP_MAPPING_PATH = new Parameter("stripMappingPath", null);
public static final Parameter URI_ENCODING = new Parameter("uriEncoding", "ISO-8859-1");
public static final Parameter PARSABLE_CONTENT_TYPES = new Parameter("parsableContentTypes",
"text/html, application/xhtml+xml");
Modified: trunk/esigate-core/src/test/java/org/esigate/DriverConfigurationTest.java
===================================================================
--- trunk/esigate-core/src/test/java/org/esigate/DriverConfigurationTest.java 2014-02-15 23:26:55 UTC (rev 1575)
+++ trunk/esigate-core/src/test/java/org/esigate/DriverConfigurationTest.java 2014-03-18 13:51:06 UTC (rev 1576)
@@ -79,4 +79,21 @@
}
}
+ public void testStripMappingPath() {
+ Properties properties = new Properties();
+ properties.setProperty(Parameters.REMOTE_URL_BASE.getName(), "http://example.com");
+
+ DriverConfiguration config = new DriverConfiguration("testStripMappingPath", properties);
+ assertFalse(config.isStripMappingPath());
+ properties.setProperty(Parameters.STRIP_MAPPING_PATH.getName(), "false");
+ config = new DriverConfiguration("testStripMappingPath", properties);
+ assertFalse(config.isStripMappingPath());
+
+ properties.setProperty(Parameters.STRIP_MAPPING_PATH.getName(), "true");
+ config = new DriverConfiguration("testStripMappingPath", properties);
+ assertTrue(config.isStripMappingPath());
+
+
+
+ }
}
Modified: trunk/esigate-servlet/src/main/java/org/esigate/servlet/ProxyFilter.java
===================================================================
--- trunk/esigate-servlet/src/main/java/org/esigate/servlet/ProxyFilter.java 2014-02-15 23:26:55 UTC (rev 1575)
+++ trunk/esigate-servlet/src/main/java/org/esigate/servlet/ProxyFilter.java 2014-03-18 13:51:06 UTC (rev 1576)
@@ -25,16 +25,14 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.apache.commons.lang3.tuple.Pair;
+
import org.apache.http.client.methods.CloseableHttpResponse;
-import org.esigate.Driver;
+
import org.esigate.DriverFactory;
import org.esigate.HttpErrorPage;
import org.esigate.http.IncomingRequest;
-import org.esigate.impl.UriMapping;
import org.esigate.servlet.impl.DriverSelector;
import org.esigate.servlet.impl.RequestFactory;
-import org.esigate.servlet.impl.RequestUrl;
import org.esigate.servlet.impl.ResponseSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -58,12 +56,10 @@
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
IncomingRequest incomingRequest = requestFactory.create(httpServletRequest, httpServletResponse, chain);
- Pair<Driver, UriMapping> dm = null;
+
try {
- dm = driverSelector.selectProvider(httpServletRequest, false);
- String relUrl = RequestUrl.getRelativeUrl(httpServletRequest, dm.getRight(), false);
- LOG.debug("Proxying {}", relUrl);
- CloseableHttpResponse driverResponse = dm.getLeft().proxy(relUrl, incomingRequest);
+ DriverSelector.ProviderContext dm = driverSelector.selectProvider(httpServletRequest, false);
+ CloseableHttpResponse driverResponse = dm.getDriver().proxy(dm.getRelUrl(), incomingRequest);
responseSender.sendResponse(driverResponse, incomingRequest, httpServletResponse);
} catch (HttpErrorPage e) {
if (!httpServletResponse.isCommitted()) {
Modified: trunk/esigate-servlet/src/main/java/org/esigate/servlet/impl/DriverSelector.java
===================================================================
--- trunk/esigate-servlet/src/main/java/org/esigate/servlet/impl/DriverSelector.java 2014-02-15 23:26:55 UTC (rev 1575)
+++ trunk/esigate-servlet/src/main/java/org/esigate/servlet/impl/DriverSelector.java 2014-03-18 13:51:06 UTC (rev 1576)
@@ -26,14 +26,14 @@
/**
* This class handles support of legacy options for Driver selection.
- * <p>
+ * <p/>
* Configuration is evaluated in the following order :
* <ol>
* <li>providers mapping (host-based) in web.xml</li>
* <li>provider mapping (servlet based) in web.xml</li>
* <li>provider mapping in configuration file (eg. instance.mappings=/* in esigate.properties)</li>
* </ol>
- *
+ *
* @author Nicolas Richeton
*/
public final class DriverSelector {
@@ -44,27 +44,65 @@
/**
* Select the provider for this request.
- * <p>
+ * <p/>
* Perform selection based on the Host header.
- *
- * @param request
+ *
+ * @param request incoming request
+ * @return Pair Driver/UriMapping
+ * @throws HttpErrorPage
+ */
+ public ProviderContext selectProvider(HttpServletRequest request) throws HttpErrorPage {
+ return selectProvider(request, false);
+ }
+
+ /**
+ * Select the provider for this request.
+ * <p/>
+ * Perform selection based on the incoming request url.
+ *
+ * @param request incoming request
* @param servlet
* @return provider name or null.
* @throws HttpErrorPage
*/
- public Pair<Driver, UriMapping> selectProvider(HttpServletRequest request, boolean servlet) throws HttpErrorPage {
+ public ProviderContext selectProvider(HttpServletRequest request, boolean servlet) throws HttpErrorPage {
String host = request.getHeader("Host");
String scheme = request.getScheme();
- String relUrl = RequestUrl.getRelativeUrl(request, null, servlet);
+ String relUrl = RequestUrl.getRelativeUrl(request, servlet);
- Pair<Driver, UriMapping> result;
+ Pair<Driver, UriMapping> result = DriverFactory.getInstanceFor(scheme, host, relUrl);
- result = DriverFactory.getInstanceFor(scheme, host, relUrl);
+ ProviderContext context = new ProviderContext();
+ Driver driver = result.getLeft();
+ UriMapping uriMapping = result.getRight();
+
+ if(driver.getConfiguration().isStripMappingPath()){
+ relUrl = RequestUrl.stripMappingPath(relUrl, uriMapping);
+ }
+ context.driver = driver;
+ context.relUrl = relUrl;
LOG.debug("Selected {} for scheme:{} host:{} relUrl:{}", result, scheme, host, relUrl);
- return result;
+ return context;
}
+
+ /**
+ * The provider context, composed of driver and remote relative url
+ */
+ public class ProviderContext {
+ private Driver driver;
+ private String relUrl;
+
+ public String getRelUrl() {
+ return relUrl;
+ }
+
+ public Driver getDriver() {
+ return driver;
+ }
+
+ }
}
Modified: trunk/esigate-servlet/src/main/java/org/esigate/servlet/impl/RequestUrl.java
===================================================================
--- trunk/esigate-servlet/src/main/java/org/esigate/servlet/impl/RequestUrl.java 2014-02-15 23:26:55 UTC (rev 1575)
+++ trunk/esigate-servlet/src/main/java/org/esigate/servlet/impl/RequestUrl.java 2014-03-18 13:51:06 UTC (rev 1576)
@@ -22,9 +22,9 @@
/**
* Utility class to work with Servlet request urls.
- *
+ *
* @author Nicolas Richeton
- *
+ *
*/
public final class RequestUrl {
private static final Logger LOG = LoggerFactory.getLogger(RequestUrl.class);
@@ -35,31 +35,26 @@
/**
* Get the relative url to the current servlet.
- * <p>
- * Uses the request URI and removes : the context path, the servlet path and the mapping path if used.
- *
+ * <p/>
+ * Uses the request URI and removes : the context path, the servlet path if used.
+ *
* @param request
* The current HTTP request
- * @param mapping
- * matched mapping or null
- * @param servlet
+ * @param servlet
+ * true to remove the servlet path
* @return the url, relative to the servlet mapping.
*/
- public static String getRelativeUrl(HttpServletRequest request, UriMapping mapping, boolean servlet) {
+ public static String getRelativeUrl(HttpServletRequest request, boolean servlet) {
// Raw request url
- String relativeUrl = request.getRequestURI();
+ String requestURI = request.getRequestURI();
// Application (war) context path
String contextPath = request.getContextPath();
// Servlet mapping
String servletPath = request.getServletPath();
- // Uri mapping
- String mappingPath = (mapping == null ? null : mapping.getPath());
- if (LOG.isDebugEnabled()) {
- LOG.debug("relativeUrl: {}, contextPath: {}, servletPath: {}, mappingPath: {}", new Object[] {relativeUrl,
- contextPath, servletPath, mappingPath});
- }
+ String relativeUrl = requestURI;
+
// Remove application context path
if (contextPath != null && relativeUrl.startsWith(contextPath)) {
relativeUrl = relativeUrl.substring(contextPath.length());
@@ -70,11 +65,37 @@
relativeUrl = relativeUrl.substring(servletPath.length());
}
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("requestURI: {}, contextPath: {}, servletPath: {}, relativeUrl: {}, ", new Object[]{requestURI,
+ contextPath, servletPath, relativeUrl});
+ }
+
+ return relativeUrl;
+ }
+
+ /**
+ * Get the relative url without the mapping url.
+ * <p/>
+ * Uses the url and remove the mapping path.
+ *
+ * @param url
+ * incoming relative url
+ * @return the url, relative to the driver remote url.
+ */
+ public static String stripMappingPath(String url, UriMapping mapping) {
+ String relativeUrl = url;
+ // Uri mapping
+ String mappingPath = (mapping == null ? null : mapping.getPath());
+
+
// Remove mapping path
- if (mappingPath != null && relativeUrl.startsWith(mappingPath)) {
+ if (mappingPath != null && url.startsWith(mappingPath)) {
relativeUrl = relativeUrl.substring(mappingPath.length());
}
-
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("url: {}, mappingPath: {}, relativeUrl: {}", new Object[]{url, mappingPath, relativeUrl});
+ }
return relativeUrl;
}
}
Modified: trunk/esigate-servlet/src/test/java/org/esigate/servlet/ProxyFilterTest.java
===================================================================
--- trunk/esigate-servlet/src/test/java/org/esigate/servlet/ProxyFilterTest.java 2014-02-15 23:26:55 UTC (rev 1575)
+++ trunk/esigate-servlet/src/test/java/org/esigate/servlet/ProxyFilterTest.java 2014-03-18 13:51:06 UTC (rev 1576)
@@ -104,7 +104,7 @@
Mockito.when(request.getServletPath()).thenReturn("servlet");
Mockito.when(request.getHeader("Host")).thenReturn("sub2.domain.com");
Mockito.when(request.getScheme()).thenReturn("http");
- Assert.assertEquals("provider2", driverSelector.selectProvider(request, true).getLeft().getConfiguration()
+ Assert.assertEquals("provider2", driverSelector.selectProvider(request, true).getDriver().getConfiguration()
.getInstanceName());
request = Mockito.mock(HttpServletRequest.class);
@@ -115,7 +115,7 @@
Mockito.when(request.getServletPath()).thenReturn("servlet");
Mockito.when(request.getHeader("Host")).thenReturn("sub.domain.com");
Mockito.when(request.getScheme()).thenReturn("http");
- Assert.assertEquals("provider1", driverSelector.selectProvider(request, true).getLeft().getConfiguration()
+ Assert.assertEquals("provider1", driverSelector.selectProvider(request, true).getDriver().getConfiguration()
.getInstanceName());
request = Mockito.mock(HttpServletRequest.class);
@@ -125,7 +125,7 @@
Mockito.when(request.getMethod()).thenReturn("GET");
Mockito.when(request.getServletPath()).thenReturn("servlet");
Mockito.when(request.getScheme()).thenReturn("http");
- Assert.assertEquals("single", driverSelector.selectProvider(request, true).getLeft().getConfiguration()
+ Assert.assertEquals("single", driverSelector.selectProvider(request, true).getDriver().getConfiguration()
.getInstanceName());
}
Modified: trunk/src/site/xdoc/reference.xml
===================================================================
--- trunk/src/site/xdoc/reference.xml 2014-02-15 23:26:55 UTC (rev 1575)
+++ trunk/src/site/xdoc/reference.xml 2014-03-18 13:51:06 UTC (rev 1576)
@@ -125,6 +125,13 @@
# Sample 4 : Process all files in css and images directories
theme.remoteUrlBase=http://host4/
theme.mappings=/css/*, /images/*
+
+# Sample 5 : Process all files in css and images directories and strip mapping path
+theme.remoteUrlBase=http://host4/css-and-images/
+theme.mappings=/css/*, /images/*
+theme.stripMappingPath=true
+
+
]]></source>
<p>
@@ -166,6 +173,36 @@
<td>Yes</td>
<td>-</td>
</tr>
+ <tr>
+ <td>mappings</td>
+ <td>
+ Paths mappings specifies the Urls pattern for wich the remote application should be called. Eg:
+ <code>mappings=/cms/*</code>
+ Use a comma-separated list to define several mappings for same provider.
+
+ <p/>
+ Mapping is split in 3 parts :
+ <ul>
+ <li>Host, including the scheme and port : http://www.example:8080</li>
+ <li>path, left part before the wildcard caracter *</li>
+ <li>extension, right part after the wildcard caracter *</li>
+ </ul>
+ </td>
+ <td>No</td>
+ <td>-</td>
+ </tr>
+ <tr>
+ <td>stripMappingPath</td>
+ <td>
+ If enabled, the mapping path will be striped from incoming url before calling the remote application. Eg:
+ <code>mappings=/cms/*</code>.
+ With <code>stripMappingPath=true</code>A request to http://esigate/cms/myresource will be proxied to http://remote/myresource
+ With <code>stripMappingPath=false</code>A request to http://esigate/cms/myresource will proxied to http://remote/cms/myresource
+
+ </td>
+ <td>No</td>
+ <td>false</td>
+ </tr>
<tr>
<td>uriEncoding</td>
<td>Charset used for encoding parameters in URI</td>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|