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 bc...@locus.apache.org on 2000/11/21 08:20:50 UTC

cvs commit: jakarta-slide/src/clients/webdav/src/org/apache/webdav/ant/taskdefs WebdavMatchingTask.java Get.java

bcholmes    00/11/20 23:20:50

  Added:       src/clients/webdav/src/org/apache/webdav/ant/taskdefs
                        WebdavMatchingTask.java Get.java
  Log:
  Revisions to the client library for:
  - removing unnecessary import statements in methods
  - parse the response from PROPFIND
  - create an Ant Dav-aware GET task
  
  Revision  Changes    Path
  1.1                  jakarta-slide/src/clients/webdav/src/org/apache/webdav/ant/taskdefs/WebdavMatchingTask.java
  
  Index: WebdavMatchingTask.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-slide/src/clients/webdav/src/org/apache/webdav/ant/taskdefs/WebdavMatchingTask.java,v 1.1 2000/11/21 07:20:50 bcholmes Exp $
   * $Revision: 1.1 $
   * $Date: 2000/11/21 07:20:50 $
   *
   * ====================================================================
   *
   * 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/>.
   */
  
  package org.apache.webdav.ant.taskdefs;
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileReader;
  import java.io.IOException;
  
  import java.net.URL;
  
  import java.util.Enumeration;
  import java.util.StringTokenizer;
  import java.util.Vector;
  
  import org.apache.tools.ant.Project;
  import org.apache.tools.ant.Task;
  
  import org.apache.webdav.ant.CollectionScanner;
  import org.apache.webdav.ant.ScanException;
  import org.apache.webdav.ant.Scanner;
  
  import org.apache.webdav.lib.WebdavClient;
  import org.apache.webdav.lib.WebdavException;
  
  public abstract class WebdavMatchingTask extends Task { // ... extends MatchingTask {
  
      // TODO: update for PatternSets, et al
  
      protected WebdavClient client = new WebdavClient();
  
      protected Vector includeList = new Vector();
      protected Vector excludeList = new Vector();
      protected boolean useDefaultExcludes = true;
  
      /**
       * Returns the collection scanner needed to access the resources to get.
       */
      protected Scanner getScanner(URL baseURL) throws ScanException {
          CollectionScanner scanner = new CollectionScanner();
          scanner.setBaseURL(baseURL);
          scanner.setIncludes(makeArray(includeList));
          scanner.setExcludes(makeArray(excludeList));
          scanner.setWebdavClient(client);
          if (useDefaultExcludes) {
              scanner.addDefaultExcludes();
          }
          scanner.scan();
          return scanner;
      }
  
      /**
       * provide access to properties from within the inner class
       */
      protected String getProperty(String name) { 
          return project.getProperty(name);
      }
  
      /**
       * inner class to hold a name on list.  "If" and "Unless" attributes
       * may be used to invalidate the entry based on the existence of a 
       * property (typically set thru the use of the Available task).
       */
      public class NameEntry {
          private boolean valid = true;
          private String name;
  
          public String getName() { return valid ? name : null; }
          public void setName(String name) { this.name = name; }
  
          public void setIf(String name) {
              if (getProperty(name) == null) valid = false;
          }
  
          public void setUnless(String name) {
              if (getProperty(name) != null) valid = false;
          }
      }
  
      /**
       * add a name entry on the include list
       */
      public NameEntry createInclude() {
          return addPatternToList(includeList);
      }
      
      /**
       * add a name entry on the exclude list
       */
      public NameEntry createExclude() {
          return addPatternToList(excludeList);
      }
  
      /**
       * Sets the set of include patterns. Patterns may be separated by a comma
       * or a space.
       *
       * @param includes the string containing the include patterns
       */
      public void setIncludes(String includes) {
          if (includes != null && includes.length() > 0) {
              createInclude().setName(includes);
          }
      }
  
      /**
       * Set this to be the items in the base directory that you want to be
       * included. You can also specify "*" for the items (ie: items="*") 
       * and it will include all the items in the base directory.
       *
       * @param itemString the string containing the files to include.
       */
      public void setItems(String itemString) {
          log("The items attribute is deprecated. " +
              "Please use the includes attribute.",
              Project.MSG_WARN);
          if (itemString == null || itemString.equals("*") 
  				               || itemString.equals(".")) {
              createInclude().setName("**");
          } else {
              StringTokenizer tok = new StringTokenizer(itemString, ", ");
              while (tok.hasMoreTokens()) {
                  String pattern = tok.nextToken().trim();
                  if (pattern.length() > 0) {
                      createInclude().setName(pattern+"/**");
                  }
              }
          }
      }
      
      /**
       * Sets the set of exclude patterns. Patterns may be separated by a comma
       * or a space.
       *
       * @param excludes the string containing the exclude patterns
       */
      public void setExcludes(String excludes) {
          if (excludes != null && excludes.length() > 0) {
              createExclude().setName(excludes);
          }
      }
  
      /**
       * List of filenames and directory names to not include. They should be 
       * either , or " " (space) separated. The ignored files will be logged.
       *
       * @param ignoreString the string containing the files to ignore.
       */
      public void setIgnore(String ignoreString) {
          log("The ignore attribute is deprecated." + 
              "Please use the excludes attribute.",
              Project.MSG_WARN);
          if (ignoreString != null && ignoreString.length() > 0) {
              Vector tmpExcludes = new Vector();
              StringTokenizer tok = new StringTokenizer(ignoreString, ", ", false);
              while (tok.hasMoreTokens()) {
                  createExclude().setName("**/"+tok.nextToken().trim()+"/**");
              }
          }
      }
      
      /**
       * Sets whether default exclusions should be used or not.
       *
       * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions 
       *                           should be used, "false"|"off"|"no" when they
       *                           shouldn't be used.
       */
      public void setDefaultexcludes(String useDefaultExcludes) {
          this.useDefaultExcludes = Project.toBoolean(useDefaultExcludes);
      }
      
      /**
       * Convert a vector of NameEntry elements into an array of Strings.
       */
      private String[] makeArray(Vector list) {
          if (list.size() == 0) return null;
  
          Vector tmpNames = new Vector();
          for (Enumeration e = list.elements() ; e.hasMoreElements() ;) {
              String includes = ((NameEntry)e.nextElement()).getName();
              if (includes == null) continue;
              StringTokenizer tok = new StringTokenizer(includes, ", ", false);
              while (tok.hasMoreTokens()) {
                  String pattern = tok.nextToken().trim();
                  if (pattern.length() > 0) {
                      tmpNames.addElement(pattern);
                  }
              }
          }
  
          String result[] = new String[tmpNames.size()];
          for (int i = 0; i < tmpNames.size(); i++) {
              result[i] = (String)tmpNames.elementAt(i);
          }
  
          return result;
      }
          
      /**
       * add a name entry to the given list
       */
      private NameEntry addPatternToList(Vector list) {
          NameEntry result = new NameEntry();
          list.addElement(result);
          return result;
      }
  
      /**
       *  Reads path matching patterns from a file and adds them to the
       *  includes or excludes list (as appropriate).  
       */
      private void readPatterns(File patternfile, Vector patternlist) {
  
          try {
              // Get a FileReader
              BufferedReader patternReader = 
                  new BufferedReader(new FileReader(patternfile)); 
          
              // Create one NameEntry in the appropriate pattern list for each 
              // line in the file.
              String line = patternReader.readLine();
              while (line != null) {
                  if (line.length() > 0) {
                      addPatternToList(patternlist).setName(line);
                  }
                  line = patternReader.readLine();
              }
          } catch(IOException ioe)  {
              log("An error occured while reading from pattern file: " 
                  + patternfile, Project.MSG_ERR); 
          }
      }
  
      /**
       * Sets the name of the file containing the includes patterns.
       *
       * @param includesfile A string containing the filename to fetch
       * the include patterns from.  
       */
       public void setIncludesfile(String includesfile) {
           if (includesfile != null && includesfile.length() > 0) {
               File incl = project.resolveFile(includesfile);
               if (!incl.exists()) {
                   log("Includesfile "+includesfile+" not found.", 
                       Project.MSG_ERR); 
               } else {
                   readPatterns(incl, includeList);
               }
           }
       }
  
      /**
       * Sets the name of the file containing the includes patterns.
       *
       * @param excludesfile A string containing the filename to fetch
       * the include patterns from.  
       */
       public void setExcludesfile(String excludesfile) {
           if (excludesfile != null && excludesfile.length() > 0) {
               File excl = project.resolveFile(excludesfile);
               if (!excl.exists()) {
                   log("Excludesfile "+excludesfile+" not found.", 
                       Project.MSG_ERR); 
               } else {
                   readPatterns(excl, excludeList);
               }
           }
       }
  }
  
  
  
  1.1                  jakarta-slide/src/clients/webdav/src/org/apache/webdav/ant/taskdefs/Get.java
  
  Index: Get.java
  ===================================================================
  /*
   * 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/>.
   */
  
  package org.apache.webdav.ant.taskdefs;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.InputStream;
  import java.io.IOException;
  
  import java.net.URL;
  import java.net.MalformedURLException;
  
  import org.apache.tools.ant.BuildException;
  
  import org.apache.webdav.ant.Scanner;
  
  import org.apache.webdav.ant.taskdefs.WebdavMatchingTask;
  
  import org.apache.webdav.lib.Credentials;
  import org.apache.webdav.lib.WebdavClient;
  import org.apache.webdav.lib.WebdavException;
  import org.apache.webdav.lib.methods.GetMethod;
  
  /**
   * Get a particular resource or collection of resources from a 
   * WebDAV-compliant web server.  This task differs from the 
   * basic Get task in Ant because it allows regular expressions 
   * to be used.
   *
   * <p>  To use this task, first you must it in your Ant build file:
   *
   * <PRE>
   * &lt;taskdef name="davget" classname="org.apache.webdav.ant.taskdefs.Get"/&gt;
   * </PRE>
   *
   * <p>  Next, specify a specific target, such as:
   *
   * <PRE>
   *
   * &lt;target name="download" depends="init"&gt;
   *   &lt;davget baseurl="http://localhost/dav/" dest="." verbose="true"&gt;
   *     &lt;include name="*.html"/&gt;
   *   &lt;/davget&gt;
   * &lt;/target&gt;
   * <PRE>
   *
   * @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
   */
  public class Get extends WebdavMatchingTask {
      private String baseURL = null; // required
      private String dest = null; // required
      private String userid = null;
      private String password = null;
      private boolean verbose = false;
      String ignoreErrors=null;
  
      /**
       * Does the work.
       *
       * @exception BuildException Thrown in unrecovrable error.
       */
      public void execute() throws BuildException {
          try {
  
              // first, is the destination directory valid?
              File destDir = new File(dest);
              if (!destDir.isDirectory()) {
                  throw new BuildException(
                      "Destination " + dest + " is not a valid directory");
              }
  
              // next, check the include list
              if (includeList.size() == 0) {
                  throw new BuildException(
                      "Includes must be specified");
              }
  
              if ((this.userid != null && !this.userid.equals("")) &&
                  (this.password != null && !this.password.equals(""))) {
                  client.setCredentials(new Credentials(this.userid, this.password));
              }
  
              // validity check on the URL
              URL url = new URL(baseURL);
  
              client.startSession(url.getHost(), 
                                  (url.getPort() < 0 ? 80 : url.getPort()));
  
              Scanner scanner = getScanner(url);
              GetMethod getMethod = new GetMethod();
  
              String[] resources = scanner.getIncludedFiles();
              log("Downloading " + resources.length + " files from " + url);
              for (int i = 0; i < resources.length; i++) {
                  String resource = resources[i];
  
                  String file = resource.replace('/',File.separatorChar);
  
                  file = concatenate(dest, file, File.separator);                    
                  FileOutputStream output = new FileOutputStream(file);
  
                  resource = concatenate(url.getFile(), resource, "/");
  
                  if (verbose) {
                      log("Getting: " + resource + " to " + file);
                  }
  
                  getMethod.setPath(resource);
  
                  client.executeMethod(getMethod);
  
                  InputStream input = getMethod.getData();
                  if( input == null ) {
                      throw new BuildException( "Can't get " + resource + " to " + file);
                  }
  		
                  byte[] buffer = new byte[100 * 1024];
                  int length;
  
                  while ((length = input.read(buffer)) >= 0) {
                      output.write(buffer, 0, length);
                  }
  
                  output.close();
                  input.close();
                  getMethod.recycle();
              }
              client.endSession();
  
          } catch (BuildException e) {
              log(e.getMessage());
              if( ignoreErrors == null ) {
                  throw e;
              }
  
          } catch (WebdavException e) {
              throw new BuildException(e.toString());
  
          } catch (MalformedURLException e) {
              throw new BuildException(e.toString());
  
          } catch (IOException e) {
              if( ignoreErrors == null )
                  throw new BuildException(e.toString());
          }
      }
  
      /**
       * Set the base URL.
       *
       * @param base URL for the request.
       */
      public void setBaseurl(String baseURL) {
          if (baseURL.endsWith("/")) {
              this.baseURL = baseURL;
          } else {
              this.baseURL = baseURL + "/";
          }
      }
  
      /**
       * Where to copy the source file.
       *
       * @param dest Path to file.
       */
      public void setDest(String dest) {
  	this.dest = dest.replace('/',File.separatorChar)
                        .replace('\\',File.separatorChar);
      }
  
      /**
       * Userid to access the resources
       *
       * @param userid - HTTP userid
       */
      public void setUserid(String userid) {
          this.userid = userid;
      }
  
      /**
       * Password to access the resources
       *
       * @param password - HTTP password
       */
      public void setPassword(String password) {
          this.password = password;
      }
  
     /**
       * Be verbose, if set to "<CODE>true</CODE>".
       *
       * @param verbose if "true" then be verbose
       */
      public void setVerbose(boolean verbose) {
          this.verbose = verbose;
      }
  
      /**
       * Don't stop if get fails if set to "<CODE>true</CODE>".
       *
       * @param ignoreErrors if "true" then be verbose
       */
      public void setIgnoreErrors(String ignoreErrors) {
  	this.ignoreErrors = ignoreErrors;
      }
  
      protected String concatenate(String first, String second, String separator) {
          if (first.endsWith(separator) && !second.startsWith(separator)) {
              return first + second;
          } else if (!first.endsWith(separator) && second.startsWith(separator)) {
              return first + second;
          } else if (!first.endsWith(separator) && !second.startsWith(separator)) {
              return first + separator + second;
          } else {
              return first + second.substring(1);
          }
      }
  }
  
  
  

