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 re...@apache.org on 2001/04/28 04:24:00 UTC

cvs commit: jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods SearchMethod.java

remm        01/04/27 19:23:59

  Added:       src/webdav/client/src/org/apache/webdav/lib/methods
                        SearchMethod.java
  Log:
  - Add a simple implementation of DASL's SEARCH method, contributed by
    Shawn C. Dodd (sdodd at agea.com).
  
  Revision  Changes    Path
  1.1                  jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/SearchMethod.java
  
  Index: SearchMethod.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/SearchMethod.java,v 1.1 2001/04/28 02:23:59 remm Exp $
   * $Revision: 1.1 $
   * $Date: 2001/04/28 02:23:59 $
   *
   * ====================================================================
   *
   * 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.lib.methods;
  
  import java.io.InputStream;
  import java.io.IOException;
  
  import java.util.Enumeration;
  import java.util.Hashtable;
  import java.util.Vector;
  
  import org.apache.util.WebdavStatus;
  import org.apache.util.XMLPrinter;
  
  import org.apache.webdav.lib.Header;
  import org.apache.webdav.lib.Property;
  import org.apache.webdav.lib.State;
  import org.apache.webdav.lib.WebdavException;
  
  import org.apache.webdav.lib.properties.GetLastModifiedProperty;
  import org.apache.webdav.lib.properties.ResourceTypeProperty;
  
  /**
   * This class implements the WebDAV SEARCH Method.
   *
   * <P>     The SEARCH method initiates a server-side search. The body of the
   * request defines the query. The server responds with a text/xml entity
   * matching the WebDAV PROPFIND response.
   *
   * <P>     According to 
   * <ahref="http://www.webdav.org/dasl/protocol/draft-dasl-protocol-00.html">
   * the DASL draft</a> a typical request looks like this:
   *
   * <PRE>
   * SEARCH /folder/ HTTP/1.1
   * Host: www.foo.bar
   * Content-type: text/xml; charset="utf-8"
   * Content-Length: xxxx
   *
   * <?xml version="1.0"?>
   * <D:searchrequest xmlns:D = "DAV:" >
   * <D:basicsearch>
   *   <D:select>
   *     <D:prop><D:getcontentlength/></D:prop>
   *   </D:select>
   *   <D:from>
   *     <D:scope>
   *       <D:href>/folder/</D:href>
   *       <D:depth>infinity</D:depth>
   *     </D:scope>
   *   </D:from>
   * </D:basicsearch>
   * </D:searchrequest>
   * </PRE>
   *
   * <P>     However, other query grammars may be used. A typical request using
   * the 
   * <ahref="http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/exchsv2k/_exch2k_sql_web_storage_system_sql.htm">
   * SQL-based grammar</a> implemented in Microsoft's Web Storage System
   * (currently shipping with Exchange 2000 and SharePoint Portal Server)
   * looks like this:
   *
   * <PRE>
   * SEARCH /folder/ HTTP/1.1
   * Host: www.foo.bar
   * Content-type: text/xml; charset="utf-8"
   * Content-Length: xxxx
   *
   * <?xml version="1.0"?>
   * <D:searchrequest xmlns:D = "DAV:" >
   *   <D:sql>
   *   SELECT "DAV:contentclass", "DAV:displayname"
   *     FROM "/folder/"
   *    WHERE "DAV:ishidden" = false
   *      AND "DAV:isfolder" = false
   *   </D:sql>
   * </D:searchrequest>
   * </PRE>
   *
   * @author <a href="mailto:sdodd@agea.com">Shawn C. Dodd</a>
   */
  
  public class SearchMethod extends XMLResponseMethodBase
      implements DepthSupport {
  
  
      // -------------------------------------------------------------- Constants
  
  
      /**
       * Request of named properties.
       */
      public static final int BY_NAME = 0;
  
  
      /**
       * Request of all properties name and value.
       */
      public static final int ALL = 1;
  
  
      /**
       * Request of all properties name.
       */
      public static final int NAMES = 2;
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Method constructor.
       */
      public SearchMethod() {
          name = "SEARCH";
      }
  
  
      /**
       * Method constructor.
       */
      public SearchMethod(String path) {
          super(path);
          name = "SEARCH";
      }
  
  
      /**
       * Method constructor.
       */
      public SearchMethod(String path, int depth) {
          this(path);
          setDepth(depth);
      }
  
  
      /**
       * Method constructor.
       */
      public SearchMethod(String path, int depth, int type) {
          this(path);
          setDepth(depth);
          setType(type);
      }
  
  
      /**
       * Construct a SearchMethod using the given XML request body.
       *
       * @param path  Relative path to the WebDAV resource 
       * (presumably a collection).
       * @param query Complete request body in XML including a search query in
       * your favorite grammar.
       */
      public SearchMethod(String path, String query) {
          this(path);
          setDepth(1);
          setQuery(query);
          setType(BY_NAME);
      }
  
  
      /**
       * Construct a SearchMethod using the given XML request body.
       *
       * @param path  relative path to the WebDAV resource 
       * (presumably a collection)
       * @param depth query scope
       * @param query complete request body in XML including a search query in
       * your favorite grammar
       */
      public SearchMethod(String path, int depth, String query) {
          this(path);
          setDepth(depth);
          setQuery(query);
          setType(BY_NAME);
      }
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * Type of the Propfind.
       */
      protected int type = ALL;
  
  
      /**
       * Depth.
       */
      protected int depth = DEPTH_INFINITY;
  
  
      /**
       * The namespace abbreviation that prefixes DAV tags
       */
      protected String prefix = null;
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * Set header. handle the special case of Depth.
       *
       * @param headerName Header name
       * @param headerValue Header value
       */
      public void setHeader(String headerName, String headerValue) {
          if (headerName.equalsIgnoreCase("Depth")){
              int depth = -1;
              if (headerValue.equals("0")){
                  depth = DEPTH_0;
              }
              if (headerValue.equals("1")){
                  depth = DEPTH_1;
              }
              else if (headerValue.equalsIgnoreCase("infinity")){
                  depth = DEPTH_INFINITY;
              }
              setDepth(depth);
          }
          else{
              super.setHeader(headerName, headerValue);
          }
      }
  
  
      /**
       * Type setter.
       *
       * @param type New type value
       */
      public void setType(int type) {
          checkNotUsed();
          this.type = type;
      }
  
  
      /**
       * Type getter.
       *
       * @return int type value
       */
      public int getType() {
          return type;
      }
  
  
      /**
       * Depth setter.
       *
       * @param depth New depth value
       */
      public void setDepth(int depth) {
          checkNotUsed();
          this.depth = depth;
      }
  
  
      /**
       * Depth getter.
       *
       * @return int depth value
       */
      public int getDepth() {
          return depth;
      }
  
  
      // --------------------------------------------------- WebdavMethod Methods
  
  
      public void recycle() {
          super.recycle();
          prefix = null;
      }
  
      /**
       * Generate additional headers needed by the request.
       *
       * @param host the host
       * @param state State token
       */
      public void generateHeaders(String host, State state) {
  
          super.generateHeaders(host, state);
  
          super.setHeader("Content-Type", "text/xml; charset=utf-8");
  
          switch (depth) {
          case DEPTH_0:
              super.setHeader("Depth", "0");
              break;
          case DEPTH_1:
              super.setHeader("Depth", "1");
              break;
          case DEPTH_INFINITY:
              super.setHeader("Depth", "infinity");
              break;
          }
  
      }
  
      /**
       * Return the query supplied in the constructor or setQuery(). In future,
       * this will construct a query in the grammar you're interested in.
       *
       * @return String query
       */
      public String generateQuery() {
  
          if (query == null || query.trim().length() < 1) {
              // TODO  Must support some mechanism for delegating the
              // generation of the query to a pluggable query grammar
              // support class or package. Right now, executing this
              // method object without first explicitly setting the
              // query is an error.
              return "";
          } else {
              return query;
          }
  
      }
  
  
      /**
       * This method returns an enumeration of URL paths.  If the PropFindMethod
       * was sent to the URL of a collection, then there will be multiple URLs.
       * The URLs are picked out of the <code>&lt;D:href&gt;</code> elements
       * of the response.
       *
       * @return an enumeration of URL paths as Strings
       */
      public Enumeration getAllResponseURLs() {
          checkUsed();
          return getResponseHashtable().keys();
      }
  
  
      /**
       * Returns an enumeration of <code>Property</code> objects.
       */
      public Enumeration getResponseProperties(String urlPath) {
          checkUsed();
  
          Response response = (Response) getResponseHashtable().get(urlPath);
          if (response != null) {
              return response.getProperties();
          } else {
              return (new Vector()).elements();
          }
  
      }
  
  
  }