chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From f...@apache.org
Subject svn commit: r1434119 - in /chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src: main/java/org/apache/chemistry/opencmis/server/filter/ test/java/org/apache/chemistry/opencmis/server/impl/
Date Wed, 16 Jan 2013 19:19:30 GMT
Author: fmui
Date: Wed Jan 16 19:19:29 2013
New Revision: 1434119

URL: http://svn.apache.org/viewvc?rev=1434119&view=rev
Log:
CMIS-621: added base path to proxy filter

Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyFilter.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyHttpServletRequestWrapper.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ProxyRequestTest.java

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyFilter.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyFilter.java?rev=1434119&r1=1434118&r2=1434119&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyFilter.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyFilter.java
Wed Jan 16 19:19:29 2013
@@ -35,11 +35,15 @@ import javax.servlet.http.HttpServletReq
  */
 public class ProxyFilter implements Filter {
 
+    public static final String PARAM_BASE_PATH = "basePath";
     public static final String PARAM_TRUSTED_PROXIES = "trustedProxies";
 
+    private String basePath;
     private Pattern trustedProxies;
 
     public void init(FilterConfig filterConfig) throws ServletException {
+        basePath = filterConfig.getInitParameter(PARAM_BASE_PATH);
+
         trustedProxies = null;
         String trustedProxiesString = filterConfig.getInitParameter(PARAM_TRUSTED_PROXIES);
         if (trustedProxiesString != null) {
@@ -57,7 +61,7 @@ public class ProxyFilter implements Filt
         // check for trusted proxy
         if (trustedProxies != null && (request instanceof HttpServletRequest)
                 && trustedProxies.matcher(request.getRemoteAddr()).matches()) {
-            request = new ProxyHttpServletRequestWrapper((HttpServletRequest) request);
+            request = new ProxyHttpServletRequestWrapper((HttpServletRequest) request, basePath);
         }
 
         // call next

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyHttpServletRequestWrapper.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyHttpServletRequestWrapper.java?rev=1434119&r1=1434118&r2=1434119&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyHttpServletRequestWrapper.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/filter/ProxyHttpServletRequestWrapper.java
Wed Jan 16 19:19:29 2013
@@ -31,8 +31,11 @@ public class ProxyHttpServletRequestWrap
     private String scheme;
     private String serverName;
     private int serverPort;
+    private final String contextPath;
+    private final String servletPath;
+    private final String requestURI;
 
-    public ProxyHttpServletRequestWrapper(HttpServletRequest request) {
+    public ProxyHttpServletRequestWrapper(HttpServletRequest request, String basePath) {
         super(request);
 
         scheme = request.getHeader(FORWARDED_PROTO_HEADER);
@@ -59,6 +62,19 @@ public class ProxyHttpServletRequestWrap
                 }
             }
         }
+
+        servletPath = request.getServletPath();
+
+        if (basePath != null) {
+            final String path = request.getRequestURI().substring(
+                    request.getContextPath().length() + request.getServletPath().length());
+
+            contextPath = (basePath.startsWith("/") ? basePath : "/" + basePath);
+            requestURI = contextPath + servletPath + path;
+        } else {
+            contextPath = request.getContextPath();
+            requestURI = request.getRequestURI();
+        }
     }
 
     private int getDefaultPort(String scheme) {
@@ -83,4 +99,32 @@ public class ProxyHttpServletRequestWrap
     public int getServerPort() {
         return serverPort;
     }
+
+    @Override
+    public String getContextPath() {
+        return contextPath;
+    }
+
+    @Override
+    public String getServletPath() {
+        return servletPath;
+    }
+
+    @Override
+    public String getRequestURI() {
+        return requestURI;
+    }
+
+    @Override
+    public StringBuffer getRequestURL() {
+        StringBuffer sb = new StringBuffer();
+        sb.append(scheme);
+        sb.append("://");
+        sb.append(serverName);
+        sb.append(":");
+        sb.append(serverPort);
+        sb.append(getRequestURI());
+
+        return sb;
+    }
 }

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ProxyRequestTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ProxyRequestTest.java?rev=1434119&r1=1434118&r2=1434119&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ProxyRequestTest.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ProxyRequestTest.java
Wed Jan 16 19:19:29 2013
@@ -59,6 +59,7 @@ public class ProxyRequestTest {
         when(this.request.getServerPort()).thenReturn(BACKEND_SERVER_PORT);
         when(this.request.getContextPath()).thenReturn(CONTEXT_PATH);
         when(this.request.getServletPath()).thenReturn(SERVLET_PATH);
+        when(this.request.getRequestURI()).thenReturn(CONTEXT_PATH + "/" + SERVLET_PATH);
     }
 
     @Test
@@ -66,7 +67,7 @@ public class ProxyRequestTest {
         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_PROTO_HEADER)).thenReturn(
                 FORWARDED_HTTPS_PROTO);
 
