Commits

Anonymous committed 69f2aeb

Nearly working. Added scheme, some niceities to the VirtualDirectory display.

Comments (0)

Files changed (7)

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

 import java.util.ArrayList;
 import java.util.HashMap;
 
+import net.sf.ehcache.Element;
+
 import org.apache.http.HttpEntity;
 import org.apache.log4j.Logger;
 import org.json.JSONArray;
   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 ();
-    }
+    /*
+     * 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
       // ();
   @SuppressWarnings("unchecked")
   public static HashMap<String, ArrayList<String>> getFileMap ( String url, String inKey, ArrayList<String> valueFields, Auth mCredentials ) throws IOException {
     // Get the subjects code
-    // String elementKey = mCredentials.user + "::" + mCredentials.password +
-    // "::ElementList::" + url;
-    // Element element = null; // XNATFS.sContentCache.get ( elementKey );
+    String elementKey = mCredentials.user + "::" + mCredentials.password + "::FileMap::" + 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, mCredentials );
-      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;
+    if ( element == null ) {
+      HttpEntity entity = null;
+      try {
+        entity = Connection.getInstance ().getEntity ( url, mCredentials );
+        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 );
         }
-        String key = subjects.getJSONObject ( idx ).getString ( inKey );
-        ArrayList<String> values = new ArrayList<String> ();
-        for ( String value : valueFields ) {
-          values.add ( subjects.getJSONObject ( idx ).getString ( value ) );
+      } catch ( Exception e ) {
+        logger.error ( "Caught exception reading " + url, e );
+        throw new IOException ( "Caught exception reading " + url );
+      } finally {
+        if ( entity != null ) {
+          entity.consumeContent ();
         }
-        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 ( elementKey, map );
+      XNATFS.sContentCache.put ( element );
     }
+    map = (HashMap<String, ArrayList<String>>) element.getObjectValue ();
     return map;
-    // element = new Element ( elementKey, map );
-    // XNATFS.sContentCache.put ( element );
-    // }
-    // map = (HashMap<String, ArrayList<String>>) element.getObjectValue ();
-    // return map;
   }
 }

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

   String mUsername = "blezek";
   String mPassword = "throwaway";
   String mPrefix = "/REST";
+  String mScheme = "http";
   String mPort = "80";
   Credentials mCredentials;
 
     mPort = s;
   }
 
+  public void setScheme ( String s ) {
+    mScheme = s;
+  }
+
   public HttpClient getClient ( Auth credentials ) {
     DefaultHttpClient mClient;
     mClient = new DefaultHttpClient ( cm, params );
   }
 
   public String formatURL ( String path ) {
-    String s = "http://" + mHost + ":" + mPort + mPrefix + path;
+    String s = mScheme + "://" + mHost + ":" + mPort + mPrefix + path;
     return s;
   }
 
     // Create and initialize scheme registry
     schemeRegistry = new SchemeRegistry ();
     schemeRegistry.register ( new Scheme ( "http", PlainSocketFactory.getSocketFactory (), 80 ) );
+    schemeRegistry.register ( new Scheme ( "https", PlainSocketFactory.getSocketFactory (), 80 ) );
 
     // Create an HttpClient with the ThreadSafeClientConnManager.
     // This connection manager must be used if more than one thread will

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

  */
 package org.xnat.xnatfs.webdav;
 
-import java.util.Date;
 import java.util.HashSet;
 
 import org.apache.log4j.Logger;
    * "got Child " + child ); list.add ( child ( child ) ); } return list; }
    */
 
