cmlenz      2002/06/14 05:57:15

  Modified:    src/webdav/server/org/apache/slide/webdav WebdavServlet.java
  Added:       src/webdav/server/org/apache/slide/webdav
                        WebdavMethodFactory.java
               src/webdav/server/org/apache/slide/webdav/method
                        DefaultMethodFactory.java
  Log:
  Second round of WebdavServlet refactoring:
  - Added abstract class WebdavMethodFactory, to encapsulate the creation
    of concrete WebdavMethod implementations
  - Added a default implementation which mimicks the current behaviour
  - Changed WebdavServlet to use the factory instead of its own porotected
    method
  
  Revision  Changes    Path
  1.46      +15 -83    
jakarta-slide/src/webdav/server/org/apache/slide/webdav/WebdavServlet.java
  
  Index: WebdavServlet.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/WebdavServlet.java,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- WebdavServlet.java        14 Jun 2002 12:24:05 -0000      1.45
  +++ WebdavServlet.java        14 Jun 2002 12:57:14 -0000      1.46
  @@ -158,84 +158,11 @@
       protected boolean handleLifecycle = true;
       
       
  -    // ------------------------------------------------------ Protected Methods
  -    
  -    
       /**
  -     * Create Slide Method form the HTTP Method.
  -     *
  -     * @return WebdavMethod
  -     * @exception WebdavException
  +     * Factory for creating the WebdavMethod implementations. Initialized in
  +     * {@link #init init()}.
        */
  -    protected WebdavMethod createWebdavMethod(String name)
  -        throws WebdavException {
  -        
  -        WebdavServletConfig config = (WebdavServletConfig)getServletConfig();
  -        
  -        WebdavMethod method = null;
  -        if (name.equalsIgnoreCase("GET")) {
  -            method = new GetMethod(token, config);
  -        } else if (name.equalsIgnoreCase("PROPFIND")) {
  -            method = new PropFindMethod(token, config);
  -        } else if (name.equalsIgnoreCase("HEAD")) {
  -            method = new HeadMethod(token, config);
  -        } else if (name.equalsIgnoreCase("LOCK")) {
  -            method = new LockMethod(token, config);
  -        } else if (name.equalsIgnoreCase("UNLOCK")) {
  -            method = new UnlockMethod(token, config);
  -        } else if (name.equalsIgnoreCase("OPTIONS")) {
  -            method = new OptionsMethod(token, config);
  -        } else if (name.equalsIgnoreCase("PUT")) {
  -            method = new PutMethod(token, config);
  -        } else if (name.equalsIgnoreCase("MKCOL")) {
  -            method = new MkcolMethod(token, config);
  -        } else if (name.equalsIgnoreCase("POST")) {
  -            method = new PostMethod(token, config);
  -        } else if (name.equalsIgnoreCase("COPY")) {
  -            method = new CopyMethod(token, config);
  -        } else if (name.equalsIgnoreCase("MOVE")) {
  -            method = new MoveMethod(token, config);
  -        } else if (name.equalsIgnoreCase("DELETE")) {
  -            method = new DeleteMethod(token, config);
  -        } else if (name.equalsIgnoreCase("PROPPATCH")) {
  -            method = new PropPatchMethod(token, config);
  -        } else if (name.equalsIgnoreCase("SEARCH")) {
  -            method = new SearchMethod(token, config);
  -        } else if (name.equalsIgnoreCase("ACL")) {
  -            if( org.apache.slide.util.Configuration.useIntegratedSecurity() )
  -                method = new AclMethod(token, config);
  -        } else if (name.equalsIgnoreCase("VERSION-CONTROL")) {
  -            if( org.apache.slide.util.Configuration.useVersionControl() )
  -                method = new VersionControlMethod(token, config);
  -        } else if (name.equalsIgnoreCase("REPORT")) {
  -            method = new ReportMethod(token, config);
  -        } else if (name.equalsIgnoreCase("CHECKIN")) {
  -            if( org.apache.slide.util.Configuration.useVersionControl() )
  -                method = new CheckinMethod(token, config);
  -        } else if (name.equalsIgnoreCase("CHECKOUT")) {
  -            if( org.apache.slide.util.Configuration.useVersionControl() )
  -                method = new CheckoutMethod(token, config);
  -        } else if (name.equalsIgnoreCase("UNCHECKOUT")) {
  -            if( org.apache.slide.util.Configuration.useVersionControl() )
  -                method = new UncheckoutMethod(token, config);
  -        } else if (name.equalsIgnoreCase("MKWORKSPACE")) {
  -            if( org.apache.slide.util.Configuration.useVersionControl() )
  -                method = new MkworkspaceMethod(token, config);
  -        } else if (name.equalsIgnoreCase("LABEL")) {
  -            if( org.apache.slide.util.Configuration.useVersionControl() )
  -                method = new LabelMethod(token, config);
  -        } else if (name.equalsIgnoreCase("UPDATE")) {
  -            if( org.apache.slide.util.Configuration.useVersionControl() )
  -                method = new UpdateMethod(token, config);
  -        }
  -        
  -        if (method == null) {
  -            throw new WebdavException(WebdavStatus.SC_BAD_REQUEST);
  -        }
  -        
  -        return method;
  -        
  -    }
  +    protected WebdavMethodFactory methodFactory;
       
       
       // -------------------------------------------------------- Servlet Methods
  @@ -292,7 +219,8 @@
                   // index pag or something similar)
                   super.service(req, resp);
               } else {
  -                WebdavMethod method = createWebdavMethod(req.getMethod());
  +                WebdavMethod method =
  +                    methodFactory.createMethod(req.getMethod());
                   method.run(req, resp);
               }
               
  @@ -368,7 +296,6 @@
           super.init(new WebdavServletConfig(config));
           
           // all the actual initialization is inside init()
  -
       }
       
       
  @@ -460,6 +387,7 @@
                   throw new UnavailableException("Namespace " + namespaceName +
                                                  " not accessible");
               }
  +            getServletContext().setAttribute(ATTRIBUTE_NAME, token);
               
               if (directoryBrowsing) {
                   directoryIndexGenerator =
  @@ -474,7 +402,11 @@
               t.printStackTrace();
               throw new ServletException(t.toString());
           }
  -     }
  +        
  +        methodFactory =
  +            WebdavMethodFactory.newInstance
  +                ((WebdavServletConfig)getServletConfig());
  +    }
       
       
       /**
  
  
  
  1.1                  
jakarta-slide/src/webdav/server/org/apache/slide/webdav/WebdavMethodFactory.java
  
  Index: WebdavMethodFactory.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/WebdavMethodFactory.java,v
 1.1 2002/06/14 12:57:14 cmlenz Exp $
   * $Revision: 1.1 $
   * $Date: 2002/06/14 12:57:14 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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", "Slide", 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 [EMAIL PROTECTED]
   *
   * 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.slide.webdav;
  
  import org.apache.slide.common.NamespaceAccessToken;
  import org.apache.slide.webdav.method.DefaultMethodFactory;
  
  /**
   * Factory encapsulating the creation of WebdavMethod implementations.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Christopher Lenz</a>
   */
  public abstract class WebdavMethodFactory {
      
      
      // ----------------------------------------------------- Instance Variables
      
      
      /**
       * Configuration of the WebDAV servlet.
       */
      private WebdavServletConfig config;
      
      
      // --------------------------------------------------------- Static Methods
      
      
      /**
       * Creates a new instance of a WebdavMethodFactory implementation.
       * 
       * @param config    configuration of the WebDAV servlet
       */
      public static WebdavMethodFactory newInstance(WebdavServletConfig config) {
          
          WebdavMethodFactory factory = null;
          String className = config.getInitParameter("method-factory");
          if (className != null) {
              try {
                  Class factoryClass = Class.forName(className);
                  factory = (WebdavMethodFactory)factoryClass.newInstance();
              } catch (Exception e) {
                  // TOO BAD, we'll use the default method factory instead
              }
          }
          
          if (factory == null) {
              factory = new DefaultMethodFactory();
          }
          factory.setConfig(config);
          
          return factory;
      }
      
      
      // ------------------------------------------------ Public Abstract Methods
      
      
      /**
       * Creates a WebdavMethod implementation for the given method name.
       * 
       * @param name      WebDAV/HTTP method name ("GET", "PROPFIND", etc.)
       * @return      the corresponding WebdavMethod implementation, or 
       *              <tt>null</tt> if the method is unknown or unsupported
       */
      public abstract WebdavMethod createMethod(String name);
      
      
      // --------------------------------------------- Protected Abstract Methods
      
      
      /**
       * Returns the configuration of the WebDAV servlet.
       * 
       * @return configuration of the WebDAV servlet
       */
      protected abstract void setConfig(WebdavServletConfig config);
      
      
  }
  
  
  
  
  1.1                  
jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/DefaultMethodFactory.java
  
  Index: DefaultMethodFactory.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/DefaultMethodFactory.java,v
 1.1 2002/06/14 12:57:15 cmlenz Exp $
   * $Revision: 1.1 $
   * $Date: 2002/06/14 12:57:15 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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", "Slide", 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 [EMAIL PROTECTED]
   *
   * 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.slide.webdav.method;
  
  import org.apache.slide.common.NamespaceAccessToken;
  import org.apache.slide.webdav.WebdavMethod;
  import org.apache.slide.webdav.WebdavMethodFactory;
  import org.apache.slide.webdav.WebdavServlet;
  import org.apache.slide.webdav.WebdavServletConfig;
  import org.apache.slide.util.Configuration;
  
  /**
   * The default factory for WebDAVMethod implementations.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Christopher Lenz</a>
   */
  public class DefaultMethodFactory
      extends WebdavMethodFactory {
      
      
      // ----------------------------------------------------- Instance Variables
      
      
      /**
       * Configuration of the WebDAV servlet.
       */
      private WebdavServletConfig config;
      
      
      /**
       * The token for accessing the namespace.
       */
      private NamespaceAccessToken token;
      
      
      // ------------------------------------- WebdavMethodFactory Implementation
      
      
      // inherit javadocs
      public WebdavMethod createMethod(String name) {
          
          if ((config == null) || (token == null)) {
              throw new IllegalStateException();
          }
          
          if (name.equals("GET")) {
              return new GetMethod(token, config);
          } else if (name.equals("PROPFIND")) {
              return new PropFindMethod(token, config);
          } else if (name.equals("HEAD")) {
              return new HeadMethod(token, config);
          } else if (name.equals("LOCK")) {
              return new LockMethod(token, config);
          } else if (name.equals("UNLOCK")) {
              return new UnlockMethod(token, config);
          } else if (name.equals("OPTIONS")) {
              return new OptionsMethod(token, config);
          } else if (name.equals("PUT")) {
              return new PutMethod(token, config);
          } else if (name.equals("MKCOL")) {
              return new MkcolMethod(token, config);
          } else if (name.equals("POST")) {
              return new PostMethod(token, config);
          } else if (name.equals("COPY")) {
              return new CopyMethod(token, config);
          } else if (name.equals("MOVE")) {
              return new MoveMethod(token, config);
          } else if (name.equals("DELETE")) {
              return new DeleteMethod(token, config);
          } else if (name.equals("PROPPATCH")) {
              return new PropPatchMethod(token, config);
          } else if (name.equals("SEARCH")) {
              return new SearchMethod(token, config);
          } else if (name.equals("REPORT")) {
              return new ReportMethod(token, config);
          } else {
              if (Configuration.useIntegratedSecurity()) {
                  if (name.equals("ACL")) {
                      return new AclMethod(token, config);
                  }
              }
              if (Configuration.useVersionControl()) {
                  if (name.equals("VERSION-CONTROL")) {
                      return new VersionControlMethod(token, config);
                  } else if (name.equals("CHECKIN")) {
                      return new CheckinMethod(token, config);
                  } else if (name.equals("CHECKOUT")) {
                      return new CheckoutMethod(token, config);
                  } else if (name.equals("UNCHECKOUT")) {
                      return new UncheckoutMethod(token, config);
                  } else if (name.equals("MKWORKSPACE")) {
                      return new MkworkspaceMethod(token, config);
                  } else if (name.equals("LABEL")) {
                      return new LabelMethod(token, config);
                  } else if (name.equals("UPDATE")) {
                      return new UpdateMethod(token, config);
                  }
              }
          }
          
          return null;
      }
      
      
      // inherit javadocs
      protected void setConfig(WebdavServletConfig config) {
          
          this.config = config;
          token = (NamespaceAccessToken)config.getServletContext().getAttribute
              (WebdavServlet.ATTRIBUTE_NAME);
      }
      
  }
  
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to