Commits

Anonymous committed 4e63af6

Added UploadDirectory, but not currently working

  • Participants
  • Parent commits 4ac4d82

Comments (0)

Files changed (8)

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

 import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 
 import net.sf.ehcache.Element;
 
   @Override
   public Resource child ( String childName ) {
     logger.debug ( "child: create " + childName );
-    HashMap<String, ArrayList<String>> s = null;
     /*
      * try { ArrayList<String> l = new ArrayList<String> (); l.add ( "URI" );
      * l.add ( "Size" ); s = getFileMap ( mElementURL, "Name", l ); } catch (
      * Exception e ) { logger.error ( "Failed to get child element list: " + e
      * ); e.printStackTrace (); }
      */
-    if ( true || s.containsKey ( childName ) ) {
-      // long size = Long.valueOf ( s.get ( childName ).get ( 1 ) ).longValue
-      // ();
-      RemoteFile remote = new RemoteFile ( xnatfs, mAbsolutePath, childName, mURL + "files/" + childName, -1l );
-      setChildAuthorization ( remote );
-      return remote;
+    if ( childName.equals ( "Upload" ) ) {
+      UploadDirectory upload = new UploadDirectory ( xnatfs, mAbsolutePath, childName, mURL + "files/" );
+      setChildAuthorization ( upload );
+      return upload;
     }
-    return null;
+    // long size = Long.valueOf ( s.get ( childName ).get ( 1 ) ).longValue
+    // ();
+    RemoteFile remote = new RemoteFile ( xnatfs, mAbsolutePath, childName, mURL + "files/" + childName, -1l );
+    setChildAuthorization ( remote );
+    return remote;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.xnat.xnatfs.webdav.VirtualDirectory#getChildren()
+   */
+  @SuppressWarnings("unchecked")
+  @Override
+  public List<? extends Resource> getChildren () {
+    List<Resource> children = (List<Resource>) super.getChildren ();
+    children.add ( child ( "Upload" ) );
+    return children;
   }
 
   protected synchronized HashMap<String, ArrayList<String>> getFileMap ( String url, String inKey, ArrayList<String> valueFields ) throws IOException {

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

  */
 package org.xnat.xnatfs.webdav;
 
-import java.util.HashSet;
-
 import org.apache.log4j.Logger;
 
 import com.bradmcevoy.http.Resource;
   @Override
   public Resource child ( String childName ) {
     logger.debug ( "child: create " + childName );
-    HashSet<String> s = null;
-    try {
-      s = getElementList ( mElementURL, mChildKey );
-    } catch ( Exception e ) {
-      logger.error ( "Failed to get child element list: " + e );
-    }
-    if ( true || s.contains ( childName ) ) {
-      Scan scan = new Scan ( xnatfs, mAbsolutePath, childName, mURL + "scans/" + childName + "/" );
-      setChildAuthorization ( scan );
-      return scan;
-    }
-    return null;
+    Scan scan = new Scan ( xnatfs, mAbsolutePath, childName, mURL + "scans/" + childName + "/" );
+    setChildAuthorization ( scan );
+    return scan;
   }
 }

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

  */
 package org.xnat.xnatfs.webdav;
 
-import java.util.HashSet;
-
 import org.apache.log4j.Logger;
 
 import com.bradmcevoy.http.Resource;
   @Override
   public Resource child ( String childName ) {
     logger.debug ( "child: create " + childName );
-    HashSet<String> s = null;
-    try {
-      s = getElementList ( mElementURL, mChildKey );
-    } catch ( Exception e ) {
-      logger.error ( "Failed to get child element list: " + e );
-    }
-    if ( true || s.contains ( childName ) ) {
-      // Look up in the cache
-      Subject subject = new Subject ( xnatfs, mAbsolutePath, childName, mURL + "subjects/" + childName + "/" );
-      setChildAuthorization ( subject );
-      return subject;
-    }
-    if ( childName.equals ( "Image1.dcm" ) ) {
-      return new RemoteFile ( xnatfs, mAbsolutePath, childName,
-          "/projects/NAMIC_TEST/subjects/1/experiments/MR1/scans/4/resources/DICOM/files/1.MR.head_DHead.4.176.20061214.091206.156000.9694718604.dcm", 191908L );
-    }
-    if ( childName.equals ( "Image2.dcm" ) ) {
-      return new RemoteFile ( xnatfs, mAbsolutePath, childName,
-          "/projects/NAMIC_TEST/subjects/1/experiments/MR1/scans/4/resources/DICOM/files/1.MR.head_DHead.4.167.20061214.091206.156000.1886718586.dcm", 191902L );
-    }
-    return null;
+    // Look up in the cache
+    Subject subject = new Subject ( xnatfs, mAbsolutePath, childName, mURL + "subjects/" + childName + "/" );
+    setChildAuthorization ( subject );
+    return subject;
   }
 
 }

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

  */
 package org.xnat.xnatfs.webdav;
 
-import java.util.HashSet;
-
 import org.apache.log4j.Logger;
 
 import com.bradmcevoy.http.Resource;
    * @see com.bradmcevoy.http.CollectionResource#child(java.lang.String)
    */
   public Resource child ( String childName ) {
-    HashSet<String> s = null;
-    try {
-      s = getElementList ( mURL + "projects?format=json", mChildKey );
-    } catch ( Exception e ) {
-      logger.error ( "Failed to get child element list: " + e );
-    }
-    if ( true || s.contains ( childName ) ) {
-      logger.debug ( "child: Creating child " + childName );
-      Project project = new Project ( xnatfs, mAbsolutePath, childName, mURL + "projects/" + childName + "/" );
-      setChildAuthorization ( project );
-      return project;
-    }
-    return null;
+    logger.debug ( "child: Creating child " + childName );
+    Project project = new Project ( xnatfs, mAbsolutePath, childName, mURL + "projects/" + childName + "/" );
+    setChildAuthorization ( project );
+    return project;
   }
 
   /*

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

  */
 package org.xnat.xnatfs.webdav;
 
-import java.util.HashSet;
-
 import org.apache.log4j.Logger;
 
 import com.bradmcevoy.http.Resource;
   @Override
   public Resource child ( String childName ) {
     logger.debug ( "child: create " + childName );
-    HashSet<String> s = null;
-    try {
-      s = getElementList ( mElementURL, null );
-    } catch ( Exception e ) {
-      logger.error ( "Failed to get child element list: " + e );
-    }
-    if ( true || s.contains ( childName ) ) {
-      Bundle bundle = new Bundle ( xnatfs, mAbsolutePath, childName, mURL + "resources/" + childName + "/" );
-      setChildAuthorization ( bundle );
-      return bundle;
-    }
-    return null;
+    Bundle bundle = new Bundle ( xnatfs, mAbsolutePath, childName, mURL + "resources/" + childName + "/" );
+    setChildAuthorization ( bundle );
+    return bundle;
   }
 }

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

  */
 package org.xnat.xnatfs.webdav;
 
