portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwat...@apache.org
Subject svn commit: r924018 - in /portals/jetspeed-2/portal/branches/JETSPEED-2.1.4: components/profiler/src/java/org/apache/jetspeed/profiler/rules/impl/ components/profiler/src/test/org/apache/jetspeed/profiler/ jetspeed-api/src/java/org/apache/jetspeed/mock...
Date Tue, 16 Mar 2010 21:57:05 GMT
Author: rwatler
Date: Tue Mar 16 21:57:05 2010
New Revision: 924018

URL: http://svn.apache.org/viewvc?rev=924018&view=rev
Log:
Hostname resolver enhancement to support regex pattern mapping

Modified:
    portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/components/profiler/src/java/org/apache/jetspeed/profiler/rules/impl/HostnameCriterionResolver.java
    portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/components/profiler/src/test/org/apache/jetspeed/profiler/TestProfiler.java
    portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/jetspeed-api/src/java/org/apache/jetspeed/mockobjects/MockHttpServletRequest.java
    portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/src/webapp/WEB-INF/assembly/profiler.xml

Modified: portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/components/profiler/src/java/org/apache/jetspeed/profiler/rules/impl/HostnameCriterionResolver.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/components/profiler/src/java/org/apache/jetspeed/profiler/rules/impl/HostnameCriterionResolver.java?rev=924018&r1=924017&r2=924018&view=diff
==============================================================================
--- portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/components/profiler/src/java/org/apache/jetspeed/profiler/rules/impl/HostnameCriterionResolver.java
(original)
+++ portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/components/profiler/src/java/org/apache/jetspeed/profiler/rules/impl/HostnameCriterionResolver.java
Tue Mar 16 21:57:05 2010
@@ -16,6 +16,11 @@
  */
 package org.apache.jetspeed.profiler.rules.impl;
 
+import java.util.Iterator;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 import org.apache.jetspeed.profiler.rules.RuleCriterion;
 import org.apache.jetspeed.profiler.rules.RuleCriterionResolver;
 import org.apache.jetspeed.request.RequestContext;
@@ -24,12 +29,13 @@ import org.apache.jetspeed.request.Reque
  * Hostname Resolver
  * 
  * @author <a href="mailto:stalherm@goodgulf.net">Frank Stalherm</a>
