Commits

Anonymous committed 2db9983

First pass at authentication

Comments (0)

Files changed (11)

Source/org/xnat/xnatfs/webdav/Bundle.java

   @Override
   public Resource child ( String childName ) {
     logger.debug ( "child: create " + childName );
-    String childPath = mAbsolutePath + childName;
     HashMap<String, ArrayList<String>> s = null;
     try {
       ArrayList<String> l = new ArrayList<String> ();
       logger.error ( "Failed to get child element list: " + e );
     }
     if ( s.containsKey ( childName ) ) {
-      if ( XNATFS.sNodeCache.get ( childPath ) != null ) {
-        return (Resource) ( XNATFS.sNodeCache.get ( childPath ).getObjectValue () );
-      }
-
       long size = Long.valueOf ( s.get ( childName ).get ( 1 ) ).longValue ();
       RemoteFile remote = new RemoteFile ( xnatfs, mAbsolutePath, childName, mURL + "files/" + childName, size );
-      Element element = new Element ( childPath, remote );
-      XNATFS.sNodeCache.put ( element );
-      return (Resource) element.getObjectValue ();
+      return remote;
     }
     return null;
   }
   @SuppressWarnings("unchecked")
   protected synchronized HashMap<String, ArrayList<String>> getFileMap ( String url, String inKey, ArrayList<String> valueFields ) throws IOException {
     // Get the subjects code
-    Element element = XNATFS.sContentCache.get ( "FileMap::" + url );
+    String elementKey = mCredentials.user + "::" + mCredentials.password + "::ElementList::" + url;
+    Element element = XNATFS.sContentCache.get ( elementKey );
     HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>> ();
     if ( element == null ) {
       HttpEntity entity = null;
       try {
-        entity = Connection.getInstance ().getEntity ( url );
+        entity = Connection.getInstance ().getEntity ( url, mCredentials );
         InputStreamReader reader = new InputStreamReader ( entity.getContent () );
         JSONTokener tokenizer = new JSONTokener ( reader );
         JSONObject json = new JSONObject ( tokenizer );
           entity.consumeContent ();
         }
       }
-      element = new Element ( "FileMap::" + url, map );
+      element = new Element ( elementKey, map );
       XNATFS.sContentCache.put ( element );
     }
     map = (HashMap<String, ArrayList<String>>) element.getObjectValue ();

Source/org/xnat/xnatfs/webdav/Connection.java

 import org.apache.http.protocol.HttpContext;
 import org.apache.log4j.Logger;
 