-  /*
-   * (non-Javadoc)
-   * 
-   * @see com.bradmcevoy.http.Resource#getModifiedDate()
-   */
-  public Date getModifiedDate () {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
-  public Date getCreateDate () {
-    return null;
-  }
-
-  /*
-   * (non-Javadoc)
-   * 
-   * @see com.bradmcevoy.http.Resource#getUniqueId()
-   */
-  public String getUniqueId () {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
 }

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

     super ( x, path, name, url );
     mElementURL = mURL + "resources?format=json";
     mChildKey = "label";
+    mFallbackChildKey = "xnat_abstractresource_id";
   }
 
   /*

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

 
     JSAPResult config = parseArguments ( args );
 
-    Connection.getInstance ().setUsername ( config.getString ( "username" ) );
-    Connection.getInstance ().setPassword ( config.getString ( "password" ) );
     Connection.getInstance ().setHost ( config.getString ( "server" ) );
+    Connection.getInstance ().setScheme ( config.getString ( "scheme" ) );
     Connection.getInstance ().setPort ( Integer.toString ( config.getInt ( "port" ) ) );
 
     Server server = new Server ( config.getInt ( "serverport" ) );
       SimpleJSAP jsap = new SimpleJSAP ( "xnatfs", "xnatfs is a WebDAV server.  xnatfs allows "
           + " local mounting of an XNAT instance.  Users of the system can browse projects, experiments, subjects and data using the " + " a WebDAV filesystem, rather than the web interface." );
 
-      FlaggedOption username = new FlaggedOption ( "username" ).setStringParser ( JSAP.STRING_PARSER ).setDefault ( JSAP.NO_DEFAULT ).setRequired ( true ).setShortFlag ( 'u' ).setLongFlag (
-          "username" );
-      jsap.registerParameter ( username );
-      username.setHelp ( "XNAT username for connection to the XNAT server." );
-
-      FlaggedOption password = new FlaggedOption ( "password" ).setStringParser ( JSAP.STRING_PARSER ).setDefault ( JSAP.NO_DEFAULT ).setRequired ( true ).setShortFlag ( 'p' ).setLongFlag (
-          "password" );
-      jsap.registerParameter ( password );
-      password.setHelp ( "Password for XNAT username for connection to the XNAT server." );
-
       FlaggedOption server = new FlaggedOption ( "server" ).setStringParser ( JSAP.STRING_PARSER ).setDefault ( "central.xnat.org" ).setRequired ( true ).setShortFlag ( 's' ).setLongFlag ( "server" );
       jsap.registerParameter ( server );
       server.setHelp ( "XNAT server.  Should be in the form of a web site address, i.e. central.xnat.org." );
 
+      FlaggedOption scheme = new FlaggedOption ( "scheme" ).setStringParser ( JSAP.STRING_PARSER ).setDefault ( "http" ).setRequired ( true ).setShortFlag ( 'h' ).setLongFlag ( "scheme" );
+      jsap.registerParameter ( scheme );
+      scheme.setHelp ( "Connection scheme for XNAT server.  Valid options are \"http\" or \"https\"." );
+
       FlaggedOption port = new FlaggedOption ( "port" ).setStringParser ( JSAP.INTEGER_PARSER ).setDefault ( "80" ).setRequired ( false ).setShortFlag ( 'o' ).setLongFlag ( "port" );
       jsap.registerParameter ( port );
       port.setHelp ( "port to use on the XNAT server, defaults to 80" );

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

   static final Logger logger = Logger.getLogger ( VirtualDirectory.class );
   String mURL;
   String mElementURL;
+  String mFallbackChildKey = "xnat_abstractresource_id";
 
   public VirtualDirectory ( XNATFS x, String path, String name, String url ) {
     super ( x, path, name );
     return list;
   }
 
+  String makeDots ( int count ) {
+    StringBuffer d = new StringBuffer ();
+    for ( int i = 0; i < count; i++ ) {
+      d.append ( "../" );
+    }
+    return d.toString ();
+  }
+
   public void sendContent ( OutputStream out, Range range, Map<String, String> params, String contentType ) throws IOException, NotAuthorizedException {
     XmlWriter w = new XmlWriter ( out );
     w.open ( "html" );
     w.open ( "body" );
     w.begin ( "h1" ).open ().writeText ( this.getName () ).close ();
+
+    com.bradmcevoy.http.XmlWriter.Element header = w.begin ( "h3" ).open ();
+    // Write out the hierarchy
+    String[] l = this.getAbsolutePath ().split ( "/" );
+    if ( l.length > 0 ) {
+      l[0] = new String ( "(root)" );
+    }
+    w.writeText ( "/" );
+    w.begin ( "nbsp" ).close ();
+    for ( int i = 0; i < l.length - 1; i++ ) {
+      if ( !l[i].equals ( "" ) ) {
+        w.begin ( "a" ).writeAtt ( "href", makeDots ( l.length - 1 - i ) ).open ().writeText ( l[i] ).close ();
+        w.begin ( "nbsp" ).close ();
+        w.writeText ( "/" );
+        w.begin ( "nbsp" ).close ();
+      }
+    }
+    if ( l.length > 0 ) {
+      w.writeText ( l[l.length - 1] );
+    }
+    header.close ();
     w.open ( "table" );
     w.open ( "tr" );
     w.open ( "td" );
         if ( id == null ) {
           id = subjects.getJSONObject ( idx ).getString ( mChildKey );
         }
+        if ( id == null || id.equals ( "" ) ) {
+          id = subjects.getJSONObject ( idx ).getString ( mFallbackChildKey );
+        }
         list.add ( id );
       }
     } catch ( Exception e1 ) {
-username=blezek
-password=throwaway
 server=central.xnat.org