Author: rvesse
Date: Tue Jul 30 17:42:47 2013
New Revision: 1508547

URL: http://svn.apache.org/r1508547
Log:
Improve the logic of how the common Base URI for authentication is determined

Modified:
    
jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java

Modified: 
jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java?rev=1508547&r1=1508546&r2=1508547&view=diff
==============================================================================
--- 
jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java
 (original)
+++ 
jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java
 Tue Jul 30 17:42:47 2013
@@ -323,8 +323,8 @@ public class RemoteEndpointDriver extend
             Map<URI, FormLogin> logins = new HashMap<URI, FormLogin>();
             String baseUri = this.getCommonBase(queryEndpoint, updateEndpoint);
             if (baseUri != null) {
-                // Since both endpoints are specified and they have a common
-                // Base URI create a single login
+                // One/both endpoints are specified and they have a common
+                // Base URI so we'll create a single login
                 try {
                     logins.put(new URI(baseUri), new FormLogin(loginURL, 
userField, pwdField, user, password.toCharArray()));
                 } catch (URISyntaxException e) {
@@ -365,25 +365,37 @@ public class RemoteEndpointDriver extend
     }
 
     /**
-     * Determines the common base of the two URIs if there is one
+     * Determines the common base of the two URIs if there is one. The common
+     * base will have irrelevant components (fragment and query string) 
stripped
+     * off of it.
+     * <p>
+     * If one URI is null and the other is non-null the non-null one is
+     * returned.
+     * </p>
      * 
      * @param x
      *            URI
      * @param y
      *            URI
-     * @return Common base if it exists, null otherwise
+     * @return Common base if it exists, null otherwise.
      */
     protected String getCommonBase(String x, String y) {
-        if (x == null || y == null) {
-            return null;
+        if (x == null) {
+            if (y == null) {
+                return null;
+            } else {
+                return stripIrrelevantComponents(y);
+            }
+        } else if (y == null) {
+            return stripIrrelevantComponents(x);
         } else if (x.equals(y)) {
-            return x;
+            return stripIrrelevantComponents(x);
         } else {
             // Is one the base of the other?
             if (x.length() < y.length() && y.startsWith(x)) {
-                return x;
+                return stripIrrelevantComponents(x);
             } else if (y.length() < x.length() && x.startsWith(y)) {
-                return y;
+                return stripIrrelevantComponents(y);
             }
 
             // Otherwise we should strip last URI component off one/both URIs
@@ -399,6 +411,13 @@ public class RemoteEndpointDriver extend
                 x = this.stripLastComponent(x);
                 y = this.stripLastComponent(y);
             }
+
+            // Be careful that if either returned null at this point bail out
+            // Must do this before recursing as otherwise the recursive call
+            // will see one input as null and treat as if the non-null is the
+            // common base which is in fact incorrect in this case
+            if (x == null || y == null) return null;
+
             return this.getCommonBase(x, y);
         }
     }
@@ -451,6 +470,25 @@ public class RemoteEndpointDriver extend
     }
 
     /**
+     * Get the URI with irrelevant components (Fragment and Querystring)
+     * stripped off
+     * 
+     * @param input
+     *            URI
+     * @return URI with irrelevant components stripped off or null if stripping
+     *         is impossible
+     */
+    private String stripIrrelevantComponents(String input) {
+        try {
+            URI orig = new URI(input);
+            return new URI(orig.getScheme(), orig.getUserInfo(), 
orig.getHost(), orig.getPort(), orig.getPath(), null, null)
+                    .toString();
+        } catch (URISyntaxException e) {
+            return null;
+        }
+    }
+
+    /**
      * Opens the actual connection
      * <p>
      * This extension point allows derived drivers to return an extended 
version


Reply via email to