+import com.bradmcevoy.http.Auth;
+
 public class Connection {
   private static final Logger logger = Logger.getLogger ( Connection.class );
 
   HttpParams params;
   SchemeRegistry schemeRegistry;
   ClientConnectionManager cm;
-  DefaultHttpClient mClient;
 
   static public Connection getInstance () {
     return sInstance;
   static private Connection sInstance = new Connection ();
 
   void setCredentials () {
-    mClient.getCredentialsProvider ().setCredentials ( new AuthScope ( AuthScope.ANY_HOST, AuthScope.ANY_PORT ), new UsernamePasswordCredentials ( mUsername, mPassword ) );
   }
 
   public void setUsername ( String s ) {
     mPort = s;
   }
 
-  public HttpClient getClient () {
+  public HttpClient getClient ( Auth credentials ) {
+    DefaultHttpClient mClient;
+    mClient = new DefaultHttpClient ( cm, params );
+    logger.debug ( "getClient: credentials are " + credentials.user + "/" + credentials.password );
+    mClient.getCredentialsProvider ().setCredentials ( new AuthScope ( AuthScope.ANY_HOST, AuthScope.ANY_PORT ), new UsernamePasswordCredentials ( credentials.user, credentials.password ) );
+    /*
+     * BasicHttpContext localcontext = new BasicHttpContext ();
+     * 
+     * // Generate BASIC scheme object and stick it to the local // execution
+     * context BasicScheme basicAuth = new BasicScheme ();
+     * localcontext.setAttribute ( "preemptive-auth", basicAuth );
+     */
+    // Add as the first request interceptor
+    mClient.addRequestInterceptor ( new PreemptiveAuth (), 0 );
     return mClient;
   }
 
     // This connection manager must be used if more than one thread will
     // be using the HttpClient.
     cm = new ThreadSafeClientConnManager ( params, schemeRegistry );
-    mClient = new DefaultHttpClient ( cm, params );
-    mClient.getCredentialsProvider ().setCredentials ( new AuthScope ( AuthScope.ANY_HOST, AuthScope.ANY_PORT ), new UsernamePasswordCredentials ( mUsername, mPassword ) );
-    /*
-     * BasicHttpContext localcontext = new BasicHttpContext ();
-     * 
-     * // Generate BASIC scheme object and stick it to the local // execution
-     * context BasicScheme basicAuth = new BasicScheme ();
-     * localcontext.setAttribute ( "preemptive-auth", basicAuth );
-     */
-    // Add as the first request interceptor
-    mClient.addRequestInterceptor ( new PreemptiveAuth (), 0 );
   }
 
-  public HttpEntity getEntity ( String url ) throws Exception {
-    HttpClient client = Connection.getInstance ().getClient ();
+  public HttpEntity getEntity ( String url, Auth credentials ) throws Exception {
+    HttpClient client = Connection.getInstance ().getClient ( credentials );
     HttpGet httpget = new HttpGet ( Connection.getInstance ().formatURL ( url ) );
     BasicHttpContext context = new BasicHttpContext ();
 

Source/org/xnat/xnatfs/webdav/Experiment.java

 
 import java.util.HashSet;
 
-import net.sf.ehcache.Element;
-
 import org.apache.log4j.Logger;
 
 import com.bradmcevoy.http.Resource;
   @Override
   public Resource child ( String childName ) {
     logger.debug ( "child: create " + childName );
-    String childPath = mAbsolutePath + childName;
     HashSet<String> s = null;
     try {
       s = getElementList ( mElementURL, mChildKey );
       logger.error ( "Failed to get child element list: " + e );
     }
     if ( s.contains ( childName ) ) {
-      // Look up in the cache
-      if ( XNATFS.sNodeCache.get ( childPath ) != null ) {
-        return (Resource) ( XNATFS.sNodeCache.get ( childPath ).getObjectValue () );
-      }
-      Element element = new Element ( childPath, new Scan ( xnatfs, mAbsolutePath, childName, mURL + "scans/" + childName + "/" ) );
-      XNATFS.sNodeCache.put ( element );
-      return (Resource) element.getObjectValue ();
+      return new Scan ( xnatfs, mAbsolutePath, childName, mURL + "scans/" + childName + "/" );
     }
     return null;
   }
-
 }

Source/org/xnat/xnatfs/webdav/Project.java

 
 import java.util.HashSet;
 
-import net.sf.ehcache.Element;
-
 import org.apache.log4j.Logger;
 
 import com.bradmcevoy.http.Resource;
   @Override
   public Resource child ( String childName ) {
     logger.debug ( "child: create " + childName );
-    String childPath = mAbsolutePath + "/" + childName;
     HashSet<String> s = null;
     try {
       s = getElementList ( mElementURL, mChildKey );
     }
     if ( s.contains ( childName ) ) {
       // Look up in the cache
-      if ( XNATFS.sNodeCache.get ( childPath ) != null ) {
-        logger.debug ( "Found child: " + childName + " in the cache" );
-        return (Resource) ( XNATFS.sNodeCache.get ( childPath ).getObjectValue () );
-      }
-      Element element = new Element ( childPath, new Subject ( xnatfs, mAbsolutePath, childName, mURL + "subjects/" + childName + "/" ) );
-      logger.debug ( "Created new subject " + mAbsolutePath + " " + childName + " as " + mURL );
-      XNATFS.sNodeCache.put ( element );
-      return (Resource) element.getObjectValue ();
+      return new Subject ( xnatfs, mAbsolutePath, childName, mURL + "subjects/" + childName + "/" );
     }
     if ( childName.equals ( "Image1.dcm" ) ) {
       return new RemoteFile ( xnatfs, mAbsolutePath, childName,

Source/org/xnat/xnatfs/webdav/RemoteFile.java

       logger.debug ( "Starting remote background fetch for " + mURL + " as file " + mPath );
       // GetMethod get = new GetMethod ( mURL );
       try {
-        HttpClient client = Connection.getInstance ().getClient ();
+        HttpClient client = Connection.getInstance ().getClient ( mCredentials );
         HttpGet httpget = new HttpGet ( Connection.getInstance ().formatURL ( mURL ) );
         BasicHttpContext context = new BasicHttpContext ();
 

Source/org/xnat/xnatfs/webdav/Root.java

 import java.util.Date;
 import java.util.HashSet;
 
-import net.sf.ehcache.Element;
-
 import org.apache.log4j.Logger;
 
 import com.bradmcevoy.http.Resource;
    * @see com.bradmcevoy.http.CollectionResource#child(java.lang.String)
    */
   public Resource child ( String childName ) {
-    String childPath = mAbsolutePath + childName;
     HashSet<String> s = null;
     try {
       s = getElementList ( mURL + "projects?format=json", mChildKey );
       logger.error ( "Failed to get child element list: " + e );
     }
     if ( s.contains ( childName ) ) {
-      // Look up in the cache
-      if ( XNATFS.sNodeCache.get ( childPath ) != null ) {
-        return (Resource) ( XNATFS.sNodeCache.get ( childPath ).getObjectValue () );
-      }
-      Element element = new Element ( childPath, new Project ( xnatfs, mAbsolutePath, childName, mURL + "projects/" + childName + "/" ) );
-      XNATFS.sNodeCache.put ( element );
-      return (Resource) element.getObjectValue ();
+      return new Project ( xnatfs, mAbsolutePath, childName, mURL + "projects/" + childName + "/" );
     }
     return null;
   }

Source/org/xnat/xnatfs/webdav/Scan.java

 
 import java.util.HashSet;
 
-import net.sf.ehcache.Element;
-
 import org.apache.log4j.Logger;
 
 import com.bradmcevoy.http.Resource;
   @Override
   public Resource child ( String childName ) {
     logger.debug ( "child: create " + childName );
-    String childPath = mAbsolutePath + childName;
     HashSet<String> s = null;
     try {
       s = getElementList ( mElementURL, null );
       logger.error ( "Failed to get child element list: " + e );
     }
     if ( s.contains ( childName ) ) {
-      // Look up in the cache
-      if ( XNATFS.sNodeCache.get ( childPath ) != null ) {
-        return (Resource) ( XNATFS.sNodeCache.get ( childPath ).getObjectValue () );
-      }
-      Element element = new Element ( childPath, new Bundle ( xnatfs, mAbsolutePath, childName, mURL + "resources/" + childName + "/" ) );
-      XNATFS.sNodeCache.put ( element );
-      return (Resource) element.getObjectValue ();
+      return new Bundle ( xnatfs, mAbsolutePath, childName, mURL + "resources/" + childName + "/" );
     }
     return null;
   }

Source/org/xnat/xnatfs/webdav/Subject.java

 
 import java.util.HashSet;
 
-import net.sf.ehcache.Element;
-
 import org.apache.log4j.Logger;
 
 import com.bradmcevoy.http.Resource;
   @Override
   public Resource child ( String childName ) {
     logger.debug ( "child: create " + childName );
-    String childPath = mAbsolutePath + childName;
     HashSet<String> s = null;
     try {
       s = getElementList ( mElementURL, null );
       logger.error ( "Failed to get child element list: " + e );
     }
     if ( s.contains ( childName ) ) {
-      // Look up in the cache
-      if ( XNATFS.sNodeCache.get ( childPath ) != null ) {
-        return (Resource) ( XNATFS.sNodeCache.get ( childPath ).getObjectValue () );
-      }
-      Element element = new Element ( childPath, new Experiment ( xnatfs, mAbsolutePath, childName, mURL + "experiments/" + childName + "/" ) );
-      XNATFS.sNodeCache.put ( element );
-      return (Resource) element.getObjectValue ();
+      return new Experiment ( xnatfs, mAbsolutePath, childName, mURL + "experiments/" + childName + "/" );
     }
     return null;
   }

Source/org/xnat/xnatfs/webdav/VirtualDirectory.java

           href = href + "/";
         }
         w.begin ( "a" ).writeAtt ( "href", href ).open ().writeText ( r.getName () ).close ();
-        // logger.debug ( "sendContent: added reference " + r.getName () +
-        // " to href " + href );
         w.close ( "td" );
 
         w.begin ( "td" ).open ().writeText ( r.getModifiedDate () + "" ).close ();
   @SuppressWarnings("unchecked")
   protected HashSet<String> getElementList ( String url, String inKey ) throws Exception {
     // See if we cached the list already
-    Element e = XNATFS.sContentCache.get ( "ElementList::" + url );
+    HashSet<String> list = new HashSet<String> ();
+    if ( mCredentials == null ) {
+      return list;
+    }
+    String key = mCredentials.user + "::" + mCredentials.password + "::ElementList::" + url;
+    Element e = XNATFS.sContentCache.get ( key );
     if ( e != null ) {
       return (HashSet<String>) e.getObjectValue ();
     }
     // Get the subjects code
-    HashSet<String> list = new HashSet<String> ();
     HttpEntity entity = null;
     try {
-      entity = Connection.getInstance ().getEntity ( url );
+      entity = Connection.getInstance ().getEntity ( url, mCredentials );
       InputStreamReader reader = new InputStreamReader ( entity.getContent () );
       JSONTokener tokenizer = new JSONTokener ( reader );
       JSONObject json = new JSONObject ( tokenizer );
       }
     } catch ( Exception e1 ) {
       logger.error ( "Caught exception reading " + url, e1 );
-      HttpEntity e2 = Connection.getInstance ().getEntity ( url );
+      HttpEntity e2 = Connection.getInstance ().getEntity ( url, mCredentials );
       InputStreamReader reader = new InputStreamReader ( e2.getContent () );
       char buffer[] = new char[1024];
       int readcount = reader.read ( buffer );
       }
     }
     // Cache it
-    XNATFS.sContentCache.put ( new Element ( "ElementList::" + url, list ) );
+    XNATFS.sContentCache.put ( new Element ( key, list ) );
     return list;
   }
 }

Source/org/xnat/xnatfs/webdav/VirtualResource.java

   String mName;
   XNATFS xnatfs;
   String mAbsolutePath;
+  Auth mCredentials;
 
   public VirtualResource ( XNATFS x, String path, String name ) {
     xnatfs = x;
     mPath = path;
     mName = name;
+    mCredentials = null;
     if ( path == null && mName.equals ( "/" ) ) {
       mAbsolutePath = "/";
     } else {
     logger.debug ( "Created virtual resource with name " + mName + " path " + mPath + " absolute Path " + mAbsolutePath );
   }
 
+  public Auth getCredentials () {
+    return mCredentials;
+  }
+
+  public void setCredentials ( Auth c ) {
+    mCredentials = c;
+  }
+
   /*
    * (non-Javadoc)
    * 
    * com.bradmcevoy.http.Request.Method, com.bradmcevoy.http.Auth)
    */
   public boolean authorise ( Request request, Method method, Auth auth ) {
-    return xnatfs.getSecurityManager ().authorise ( request, method, auth, this );
+    if ( auth == null ) {
+      return false;
+    }
+    mCredentials = auth;
+    // return xnatfs.getSecurityManager ().authorise ( request, method, auth,
+    // this );
+    return true;
   }
 
   /*

Source/org/xnat/xnatfs/webdav/xnatfs.java

 
 import net.sf.ehcache.Cache;
 import net.sf.ehcache.CacheManager;
-import net.sf.ehcache.Element;
 
 import org.apache.log4j.BasicConfigurator;
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.apache.log4j.PropertyConfigurator;
 
+import com.bradmcevoy.http.Auth;
+import com.bradmcevoy.http.Request;
 import com.bradmcevoy.http.Resource;
 import com.bradmcevoy.http.ResourceFactory;
 import com.bradmcevoy.http.SecurityManager;
-import com.ettrema.http.fs.NullSecurityManager;
+import com.bradmcevoy.http.Request.Method;
 
 /**
  * Main xnatfs-webdav class
  * @author blezek
  * 
  */
-public class XNATFS implements ResourceFactory {
+public class XNATFS implements ResourceFactory, SecurityManager {
   private static final Logger logger = Logger.getLogger ( XNATFS.class );
 
   /** Cache services from ehcache */
-  public static Cache sNodeCache;
   public static Cache sContentCache;
-  public static Cache sFileHandleCache;
   public static CacheManager mMemoryCacheManager;
 
   /** Thread pool for background downloads of files */
     sTemporaryDirectory = new File ( sTemporaryDirectory, "FileCache" );
     sTemporaryDirectory.mkdirs ();
 
-    Root root = new Root ( this, null, "/", "/" );
-    Element e = new Element ( "/", root );
-    e.setEternal ( true );
-    sNodeCache.put ( e );
-    logger.debug ( "Put root node in cache" );
-    securityManager = new NullSecurityManager ();
+    securityManager = this;
   }
 
   /*
     if ( mMemoryCacheManager.getCache ( "Content" ) == null ) {
       mMemoryCacheManager.addCache ( "Content" );
     }
-    sNodeCache = mMemoryCacheManager.getCache ( "Node" );
     sContentCache = mMemoryCacheManager.getCache ( "Content" );
     // sContentCache.getCacheEventNotificationService ().registerListener ( new
     // CacheFileCleanup () );
-    sFileHandleCache = mMemoryCacheManager.getCache ( "FileHandle" );
-    if ( sNodeCache == null ) {
-      logger.error ( "Failed to create filecache" );
-    }
+
   }
 
   /*
     if ( path.startsWith ( "/xnatfs" ) ) {
       path = path.replaceFirst ( "/xnatfs", "" );
     }
-    Element element = XNATFS.sNodeCache.get ( path );
-    if ( element != null ) {
-      return (Resource) element.getObjectValue ();
-    }
     logger.debug ( "Couldn't find node '" + path + "', trying to create the file in the parent" );
-    return createChild ( VirtualResource.dirname ( path ), VirtualResource.tail ( path ) );
+    return createChild ( path );
   }
 
   /**
    *          Name of the Node to create
    * @return The new Node. Should never return null.
    */
-  synchronized Resource createChild ( String path, String child ) {
-    logger.debug ( "createChild: " + path + " child: " + child );
-    if ( path.equals ( "" ) ) {
+  synchronized Resource createChild ( String path ) {
+    logger.debug ( "createChild: " + path );
+    if ( path == null || path.equals ( "" ) ) {
       logger.error ( "createChild: Null path" );
       return null;
     }
-    if ( path.equals ( "/" ) && child.equals ( "" ) ) {
-      return null;
+    // End the recursion
+    if ( path.equals ( "/" ) ) {
+      logger.error ( "createChild: Should have found root, creating new" );
+      Root root = new Root ( this, null, "/", "/" );
+      return root;
     }
-    Element element = XNATFS.sNodeCache.get ( path );
     VirtualDirectory parent = null;
     Resource r = null;
-    if ( element != null ) {
-      logger.debug ( "found parent " + element.getObjectValue () );
-      r = (Resource) element.getObjectValue ();
-    } else {
-      if ( path.equals ( "/" ) ) {
-        logger.error ( "createChild: Should have found root, creating new" );
-        Root root = new Root ( this, null, "/", "/" );
-        Element e = new Element ( "/", root );
-        sNodeCache.put ( e );
-        return root;
-      }
-      logger.debug ( "Didn't find parent " + path + ", attempting to create" );
-      r = createChild ( VirtualResource.dirname ( path ), VirtualResource.tail ( path ) );
-    }
+    r = createChild ( VirtualResource.dirname ( path ) );
+
     if ( r != null && r instanceof VirtualResource ) {
       parent = (VirtualDirectory) r;
     }
     if ( parent == null ) {
-      logger.error ( "Couldn't find parent of " + path + child );
+      logger.error ( "Couldn't find parent of " + path );
       return null;
     }
     try {
-      return parent.child ( child );
+      logger.debug ( "createChild: having parent " + VirtualResource.dirname ( path ) + " create child " + VirtualResource.tail ( path ) );
+      return parent.child ( VirtualResource.tail ( path ) );
     } catch ( Exception e ) {
-      logger.error ( "Falied to create child: " + child, e );
+      logger.error ( "Falied to create child: " + VirtualResource.tail ( path ), e );
     }
     return null;
   }
   }
 
   /**
-   * @return
-   */
-  public String getRealm () {
-    return securityManager.getRealm ();
-  }
-
-  /**
    * Returns the appropriate working directory for storing application data. The
    * result of this method is platform dependant: On linux, it will return
    * ~/applicationName, on windows, the working directory will be located in the
       return OSType.UNIX;
     }
   }
+
+  public Object authenticate ( String user, String password ) {
+    logger.debug ( "authenticate: " + user + "/" + password );
+    return user;
+  }
+
+  public boolean authorise ( Request request, Method method, Auth auth, Resource resource ) {
+    logger.debug ( "authorise: " + request + "/" + method + "/" + auth + "/" + resource );
+    if ( auth == null ) {
+      return false;
+    }
+    logger.debug ( "authorise: " + auth.user + "/" + auth.password );
+    return true;
+  }
+
+  public String getRealm () {
+    logger.debug ( "getRealm" );
+    return new String ( "xnatfs" );
+  }
 }