You are viewing a plain text version of this content. The canonical link for it is here.
Posted to portalapps-dev@portals.apache.org by wo...@apache.org on 2014/07/22 07:10:28 UTC

svn commit: r1612465 - in /portals/applications/webcontent/trunk/reverse-proxy/src: main/java/org/apache/portals/applications/webcontent2/proxy/ main/java/org/apache/portals/applications/webcontent2/proxy/command/ main/java/org/apache/portals/applicati...

Author: woonsan
Date: Tue Jul 22 05:10:28 2014
New Revision: 1612465

URL: http://svn.apache.org/r1612465
Log:
APA-64: adding regex mapping option

Added:
    portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/RegexProxyMapping.java
    portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/
    portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/command/
    portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/command/AddCookiesToResponseCommandTest.java
    portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/impl/
    portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/impl/RegexProxyMappingTest.java
Modified:
    portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/ProxyMapping.java
    portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/command/AddCookiesToResponseCommand.java
    portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/AbstractProxyMapping.java
    portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/SimpleProxyMapping.java
    portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/util/YamlConfigUtils.java
    portals/applications/webcontent/trunk/reverse-proxy/src/test/webapp/WEB-INF/web.xml

Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/ProxyMapping.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/ProxyMapping.java?rev=1612465&r1=1612464&r2=1612465&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/ProxyMapping.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/ProxyMapping.java Tue Jul 22 05:10:28 2014
@@ -24,10 +24,6 @@ import org.apache.portals.applications.w
 public interface ProxyMapping
 {
 
-    public String getLocalBasePath();
-
-    public String getRemoteBaseURI();
-
     public boolean matchesLocal(String localPath);
 
     public String resolveRemoteFromLocal(String localPath);

Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/command/AddCookiesToResponseCommand.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/command/AddCookiesToResponseCommand.java?rev=1612465&r1=1612464&r2=1612465&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/command/AddCookiesToResponseCommand.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/command/AddCookiesToResponseCommand.java Tue Jul 22 05:10:28 2014
@@ -25,7 +25,6 @@ import javax.servlet.http.Cookie;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.http.client.HttpClient;
-import org.apache.portals.applications.webcontent2.proxy.ProxyMapping;
 import org.apache.portals.applications.webcontent2.proxy.ReverseProxyException;
 import org.apache.portals.applications.webcontent2.proxy.impl.AbstractProxyCommand;
 import org.apache.portals.applications.webcontent2.proxy.impl.ProxyContext;
@@ -35,8 +34,6 @@ import org.apache.portals.applications.w
 public class AddCookiesToResponseCommand extends AbstractProxyCommand
 {
 
-    private static final String REMOTE_BASE_URI = AddCookiesToResponseCommand.class.getName() + ".remote.base.uri";
-
     @Override
     protected boolean executeInternal(final ProxyContext context) throws ReverseProxyException, IOException
     {
@@ -80,40 +77,18 @@ public class AddCookiesToResponseCommand
         return false;
     }
 
-    protected URI getRemoteBaseAsURI(final ProxyContext context) 
-    {
-        URI remoteBaseURI = (URI) context.get(REMOTE_BASE_URI);
-
-        if (remoteBaseURI == null) 
-        {
-            remoteBaseURI = (URI) context.put(REMOTE_BASE_URI, URI.create(context.getResolvedMapping().getRemoteBaseURI()));
-        }
-
-        return remoteBaseURI;
-    }
-
     protected String getReverseCookiePath(final ProxyContext context, final org.apache.http.cookie.Cookie responseCookie) 
     {
-        String responseCookiePath = responseCookie.getPath();
-        URI remoteBaseURI = getRemoteBaseAsURI(context);
-        String remoteBaseURIPath = remoteBaseURI.getPath();
+        String cookiePath = responseCookie.getPath();
 
-        if (StringUtils.isEmpty(remoteBaseURIPath)) 
+        if (cookiePath != null)
         {
-            remoteBaseURIPath = "/";
-        }
-
-        ProxyMapping resolvedMapping = context.getResolvedMapping();
-        String localBasePath = context.getRequestContext().getRequestBasePath() + resolvedMapping.getLocalBasePath();
-        String cookiePath = localBasePath;
-
-        if (responseCookiePath != null && responseCookiePath.startsWith(remoteBaseURIPath))
-        {
-            String subPath = StringUtils.removeStart(responseCookiePath.substring(remoteBaseURIPath.length()), "/");
-
-            if (StringUtils.isNotEmpty(subPath)) {
-                cookiePath = StringUtils.removeEnd(localBasePath, "/") + "/" + subPath;
-            }
+            URI remoteURI = URI.create(context.getRemoteURI());
+            String remotePath = remoteURI.getPath();
+            String baseURI = StringUtils.substringBefore(remoteURI.toString(), "?");
+            baseURI = baseURI.substring(0, baseURI.length() - remotePath.length());
+            String cookieRemoteURI = baseURI + cookiePath;
+            cookiePath = context.getResolvedMapping().resolveLocalFromRemote(cookieRemoteURI);
         }
 
         return cookiePath;

Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/AbstractProxyMapping.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/AbstractProxyMapping.java?rev=1612465&r1=1612464&r2=1612465&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/AbstractProxyMapping.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/AbstractProxyMapping.java Tue Jul 22 05:10:28 2014
@@ -21,11 +21,13 @@ import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.portals.applications.webcontent2.proxy.ProxyMapping;
+import org.apache.portals.applications.webcontent2.rewriter.ContentRewriter;
 
 public abstract class AbstractProxyMapping implements ProxyMapping
 {
 
     private Map<String, Object> attributes;
+    private ContentRewriter contentRewriter;
 
     public Object getAttribute(String name)
     {
@@ -77,4 +79,14 @@ public abstract class AbstractProxyMappi
         }
     }
 
+    public ContentRewriter getContentRewriter()
+    {
+        return contentRewriter;
+    }
+
+    public void setContentRewriter(ContentRewriter contentRewriter)
+    {
+        this.contentRewriter = contentRewriter;
+    }
+
 }

Added: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/RegexProxyMapping.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/RegexProxyMapping.java?rev=1612465&view=auto
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/RegexProxyMapping.java (added)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/RegexProxyMapping.java Tue Jul 22 05:10:28 2014
@@ -0,0 +1,116 @@
+/*
+ * 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.webcontent2.proxy.impl;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class RegexProxyMapping extends AbstractProxyMapping
+{
+
+    private String localPattern;
+    private String remoteReplace;
+
+    private String remotePattern;
+    private String localReplace;
+
+    private Pattern localPatternObject;
+    private Pattern remotePatternObject;
+
+    public RegexProxyMapping()
+    {
+    }
+
+    public String getLocalPattern()
+    {
+        return localPattern;
+    }
+
+    public void setLocalPattern(String localPattern)
+    {
+        this.localPattern = localPattern;
+        this.localPatternObject = Pattern.compile(localPattern);
+    }
+
+    public String getRemoteReplace()
+    {
+        return remoteReplace;
+    }
+
+    public void setRemoteReplace(String remoteReplace)
+    {
+        this.remoteReplace = remoteReplace;
+    }
+
+    public String getRemotePattern()
+    {
+        return remotePattern;
+    }
+
+    public void setRemotePattern(String remotePattern)
+    {
+        this.remotePattern = remotePattern;
+        this.remotePatternObject = Pattern.compile(remotePattern);
+    }
+
+    public String getLocalReplace()
+    {
+        return localReplace;
+    }
+
+    public void setLocalReplace(String localReplace)
+    {
+        this.localReplace = localReplace;
+    }
+
+    public boolean matchesLocal(String localPath)
+    {
+        Matcher m = localPatternObject.matcher(localPath);
+        return m.matches();
+    }
+
+    public String resolveRemoteFromLocal(String localPath)
+    {
+        Matcher m = localPatternObject.matcher(localPath);
+
+        if (m.matches())
+        {
+            return m.replaceFirst(remoteReplace);
+        }
+
+        return null;
+    }
+
+    public boolean matchesRemote(String remoteURI)
+    {
+        Matcher m = remotePatternObject.matcher(remoteURI);
+        return m.matches();
+    }
+
+    public String resolveLocalFromRemote(String remoteURI)
+    {
+        Matcher m = remotePatternObject.matcher(remoteURI);
+
+        if (m.matches())
+        {
+            return m.replaceFirst(localReplace);
+        }
+
+        return null;
+    }
+
+}

Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/SimpleProxyMapping.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/SimpleProxyMapping.java?rev=1612465&r1=1612464&r2=1612465&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/SimpleProxyMapping.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/impl/SimpleProxyMapping.java Tue Jul 22 05:10:28 2014
@@ -17,49 +17,37 @@
 package org.apache.portals.applications.webcontent2.proxy.impl;
 
 import org.apache.commons.lang.StringUtils;
-import org.apache.portals.applications.webcontent2.rewriter.ContentRewriter;
 
 public class SimpleProxyMapping extends AbstractProxyMapping
 {
 
     private String localBasePath;
     private String remoteBaseURI;
-    private ContentRewriter contentRewriter;
 
     public SimpleProxyMapping()
     {
     }
 
-    public String getLocalBasePath()
+    public String getLocal()
     {
         return localBasePath;
     }
 
-    public void setLocalBasePath(String localBasePath)
+    public void setLocal(String localBasePath)
     {
         this.localBasePath = localBasePath;
     }
 
-    public String getRemoteBaseURI()
+    public String getRemote()
     {
         return remoteBaseURI;
     }
 
-    public void setRemoteBaseURI(String remoteBaseURI)
+    public void setRemote(String remoteBaseURI)
     {
         this.remoteBaseURI = remoteBaseURI;
     }
 
-    public ContentRewriter getContentRewriter()
-    {
-        return contentRewriter;
-    }
-
-    public void setContentRewriter(ContentRewriter contentRewriter)
-    {
-        this.contentRewriter = contentRewriter;
-    }
-
     public boolean matchesLocal(String localPath)
     {
         return StringUtils.startsWith(localPath, localBasePath);
@@ -84,7 +72,7 @@ public class SimpleProxyMapping extends 
     {
         if (matchesRemote(remoteURI))
         {
-            return localBasePath + remoteURI.substring(remoteURI.length());
+            return localBasePath + remoteURI.substring(remoteBaseURI.length());
         }
 
         return null;

Modified: portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/util/YamlConfigUtils.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/util/YamlConfigUtils.java?rev=1612465&r1=1612464&r2=1612465&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/util/YamlConfigUtils.java (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/main/java/org/apache/portals/applications/webcontent2/proxy/util/YamlConfigUtils.java Tue Jul 22 05:10:28 2014
@@ -29,6 +29,7 @@ import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.text.StrSubstitutor;
 import org.apache.portals.applications.webcontent2.proxy.ProxyMapping;
+import org.apache.portals.applications.webcontent2.proxy.impl.RegexProxyMapping;
 import org.apache.portals.applications.webcontent2.proxy.impl.SimpleProxyMapping;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -104,7 +105,8 @@ public class YamlConfigUtils
         List<ProxyMapping> proxyMappings = new ArrayList<ProxyMapping>();
 
         Constructor constructor = new Constructor();
-        constructor.addTypeDescription(new TypeDescription(SimpleProxyMapping.class, "!mapping"));
+        constructor.addTypeDescription(new TypeDescription(SimpleProxyMapping.class, "!simple"));
+        constructor.addTypeDescription(new TypeDescription(RegexProxyMapping.class, "!regex"));
 
         Iterable<Object> allObjects = loadYaml(constructor, yamlConfig, servletContextOrPortletContext);
 

Added: portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/command/AddCookiesToResponseCommandTest.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/command/AddCookiesToResponseCommandTest.java?rev=1612465&view=auto
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/command/AddCookiesToResponseCommandTest.java (added)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/command/AddCookiesToResponseCommandTest.java Tue Jul 22 05:10:28 2014
@@ -0,0 +1,56 @@
+/*
+ * 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.webcontent2.proxy.command;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.http.impl.cookie.BasicClientCookie;
+import org.apache.portals.applications.webcontent2.proxy.impl.ProxyContext;
+import org.apache.portals.applications.webcontent2.proxy.impl.SimpleProxyMapping;
+import org.junit.Test;
+
+public class AddCookiesToResponseCommandTest
+{
+
+    @Test
+    public void testGetReverseCookiePath() throws Exception
+    {
+        AddCookiesToResponseCommand command = new AddCookiesToResponseCommand();
+
+        ProxyContext proxyContext = new ProxyContext(null);
+
+        SimpleProxyMapping mapping = new SimpleProxyMapping();
+        mapping.setLocal("/apache/portals/");
+        mapping.setRemote("http://portals.apache.org/");
+        proxyContext.setResolvedMapping(mapping);
+
+        proxyContext.setLocalPath("/apache/portals/docs/mission.html");
+        proxyContext.setRemoteURI("http://portals.apache.org/docs/mission.html");
+
+        BasicClientCookie cookie = new BasicClientCookie("MYSESSIONID", "1234567890");
+        cookie.setPath("/");
+
+        String reverseCookiePath = command.getReverseCookiePath(proxyContext, cookie);
+        assertEquals("/apache/portals/", reverseCookiePath);
+
+        cookie = new BasicClientCookie("MYSESSIONID", "1234567890");
+        cookie.setPath("/docs/");
+
+        reverseCookiePath = command.getReverseCookiePath(proxyContext, cookie);
+        assertEquals("/apache/portals/docs/", reverseCookiePath);
+    }
+}

Added: portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/impl/RegexProxyMappingTest.java
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/impl/RegexProxyMappingTest.java?rev=1612465&view=auto
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/impl/RegexProxyMappingTest.java (added)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/test/java/org/apache/portals/applications/webcontent2/proxy/impl/RegexProxyMappingTest.java Tue Jul 22 05:10:28 2014
@@ -0,0 +1,47 @@
+/*
+ * 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.webcontent2.proxy.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+public class RegexProxyMappingTest
+{
+
+    @Test
+    public void testRegexMappings() throws Exception
+    {
+        RegexProxyMapping mapping = new RegexProxyMapping();
+
+        mapping.setLocalPattern("^/apache/(\\w+)/(.*)$");
+        mapping.setRemoteReplace("http://$1.apache.org/$2");
+        mapping.setRemotePattern("^http://(\\w+)\\.apache\\.org/(.*)$");
+        mapping.setLocalReplace("/apache/$1/$2");
+
+        assertTrue(mapping.matchesLocal("/apache/portals/"));
+        assertTrue(mapping.matchesLocal("/apache/portals/mission.html"));
+
+        assertEquals("http://portals.apache.org/", mapping.resolveRemoteFromLocal("/apache/portals/"));
+        assertEquals("http://portals.apache.org/mission.html", mapping.resolveRemoteFromLocal("/apache/portals/mission.html"));
+
+        assertEquals("/apache/portals/", mapping.resolveLocalFromRemote("http://portals.apache.org/"));
+        assertEquals("/apache/portals/mission.html", mapping.resolveLocalFromRemote("http://portals.apache.org/mission.html"));
+    }
+
+}

Modified: portals/applications/webcontent/trunk/reverse-proxy/src/test/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/portals/applications/webcontent/trunk/reverse-proxy/src/test/webapp/WEB-INF/web.xml?rev=1612465&r1=1612464&r2=1612465&view=diff
==============================================================================
--- portals/applications/webcontent/trunk/reverse-proxy/src/test/webapp/WEB-INF/web.xml (original)
+++ portals/applications/webcontent/trunk/reverse-proxy/src/test/webapp/WEB-INF/web.xml Tue Jul 22 05:10:28 2014
@@ -28,13 +28,13 @@
     <init-param>
       <param-name>mappings</param-name>
       <param-value>
---- !mapping
-localBasePath: /applications/
-remoteBaseURI: http://portals.apache.org/applications/
+--- !simple
+local: /applications/
+remote: http://portals.apache.org/applications/
 
---- !mapping
-localBasePath: /bridges/
-remoteBaseURI: http://portals.apache.org/bridges/
+--- !simple
+local: /bridges/
+remote: http://portals.apache.org/bridges/
       </param-value>
     </init-param>
     <load-on-startup>0</load-on-startup>