[Yanel-commits] rev 22510 - in public/yanel/trunk: conf src/core/java/org/wyona/yanel/core/map src/impl/java/org/wyona/yanel/impl/map

josias at wyona.com josias at wyona.com
Wed Feb 7 18:47:49 CET 2007


Author: josias
Date: 2007-02-07 18:47:48 +0100 (Wed, 07 Feb 2007)
New Revision: 22510

Modified:
   public/yanel/trunk/conf/realms.xml
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/map/Map.java
   public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/map/MapImpl.java
Log:
implemented longest prefix matching: now the order of the realms in realms.xml does not matter anymore. makes it easier for the user, and also makes it easier for yanel to automatically add new realms

Modified: public/yanel/trunk/conf/realms.xml
===================================================================
--- public/yanel/trunk/conf/realms.xml	2007-02-07 17:11:44 UTC (rev 22509)
+++ public/yanel/trunk/conf/realms.xml	2007-02-07 17:47:48 UTC (rev 22510)
@@ -2,16 +2,6 @@
 
 <realms xmlns="http://www.wyona.org/yanel/1.0">
 
-<!-- 
-  IMPORTANT NOTE:
-  Incoming requests will be matched to a realm using the information in this file.
-  The realm for a request will be determined by checking the realms sequentially 
-  as they appear in this file, whereas the first realm with a matching mount-point 
-  will be used.
-  This means that the order of the realms in this file matters, and the realm with 
-  mount-point '/' has to come last.
-  TODO: change this behaviour to "longest prefix matching"
--->
 
 <!-- WARNING: Access Control does not work properly for nested realms (also the mount-point needs to match the id in order to make the access control work properly, e.g. id="zurich" and mount-point="/zurich/")! -->
 <realm id="javadoc" mount-point="/yanel-website/javadoc/">

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/map/Map.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/map/Map.java	2007-02-07 17:11:44 UTC (rev 22509)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/map/Map.java	2007-02-07 17:47:48 UTC (rev 22510)
@@ -51,7 +51,7 @@
      * Gets the realm for the given url, according to the configuration in realms.xml.
      * 
      * @param url url without context path prefix
-     * @return the first realm whose mount-point is a matching prefix of the given url.
+     * @return the realm with the longest mount-point matching the given url.
      * @throws Exception
      */
     public Realm getRealm(String url) throws Exception;

Modified: public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/map/MapImpl.java
===================================================================
--- public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/map/MapImpl.java	2007-02-07 17:11:44 UTC (rev 22509)
+++ public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/map/MapImpl.java	2007-02-07 17:47:48 UTC (rev 22510)
@@ -154,18 +154,39 @@
      * @param url URL of request but without servlet context
      */
     public Realm getRealm(String url) throws Exception {
-        log.debug("URL: " + url);
+        if (log.isDebugEnabled()) {
+            log.debug("URL: " + url);
+        }
         Realm[] realms = realmConfig.getRealms();
         
+        Realm matchingRealm = null;
         for (int i=0; i<realms.length; i++) {
-            log.debug("checking realm : " + realms[i].getID() + " with mountpoint: " + realms[i].getMountPoint().toString());
-            if (url.startsWith(realms[i].getMountPoint().toString())) {
-                log.debug("matched!");
-                return realms[i];
+            if (log.isDebugEnabled()) {
+                log.debug("checking realm: " + realms[i].getID() + " with mountpoint: " 
+                        + realms[i].getMountPoint());
             }
+            if (url.startsWith(realms[i].getMountPoint())) {
+                if (matchingRealm != null) {
+                    if (realms[i].getMountPoint().length() > matchingRealm.getMountPoint().length()) {
+                        matchingRealm = realms[i];
+                    }
+                } else {
+                    matchingRealm = realms[i];
+                }
+            }
         }
-        log.debug("nothing matched! - > root realm");
-        return realmConfig.getRootRealm();
+        if (matchingRealm != null) {
+            if (log.isDebugEnabled()) {
+                log.debug("found matching realm: " + matchingRealm + " with mountpoint: " 
+                        + matchingRealm.getMountPoint());
+            }
+            return matchingRealm;
+        } else {
+            if (log.isDebugEnabled()) {
+                log.debug("nothing matched! - > root realm");
+            }
+            return realmConfig.getRootRealm();
+        }
     }
 
     /**
@@ -175,9 +196,11 @@
      * @param url URL of request but without servlet context
      */
     public String getPath(Realm realm, String url) throws Exception {
-        log.debug("URL: " + url);
+        if (log.isDebugEnabled()) {
+            log.debug("URL: " + url);
+        }
 
-        String mountPoint = realm.getMountPoint().toString();
+        String mountPoint = realm.getMountPoint();
         if (!url.startsWith(mountPoint)) {
             throw new Exception("Cannot map url [" + url + "] to path because the url does not " + 
                     "belong to the given realm : " + realm.getID() + ": " + mountPoint);




More information about the Yanel-commits mailing list