- * @version $Id:$
+ * @version $Id$
  */
 public class HostnameCriterionResolver extends StandardResolver implements
         RuleCriterionResolver
 {
-    boolean useDotPrefix = false;
+    private boolean useDotPrefix;
+    private List hostnameMappingRules;
     
     public HostnameCriterionResolver(boolean usePrefix)
     {
@@ -37,6 +43,12 @@ public class HostnameCriterionResolver e
         this.useDotPrefix = usePrefix;
     }
 
+    public HostnameCriterionResolver(List hostnameMappingRules)
+    {
+        super();
+        this.hostnameMappingRules = hostnameMappingRules;
+    }
+
     /*
      * (non-Javadoc)
      * 
@@ -69,7 +81,39 @@ public class HostnameCriterionResolver e
                 serverName = serverName.substring(0, idx);
             }
         }
+        else if (hostnameMappingRules != null)
+        {
+            Iterator rulesIter = hostnameMappingRules.iterator();
+            while (rulesIter.hasNext())
+            {
+                Rule rule = (Rule)rulesIter.next();
+                serverName = rule.map(serverName);
+            }
+        }
         return serverName;
     }
 
+    public static class Rule
+    {
+        private String pattern;
+        private String replacement;
+        private Pattern compiledPattern;        
+        
+        public Rule(String pattern, String replacement)
+        {
+            this.pattern = pattern;
+            this.replacement = replacement;
+            this.compiledPattern = Pattern.compile(pattern);
+        }
+        
+        public String map(String hostname)
+        {
+            Matcher patternMatcher = compiledPattern.matcher(hostname);
+            if (patternMatcher.find())
+            {
+                return patternMatcher.replaceAll(replacement);
+            }
+            return hostname;
+        }
+    }
 }

Modified: portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/components/profiler/src/test/org/apache/jetspeed/profiler/TestProfiler.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/components/profiler/src/test/org/apache/jetspeed/profiler/TestProfiler.java?rev=924018&r1=924017&r2=924018&view=diff
==============================================================================
--- portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/components/profiler/src/test/org/apache/jetspeed/profiler/TestProfiler.java
(original)
+++ portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/components/profiler/src/test/org/apache/jetspeed/profiler/TestProfiler.java
Tue Mar 16 21:57:05 2010
@@ -25,11 +25,13 @@ import java.util.Properties;
 import java.util.Set;
 
 import javax.security.auth.Subject;
+import javax.servlet.http.HttpServletRequest;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
 import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
+import org.apache.jetspeed.mockobjects.MockHttpServletRequest;
 import org.apache.jetspeed.mockobjects.request.MockRequestContext;
 import org.apache.jetspeed.profiler.impl.JetspeedProfilerImpl;
 import org.apache.jetspeed.profiler.rules.ProfileResolvers;
@@ -708,4 +710,36 @@ public class TestProfiler extends Dataso
         assertTrue("didnt find expected number of fallback rules, expected = " + EXPECTED_FALLBACK,
fallbackCount == 1);
     }
     
+    public void testSubsiteRules()
+    {
+        RequestContext request = new MockRequestContext("/");
+        Set principals = new PrincipalsSet();
+        Set publicCredentials = new HashSet();
+        Set privateCredentials = new HashSet();        
+        principals.add(new UserPrincipalImpl("user"));
+        principals.add(new RolePrincipalImpl("role"));
+        request.setSubject(new Subject(true, principals, publicCredentials, privateCredentials));
+        request.setLocale(new Locale("en", "US"));
+        MockHttpServletRequest servletRequest = new MockHttpServletRequest();
+        servletRequest.setServerName("www.domain.com");
+        request.setRequest(servletRequest);
+        try
+        {
+            ProfilingRule rule = profiler.getRule("subsite-by-hostname");
+            assertNotNull(rule);
+            ProfileLocator locator = profiler.getProfile(request, rule);
+            assertNotNull(locator);
+            assertEquals("/", locator.getRequestPath());
+            // default configuration, (see profiler.xml)
+            assertEquals("navigation:subsite-root:hostname:www.domain.com:user:user:navigation-2:subsite-root:hostname:www.domain.com:role:role:path:home",
locator.getLocatorPath());
+            // 'dot prefix' configuration result, (see profiler.xml)
+            //assertEquals("navigation:subsite-root:hostname:www:user:user:navigation-2:subsite-root:hostname:www:role:role:path:home",
locator.getLocatorPath());
+            // 'hostname to domain mapping' configuration result, (see profiler.xml)
+            //assertEquals("navigation:subsite-root:hostname:domain.com:user:user:navigation-2:subsite-root:hostname:domain.com:role:role:path:home",
locator.getLocatorPath());
+        }
+        catch (ProfilerException pe)
+        {
+            fail("Unexpected ProfilerException: "+pe.getMessage());
+        }
+    }
 }

Modified: portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/jetspeed-api/src/java/org/apache/jetspeed/mockobjects/MockHttpServletRequest.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/jetspeed-api/src/java/org/apache/jetspeed/mockobjects/MockHttpServletRequest.java?rev=924018&r1=924017&r2=924018&view=diff
==============================================================================
--- portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/jetspeed-api/src/java/org/apache/jetspeed/mockobjects/MockHttpServletRequest.java
(original)
+++ portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/jetspeed-api/src/java/org/apache/jetspeed/mockobjects/MockHttpServletRequest.java
Tue Mar 16 21:57:05 2010
@@ -35,6 +35,17 @@ import javax.servlet.http.HttpSession;
  */
 public class MockHttpServletRequest implements HttpServletRequest
 {
+    private String serverName;
+
+    /**
+     * Set server name for request.
+     * 
+     * @param serverName server name
+     */
+    public void setServerName(String serverName)
+    {
+        this.serverName = serverName;
+    }
 
     /* (non-Javadoc)
      * @see javax.servlet.http.HttpServletRequest#getAuthType()
@@ -384,8 +395,7 @@ public class MockHttpServletRequest impl
      */
     public String getServerName()
     {
-        // TODO Auto-generated method stub
-        return null;
+        return serverName;
     }
 
     /* (non-Javadoc)

Modified: portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/src/webapp/WEB-INF/assembly/profiler.xml
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/src/webapp/WEB-INF/assembly/profiler.xml?rev=924018&r1=924017&r2=924018&view=diff
==============================================================================
--- portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/src/webapp/WEB-INF/assembly/profiler.xml
(original)
+++ portals/jetspeed-2/portal/branches/JETSPEED-2.1.4/src/webapp/WEB-INF/assembly/profiler.xml
Tue Mar 16 21:57:05 2010
@@ -158,8 +158,23 @@
     <bean id='HostnameCriterionResolver' class='org.apache.jetspeed.profiler.rules.impl.HostnameCriterionResolver'>
       <!--  use the dot prefix, for ex: "accounting.xyz.com" returns "accounting" -->
       <constructor-arg type="boolean" index="0">
-            <value>false</value> 
-        </constructor-arg>    	
+        <value>false</value> 
+      </constructor-arg>    	
+      <!--
+          use hostname mapping rules, for ex: ^.*[.]([^.]+[.][^.]+)$ -> $1
+          maps hostnames like 'www.domain.com' and 'www.secure.domain.com' to
+          domain 'domain.com'; note: patterns and replacements use '&#36;' for '$'
+      -->
+      <!--
+      <constructor-arg index="0">
+        <list>
+          <bean class="org.apache.jetspeed.profiler.rules.impl.HostnameCriterionResolver$Rule">
+            <constructor-arg index="0" value="^(.*[.])?([^.]+[.][^.]+)&#36;" />
+            <constructor-arg index="1" value="&#36;2" />
+          </bean>
+        </list>
+      </constructor-arg>
+      -->
     </bean>    
     <bean id='DomainCriterionResolver' class='org.apache.jetspeed.profiler.rules.impl.DomainCriterionResolver'></bean>
     
 



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message