You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by je...@apache.org on 2001/03/08 09:19:06 UTC

cvs commit: jakarta-slide/src/webdav/client/src/org/apache/webdav/util WebdavSession.java WebdavResource.java WebdavClientSession.java

jericho     01/03/08 00:19:05

  Modified:    src/webdav/client/src/org/apache/webdav/util
                        WebdavResource.java
  Added:       src/webdav/client/src/org/apache/webdav/util
                        WebdavSession.java
  Removed:     src/webdav/client/src/org/apache/webdav/util
                        WebdavClientSession.java
  Log:
  - Add handy session methods.
  - Change the session methods for static.
  
  Revision  Changes    Path
  1.13      +25 -33    jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavResource.java
  
  Index: WebdavResource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavResource.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- WebdavResource.java	2001/03/07 02:08:02	1.12
  +++ WebdavResource.java	2001/03/08 08:19:04	1.13
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavResource.java,v 1.12 2001/03/07 02:08:02 jericho Exp $
  - * $Revision: 1.12 $
  - * $Date: 2001/03/07 02:08:02 $
  + * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavResource.java,v 1.13 2001/03/08 08:19:04 jericho Exp $
  + * $Revision: 1.13 $
  + * $Date: 2001/03/08 08:19:04 $
    *
    * ====================================================================
    *
  @@ -240,13 +240,6 @@
   
   
       /**
  -     * The table of clients for multi-session management.
  -     * Using WebdavClientSession can keep to have the multiple connections.
  -     */
  -    private WebdavClientSession clients = new WebdavClientSession();
  -
  -
  -    /**
        * A WebdavResource flag to check its existence;
        */
       private boolean exists = false;
  @@ -504,7 +497,7 @@
           httpUrlChecked = httpUrl.toString();
           // Save the current session.
           if (client != null)
  -            clients.setSession(httpUrl, client);
  +            WebdavSession.setSession(httpUrl, client);
           client = null;
       }
   
  @@ -842,7 +835,7 @@
        */
       public void close() throws IOException {
   
  -        clients.unsetSession(httpUrl);
  +        WebdavSession.unsetSession(httpUrl);
       }
   
   
  @@ -1048,7 +1041,7 @@
       public InputStream getInputStream(String tempDir)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           // use disk to save by default
           GetMethod method = new GetMethod(httpUrl.getPath(), tempDir);
  @@ -1089,7 +1082,7 @@
           FileOutputStream fos = null;
           GetMethod method = null;
           try {
  -            WebdavClient client = clients.getSessionInstance(httpUrl);
  +            WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
               // use disk to save by default
               String source = httpUrl.getPath();
  @@ -1158,7 +1151,7 @@
       public boolean getMethod(String path, File file)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           // use disk to save by default
           GetMethod method = new GetMethod(HttpURL.getPath(path), file);
  @@ -1198,7 +1191,7 @@
       public boolean putMethod(String path, String data)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           PutMethod method = new PutMethod(HttpURL.getPath(path));
           method.sendData(data);
  @@ -1243,7 +1236,7 @@
       public boolean putMethod(String path, File file)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           PutMethod method = new PutMethod(HttpURL.getPath(path));
           method.sendData(file);
  @@ -1287,7 +1280,7 @@
       public boolean optionsMethod(String path)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           OptionsMethod method;
           if (path.trim().equals("*"))
  @@ -1346,8 +1339,7 @@
       public static Enumeration optionsMethod(HttpURL httpUrl)
           throws WebdavException, IOException {
   
  -        WebdavClientSession session = new WebdavClientSession();
  -        WebdavClient client = session.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           OptionsMethod method = new OptionsMethod(httpUrl.getPath());
           client.executeMethod(method);
  @@ -1366,7 +1358,7 @@
                   options.addElement(davCapabilities.nextElement());
               }
           }
  -        session.unsetSession(httpUrl);
  +        WebdavSession.unsetSession(httpUrl);
   
           return options.elements();
       }
  @@ -1413,7 +1405,7 @@
       public Enumeration propfindMethod(String path, int depth)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           // Check the path alright.
           path = HttpURL.getPath(path);
  @@ -1498,7 +1490,7 @@
       public Enumeration propfindMethod(String path, Vector properties)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(this.httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(this.httpUrl);
   
           // Check the path alright.
           path = HttpURL.getPath(path);
  @@ -1598,7 +1590,7 @@
       public boolean proppatchMethod(String path, Hashtable property)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           PropPatchMethod method = new PropPatchMethod(HttpURL.getPath(path));
           Enumeration names = property.keys();
  @@ -1655,7 +1647,7 @@
       public boolean headMethod(String path)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           HeadMethod method = new HeadMethod(HttpURL.getPath(path));
           client.executeMethod(method);
  @@ -1692,7 +1684,7 @@
       public boolean deleteMethod(String path)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           DeleteMethod method = new DeleteMethod(HttpURL.getPath(path));
           client.executeMethod(method);
  @@ -1736,7 +1728,7 @@
       public boolean moveMethod(String source, String destination)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           MoveMethod method = new MoveMethod(source, destination);
           client.executeMethod(method);
  @@ -1783,7 +1775,7 @@
       public boolean copyMethod(String source, String destination)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           CopyMethod method = new CopyMethod(source, destination);
           client.executeMethod(method);
  @@ -1828,7 +1820,7 @@
       public boolean mkcolMethod(String path)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           MkcolMethod method = new MkcolMethod(HttpURL.getPath(path));
           client.executeMethod(method);
  @@ -1922,7 +1914,7 @@
       public boolean lockMethod(String path, String owner, short timeout)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           // default lock type setting
           short lockType = LockMethod.SCOPE_EXCLUSIVE;
  @@ -1942,7 +1934,7 @@
           setStatusCode(statusCode, lock);
           if (statusCode >= 200 && statusCode < 300) {
               client.setState(state);
  -            clients.setSession(httpUrl, client);
  +            WebdavSession.setSession(httpUrl, client);
               return true;
           }
   
  @@ -1975,7 +1967,7 @@
       public boolean unlockMethod(String path)
           throws WebdavException, IOException {
   
  -        WebdavClient client = clients.getSessionInstance(httpUrl);
  +        WebdavClient client = WebdavSession.getSessionInstance(httpUrl);
   
           // Get the lock for the given path.
           State state = client.getState();
  @@ -2000,7 +1992,7 @@
           if (statusCode >= 200 && statusCode < 300) {
               state.removeLocks(path);
               client.setState(state);
  -            clients.setSession(httpUrl, client);
  +            WebdavSession.setSession(httpUrl, client);
               return true;
           }
   
  
  
  
  1.1                  jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavSession.java
  
  Index: WebdavSession.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/util/WebdavSession.java,v 1.1 2001/03/08 08:19:04 jericho Exp $
   * $Revision: 1.1 $
   * $Date: 2001/03/08 08:19:04 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.webdav.util;
  
  import java.io.IOException;
  import java.util.Vector;
  import java.util.Hashtable;
  import java.util.Enumeration;
  import java.net.MalformedURLException;
  import org.apache.webdav.lib.WebdavClient;
  import org.apache.webdav.lib.Credentials;
  
  /**
   *
   * This WebdavSession class is for the multi-session management of
   * WebDAV clients.   This class saves and restores the requested client.
   *
   * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
   */
  public class WebdavSession {
  
  
      /**
       * Infomation hashtable for WebDAV clients for HTTP/1.1 communication.
       */
      private static Hashtable clientInfo = new Hashtable();
  
  
      // ------------------------------------------------------  Public methods
  
  
      /**
       * Get a <code>WebdavClient</code> instance.
       * If the same authority is requested, the method returns the same client.
       * Otherwise, the method returns a new client instance.
       *
       * @param httpUrl The HTTP URL to connect.  only used the authority part.
       * @return An instance of <code>WebdavClient</code>.
       * @exception IOException
       */
      public static WebdavClient getSessionInstance(HttpURL httpUrl)
          throws IOException {
  
          String authority = httpUrl.getAuthority();
          WebdavClient client = (WebdavClient) clientInfo.get(authority);
          // setting for the first time.
          if (client == null) {
              client = new WebdavClient(httpUrl.toURL());
  
              String userName = httpUrl.getUserName();
              if (userName != null && userName.length() > 0) {
                  String password = httpUrl.getPassword();
                  client.setCredentials(new Credentials(userName, password));
              }
          }
  
          return client;
      }
  
  
      /**
       * Get a <code>WebdavClient</code> instance with authority string.
       * If the same authority is requested, the method returns the same client.
       * Otherwise, the method returns a new client instance.
       *
       * @param authority the authority string
       * @return An instance of <code>WebdavClient</code>.
       * @exception IOException
       */
      public static WebdavClient getSessionInstance(String authority)
          throws IOException {
  
          HttpURL httpUrl = new HttpURL(authority);
  
          return getSessionInstance(httpUrl);
      }
  
  
      /**
       * Seek a <code>WebdavClient</code> instance.
       * If the return value is null, there is no instance.
       *
       * @param httpUrl The http URL to connect. only used the authority part.
       * @return An instance of <code>WebdavClient</code>.
       * @exception MalformedURLException
       */
      public static WebdavClient seekSession(HttpURL httpUrl)
          throws MalformedURLException {
  
          String authority = httpUrl.getAuthority();
          WebdavClient client = (WebdavClient) clientInfo.get(authority);
  
          return client;
      }
  
  
      /**
       * Seek a <code>WebdavClient</code> instance with the authority string.
       * If the return value is null, there is no instance.
       *
       * @param authority The authority string.
       * @return An instance of <code>WebdavClient</code>.
       */
      public static WebdavClient seekSession(String authority) {
  
          WebdavClient client = (WebdavClient) clientInfo.get(authority);
  
          return client;
      }
  
  
      /**
       * Seek the keys of session having the part of the authority string.
       * The <code>partOfAuthority</code> can be user or host part.
       *
       * @param partOfAuthority The part of the authority string.
       * @return Authorities to have the given string.
       */
      public static Enumeration seekSessionKeys(String partOfAuthority) {
  
          Vector keys = new Vector();
  
          Enumeration authorities = clientInfo.keys();
          while (authorities.hasMoreElements()) {
              String authority = (String) authorities.nextElement();
              if (authority.indexOf(partOfAuthority) >= 0) {
                  keys.addElement(authority);
              }
          }
  
          return keys.elements();
      }
  
  
      /**
       * Seek the keys of all session.
       *
       * @return Authorities of all session.
       */
      public static Enumeration seekSessionKeys() {
  
          return clientInfo.keys();
      }
  
  
      /**
       * Save the status of the <code>WebdavClient</code>.
       *
       * <p>In order to save information of the WebDAV client,
       * It is used the authority part of http URL that is useful to save
       * each connection having the user, host and port information.
       *
       * @param httpUrl The http URL to save information of the WebDAV client.
       * @param client The <code>WebdavClient</code> instance to save.
       * @exception MalformedURLException Required the given arguments matched.
       */
      public static void setSession(HttpURL httpUrl, WebdavClient client)
          throws MalformedURLException {
  
          // Check that the given http URL is going alright.
          if (!httpUrl.getHost().equalsIgnoreCase(client.getHost()) ||
              httpUrl.getPort() != client.getPort())
              throw new MalformedURLException
                  ("Not matched between the given URL and client");
  
          clientInfo.put(httpUrl.getAuthority(), client);
      }
  
  
      /**
       * Save the status of the <code>WebdavClient</code>
       *
       * <p>In order to save information of the WebDAV client,
       * It is used the authority part of http URL that is useful to save
       * each connection having the user, host and port information.
       *
       * @param authority the authority string
       * @param client The <code>WebdavClient</code> instance to save.
       * @exception MalformedURLException Required the given arguments matched.
       */
      public static void setSession(String authority, WebdavClient client)
          throws MalformedURLException {
  
          HttpURL httpUrl = new HttpURL(authority);
  
          setSession(httpUrl, client);
      }
  
  
      /**
       * Close connection and unset the <code>WebdavClient</code> instance.
       *
       * @param httpUrl The http URL to remove a WebDAV client.
       * @exception IOException Error in closing socket.
       */
      public static void unsetSession(HttpURL httpUrl)
          throws IOException {
  
          String authority = httpUrl.getAuthority();
  
          unsetSession(authority);
      }
  
  
      /**
       * Close connection and unset the <code>WebdavClient</code> instance.
       *
       * @param authority The authority string.
       * @exception IOException Error in closing socket.
       */
      public static void unsetSession(String authority)
          throws IOException {
  
          WebdavClient webdavClient = seekSession(authority);
          // in case of aleady not unset.
          if (webdavClient != null) {
              webdavClient.endSession();
              clientInfo.remove(authority);
          }
      }
  
  
      /**
       * Close all connection and unset <code>WebdavClient</code> instances.
       *
       * @exception IOException Error in closing socket.
       */
      public static void unsetSessionAll() throws IOException {
  
          Enumeration authorities = clientInfo.keys();
          while (authorities.hasMoreElements()) {
              String authority = (String) authorities.nextElement();
              WebdavClient webdavClient = seekSession(authority);
              if (webdavClient != null) {
                  webdavClient.endSession();
                  clientInfo.remove(authority);
              }
          }
      }
  }