-        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request,
null);
 
         assertEquals(FORWARDED_HTTPS_PROTO, proxyRequest.getScheme());
 
@@ -85,7 +86,7 @@ public class ProxyRequestTest {
         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
                 FORWARDED_SERVER_NAME);
 
-        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request,
null);
 
         assertEquals(FORWARDED_HTTPS_PROTO, proxyRequest.getScheme());
         assertEquals(FORWARDED_SERVER_NAME, proxyRequest.getServerName());
@@ -103,7 +104,7 @@ public class ProxyRequestTest {
         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
                 FORWARDED_SERVER_NAME);
 
-        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request,
null);
 
         assertEquals(FORWARDED_SERVER_NAME, proxyRequest.getServerName());
 
@@ -116,10 +117,29 @@ public class ProxyRequestTest {
     }
 
     @Test
+    public void testGetProxiedHostBaseAddressAndPath() throws URISyntaxException {
+        String path = "/test";
+
+        when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
+                FORWARDED_SERVER_NAME);
+
+        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request,
path);
+
+        assertEquals(FORWARDED_SERVER_NAME, proxyRequest.getServerName());
+
+        URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
+
+        assertEquals(BACKEND_SERVER_PROTO, baseUri.getScheme());
+        assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
+        assertEquals(-1, baseUri.getPort());
+        assertEquals(path + "/" + SERVLET_PATH + "/" + REPOSITORY_ID, baseUri.getPath());
+    }
+
+    @Test
     public void testGetProxiedHostAndPortBaseAddress() throws URISyntaxException {
         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(FORWARDED_HOST);
 
-        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request,
null);
 
         assertTrue(FORWARDED_HOST.startsWith(proxyRequest.getServerName()));
 
@@ -137,7 +157,7 @@ public class ProxyRequestTest {
                 FORWARDED_HTTPS_PROTO);
         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(FORWARDED_HOST);
 
-        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request,
null);
 
         assertEquals(FORWARDED_HTTPS_PROTO, proxyRequest.getScheme());
         assertTrue(FORWARDED_HOST.startsWith(proxyRequest.getServerName()));
@@ -157,7 +177,7 @@ public class ProxyRequestTest {
         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
                 FORWARDED_SERVER_NAME + ":noportnumber");
 
-        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request,
null);
 
         assertEquals(FORWARDED_HTTP_PROTO, proxyRequest.getScheme());
         assertTrue(FORWARDED_HOST.startsWith(proxyRequest.getServerName()));
@@ -177,7 +197,7 @@ public class ProxyRequestTest {
         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
                 FORWARDED_SERVER_NAME + ":noportnumber");
 
-        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
+        ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request,
null);
 
         assertEquals(FORWARDED_HTTPS_PROTO, proxyRequest.getScheme());
         assertTrue(FORWARDED_HOST.startsWith(proxyRequest.getServerName()));



Mime
View raw message