-import java.util.HashSet;
-
 import org.apache.log4j.Logger;
 
 import com.bradmcevoy.http.Resource;
   @Override
   public Resource child ( String childName ) {
     logger.debug ( "child: create " + childName );
-    HashSet<String> s = null;
-    try {
-      s = getElementList ( mElementURL, null );
-    } catch ( Exception e ) {
-      logger.error ( "Failed to get child element list: " + e );
-    }
-    if ( true || s.contains ( childName ) ) {
-      Experiment experiment = new Experiment ( xnatfs, mAbsolutePath, childName, mURL + "experiments/" + childName + "/" );
-      setChildAuthorization ( experiment );
-      return experiment;
-    }
-    return null;
+    Experiment experiment = new Experiment ( xnatfs, mAbsolutePath, childName, mURL + "experiments/" + childName + "/" );
+    setChildAuthorization ( experiment );
+    return experiment;
   }
 }

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

+/**
+ * 
+ */
+package org.xnat.xnatfs.webdav;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import com.bradmcevoy.http.PutableResource;
+import com.bradmcevoy.http.Resource;
+
+/**
+ * @author blezek
+ * 
+ */
+public class UploadDirectory extends VirtualDirectory implements PutableResource {
+  static final Logger logger = Logger.getLogger ( UploadDirectory.class );
+
+  /**
+   * @param x
+   * @param path
+   * @param name
+   * @param url
+   */
+  public UploadDirectory ( XNATFS x, String path, String name, String url ) {
+    super ( x, path, name, url );
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.xnat.xnatfs.webdav.VirtualDirectory#child(java.lang.String)
+   */
+  @Override
+  public Resource child ( String arg0 ) {
+    // UploadDirectory never has any children
+    return null;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.xnat.xnatfs.webdav.VirtualDirectory#getChildren()
+   */
+  @Override
+  public List<? extends Resource> getChildren () {
+    return new ArrayList<Resource> ();
+  }
+
+  /**
+   * 
+   * @see com.bradmcevoy.http.PutableResource#createNew(java.lang.String,
+   *      java.io.InputStream, java.lang.Long, java.lang.String)
+   */
+  public Resource createNew ( String name, InputStream in, Long length, String contentType ) throws IOException {
+    logger.debug ( "createNew: asked to create a new resource called " + name + " of length " + length + " and type " + contentType );
+    return null;
+  }
+
+}

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

+/**
+ * 
+ */
+package org.xnat.xnatfs.webdav;
+
+import java.io.File;
+import java.net.URL;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.CacheManager;
+
+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.bradmcevoy.http.Request.Method;
+
+/**
+ * Main xnatfs-webdav class
+ * 
+ * @author blezek
+ * 
+ */
+public class XNATFS implements ResourceFactory, SecurityManager {
+  private static final Logger logger = Logger.getLogger ( XNATFS.class );
+
+  /** Cache services from ehcache */
+  public static Cache sContentCache;
+  public static CacheManager mMemoryCacheManager;
+
+  /** Thread pool for background downloads of files */
+  public static ExecutorService sExecutor = Executors.newCachedThreadPool ();
+  public static File sTemporaryDirectory;
+
+  SecurityManager securityManager;
+
+  public XNATFS () {
+    // Configure log4j
+    // First try to configure from local directory, then from application
+    // support directory
+    File props;
+    props = new File ( System.getProperty ( "user.dir", "." ), "log4j.properties" );
+    props = new File ( "/Users/blezek/Source/xnatfs-webapp/log4j.properties" );
+    if ( props.exists () && props.canRead () ) {
+      System.out.println ( "Found props file: " + props.getAbsoluteFile () );
+      PropertyConfigurator.configure ( props.getAbsolutePath () );
+    } else {
+      props = new File ( getApplicationResourceDirectory ( "xnatfs" ), "log4j.properties" );
+      if ( props.exists () && props.canRead () ) {
+        System.out.println ( "Found props file: " + props.getAbsoluteFile () );
+        PropertyConfigurator.configure ( props.getAbsolutePath () );
+      } else {
+        URL url = xnatfsServlet.class.getResource ( "/log4j.properties" );
+        if ( url != null ) {
+          System.out.println ( "Found props url: " + url );
+          PropertyConfigurator.configure ( url );
+        } else {
+          System.out.println ( "No props file found falling back to defaults" );
+          BasicConfigurator.configure ();
+        }
+      }
+    }
+    Logger.getRootLogger ().setLevel ( Level.WARN );
+    Logger.getLogger ( "org.apache.commons" ).setLevel ( Level.WARN );
+    Logger.getLogger ( "httpclient.wire" ).setLevel ( Level.WARN );
+    Logger.getLogger ( "org.xnat.xnatfs.webdav." ).setLevel ( Level.DEBUG );
+    configureCache ();
+    configureConnection ();
+    sTemporaryDirectory = new File ( System.getProperty ( "java.io.tmpdir" ) );
+    if ( mMemoryCacheManager.getDiskStorePath () != null ) {
+      sTemporaryDirectory = new File ( mMemoryCacheManager.getDiskStorePath () );
+    }
+    sTemporaryDirectory = new File ( sTemporaryDirectory, "FileCache" );
+    sTemporaryDirectory.mkdirs ();
+
+    securityManager = this;
+  }
+
+  /*
+   * Configure the cache by looking up the ehcache.xml file in the system
+   * classpath. Expects to have a "Node", "Content" and "FileHandle" cache. If
+   * any cache does not exist, it is created on the fly.
+   * 
+   * First checks the local directory, then the application directory, and
+   * finally falls back on the embedded file.
+   * 
+   * Register a caches listener for the content cache to close and delete files.
+   */
+  static public void configureCache () {
+    URL url = ClassLoader.getSystemResource ( "ehcache.xml" );
+    logger.info ( "Found configuration URL: " + url );
+    File SystemConfig = new File ( getApplicationResourceDirectory ( "xnatfs" ), "ehcache.xml" );
+    File LocalConfig = new File ( System.getProperty ( "user.dir", "." ), "ehcache.xml" );
+    if ( LocalConfig.exists () && LocalConfig.canRead () ) {
+      mMemoryCacheManager = CacheManager.create ( LocalConfig.getAbsolutePath () );
+    } else if ( SystemConfig.exists () && SystemConfig.canRead () ) {
+      mMemoryCacheManager = CacheManager.create ( SystemConfig.getAbsolutePath () );
+    } else if ( url != null ) {
+      mMemoryCacheManager = CacheManager.create ( url );
+    } else {
+      mMemoryCacheManager = CacheManager.create ();
+    }
+
+    if ( mMemoryCacheManager.getCache ( "Node" ) == null ) {
+      mMemoryCacheManager.addCache ( "Node" );
+    }
+    if ( mMemoryCacheManager.getCache ( "Content" ) == null ) {
+      mMemoryCacheManager.addCache ( "Content" );
+    }
+    sContentCache = mMemoryCacheManager.getCache ( "Content" );
+    // sContentCache.getCacheEventNotificationService ().registerListener ( new
+    // CacheFileCleanup () );
+
+  }
+
+  /*
+   * Configure the connection. Hard coded username and password for now. NB:
+   * username and password need to be set before the first
+   */
+  static public void configureConnection () {
+    Connection.getInstance ().setUsername ( "blezek" );
+    Connection.getInstance ().setPassword ( "throwaway" );
+  }
+
+  /**
+   * Returns the Node indicated by the path argument. If the note is not in the
+   * xnatfs.sNodeCache, call createChild to try to create the parent and this
+   * child within it.
+   * 
+   * @see com.bradmcevoy.http.ResourceFactory#getResource(java.lang.String,
+   *      java.lang.String)
+   */
+  public Resource getResource ( String host, String path ) {
+    logger.debug ( "getResource: " + host + " path: " + path );
+    if ( path.startsWith ( "/xnatfs" ) ) {
+      path = path.replaceFirst ( "/xnatfs", "" );
+    }
+    logger.debug ( "Couldn't find node '" + path + "', trying to create the file in the parent" );
+    return createChild ( path );
+  }
+
+  /**
+   * Lookup the path, if it exists, try to create the child. If the Node
+   * indicated by the path is not in the cache, try to create it by looking up
+   * the parent by calling this function recursively.
+   * 
+   * @param path
+   *          Path to the node in which to create child
+   * @param child
+   *          Name of the Node to create
+   * @return The new Node. Should never return null.
+   */
+  synchronized Resource createChild ( String path ) {
+    logger.debug ( "createChild: " + path );
+    if ( path == null || path.equals ( "" ) ) {
+      logger.error ( "createChild: Null path" );
+      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;
+    }
+    VirtualDirectory parent = null;
+    Resource r = null;
+    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 );
+      return null;
+    }
+    try {
+      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: " + VirtualResource.tail ( path ), e );
+    }
+    return null;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see com.bradmcevoy.http.ResourceFactory#getSupportedLevels()
+   */
+  public String getSupportedLevels () {
+    return "1,2";
+  }
+
+  /**
+   * @return
+   */
+  public SecurityManager getSecurityManager () {
+    return securityManager;
+  }
+
+  /**
+   * 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
+   * user's application data folder. For Mac OS systems, the working directory
+   * will be placed in the proper location in "Library/Application Support".
+   * <p/>
+   * This method will also make sure that the working directory exists. When
+   * invoked, the directory and all required subfolders will be created.
+   * 
+   * @param applicationName
+   *          Name of the application, used to determine the working directory.
+   * @return the appropriate working directory for storing application data.
+   */
+  public static File getApplicationResourceDirectory ( final String applicationName ) {
+    final String userHome = System.getProperty ( "user.home", "." );
+    final File workingDirectory;
+    switch ( getOS () ) {
+    case UNIX:
+      workingDirectory = new File ( userHome, '.' + applicationName + '/' );
+      break;
+    case WINDOWS:
+      final String applicationData = System.getenv ( "APPDATA" );
+      if ( applicationData != null )
+        workingDirectory = new File ( applicationData, "." + applicationName + '/' );
+      else
+        workingDirectory = new File ( userHome, '.' + applicationName + '/' );
+      break;
+    case MACINTOSH:
+      workingDirectory = new File ( userHome, "Library/Application Support/" + applicationName );
+      break;
+    default:
+      return new File ( "." );
+    }
+    if ( !workingDirectory.exists () && !workingDirectory.mkdirs () ) {
+      throw new RuntimeException ( "The working directory could not be created: " + workingDirectory );
+
+    }
+    return workingDirectory;
+  }
+
+  public enum OSType {
+    WINDOWS, MACINTOSH, UNIX
+  }
+
+  static OSType getOS () {
+    String sysName = System.getProperty ( "os.name" ).toLowerCase ();
+    if ( sysName.contains ( "windows" ) ) {
+      return OSType.WINDOWS;
+    } else if ( sysName.contains ( "mac" ) ) {
+      return OSType.MACINTOSH;
+    } else {
+      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 );
+    if ( resource instanceof VirtualResource ) {
+      VirtualResource r = (VirtualResource) resource;
+      r.authenticate ( auth.user, auth.password );
+    }
+    return true;
+  }
+
+  public String getRealm () {
+    logger.debug ( "getRealm" );
+    return new String ( "xnatfs" );
+  }
+}