Commits

Daniel Blezek  committed a545cb3

Full directory working, but still having errors in RemoteFile

  • Participants
  • Parent commits 1ac9269

Comments (0)

Files changed (8)

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

+/**
+ * 
+ */
+package org.xnat.xnatfs.webdav;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import net.sf.ehcache.Element;
+
+import org.apache.http.HttpEntity;
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.json.JSONTokener;
+
+import com.bradmcevoy.http.Resource;
+
+/**
+ * @author blezek
+ * 
+ */
+public class Bundle extends VirtualDirectory {
+
+  /**
+   * @param x
+   * @param path
+   * @param name
+   * @param url
+   */
+  public Bundle ( XNATFS x, String path, String name, String url ) {
+    super ( x, path, name, url );
+    mElementURL = mURL + "files?format=json";
+    mChildKey = "Name";
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.xnat.xnatfs.webdav.VirtualDirectory#child(java.lang.String)
+   */
+  @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> ();
+      l.add ( "URI" );
+      l.add ( "Size" );
+      s = getFileMap ( mElementURL, "Name", l );
+    } catch ( Exception e ) {
+      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 + "resources/" + childName, size );
+      Element element = new Element ( childPath, remote );
+      XNATFS.sNodeCache.put ( element );
+      return (Resource) element.getObjectValue ();
+    }
+    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 );
+    HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>> ();
+    if ( element == null ) {
+      HttpEntity entity = null;
+      try {
+        entity = Connection.getInstance ().getEntity ( url );
+        InputStreamReader reader = new InputStreamReader ( entity.getContent () );
+        JSONTokener tokenizer = new JSONTokener ( reader );
+        JSONObject json = new JSONObject ( tokenizer );
+        JSONArray subjects = json.getJSONObject ( "ResultSet" ).getJSONArray ( "Result" );
+        logger.debug ( "Found: " + subjects.length () + " elements" );
+        for ( int idx = 0; idx < subjects.length (); idx++ ) {
+          if ( subjects.isNull ( idx ) ) {
+            continue;
+          }
+          String key = subjects.getJSONObject ( idx ).getString ( inKey );
+          ArrayList<String> values = new ArrayList<String> ();
+          for ( String value : valueFields ) {
+            values.add ( subjects.getJSONObject ( idx ).getString ( value ) );
+          }
+          map.put ( key, values );
+        }
+      } catch ( Exception e ) {
+        logger.error ( "Caught exception reading " + url, e );
+        throw new IOException ( "Caught exception reading " + url );
+      } finally {
+        if ( entity != null ) {
+          entity.consumeContent ();
+        }
+      }
+      element = new Element ( "FileMap::" + url, map );
+      XNATFS.sContentCache.put ( element );
+    }
+    map = (HashMap<String, ArrayList<String>>) element.getObjectValue ();
+    return map;
+  }
+}

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

  */
 package org.xnat.xnatfs.webdav;
 
