cmlenz      2002/06/13 06:10:58

  Modified:    src/webdav/server/org/apache/slide/webdav WebdavServlet.java
  Added:       src/webdav/server/org/apache/slide/webdav/util
                        DirectoryIndexGenerator.java
  Log:
  - Factored directory-index generation into the new utility class
    DirectoryIndexGenerator, and removed the corresponding code from the
    WebdavServlet
  - Cleaned up imports
  
  Revision  Changes    Path
  1.1                  
jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/DirectoryIndexGenerator.java
  
  Index: DirectoryIndexGenerator.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/DirectoryIndexGenerator.java,v
 1.1 2002/06/13 13:10:58 cmlenz Exp $
   * $Revision: 1.1 $
   * $Date: 2002/06/13 13:10:58 $
   *
   * ====================================================================
   *
   * 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.util;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.Enumeration;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.apache.slide.common.NamespaceAccessToken;
  import org.apache.slide.common.SlideException;
  import org.apache.slide.common.SlideToken;
  import org.apache.slide.content.Content;
  import org.apache.slide.content.NodeRevisionDescriptor;
  import org.apache.slide.content.NodeRevisionDescriptors;
  import org.apache.slide.lock.Lock;
  import org.apache.slide.lock.NodeLock;
  import org.apache.slide.structure.ObjectNode;
  import org.apache.slide.structure.Structure;
  import org.apache.slide.security.Security;
  import org.apache.slide.security.NodePermission;
  import org.apache.slide.util.Messages;
  import org.apache.slide.webdav.WebdavServletConfig;
  
  /**
   * Utility class that encapsulates the generation of HTML directory index 
   * pages.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Christopher Lenz</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Remy Maucherat</a>
   * @author Dirk Verbeeck
   * @version $Revision: 1.1 $
   */
  public class DirectoryIndexGenerator {
      
      
      // -------------------------------------------------------------- Constants
      
      
      /**
       * HTTP Date format pattern (RFC 2068, 822, 1123).
       */
      public static final String DATE_FORMAT =
          "EEE, d MMM yyyy kk:mm:ss z";
      
      
      /**
       * Date formatter.
       */
      private static final DateFormat formatter =
          new SimpleDateFormat(DATE_FORMAT);
      
      
      // ----------------------------------------------------- Instance Variables
      
      
      /**
       * Access token to the namespace.
       */
      protected NamespaceAccessToken nat;
      
      
      /**
       * Configuration of the WebDAV servlet.
       */
      protected WebdavServletConfig config;
      
      
      // ----------------------------------------------------------- Constructors
      
      
      /**
       * Constructor.
       * 
       * @param nat       the namespace access token
       */
      public DirectoryIndexGenerator(NamespaceAccessToken nat,
                                     WebdavServletConfig config) {
          
          if (nat == null) {
              throw new IllegalArgumentException(
                  "NamespaceAccessToken must not be null");
          }
          this.nat = nat;
          if (config == null) {
              throw new IllegalArgumentException(
                  "WebdavServletConfig must not be null");
          }
          this.config = config;
      }
      
      
      // --------------------------------------------------------- Public Methods
      
      
      /**
       * Display a directory browsing page.
       * 
       * @param req       the HTTP request
       * @param res       the HTTP response
       * @throw IOException       if an IO exception occurrs while writing the 
       *                          response
       * @throw SlideException    if an exception occurrs accessing Slide
       */
      public void generate(HttpServletRequest req, HttpServletResponse res)
          throws IOException, SlideException {
          
          res.setContentType("text/html");
          
          String contextPath = req.getContextPath();
          if (contextPath == null)
              contextPath = "";
          
          // get the helpers
          Content content = nat.getContentHelper();
          Lock lock = nat.getLockHelper();
          Security security = nat.getSecurityHelper();
          Structure structure = nat.getStructureHelper();
          
          SlideToken slideToken = WebdavUtils.getSlideToken(req);
          String resourcePath = WebdavUtils.getRelativePath(req, config);
          ObjectNode object = structure.retrieve(slideToken, resourcePath);
          String name = object.getUri();
          
          // Number of characters to trim from the beginnings of filenames
          int trim = name.length();
          if (!name.endsWith("/"))
              trim += 1;
          if (name.equals("/"))
              trim = 1;
          
          PrintWriter writer = new PrintWriter(res.getWriter());
          
          // Render the page header
          writer.print("<html>\r\n");
          writer.print("<head>\r\n");
          writer.print("<meta http-equiv=\"Content-type\" content=\"text/html; 
charset=UTF-8\" >\r\n");
          writer.print("</meta>\r\n");
          writer.print("<title>");
          writer.print
              (Messages.format
                   ("org.apache.slide.webdav.GetMethod.directorylistingfor",
                    name));
          writer.print("</title>\r\n</head>\r\n");
          writer.print("<body bgcolor=\"white\">\r\n");
          writer.print("<table width=\"90%\" cellspacing=\"0\"" +
                           " cellpadding=\"5\" align=\"center\">\r\n");
          
          // Render the in-page title
          writer.print("<tr><td colspan=\"3\"><font size=\"+2\">\r\n<strong>");
          writer.print
              (Messages.format
                   ("org.apache.slide.webdav.GetMethod.directorylistingfor",
                    name));
          writer.print("</strong>\r\n</font></td></tr>\r\n");
          
          // Render the link to our parent (if required)
          String parentDirectory = name;
          if (parentDirectory.endsWith("/")) {
              parentDirectory =
                  parentDirectory.substring(0, parentDirectory.length() - 1);
          }
          String scope = config.getScope();
          if (parentDirectory.substring(scope.length()).lastIndexOf("/") >= 0) {
              String parent = name.substring(0, name.lastIndexOf("/"));
              writer.print("<tr><td colspan=\"5\" bgcolor=\"#ffffff\">\r\n");
              writer.print("<a href=\"");
              writer.print(WebdavUtils.encodeURL(contextPath));
              if (parent.equals(""))
                  parent = "/";
              writer.print(parent);
              writer.print("\">");
              writer.print(Messages.format
                               ("org.apache.slide.webdav.GetMethod.parent",
                                parent));
              writer.print("</a>\r\n");
              writer.print("</td></tr>\r\n");
          }
          
          Enumeration permissionsList = null;
          Enumeration locksList = null;
          try {
              permissionsList =
                  security.enumeratePermissions(slideToken, object.getUri());
              locksList = lock.enumerateLocks(slideToken, object.getUri(), false);
          } catch (SlideException e) {
              // Any security based exception will be trapped here
              // Any locking based exception will be trapped here
          }
          
          // Displaying ACL info
          if (org.apache.slide.util.Configuration.useIntegratedSecurity()) {
              displayPermissions(permissionsList, writer, false);
          }
          
          // Displaying lock info
          displayLocks(locksList, writer, false);
          
          writer.print("<tr><td colspan=\"5\" bgcolor=\"#ffffff\">");
          writer.print("&nbsp;");
          writer.print("</td></tr>\r\n");
          
          // Render the column headings
          writer.print("<tr bgcolor=\"#cccccc\">\r\n");
          writer.print("<td align=\"left\" colspan=\"3\">");
          writer.print("<font size=\"+1\"><strong>");
          writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.filename"));
          writer.print("</strong></font></td>\r\n");
          writer.print("<td align=\"center\"><font size=\"+1\"><strong>");
          writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.size"));
          writer.print("</strong></font></td>\r\n");
          writer.print("<td align=\"right\"><font size=\"+1\"><strong>");
          writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.lastModified"));
          writer.print("</strong></font></td>\r\n");
          writer.print("</tr>\r\n");
          
          Enumeration resources = object.enumerateChildren();
          boolean shade = false;
          
          while (resources.hasMoreElements()) {
              String currentResource = (String) resources.nextElement();
              NodeRevisionDescriptor currentDescriptor = null;
              permissionsList = null;
              locksList = null;
              try {
                  NodeRevisionDescriptors revisionDescriptors =
                      content.retrieve(slideToken, currentResource);
                  // Retrieve latest revision descriptor
                  currentDescriptor =
                      content.retrieve(slideToken, revisionDescriptors);
              } catch (SlideException e) {
                  // Silent exception : Objects without any revision are
                  // considered collections, and do not have any attributes
                  // Any security based exception will be trapped here
                  // Any locking based exception will be trapped here
              }
              
              try {
                  permissionsList =
                      security.enumeratePermissions(slideToken, currentResource);
                  locksList = lock.enumerateLocks(slideToken, currentResource, false);
              } catch (SlideException e) {
                  // Any security based exception will be trapped here
                  // Any locking based exception will be trapped here
              }
              
              String trimmed = currentResource.substring(trim);
              if (trimmed.equalsIgnoreCase("WEB-INF") ||
                  trimmed.equalsIgnoreCase("META-INF")) {
                  continue;
              }
              
              writer.print("<tr");
              if (shade) {
                  writer.print(" bgcolor=\"dddddd\"");
              } else {
                  writer.print(" bgcolor=\"eeeeee\"");
              }
              writer.print(">\r\n");
              shade = !shade;
              
              writer.print("<td align=\"left\" colspan=\"3\">&nbsp;&nbsp;\r\n");
              writer.print("<a href=\"");
              writer.print(WebdavUtils.encodeURL(contextPath + currentResource));
              writer.print("\"><tt>");
              writer.print(WebdavUtils.encodeURL(trimmed, "UTF-8"));
                  // make it I18N
              if (WebdavUtils.isCollection(currentDescriptor)) {
                  writer.print("/");
              }
              writer.print("</tt></a></td>\r\n");
              
              writer.print("<td align=\"right\"><tt>");
              if (WebdavUtils.isCollection(currentDescriptor)) {
                  writer.print("&nbsp;");
              }
              else {
                  writer.print(renderSize(currentDescriptor.getContentLength()));
              }
              writer.print("</tt></td>\r\n");
              
              writer.print("<td align=\"right\"><tt>");
              if (currentDescriptor != null) {
                  writer.print(currentDescriptor.getLastModified());
              } else {
                  writer.print("&nbsp;");
              }
              writer.print("</tt></td>\r\n");
              
              writer.print("</tr>\r\n");
              
              // Displaying ACL info
              if (org.apache.slide.util.Configuration.useIntegratedSecurity()) {
                  displayPermissions(permissionsList, writer, shade);
              }
              
              // Displaying lock info
              displayLocks(locksList, writer, shade);
          }
          
          // Render the page footer
          writer.print("<tr><td colspan=\"5\">&nbsp;</td></tr>\r\n");
          writer.print("<tr><td colspan=\"3\" bgcolor=\"#cccccc\">");
          writer.print("<font size=\"-1\">");
          writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.version"));
          writer.print("</font></td>\r\n");
          writer.print("<td colspan=\"2\" align=\"right\" bgcolor=\"#cccccc\">");
          writer.print("<font size=\"-1\">");
          writer.print(formatter.format(new Date()));
          writer.print("</font></td></tr>\r\n");
          writer.print("</table>\r\n");
          writer.print("</body>\r\n");
          writer.print("</html>\r\n");
          
          // Return an input stream to the underlying bytes
          writer.flush();
      }
      
      
      // ------------------------------------------------------ Protected Methods
      
      
      /**
       * Display an ACL list.
       *
       * @param permissionsList   the list of NodePermission objects
       * @param writer            the output will be appended to this writer
       * @param shade             whether the row should be displayed darker
       */
      protected void displayPermissions(Enumeration permissionsList,
                                      PrintWriter writer,
                                      boolean shade)
          throws IOException {
          
          if ((permissionsList != null) && (permissionsList.hasMoreElements())) {
              writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\""
                                          : " bgcolor=\"dddddd\"") +
                           ">\r\n");
              writer.print("<td align=\"left\" colspan=\"5\"><tt><b>");
              writer.print(Messages.message
                               ("org.apache.slide.webdav.GetMethod.aclinfo"));
              writer.print("</b></tt></td>\r\n");
              writer.print("</tr>\r\n");
              writer.print("<tr");
              if (!shade) {
                  writer.print(" bgcolor=\"dddddd\"");
              } else {
                  writer.print(" bgcolor=\"eeeeee\"");
              }
              writer.print(">\r\n");
              writer.print("<td align=\"left\" colspan=\"2\"><tt><b>");
              writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.subject"));
              writer.print("</b></tt></td>\r\n");
              writer.print("<td align=\"left\"><tt><b>");
              writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.action"));
              writer.print("</b></tt></td>\r\n");
              writer.print("<td align=\"right\"><tt><b>");
              writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.inheritable"));
              writer.print("</b></tt></td>\r\n");
              writer.print("<td align=\"right\"><tt><b>");
              writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.deny"));
              writer.print("</b></tt></td>\r\n");
              writer.print("</tr>\r\n");
              
              while (permissionsList.hasMoreElements()) {
                  writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\""
                                              : " bgcolor=\"dddddd\"") +
                               ">\r\n");
                  NodePermission currentPermission =
                      (NodePermission) permissionsList.nextElement();
                  writer.print("<td align=\"left\" colspan=\"2\"><tt>");
                  writer.print(currentPermission.getSubjectUri());
                  writer.print("</tt></td>\r\n");
                  writer.print("<td align=\"left\"><tt>");
                  writer.print(currentPermission.getActionUri());
                  writer.print("</tt></td>\r\n");
                  writer.print("<td align=\"right\"><tt>");
                  writer.print(currentPermission.isInheritable());
                  writer.print("</tt></td>\r\n");
                  writer.print("<td align=\"right\"><tt>");
                  writer.print(currentPermission.isNegative());
                  writer.print("</tt></td>\r\n");
                  writer.print("</tr>\r\n");
              }
          }
      }
      
      
      /**
       * Display a lock list.
       *
       * @param permissionsList   the list of NodePermission objects
       * @param writer            the output will be appended to this writer
       * @param shade             whether the row should be displayed darker
       */
      protected void displayLocks(Enumeration locksList, PrintWriter writer,
                                boolean shade)
          throws IOException {
          
          if ((locksList != null) && (locksList.hasMoreElements())) {
              writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\""
                                          : " bgcolor=\"dddddd\"") +
                           ">\r\n");
              writer.print("<td align=\"left\" colspan=\"5\"><tt><b>");
              writer.print(Messages.message
                               ("org.apache.slide.webdav.GetMethod.locksinfo"));
              writer.print("</b></tt></td>\r\n");
              writer.print("</tr>\r\n");
              writer.print("<tr");
              if (!shade) {
                  writer.print(" bgcolor=\"dddddd\"");
              } else {
                  writer.print(" bgcolor=\"eeeeee\"");
              }
              writer.print(">\r\n");
              writer.print("<td align=\"left\"><tt><b>");
              writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.subject"));
              writer.print("</b></tt></td>\r\n");
              writer.print("<td align=\"left\"><tt><b>");
              writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.type"));
              writer.print("</b></tt></td>\r\n");
              writer.print("<td align=\"right\"><tt><b>");
              writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.expiration"));
              writer.print("</b></tt></td>\r\n");
              writer.print("<td align=\"right\"><tt><b>");
              writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.inheritable"));
              writer.print("</b></tt></td>\r\n");
              writer.print("<td align=\"right\"><tt><b>");
              writer.print(Messages.message
                           ("org.apache.slide.webdav.GetMethod.exclusive"));
              writer.print("</b></tt></td>\r\n");
              writer.print("</tr>\r\n");
              
              while (locksList.hasMoreElements()) {
                  writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\""
                                              : " bgcolor=\"dddddd\"") +
                               ">\r\n");
                  NodeLock currentLock = (NodeLock) locksList.nextElement();
                  writer.print("<td align=\"left\"><tt>");
                  writer.print(currentLock.getSubjectUri());
                  writer.print("</tt></td>\r\n");
                  writer.print("<td align=\"left\"><tt>");
                  writer.print(currentLock.getTypeUri());
                  writer.print("</tt></td>\r\n");
                  writer.print("<td align=\"right\"><tt>");
                  writer.print
                      (formatter.format(currentLock.getExpirationDate()));
                  writer.print("</tt></td>\r\n");
                  writer.print("<td align=\"right\"><tt>");
                  writer.print(currentLock.isInheritable());
                  writer.print("</tt></td>\r\n");
                  writer.print("<td align=\"right\"><tt>");
                  writer.print(currentLock.isExclusive());
                  writer.print("</tt></td>\r\n");
              }
          }
      }
      
      
      /**
       * Render the specified file size (in bytes).
       *
       * @param size File size (in bytes)
       */
      protected String renderSize(long size) {
          
          long leftSide = size / 1024;
          long rightSide = (size % 1024) / 103;   // Makes 1 digit
          if ((leftSide == 0) && (rightSide == 0) && (size > 0))
              rightSide = 1;
          
          return ("" + leftSide + "." + rightSide + " kb");
      }
      
      
  }
  
  
  
  
  1.44      +46 -491   
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.43
  retrieving revision 1.44
  diff -u -r1.43 -r1.44
  --- WebdavServlet.java        2 May 2002 09:30:00 -0000       1.43
  +++ WebdavServlet.java        13 Jun 2002 13:10:58 -0000      1.44
  @@ -63,47 +63,36 @@
   
   package org.apache.slide.webdav;
   
  -import java.io.*;
  +import java.io.IOException;
   import java.net.URL;
   import java.security.Principal;
  -import java.text.DateFormat;
  -import java.text.SimpleDateFormat;
  -import java.util.*;
  -import javax.servlet.*;
  -import javax.servlet.http.*;
  -import javax.xml.parsers.SAXParser;
  -import javax.xml.parsers.SAXParserFactory;
  -import org.xml.sax.*;
  -import org.xml.sax.helpers.*;
   
  -import org.apache.util.WebdavStatus;
  -import org.apache.util.DOMUtils;
  +import javax.servlet.RequestDispatcher;
  +import javax.servlet.ServletConfig;
  +import javax.servlet.ServletException;
  +import javax.servlet.UnavailableException;
  +import javax.servlet.http.HttpServlet;
  +import javax.servlet.http.HttpServletRequest;
  +import javax.servlet.http.HttpServletResponse;
   
   import org.apache.slide.authenticate.SecurityToken;
  -import org.apache.slide.common.*;
  -import org.apache.slide.content.Content;
  -import org.apache.slide.content.NodeRevisionDescriptor;
  -import org.apache.slide.content.NodeRevisionDescriptors;
  -import org.apache.slide.lock.Lock;
  -import org.apache.slide.lock.NodeLock;
  +import org.apache.slide.common.Domain;
  +import org.apache.slide.common.DomainInitializationFailedError;
  +import org.apache.slide.common.NamespaceAccessToken;
  +import org.apache.slide.common.SlideException;
   import org.apache.slide.security.AccessDeniedException;
  -import org.apache.slide.security.NodePermission;
  -import org.apache.slide.security.Security;
   import org.apache.slide.structure.LinkedObjectNotFoundException;
  -import org.apache.slide.structure.ObjectNode;
   import org.apache.slide.structure.ObjectNotFoundException;
  -import org.apache.slide.structure.Structure;
  -import org.apache.slide.util.conf.*;
  -import org.apache.slide.util.Messages;
   import org.apache.slide.util.logger.Logger;
  -import org.apache.slide.webdav.util.WebdavUtils;
  -
   import org.apache.slide.webdav.logger.XHttpServletRequestFacade;
   import org.apache.slide.webdav.logger.XHttpServletResponseFacade;
   import org.apache.slide.webdav.logger.XHttpServletResponseSimpleFacade;
   import org.apache.slide.webdav.logger.XMLTestCaseGenerator;
   import org.apache.slide.webdav.method.*;
  -
  +import org.apache.slide.webdav.util.DirectoryIndexGenerator;
  +import org.apache.slide.webdav.util.WebdavUtils;
  +import org.apache.util.WebdavStatus;
  +import org.apache.util.DOMUtils;
   
   /**
    * WebDAV Servlet.
  @@ -117,22 +106,18 @@
       
       // -------------------------------------------------------------- Constants
       
  -    private static final String LOG_CHANNEL = WebdavServlet.class.getName();
  -    
       
       /**
  -     * HTTP Date format pattern (RFC 2068, 822, 1123).
  +     * Name of the log channel used by the WebDAV servlet.
        */
  -    public static final String DATE_FORMAT = "EEE, d MMM yyyy kk:mm:ss z";
  +    private static final String LOG_CHANNEL = WebdavServlet.class.getName();
       
       
       /**
  -     * Date formatter.
  +     * Name under which the namespace access token is stored in the application
  +     * attributes. This is used when the WebDAV servlet doesn't initialize 
  +     * Slide itself, but rather the initialization is done outside.
        */
  -    private static final DateFormat formatter =
  -        new SimpleDateFormat(DATE_FORMAT);
  -    
  -    
       public static final String ATTRIBUTE_NAME =
           "org.apache.slide.NamespaceAccessToken";
       
  @@ -159,6 +144,12 @@
       
       
       /**
  +     * Directory browsing enabled.
  +     */
  +    protected DirectoryIndexGenerator directoryIndexGenerator;
  +    
  +    
  +    /**
        * Set to true if the servlet is handling the lifecycle of the domain.
        */
       protected boolean handleLifecycle = true;
  @@ -442,7 +433,7 @@
                       directoryBrowsing = false;
                   }
               } else {
  -                directoryBrowsing = new Boolean(value).booleanValue();
  +                directoryBrowsing = Boolean.valueOf(value).booleanValue();
               }
           }
           
  @@ -470,6 +461,12 @@
                                                  " not accessible");
               }
               
  +            if (directoryBrowsing) {
  +                directoryIndexGenerator =
  +                    new DirectoryIndexGenerator
  +                        (token, (WebdavServletConfig)getServletConfig());
  +            }
  +            
           } catch (DomainInitializationFailedError e) {
               log("Could not initialize domain", e);
               throw new UnavailableException(e.toString());
  @@ -477,7 +474,6 @@
               t.printStackTrace();
               throw new ServletException(t.toString());
           }
  -
        }
       
       
  @@ -497,7 +493,7 @@
       /**
        * Handle a GET request on a collection resource.
        */
  -    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  +    protected void doGet(HttpServletRequest req, HttpServletResponse res)
           throws ServletException, IOException {
           
           if (directoryBrowsing) {
  @@ -510,466 +506,25 @@
                   req.setAttribute
                       ("slide_uri", WebdavUtils.getRelativePath
                           (req, (WebdavServletConfig)getServletConfig()));
  -                directoryBrowsingTemplate.forward(req, resp);
  +                directoryBrowsingTemplate.forward(req, res);
               } else {
  -                Writer writer = resp.getWriter();
  -                resp.setContentType("text/html");
                   try {
  -                    displayDirectoryBrowsing(req, writer);
  +                    directoryIndexGenerator.generate(req, res);
                   } catch (AccessDeniedException e) {
  -                    resp.sendError(WebdavStatus.SC_FORBIDDEN);
  +                    res.sendError(WebdavStatus.SC_FORBIDDEN);
                   } catch (ObjectNotFoundException e) {
  -                    resp.sendError(WebdavStatus.SC_NOT_FOUND);
  +                    res.sendError(WebdavStatus.SC_NOT_FOUND);
                   } catch (LinkedObjectNotFoundException e) {
  -                    resp.sendError(WebdavStatus.SC_NOT_FOUND);
  +                    res.sendError(WebdavStatus.SC_NOT_FOUND);
                   } catch (SlideException e) {
  -                    resp.setStatus(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
  +                    res.setStatus(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
                   }
               }
           } else {
  -            resp.sendError(WebdavStatus.SC_FORBIDDEN);
  +            res.sendError(WebdavStatus.SC_FORBIDDEN);
           }
       }
       
  -
  -    /**
  -     * Display a directory browsing page.
  -     */
  -    protected void displayDirectoryBrowsing(HttpServletRequest req,
  -                                            Writer servletWriter)
  -        throws IOException, SlideException {
  -        
  -        String contextPath = req.getContextPath();
  -        if (contextPath == null)
  -            contextPath = "";
  -        
  -        // get the helpers
  -        Content content = token.getContentHelper();
  -        Lock lock = token.getLockHelper();
  -        Security security = token.getSecurityHelper();
  -        Structure structure = token.getStructureHelper();
  -
  -        SlideToken slideToken = WebdavUtils.getSlideToken(req);
  -        String resourcePath =
  -            WebdavUtils.getRelativePath(
  -                req, (WebdavServletConfig)getServletConfig());
  -        ObjectNode object = structure.retrieve(slideToken, resourcePath);
  -        String name = object.getUri();
  -        
  -        // Number of characters to trim from the beginnings of filenames
  -        int trim = name.length();
  -        if (!name.endsWith("/"))
  -            trim += 1;
  -        if (name.equals("/"))
  -            trim = 1;
  -        
  -        PrintWriter writer = new PrintWriter(servletWriter);
  -        
  -        // Render the page header
  -        writer.print("<html>\r\n");
  -        writer.print("<head>\r\n");
  -        writer.print("<meta http-equiv=\"Content-type\" content=\"text/html; 
charset=UTF-8\" >\r\n");
  -        writer.print("</meta>\r\n");
  -        writer.print("<title>");
  -        writer.print
  -            (Messages.format
  -                 ("org.apache.slide.webdav.GetMethod.directorylistingfor", name));
  -        writer.print("</title>\r\n</head>\r\n");
  -        writer.print("<body bgcolor=\"white\">\r\n");
  -        writer.print("<table width=\"90%\" cellspacing=\"0\"" +
  -                         " cellpadding=\"5\" align=\"center\">\r\n");
  -        
  -        // Render the in-page title
  -        writer.print("<tr><td colspan=\"3\"><font size=\"+2\">\r\n<strong>");
  -        writer.print
  -            (Messages.format
  -                 ("org.apache.slide.webdav.GetMethod.directorylistingfor", name));
  -        writer.print("</strong>\r\n</font></td></tr>\r\n");
  -        
  -        // Render the link to our parent (if required)
  -        String parentDirectory = name;
  -        if (parentDirectory.endsWith("/")) {
  -            parentDirectory =
  -                parentDirectory.substring(0, parentDirectory.length() - 1);
  -        }
  -        String scope = ((WebdavServletConfig)getServletConfig()).getScope();
  -        if (parentDirectory.substring(scope.length()).lastIndexOf("/") >= 0) {
  -            String parent = name.substring(0, name.lastIndexOf("/"));
  -            writer.print("<tr><td colspan=\"5\" bgcolor=\"#ffffff\">\r\n");
  -            writer.print("<a href=\"");
  -            writer.print(WebdavUtils.encodeURL(contextPath));
  -            if (parent.equals(""))
  -                parent = "/";
  -            writer.print(parent);
  -            writer.print("\">");
  -            writer.print(Messages.format
  -                             ("org.apache.slide.webdav.GetMethod.parent", parent));
  -            writer.print("</a>\r\n");
  -            writer.print("</td></tr>\r\n");
  -        }
  -        
  -        Enumeration permissionsList = null;
  -        Enumeration locksList = null;
  -        
  -        try {
  -            
  -            permissionsList =
  -                security.enumeratePermissions(slideToken, object.getUri());
  -            locksList = lock.enumerateLocks(slideToken, object.getUri(), false);
  -            
  -        } catch (SlideException e) {
  -            
  -            // Any security based exception will be trapped here
  -            
  -            // Any locking based exception will be trapped here
  -            
  -        }
  -        
  -        // Displaying ACL info
  -        if( org.apache.slide.util.Configuration.useIntegratedSecurity() )
  -            displayPermissions(permissionsList, writer, false);
  -        
  -        // Displaying lock info
  -        displayLocks(locksList, writer, false);
  -        
  -        writer.print("<tr><td colspan=\"5\" bgcolor=\"#ffffff\">");
  -        writer.print("&nbsp;");
  -        writer.print("</td></tr>\r\n");
  -        
  -        // Render the column headings
  -        writer.print("<tr bgcolor=\"#cccccc\">\r\n");
  -        writer.print("<td align=\"left\" colspan=\"3\">");
  -        writer.print("<font size=\"+1\"><strong>");
  -        writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.filename"));
  -        writer.print("</strong></font></td>\r\n");
  -        writer.print("<td align=\"center\"><font size=\"+1\"><strong>");
  -        writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.size"));
  -        writer.print("</strong></font></td>\r\n");
  -        writer.print("<td align=\"right\"><font size=\"+1\"><strong>");
  -        writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.lastModified"));
  -        writer.print("</strong></font></td>\r\n");
  -        writer.print("</tr>\r\n");
  -        
  -        Enumeration resources = object.enumerateChildren();
  -        boolean shade = false;
  -        
  -        while (resources.hasMoreElements()) {
  -            
  -            String currentResource = (String) resources.nextElement();
  -            
  -            NodeRevisionDescriptor currentDescriptor = null;
  -            
  -            permissionsList = null;
  -            locksList = null;
  -            
  -            try {
  -                
  -                NodeRevisionDescriptors revisionDescriptors =
  -                    content.retrieve(slideToken, currentResource);
  -                // Retrieve latest revision descriptor
  -                currentDescriptor =
  -                    content.retrieve(slideToken, revisionDescriptors);
  -                
  -            } catch (SlideException e) {
  -                
  -                // Silent exception : Objects without any revision are
  -                // considered collections, and do not have any attributes
  -                
  -                // Any security based exception will be trapped here
  -                
  -                // Any locking based exception will be trapped here
  -                
  -            }
  -            
  -            try {
  -                
  -                permissionsList =
  -                    security.enumeratePermissions(slideToken, currentResource);
  -                locksList = lock.enumerateLocks(slideToken, currentResource, false);
  -                
  -            } catch (SlideException e) {
  -                
  -                // Any security based exception will be trapped here
  -                
  -                // Any locking based exception will be trapped here
  -                
  -            }
  -            
  -            String trimmed = currentResource.substring(trim);
  -            if (trimmed.equalsIgnoreCase("WEB-INF") ||
  -                trimmed.equalsIgnoreCase("META-INF"))
  -                continue;
  -            
  -            writer.print("<tr");
  -            if (shade) {
  -                writer.print(" bgcolor=\"dddddd\"");
  -            } else {
  -                writer.print(" bgcolor=\"eeeeee\"");
  -            }
  -            writer.print(">\r\n");
  -            shade = !shade;
  -            
  -            writer.print("<td align=\"left\" colspan=\"3\">&nbsp;&nbsp;\r\n");
  -            writer.print("<a href=\"");
  -            writer.print(WebdavUtils.encodeURL(contextPath + currentResource));
  -            writer.print("\"><tt>");
  -            writer.print(WebdavUtils.encodeURL(trimmed, "UTF-8")); // make it I18N
  -            if (WebdavUtils.isCollection(currentDescriptor)) {
  -                writer.print("/");
  -            }
  -            writer.print("</tt></a></td>\r\n");
  -            
  -            writer.print("<td align=\"right\"><tt>");
  -            if (WebdavUtils.isCollection(currentDescriptor))
  -                writer.print("&nbsp;");
  -            else
  -                writer.print(renderSize(currentDescriptor.getContentLength()));
  -            writer.print("</tt></td>\r\n");
  -            
  -            writer.print("<td align=\"right\"><tt>");
  -            if (currentDescriptor != null) {
  -                writer.print(currentDescriptor.getLastModified());
  -            } else {
  -                writer.print("&nbsp;");
  -            }
  -            writer.print("</tt></td>\r\n");
  -            
  -            writer.print("</tr>\r\n");
  -            
  -            // Displaying ACL info
  -            if( org.apache.slide.util.Configuration.useIntegratedSecurity() )
  -                displayPermissions(permissionsList, writer, shade);
  -            
  -            // Displaying lock info
  -            displayLocks(locksList, writer, shade);
  -            
  -        }
  -        
  -        // Render the page footer
  -        writer.print("<tr><td colspan=\"5\">&nbsp;</td></tr>\r\n");
  -        writer.print("<tr><td colspan=\"3\" bgcolor=\"#cccccc\">");
  -        writer.print("<font size=\"-1\">");
  -        writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.version"));
  -        writer.print("</font></td>\r\n");
  -        writer.print("<td colspan=\"2\" align=\"right\" bgcolor=\"#cccccc\">");
  -        writer.print("<font size=\"-1\">");
  -        writer.print(formatter.format(new Date()));
  -        writer.print("</font></td></tr>\r\n");
  -        writer.print("</table>\r\n");
  -        writer.print("</body>\r\n");
  -        writer.print("</html>\r\n");
  -        
  -        // Return an input stream to the underlying bytes
  -        writer.flush();
  -        
  -    }
       
  -    
  -    /**
  -     * Display an ACL list.
  -     *
  -     * @param permissionsList List of NodePermission objects
  -     * @param shade
  -     * @param writer The output will be appended to this writer
  -     */
  -    private void displayPermissions(Enumeration permissionsList,
  -                                    PrintWriter writer,
  -                                    boolean shade)
  -        throws IOException {
  -        
  -        if ((permissionsList != null) && (permissionsList.hasMoreElements())) {
  -            
  -            writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\""
  -                                        : " bgcolor=\"dddddd\"") +
  -                         ">\r\n");
  -            writer.print("<td align=\"left\" colspan=\"5\"><tt><b>");
  -            writer.print(Messages.message
  -                             ("org.apache.slide.webdav.GetMethod.aclinfo"));
  -            writer.print("</b></tt></td>\r\n");
  -            writer.print("</tr>\r\n");
  -            
  -            writer.print("<tr");
  -            if (!shade) {
  -                writer.print(" bgcolor=\"dddddd\"");
  -            } else {
  -                writer.print(" bgcolor=\"eeeeee\"");
  -            }
  -            writer.print(">\r\n");
  -            
  -            writer.print("<td align=\"left\" colspan=\"2\"><tt><b>");
  -            writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.subject"));
  -            writer.print("</b></tt></td>\r\n");
  -            
  -            writer.print("<td align=\"left\"><tt><b>");
  -            writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.action"));
  -            writer.print("</b></tt></td>\r\n");
  -            
  -            writer.print("<td align=\"right\"><tt><b>");
  -            writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.inheritable"));
  -            writer.print("</b></tt></td>\r\n");
  -            
  -            writer.print("<td align=\"right\"><tt><b>");
  -            writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.deny"));
  -            writer.print("</b></tt></td>\r\n");
  -            
  -            writer.print("</tr>\r\n");
  -            
  -            while (permissionsList.hasMoreElements()) {
  -                
  -                writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\""
  -                                            : " bgcolor=\"dddddd\"") +
  -                             ">\r\n");
  -                
  -                NodePermission currentPermission =
  -                    (NodePermission) permissionsList.nextElement();
  -                
  -                writer.print("<td align=\"left\" colspan=\"2\"><tt>");
  -                writer.print(currentPermission.getSubjectUri());
  -                writer.print("</tt></td>\r\n");
  -                
  -                writer.print("<td align=\"left\"><tt>");
  -                writer.print(currentPermission.getActionUri());
  -                writer.print("</tt></td>\r\n");
  -                
  -                writer.print("<td align=\"right\"><tt>");
  -                writer.print(currentPermission.isInheritable());
  -                writer.print("</tt></td>\r\n");
  -                
  -                writer.print("<td align=\"right\"><tt>");
  -                writer.print(currentPermission.isNegative());
  -                writer.print("</tt></td>\r\n");
  -                
  -                writer.print("</tr>\r\n");
  -            }
  -            
  -        }
  -        
  -    }
  -    
  -    
  -    /**
  -     * Display a lock list.
  -     *
  -     * @param locksList List of NodeLock objects
  -     * @param shade
  -     * @param writer The output will be appended to this writer
  -     */
  -    private void displayLocks(Enumeration locksList, PrintWriter writer,
  -                              boolean shade)
  -        throws IOException {
  -        
  -        if ((locksList != null) && (locksList.hasMoreElements())) {
  -            
  -            writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\""
  -                                        : " bgcolor=\"dddddd\"") +
  -                         ">\r\n");
  -            writer.print("<td align=\"left\" colspan=\"5\"><tt><b>");
  -            writer.print(Messages.message
  -                             ("org.apache.slide.webdav.GetMethod.locksinfo"));
  -            writer.print("</b></tt></td>\r\n");
  -            writer.print("</tr>\r\n");
  -            
  -            writer.print("<tr");
  -            if (!shade) {
  -                writer.print(" bgcolor=\"dddddd\"");
  -            } else {
  -                writer.print(" bgcolor=\"eeeeee\"");
  -            }
  -            writer.print(">\r\n");
  -            
  -            writer.print("<td align=\"left\"><tt><b>");
  -            writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.subject"));
  -            writer.print("</b></tt></td>\r\n");
  -            
  -            writer.print("<td align=\"left\"><tt><b>");
  -            writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.type"));
  -            writer.print("</b></tt></td>\r\n");
  -            
  -            writer.print("<td align=\"right\"><tt><b>");
  -            writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.expiration"));
  -            writer.print("</b></tt></td>\r\n");
  -            
  -            writer.print("<td align=\"right\"><tt><b>");
  -            writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.inheritable"));
  -            writer.print("</b></tt></td>\r\n");
  -            
  -            writer.print("<td align=\"right\"><tt><b>");
  -            writer.print(Messages.message
  -                         ("org.apache.slide.webdav.GetMethod.exclusive"));
  -            writer.print("</b></tt></td>\r\n");
  -            
  -            writer.print("</tr>\r\n");
  -            
  -            while (locksList.hasMoreElements()) {
  -                
  -                writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\""
  -                                            : " bgcolor=\"dddddd\"") +
  -                             ">\r\n");
  -                
  -                NodeLock currentLock = (NodeLock) locksList.nextElement();
  -                
  -                writer.print("<td align=\"left\"><tt>");
  -                writer.print(currentLock.getSubjectUri());
  -                writer.print("</tt></td>\r\n");
  -                
  -                writer.print("<td align=\"left\"><tt>");
  -                writer.print(currentLock.getTypeUri());
  -                writer.print("</tt></td>\r\n");
  -                
  -                writer.print("<td align=\"right\"><tt>");
  -                writer.print
  -                    (formatter.format(currentLock.getExpirationDate()));
  -                writer.print("</tt></td>\r\n");
  -                
  -                writer.print("<td align=\"right\"><tt>");
  -                writer.print(currentLock.isInheritable());
  -                writer.print("</tt></td>\r\n");
  -                
  -                writer.print("<td align=\"right\"><tt>");
  -                writer.print(currentLock.isExclusive());
  -                writer.print("</tt></td>\r\n");
  -                
  -            }
  -            
  -        }
  -        
  -    }
  -    
  -    
  -    // -------------------------------------------------------- Private Methods
  -    
  -    
  -    /**
  -     * Render the specified file size (in bytes).
  -     *
  -     * @param size File size (in bytes)
  -     */
  -    private String renderSize(long size) {
  -        
  -        long leftSide = size / 1024;
  -        long rightSide = (size % 1024) / 103;   // Makes 1 digit
  -        if ((leftSide == 0) && (rightSide == 0) && (size > 0))
  -            rightSide = 1;
  -        
  -        return ("" + leftSide + "." + rightSide + " kb");
  -    }
  -
  -
   }
  -
  -
  -
  -
   
  
  
  

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

Reply via email to