Commits

Daniel Blezek committed c57f220

Moved from individual components for the URL to a single URL argument. Existing arguments maintained for backwards compatability.

Comments (0)

Files changed (5)

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

 import com.bradmcevoy.http.Auth;
 
 public class Connection {
+  static class PreemptiveAuth implements HttpRequestInterceptor {
+    public void process ( final HttpRequest request, final HttpContext context ) throws HttpException, IOException {
+      AuthState authState = (AuthState) context.getAttribute ( ClientContext.TARGET_AUTH_STATE );
+      // If no auth scheme avaialble yet, try to initialize it preemptively
+      if ( authState.getAuthScheme () == null ) {
+        AuthScheme authScheme = (AuthScheme) context.getAttribute ( "preemptive-auth" );
+        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute ( ClientContext.CREDS_PROVIDER );
+        HttpHost targetHost = (HttpHost) context.getAttribute ( ExecutionContext.HTTP_TARGET_HOST );
+        if ( authScheme != null ) {
+          Credentials creds = credsProvider.getCredentials ( new AuthScope ( targetHost.getHostName (), targetHost.getPort () ) );
+          if ( creds == null ) {
+            throw new HttpException ( "No credentials for preemptive authentication" );
+          }
+          authState.setAuthScheme ( authScheme );
+          authState.setCredentials ( creds );
+        }
+      }
+    }
+  }
+
   private static final Logger logger = Logger.getLogger ( Connection.class );
-
   String mHost = "central.xnat.org";
   String mUsername = "blezek";
   String mPassword = "throwaway";
   String mPrefix = "/REST";
   String mScheme = "http";
   String mPort = "80";
+  String mURL = "http://central.xnat.org:80/REST";
+
   Credentials mCredentials;
-
   HttpParams params;
   SchemeRegistry schemeRegistry;
+
   ClientConnectionManager cm;
 
+  static private Connection sInstance = new Connection ();
+
   static public Connection getInstance () {
     return sInstance;
   }
 
-  static private Connection sInstance = new Connection ();
+  protected Connection () {
+    params = new BasicHttpParams ();
+    ConnManagerParams.setMaxTotalConnections ( params, 100 );
+    HttpProtocolParams.setVersion ( params, HttpVersion.HTTP_1_1 );
 
-  void setCredentials () {
+    // 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
+    // be using the HttpClient.
+    cm = new ThreadSafeClientConnManager ( params, schemeRegistry );
   }
 
-  public void setUsername ( String s ) {
-    mUsername = s;
-    setCredentials ();
+  private String constructURL () {
+    mURL = mScheme + "://" + mHost + ":" + mPort + mPrefix;
+    return mURL;
   }
 
-  public void setPassword ( String s ) {
-    mPassword = s;
-    setCredentials ();
-  }
-
-  public void setHost ( String s ) {
-    mHost = s;
-  }
-
-  /**
-   * @return the mHost
-   */
-  public String getHost () {
-    return mHost;
-  }
-
-  /**
-   * @return the mPrefix
-   */
-  public String getPrefix () {
-    return mPrefix;
-  }
-
-  /**
-   * @return the mScheme
-   */
-  public String getScheme () {
-    return mScheme;
-  }
-
-  /**
-   * @return the mPort
-   */
-  public String getPort () {
-    return mPort;
-  }
-
-  public void setPort ( String s ) {
-    mPort = s;
-  }
-
-  public void setScheme ( String s ) {
-    mScheme = s;
+  public String formatURL ( String path ) {
+    String s = mURL + path;
+    return s;
   }
 
   public HttpClient getClient ( Auth credentials ) {
     return mClient;
   }
 
-  public String formatURL ( String path ) {
-    String s = mScheme + "://" + mHost + ":" + mPort + mPrefix + path;
-    return s;
-  }
-
-  protected Connection () {
-    params = new BasicHttpParams ();
-    ConnManagerParams.setMaxTotalConnections ( params, 100 );
-    HttpProtocolParams.setVersion ( params, HttpVersion.HTTP_1_1 );
-
-    // 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
-    // be using the HttpClient.
-    cm = new ThreadSafeClientConnManager ( params, schemeRegistry );
-  }
-
   public HttpEntity getEntity ( String url, Auth credentials ) throws Exception {
     HttpClient client = Connection.getInstance ().getClient ( credentials );
     HttpGet httpget = new HttpGet ( Connection.getInstance ().formatURL ( url ) );
     return response.getEntity ();
   }
 
-  static class PreemptiveAuth implements HttpRequestInterceptor {
-    public void process ( final HttpRequest request, final HttpContext context ) throws HttpException, IOException {
-      AuthState authState = (AuthState) context.getAttribute ( ClientContext.TARGET_AUTH_STATE );
-      // If no auth scheme avaialble yet, try to initialize it preemptively
-      if ( authState.getAuthScheme () == null ) {
-        AuthScheme authScheme = (AuthScheme) context.getAttribute ( "preemptive-auth" );
-        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute ( ClientContext.CREDS_PROVIDER );
-        HttpHost targetHost = (HttpHost) context.getAttribute ( ExecutionContext.HTTP_TARGET_HOST );
-        if ( authScheme != null ) {
-          Credentials creds = credsProvider.getCredentials ( new AuthScope ( targetHost.getHostName (), targetHost.getPort () ) );
-          if ( creds == null ) {
-            throw new HttpException ( "No credentials for preemptive authentication" );
-          }
-          authState.setAuthScheme ( authScheme );
-          authState.setCredentials ( creds );
-        }
-      }
+  /**
+   * @return the mHost
+   */
+  public String getHost () {
+    return mHost;
+  }
+
+  /**
+   * @return the mPort
+   */
+  public String getPort () {
+    return mPort;
+  }
+
+  /**
+   * @return the mPrefix
+   */
+  public String getPrefix () {
+    return mPrefix;
+  }
+
+  /**
+   * @return the mScheme
+   */
+  public String getScheme () {
+    return mScheme;
+  }
+
+  public String getURL () {
+    return mURL;
+  }
+
+  void setCredentials () {
+  }
+
+  public void setHost ( String s ) {
+    mHost = s;
+    constructURL ();
+  }
+
+  public void setPassword ( String s ) {
+    mPassword = s;
+    setCredentials ();
+  }
+
+  public void setPort ( String s ) {
+    mPort = s;
+    constructURL ();
+  }
+
+  public void setScheme ( String s ) {
+    mScheme = s;
+    constructURL ();
+  }
+
+  public void setURL ( String url ) {
+    mURL = url;
+    if ( !mURL.endsWith ( "/REST" ) ) {
+      logger
+          .warn ( "Configured url '"
+              + mURL
+              + "' does not end with '/REST'.  '/REST' is the common path name for the XNAT REST api, and xnatfs may not properly work without this path.  Proceeding anyway, but be aware that I warned you!" );
+
     }
   }
+
+  public void setUsername ( String s ) {
+    mUsername = s;
+    setCredentials ();
+  }
 }

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

    * @param args
    */
   public static void main ( String[] args ) {
-
+    XNATFS.configureLogging ();
     JSAPResult config = parseArguments ( args );
 
-    Connection.getInstance ().setHost ( config.getString ( "server" ) );
-    Connection.getInstance ().setScheme ( config.getString ( "scheme" ) );
-    Connection.getInstance ().setPort ( Integer.toString ( config.getInt ( "port" ) ) );
-
+    if ( config.contains ( "server" ) || config.contains ( "scheme" ) || config.contains ( "port" ) ) {
+      logger.error ( "Command line arguments of scheme, server, and port are deprecatated and will be removed in the next release.  Please use the --url argument." );
+      Connection.getInstance ().setHost ( config.getString ( "server" ) );
+      Connection.getInstance ().setScheme ( config.getString ( "scheme" ) );
+      Connection.getInstance ().setPort ( Integer.toString ( config.getInt ( "port" ) ) );
+    }
+    if ( config.contains ( "url" ) ) {
+      Connection.getInstance ().setURL ( config.getString ( "url" ) );
+    }
     Server server = new Server ( config.getInt ( "serverport" ) );
 
     ServletContextHandler context = new ServletContextHandler ( ServletContextHandler.SESSIONS );
     ServletHolder holder = new ServletHolder ( new xnatfsServlet () );
     holder.setInitParameter ( "resource.factory.class", "org.xnat.xnatfs.webdav.XNATFS" );
     holder.setInitParameter ( "response.handler.class", "com.bradmcevoy.http.MsOfficeResponseHandler" );
-    holder.setInitParameter ( "server", config.getString ( "server" ) );
-    holder.setInitParameter ( "scheme", config.getString ( "scheme" ) );
-    holder.setInitParameter ( "port", Integer.toString ( config.getInt ( "port" ) ) );
+    // holder.setInitParameter ( "server", config.getString ( "server" ) );
+    // holder.setInitParameter ( "scheme", config.getString ( "scheme" ) );
+    // holder.setInitParameter ( "port", Integer.toString ( config.getInt (
+    // "port" ) ) );
+    holder.setInitParameter ( "url", Connection.getInstance ().getURL () );
     context.addServlet ( holder, "/*" );
 
     try {
       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 server = new FlaggedOption ( "server" ).setStringParser ( JSAP.STRING_PARSER ).setDefault ( "central.xnat.org" ).setRequired ( true ).setShortFlag ( 's' ).setLongFlag ( "server" );
+      FlaggedOption server = new FlaggedOption ( "server" ).setStringParser ( JSAP.STRING_PARSER ).setDefault ( "central.xnat.org" ).setRequired ( false ).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" );
+      FlaggedOption scheme = new FlaggedOption ( "scheme" ).setStringParser ( JSAP.STRING_PARSER ).setDefault ( "http" ).setRequired ( false ).setShortFlag ( 'h' ).setLongFlag ( "scheme" );
       jsap.registerParameter ( scheme );
       scheme.setHelp ( "Connection scheme for XNAT server.  Valid options are \"http\" or \"https\"." );
 
       jsap.registerParameter ( port );
       port.setHelp ( "port to use on the XNAT server, defaults to 80" );
 
+      FlaggedOption url = new FlaggedOption ( "url" ).setStringParser ( JSAP.STRING_PARSER ).setDefault ( "http://central.xnat.org/REST" ).setRequired ( false ).setShortFlag ( 'u' ).setLongFlag (
+          "url" );
+      jsap.registerParameter ( url );
+      url.setHelp ( "URL for XNAT server.  Should be in the form of a web site address, i.e. http://central.xnat.org/REST.  "
+          + "The URL should end with '/REST' so xnatfs can properly find the REST api of the XNAT server." );
+
       // Get a list of remaining options
       UnflaggedOption serverPort = new UnflaggedOption ( "serverport" ).setStringParser ( JSAP.INTEGER_PARSER ).setDefault ( "8081" ).setRequired ( false ).setGreedy ( false );
       serverPort.setHelp ( "Port number to run the local server on, default is 8081" );

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

 
   public XNATFS () {
     // Configure log4j
+    configureLogging ();
     // 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 );
     return true;
   }
 
+  static boolean isLoggingConfigured = false;
+
+  public static void configureLogging () {
+    if ( isLoggingConfigured ) {
+      return;
+    }
+    isLoggingConfigured = true;
+    File props;
+    props = new File ( System.getProperty ( "user.dir", "." ), "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 ();
+        }
+      }
+    }
+  }
+
   public String getRealm () {
     logger.debug ( "getRealm" );
     return new String ( "xnatfs" );

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

         // Configure server, scheme, port, and serverport
         String server = config.getInitParameter ( "server" );
         if ( server == null ) {
+          logger.warn ( "Configuration parameter 'server' is deprecated and will be removed in a future version.  Please set the 'url' parameter." );
           server = "central.xnat.org";
         }
         String scheme = config.getInitParameter ( "scheme" );
         if ( scheme == null ) {
+          logger.warn ( "Configuration parameter 'scheme' is deprecated and will be removed in a future version.  Please set the 'url' parameter." );
           scheme = "http";
         }
         String port = config.getInitParameter ( "port" );
         if ( port == null ) {
+          logger.warn ( "Configuration parameter 'port' is deprecated and will be removed in a future version.  Please set the 'url' parameter." );
           port = "80";
         }
         Connection.getInstance ().setHost ( server );
         Connection.getInstance ().setScheme ( scheme );
         Connection.getInstance ().setPort ( port );
+        String url = config.getInitParameter ( "url" );
+        if ( url != null ) {
+          Connection.getInstance ().setURL ( url );
+        }
+
         logger.debug ( "Starting XNATFS servlet" );
-        logger.debug ( "Host: " + Connection.getInstance ().getHost () );
-        logger.debug ( "Scheme: " + Connection.getInstance ().getScheme () );
-        logger.debug ( "Port: " + Connection.getInstance ().getPort () );
+        logger.debug ( "URL: " + Connection.getInstance ().getURL () );
 
       }
     } catch ( ServletException ex ) {

WebContent/WEB-INF/web.xml

             <param-value>/404.jsp</param-value>
         </init-param>
         <init-param>
-          <!-- IP address of the XNAT server to connect this instance of xnatfs -->
-          <param-name>server</param-name>
-          <param-value>central.xnat.org</param-value>
-        </init-param>
-        <init-param>
-          <!-- Connection scheme to contact the server, should be http or https -->
-          <param-name>scheme</param-name>
-          <param-value>http</param-value>
-        </init-param>
-        <init-param>
-          <!-- Port on the XNAT instance, 80 is default -->
-          <param-name>port</param-name>
-          <param-value>80</param-value>
+          <!-- URL of the XNAT server to connect this instance of xnatfs.  NB: should end in /REST -->
+          <param-name>url</param-name>
+          <param-value>http://central.xnat.org/REST</param-value>
         </init-param>
     </servlet>
         <servlet-mapping>