juergen     02/05/28 02:16:54

  Modified:    src/webdav/server/org/apache/slide/webdav/method
                        AclMethod.java
  Log:
  Replaced usage of org.w3c.dom by JDOM.
  (ralf)
  
  Revision  Changes    Path
  1.20      +132 -92   
jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AclMethod.java
  
  Index: AclMethod.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AclMethod.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- AclMethod.java    16 May 2002 07:39:24 -0000      1.19
  +++ AclMethod.java    28 May 2002 09:16:54 -0000      1.20
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AclMethod.java,v
 1.19 2002/05/16 07:39:24 juergen Exp $
  - * $Revision: 1.19 $
  - * $Date: 2002/05/16 07:39:24 $
  + * $Header: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AclMethod.java,v
 1.20 2002/05/28 09:16:54 juergen Exp $
  + * $Revision: 1.20 $
  + * $Date: 2002/05/28 09:16:54 $
    *
    * ====================================================================
    *
  @@ -70,7 +70,6 @@
   import javax.servlet.http.*;
   import javax.xml.parsers.ParserConfigurationException;
   
  -import org.w3c.dom.*;
   import org.xml.sax.SAXException;
   
   import org.apache.util.WebdavStatus;
  @@ -82,6 +81,14 @@
   import org.apache.slide.structure.*;
   import org.apache.slide.lock.*;
   import org.apache.slide.content.*;
  +import org.apache.slide.content.NodeProperty.NamespaceCache;
  +
  +import org.apache.slide.webdav.util.AclConstants;
  +
  +import org.jdom.Document;
  +import org.jdom.Element;
  +import org.jdom.Namespace;
  +import org.jdom.JDOMException;
   
   /**
    * ACL method.
  @@ -89,7 +96,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Remy Maucherat</a>
    * @author Dirk Verbeeck
    */
  -public class AclMethod extends WebdavMethod {
  +public class AclMethod extends WebdavMethod implements AclConstants {
       
       
       // -------------------------------------------------------------- Constants
  @@ -175,86 +182,88 @@
           if (requestBody.length() != 0) {
               try {
                   
  -                Document document = parseRequestContent();
  +                retrieveRequestContent();
  +                Document document = getRequestContent();
                   
                   // Get the root element of the document
  -                Element acl = document.getDocumentElement();
  +                Element acl = document.getRootElement();
  +                if ( (acl == null) ||  ( ! E_ACL.equals(acl.getName()) ) ) {
  +                    throw new SAXException("Request content document must start 
with an &lt;"+
  +                                               E_ACL+"&gt; element");
  +                }
                   
  -                NodeList aceList = acl.getElementsByTagNameNS
  -                    (NodeProperty.DEFAULT_NAMESPACE, "ace");
  +                List aceList = acl.getChildren(E_ACE, 
NamespaceCache.DEFAULT_NAMESPACE);
  +                Iterator aceIterator = aceList.iterator();
  +                while (aceIterator.hasNext()) {
                   
  -                for (int i = 0; i < aceList.getLength(); i++) {
                       boolean inheritable = true;
  -                    Element ace = (Element) aceList.item(i);
  +                    Element ace = (Element) aceIterator.next();
                       
                       // inherited permissions
  -                    NodeList inheritedList = ace.getElementsByTagNameNS
  -                        (NodeProperty.DEFAULT_NAMESPACE, "inherited");
  -                    int inheritedCount = inheritedList.getLength();
  +                    List inheritedList = ace.getChildren(E_INHERITED,
  +                                                         
NamespaceCache.DEFAULT_NAMESPACE);
  +                    int inheritedCount = inheritedList.size();
                       if (inheritedCount == 1) {
  -                        Element inheritedElement = (Element) inheritedList.item(0);
  +                        Element inheritedElement = (Element) inheritedList.get(0);
                           String inherited = parseInheritence(inheritedElement);
                           if (inherited.equalsIgnoreCase("false")) inheritable = 
false;
                           //                         continue;
                       }
                       if (inheritedCount > 1) {
  -                        resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  -                        throw new WebdavException(WebdavStatus.SC_BAD_REQUEST);
  +                        throw new SAXException("Element &lt;"+E_ACE+"&gt; must 
contain at most one &lt;"+E_INHERITED+"&gt; element");
                       }
                       
                       // Ignore protected (protected <-> inherited for Slide)
  -                    int protectedCount = ace.getElementsByTagNameNS
  -                        (NodeProperty.DEFAULT_NAMESPACE, "protected")
  -                        .getLength();
  +                    int protectedCount = ace.getChildren(E_PROTECTED,
  +                                                         
NamespaceCache.DEFAULT_NAMESPACE).size();
                       if (protectedCount == 1)
                           continue;
                       if (protectedCount > 1) {
  -                        resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  -                        throw new WebdavException(WebdavStatus.SC_BAD_REQUEST);
  +                        throw new SAXException("Element &lt;"+E_ACE+"&gt; must 
contain at most one &lt;"+E_PROTECTED+"&gt; element");
                       }
                       
  -                    NodeList principalList = ace.getElementsByTagNameNS
  -                        (NodeProperty.DEFAULT_NAMESPACE, "principal");
  -                    if (principalList.getLength() != 1) {
  -                        resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  -                        throw new WebdavException(WebdavStatus.SC_BAD_REQUEST);
  +                    List principalList = ace.getChildren(E_PRINCIPAL,
  +                                                         
NamespaceCache.DEFAULT_NAMESPACE);
  +                    if (principalList.size() != 1) {
  +                        throw new SAXException("Element &lt;"+E_ACE+"&gt; must 
contain a &lt;"+E_PRINCIPAL+"&gt; element");
                       }
                       
  -                    Element principalElement = (Element) principalList.item(0);
  +                    Element principalElement = (Element) principalList.get(0);
                       String principal = parsePrincipal(principalElement);
                       
  -                    NodeList grantList = ace.getElementsByTagNameNS
  -                        (NodeProperty.DEFAULT_NAMESPACE, "grant");
  -                    NodeList denyList = ace.getElementsByTagNameNS
  -                        (NodeProperty.DEFAULT_NAMESPACE, "deny");
  +                    List grantList = ace.getChildren(E_GRANT,
  +                                                     
NamespaceCache.DEFAULT_NAMESPACE);
  +                    List denyList = ace.getChildren(E_DENY,
  +                                                    
NamespaceCache.DEFAULT_NAMESPACE);
                       
                       // there must be only ONE grant or deny element
  -                    if ((grantList.getLength()+denyList.getLength())!=1) {
  -                        resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  -                        throw new WebdavException(WebdavStatus.SC_BAD_REQUEST);
  +                    if ((grantList.size()+denyList.size())!=1) {
  +                        throw new SAXException("Element &lt;"+E_ACE+"&gt; must 
contain either a &lt;"+
  +                                                   E_GRANT+"&gt; or a &lt;"+
  +                                                   E_DENY+"&gt; element");
                       }
                       
  -                    boolean negative = (denyList.getLength() > 0);
  +                    boolean negative = (denyList.size() > 0);
                       
  -                    NodeList privilegeList = null;
  +                    List privilegeList = null;
                       
                       if (negative) {
  -                        Element denyElement=(Element) denyList.item(0);
  -                        privilegeList = denyElement.getElementsByTagNameNS
  -                            (NodeProperty.DEFAULT_NAMESPACE, "privilege");
  +                        Element denyElement=(Element) denyList.get(0);
  +                        privilegeList = denyElement.getChildren(E_PRIVILEGE,
  +                                                                
NamespaceCache.DEFAULT_NAMESPACE);
                       } else {
  -                        Element grantElement=(Element) grantList.item(0);
  -                        privilegeList = grantElement.getElementsByTagNameNS
  -                            (NodeProperty.DEFAULT_NAMESPACE, "privilege");
  +                        Element grantElement=(Element) grantList.get(0);
  +                        privilegeList = grantElement.getChildren(E_PRIVILEGE,
  +                                                                 
NamespaceCache.DEFAULT_NAMESPACE);
                       }
                       
                       
                       // FIXME : Use the very unofficial Advanced ACL spec here.
                       
  -                    for (int j = 0; j < privilegeList.getLength(); j++) {
  +                    for (int j = 0; j < privilegeList.size(); j++) {
                           
                           Element privilegeElement =
  -                            (Element) privilegeList.item(j);
  +                            (Element) privilegeList.get(j);
                           int privilege = parsePrivilege(privilegeElement);
                           
                           switch (privilege) {
  @@ -352,6 +361,9 @@
                   System.err.println(requestBody);
                   e.printStackTrace();
                   resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  +                try {
  +                    resp.sendError(WebdavStatus.SC_BAD_REQUEST, e.getMessage());
  +                } catch (IOException ioe) {}
                   throw new WebdavException(WebdavStatus.SC_BAD_REQUEST);
               } catch (ParserConfigurationException e) {
                   System.err.println(e.getMessage());
  @@ -409,40 +421,53 @@
        *
        * @return the URI of the principal
        */
  -    protected String parsePrincipal(Element principal) throws WebdavException {
  +    protected String parsePrincipal(Element principal) throws SAXException {
           
           // FIXME: make constants and make sure they are used in
           // AclMethod:parsePrincipal and PropFindMethod:writePrincipal
  -        NodeList hrefList = 
principal.getElementsByTagNameNS(NodeProperty.DEFAULT_NAMESPACE, "href");
  -        if (hrefList.getLength() == 1) {
  -            Element href = (Element) hrefList.item(0);
  -            if (href.getFirstChild().getNodeType() == Node.TEXT_NODE)
  -                return getSlidePath(href.getFirstChild().getNodeValue());
  -        } else if (hasChild(principal, NodeProperty.DEFAULT_NAMESPACE, "all")) {
  +        Element href = getChild(principal, NamespaceCache.DEFAULT_NAMESPACE, 
E_HREF);
  +        if (href != null) {
  +            String text = href.getText();
  +            if (text.length() == 0) {
  +                throw new SAXException("&lt;"+E_HREF+"&gt; element of 
&lt;"+principal.getName()+
  +                                           "&gt; must not be emtpy");
  +            }
  +            return getSlidePath(text);
  +        }
  +        else if (hasChild(principal, NamespaceCache.DEFAULT_NAMESPACE, E_ALL)) {
               return "nobody";
  -        } else if (hasChild(principal, NodeProperty.DEFAULT_NAMESPACE, "self")) {
  +        }
  +        else if (hasChild(principal, NamespaceCache.DEFAULT_NAMESPACE, E_SELF)) {
               return "~";
  -        } else if (hasChild(principal, NodeProperty.DEFAULT_NAMESPACE, 
"unauthenticated")) {
  +        }
  +        else if (hasChild(principal, NamespaceCache.DEFAULT_NAMESPACE, 
E_UNAUTHENTICATED)) {
               return token.getNamespaceConfig().getUsersPath() + "/" +
                   token.getNamespaceConfig().getGuestPath();
           }
  -        resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  -        throw new WebdavException(WebdavStatus.SC_BAD_REQUEST);
  +        throw new SAXException("&lt;"+principal.getName()+"&gt; element must 
contain either a "+
  +                              "&lt;"+E_HREF+"&gt;, "+
  +                              "&lt;"+E_ALL+"&gt;, "+
  +                              "&lt;"+E_SELF+"&gt; or "+
  +                              "&lt;"+E_UNAUTHENTICATED+"&gt; element.");
       }
       
       /**
        *
        * @return the URI of the inheritence
        */
  -    protected String parseInheritence(Element inheritence) throws WebdavException {
  +    protected String parseInheritence(Element inheritence) throws SAXException {
           
  -        NodeList hrefList = 
inheritence.getElementsByTagNameNS(NodeProperty.DEFAULT_NAMESPACE, "href");
  -        if (hrefList.getLength() == 1) {
  -            Element href = (Element) hrefList.item(0);
  -            if (href.getFirstChild().getNodeType() == Node.TEXT_NODE)
  -                return getSlidePath(href.getFirstChild().getNodeValue());
  +        Element href = getChild(inheritence, NamespaceCache.DEFAULT_NAMESPACE, 
E_HREF);
  +        if (href != null) {
  +            String text = href.getText();
  +            if (text.length() == 0) {
  +                throw new SAXException("&lt;"+E_HREF+"&gt; element of 
&lt;"+inheritence.getName()+
  +                                           "&gt; must not be emtpy");
  +        }
  +            return getSlidePath(text);
           }
  -        throw new WebdavException(WebdavStatus.SC_BAD_REQUEST);
  +        throw new SAXException("&lt;"+inheritence.getName()+"&gt; element must 
contain a &lt;"+
  +                                   E_HREF+"&gt; element");
       }
       
       
  @@ -452,67 +477,82 @@
        * @return privilege
        */
       protected int parsePrivilege(Element privilege)
  -        throws WebdavException {
  +        throws SAXException {
           
  -        if (hasChild(privilege, NodeProperty.DEFAULT_NAMESPACE, "all")) {
  +        if (hasChild(privilege, NamespaceCache.DEFAULT_NAMESPACE, "all")) {
               return PRIVILEGE_ALL;
  -        } else if (hasChild(privilege, NodeProperty.DEFAULT_NAMESPACE, "read")) {
  +        } else if (hasChild(privilege, NamespaceCache.DEFAULT_NAMESPACE, "read")) {
               return PRIVILEGE_READ;
  -        } else if (hasChild(privilege, NodeProperty.DEFAULT_NAMESPACE, "write")) {
  +        } else if (hasChild(privilege, NamespaceCache.DEFAULT_NAMESPACE, "write")) {
               return PRIVILEGE_WRITE;
  -        } else if (hasChild(privilege, NodeProperty.DEFAULT_NAMESPACE, "read-acl")) 
{
  +        } else if (hasChild(privilege, NamespaceCache.DEFAULT_NAMESPACE, 
"read-acl")) {
               return PRIVILEGE_READ_ACL;
  -        } else if (hasChild(privilege, NodeProperty.DEFAULT_NAMESPACE, 
"write-acl")) {
  +        } else if (hasChild(privilege, NamespaceCache.DEFAULT_NAMESPACE, 
"write-acl")) {
               return PRIVILEGE_WRITE_ACL;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"read-object")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "read-object")) {
               //            return PRIVILEGE_READ_OBJECT;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"read-revision-metadata")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "read-revision-metadata")) {
               //            return PRIVILEGE_READ_REVISION_METADATA;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"read-revision-content")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "read-revision-content")) {
               //            return PRIVILEGE_READ_REVISION_CONTENT;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"create-object")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "create-object")) {
               //            return PRIVILEGE_CREATE_OBJECT;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"remove-object")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "remove-object")) {
               //            return PRIVILEGE_REMOVE_OBJECT;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"lock-object")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "lock-object")) {
               //            return PRIVILEGE_LOCK_OBJECT;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"read-locks")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "read-locks")) {
               //            return PRIVILEGE_READ_LOCKS;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"create-revision-metadata")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "create-revision-metadata")) {
               //            return PRIVILEGE_CREATE_REVISION_METADATA;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"modify-revision-metadata")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "modify-revision-metadata")) {
               //            return PRIVILEGE_MODIFY_REVISION_METADATA;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"remove-revision-metadata")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "remove-revision-metadata")) {
               //            return PRIVILEGE_REMOVE_REVISION_METADATA;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"create-revision-content")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "create-revision-content")) {
               //            return PRIVILEGE_CREATE_REVISION_CONTENT;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"modify-revision-content")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "modify-revision-content")) {
               //            return PRIVILEGE_MODIFY_REVISION_CONTENT;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"remove-revision-content")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "remove-revision-content")) {
               //            return PRIVILEGE_REMOVE_REVISION_CONTENT;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"grant-permission")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "grant-permission")) {
               //            return PRIVILEGE_GRANT_PERMISSION;
  -            //        } else if (hasChild(privilege, SLIDE_NAMESPACE, 
"revoke-permission")) {
  +            //        } else if (hasChild(privilege, 
NamespaceCache.SLIDE_NAMESPACE, "revoke-permission")) {
               //            return PRIVILEGE_REVOKE_PERMISSION;
           } else {
               System.err.println("Error: Unknown privilege !!!");
  -            resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  -            throw new WebdavException(WebdavStatus.SC_BAD_REQUEST);
  +            throw new SAXException("Unknown privilege 
&lt;"+privilege.getName()+"&gt;");
           }
  -        
       }
       
       
       /**
        * Has specified child element ?
        */
  -    protected boolean hasChild(Element element, String namespace,
  -                               String name) {
  -        return
  -            (element.getElementsByTagNameNS(namespace, name).getLength() == 1);
  +    protected boolean hasChild(Element parent, Namespace namespace, String name) {
  +        return (parent.getChildren(name, namespace).size() == 1);
       }
       
  -    
  +    /**
  +     * Returns the child with the specified <code>name</code> and
  +     * <code>namespace</code> if one exists, otherwise <code>null</code>.
  +     *
  +     * @param      parent     the parent Element.
  +     * @param      namespace  the namespace of the child to return.
  +     * @param      name       the name of the child to return.
  +     *
  +     * @return     the child with the specified <code>name</code> and
  +     *             <code>namespace</code>.
  +     */
  +    protected Element getChild(Element parent, Namespace namespace, String name) {
  +        List childrenList = parent.getChildren(name, namespace);
  +        if (childrenList.size() > 0) {
  +            return (Element)childrenList.get(0);
  +        }
  +        else {
  +            return null;
  +        }
  +    }
       
       /**
        * Add permission to the list of permissions to set.
  
  
  

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

Reply via email to