-import java.util.List;
+import java.util.HashSet;
+
+import net.sf.ehcache.Element;
 
 import com.bradmcevoy.http.Resource;
 
 /**
  * @author blezek
- *
+ * 
  */
 public class Experiment extends VirtualDirectory {
 
    */
   public Experiment ( XNATFS x, String path, String name, String url ) {
     super ( x, path, name, url );
-    // TODO Auto-generated constructor stub
+    mElementURL = mURL + "scans?format=json";
+    mChildKey = "id";
   }
 
-  /* (non-Javadoc)
+  /*
+   * (non-Javadoc)
+   * 
    * @see org.xnat.xnatfs.webdav.VirtualDirectory#child(java.lang.String)
    */
   @Override
-  public Resource child ( String arg0 ) {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
-  /* (non-Javadoc)
-   * @see org.xnat.xnatfs.webdav.VirtualDirectory#getChildren()
-   */
-  @Override
-  public List<? extends Resource> getChildren () {
-    // TODO Auto-generated method stub
+  public Resource child ( String childName ) {
+    logger.debug ( "child: create " + childName );
+    String childPath = mAbsolutePath + childName;
+    HashSet<String> s = null;
+    try {
+      s = getElementList ( mElementURL, mChildKey );
+    } catch ( Exception e ) {
+      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 null;
   }
 

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

       mURL = url;
     }
     mContentLength = length;
+    logger.debug ( "RemoteFile constructor absolute path is: " + mAbsolutePath );
   }
 
   public void setContentLength ( Long l ) {
           buffer.limit ( readCount );
           buffer.position ( 0 );
           synchronized ( mChannel ) {
-            logger.debug ( "Writing " + readCount + " bytes to virtual file " + mPath + " for URL: " + mURL );
+            logger.debug ( "Writing " + readCount + " bytes to virtual file " + mAbsolutePath + " for URL: " + mURL );
             mChannel.write ( buffer );
           }
           TotalCount += readCount;
           if ( TotalCount > NextReportCount ) {
             NextReportCount += 1000000;
-            logger.debug ( "Read " + readCount + " ( " + TotalCount + " ) from remote file at " + mPath );
+            logger.debug ( "Read " + readCount + " ( " + TotalCount + " ) from remote file at " + mAbsolutePath );
           }
         }
-        logger.debug ( "Finished fetching " + mURL + " as virtual file " + mPath + " into cache file " + mCachedFile );
+        logger.debug ( "Finished fetching " + mURL + " as virtual file " + mAbsolutePath + " into cache file " + mCachedFile );
         mDownloadComplete = true;
         entity.consumeContent ();
         mContentLength = mChannel.size ();
       } catch ( Exception ex ) {
-        logger.error ( "Failed to get body of " + mPath + " from URL " + mURL, ex );
+        logger.error ( "Failed to get body of " + mAbsolutePath + " from URL " + mURL, ex );
         return Boolean.FALSE;
       } catch ( Throwable t ) {
-        logger.error ( "Caught throwable for " + mPath + " URL " + mURL, t );
+        logger.error ( "Caught throwable for " + mAbsolutePath + " URL " + mURL, t );
         return Boolean.FALSE;
       }
       Element n = new Element ( mAbsolutePath, mCachedFile );

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

+/**
+ * 
+ */
+package org.xnat.xnatfs.webdav;
+
+import java.util.HashSet;
+
+import net.sf.ehcache.Element;
+
+import org.apache.log4j.Logger;
+
+import com.bradmcevoy.http.Resource;
+
+/**
+ * @author blezek
+ * 
+ */
+public class Scan extends VirtualDirectory {
+  static final Logger logger = Logger.getLogger ( Scan.class );
+
+  /**
+   * @param x
+   * @param path
+   * @param name
+   * @param url
+   */
+  public Scan ( XNATFS x, String path, String name, String url ) {
+    super ( x, path, name, url );
+    mElementURL = mURL + "resources?format=json";
+    mChildKey = "label";
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.xnat.xnatfs.webdav.VirtualDirectory#child(java.lang.String)
+   */
+  @Override
+  public Resource child ( String childName ) {
+    logger.debug ( "child: create " + childName );
+    String childPath = mAbsolutePath + childName;
+    HashSet<String> s = null;
+    try {
+      s = getElementList ( mElementURL, null );
+    } catch ( Exception e ) {
+      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 null;
+  }
+}

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

   public Subject ( XNATFS x, String path, String name, String url ) {
     super ( x, path, name, url );
     mElementURL = mURL + "experiments?format=json";
+    mChildKey = "label";
   }
 
   /*
     String childPath = mAbsolutePath + childName;
     HashSet<String> s = null;
     try {
-      s = getElementList ( mElementURL, mChildKey );
+      s = getElementList ( mElementURL, null );
     } catch ( Exception e ) {
       logger.error ( "Failed to get child element list: " + e );
     }
       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 + "/" ) );
+      Element element = new Element ( childPath, new Experiment ( xnatfs, mAbsolutePath, childName, mURL + "experiments/" + childName + "/" ) );
       XNATFS.sNodeCache.put ( element );
       return (Resource) element.getObjectValue ();
     }

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

 
 abstract public class VirtualDirectory extends VirtualResource implements CollectionResource, GetableResource {
   String mChildKey;
-  static final Logger logger = Logger.getLogger ( VirtualResource.class );
+  static final Logger logger = Logger.getLogger ( VirtualDirectory.class );
   String mURL;
   String mElementURL;
 

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

     // 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 ();
         }
       }

File properties/log4j.properties

-log4j.rootLogger=warn, log, stdout
+log4j.rootLogger=debug, log, stdout
 
 
 log4j.appender.stdout=org.apache.log4j.ConsoleAppender