Re: cvs commit: jakarta-slide/src/clients/webdav/src/org/apache/webdav/ant/taskdefs WebdavMatchingTask.java Get.java

Posted by "B.C. Holmes" <bc...@roxton.com>.
Remy Maucherat wrote:
> 
> >   - create an Ant Dav-aware GET task
> 
> Hey, does this allow to build *remote* code ????? If yes, that's amazing :))

     <nod>  The idea is that it should work like a CVS checkout, but
with the pattern-matching capable in other Ant tasks.  It gets back to
that whole "abstract the repository" idea.

     When I have a moment, I'm gonna create some other Ant/DAV tasks
(PUT being the next obvious one).

BCing you
-- 
B.C. Holmes         \u2625           http://www.roxton.com/~bcholmes/
"Wherefore now, baby, time will tell/I chose my roads and I chose 
 them well/But will I go forth, or just go to hell/For I am a 
 terrible man."
         - Jory Nash, _Terrible Man_

Re: cvs commit: jakarta-slide/src/clients/webdav/src/org/apache/webdav/ant/taskdefs WebdavMatchingTask.java Get.java

Posted by Remy Maucherat <re...@apache.org>.
>   - create an Ant Dav-aware GET task

Hey, does this allow to build *remote* code ????? If yes, that's amazing :))

Remy