Anonymous avatar Anonymous committed 4ac4d82

Finally working with content length and authentication

Comments (0)

Files changed (8)

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

     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, 0l );
+      RemoteFile remote = new RemoteFile ( xnatfs, mAbsolutePath, childName, mURL + "files/" + childName, -1l );
+      setChildAuthorization ( remote );
       return remote;
     }
     return null;

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

       logger.error ( "Failed to get child element list: " + e );
     }
     if ( true || s.contains ( childName ) ) {
-      return new Scan ( xnatfs, mAbsolutePath, childName, mURL + "scans/" + childName + "/" );
+      Scan scan = new Scan ( xnatfs, mAbsolutePath, childName, mURL + "scans/" + childName + "/" );
+      setChildAuthorization ( scan );
+      return scan;
     }
     return null;
   }

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

     }
     if ( true || s.contains ( childName ) ) {
       // Look up in the cache
-      return new Subject ( xnatfs, mAbsolutePath, childName, mURL + "subjects/" + childName + "/" );
+      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,

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

       mURL = url;
     }
     mContentLength = length;
-    logger.debug ( "RemoteFile constructor absolute path is: " + mAbsolutePath );
+    logger.debug ( "RemoteFile constructor absolute path is: " + mAbsolutePath + " length: " + length );
   }
 
   public void setContentLength ( Long l ) {
+    logger.debug ( "setContentLength: " + l );
     mContentLength = l;
   }
 
    */
   public boolean authorise ( Request request, Method method, Auth auth ) {
     boolean b = super.authorise ( request, method, auth );
-    if ( b ) {
-      // Get the size from the parent
-      String parent = dirname ( mURL );
-      // Remove the trailing / if any
-      if ( parent.endsWith ( "/" ) ) {
-        parent = parent.substring ( 0, parent.length () - 2 );
-      }
-      HashMap<String, ArrayList<String>> s = null;
-      try {
-        ArrayList<String> l = new ArrayList<String> ();
-        l.add ( "URI" );
-        l.add ( "Size" );
-        s = Bundle.getFileMap ( parent + "?format=json", "Name", l, mCredentials );
-      } catch ( Exception e ) {
-        logger.error ( "Failed to get child element list: " + e );
-        e.printStackTrace ();
-        return false;
-      }
-      if ( s.containsKey ( mName ) ) {
-        mContentLength = Long.valueOf ( s.get ( mName ).get ( 1 ) ).longValue ();
-      }
-    }
-
-    return b;
+    logger.debug ( "authorise: checking for existance on the remote server" );
+    return b && getContentLength () != -1L;
   }
 
   public Long getContentLength () {
+    logger.debug ( "getContentLength: " + mContentLength );
+    if ( mContentLength != -1 ) {
+      return mContentLength;
+    }
+    // Get the size from the parent
+    String parent = dirname ( mURL );
+    // Remove the trailing / if any
+    if ( parent.endsWith ( "/" ) ) {
+      parent = parent.substring ( 0, parent.length () - 2 );
+    }
+    HashMap<String, ArrayList<String>> s = null;
+    try {
+      ArrayList<String> l = new ArrayList<String> ();
+      l.add ( "URI" );
+      l.add ( "Size" );
+      s = Bundle.getFileMap ( parent + "?format=json", "Name", l, mCredentials );
+    } catch ( Exception e ) {
+      logger.error ( "Failed to get child element list: " + e );
+      e.printStackTrace ();
+      return -1L;
+    }
+    if ( s.containsKey ( mName ) ) {
+      mContentLength = Long.valueOf ( s.get ( mName ).get ( 1 ) ).longValue ();
+      mURL = s.get ( mName ).get ( 0 );
+      if ( mURL.startsWith ( "/REST" ) ) {
+        mURL = mURL.substring ( 5 );
+      }
+      logger.debug ( "getContentLength: length: " + mContentLength + " URL: " + mURL );
+    } else {
+      return -1L;
+    }
     return mContentLength;
   }
 
   }
 
   public void sendContent ( OutputStream out, Range range, Map<String, String> params, String contentType ) throws IOException, NotAuthorizedException {
+    if ( getContentLength () == -1L ) {
+      throw new NotAuthorizedException ( this );
+    }
     try {
       logger.debug ( "sendContent: request for " + mAbsolutePath );
       InputStream in = getContents ();
           buffer.limit ( readCount );
           buffer.position ( 0 );
           synchronized ( mChannel ) {
-            logger.debug ( "Writing " + readCount + " bytes to virtual file " + mAbsolutePath + " 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;
+            NextReportCount += 100000;
             logger.debug ( "Read " + readCount + " ( " + TotalCount + " ) from remote file at " + mAbsolutePath );
           }
         }

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

     }
     if ( true || s.contains ( childName ) ) {
       logger.debug ( "child: Creating child " + childName );
-      return new Project ( xnatfs, mAbsolutePath, childName, mURL + "projects/" + childName + "/" );
+      Project project = new Project ( xnatfs, mAbsolutePath, childName, mURL + "projects/" + childName + "/" );
+      setChildAuthorization ( project );
+      return project;
     }
     return null;
   }

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

       logger.error ( "Failed to get child element list: " + e );
     }
     if ( true || s.contains ( childName ) ) {
-      return new Bundle ( xnatfs, mAbsolutePath, childName, mURL + "resources/" + childName + "/" );
+      Bundle bundle = new Bundle ( xnatfs, mAbsolutePath, childName, mURL + "resources/" + childName + "/" );
+      setChildAuthorization ( bundle );
+      return bundle;
     }
     return null;
   }

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

       logger.error ( "Failed to get child element list: " + e );
     }
     if ( true || s.contains ( childName ) ) {
-      return new Experiment ( xnatfs, mAbsolutePath, childName, mURL + "experiments/" + childName + "/" );
+      Experiment experiment = new Experiment ( xnatfs, mAbsolutePath, childName, mURL + "experiments/" + childName + "/" );
+      setChildAuthorization ( experiment );
+      return experiment;
     }
     return null;
   }

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

    * java.lang.String)
    */
   public Object authenticate ( String user, String password ) {
+    logger.debug ( "authenticate: Calling from class " + this.getClass ().getName () );
     return xnatfs.getSecurityManager ().authenticate ( user, password );
   }
 
+  void setChildAuthorization ( VirtualResource child ) {
+    child.authorise ( null, null, mCredentials );
+  }
+
   /*
    * (non-Javadoc)
    * 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.