juergen     02/05/27 05:36:17

  Modified:    src/webdav/server/org/apache/slide/webdav/method
                        LockMethod.java
  Log:
  Replaced usage of org.w3c.dom by JDOM.
  (ralf)
  
  Revision  Changes    Path
  1.35      +123 -117  
jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/LockMethod.java
  
  Index: LockMethod.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/LockMethod.java,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- LockMethod.java   17 May 2002 11:54:32 -0000      1.34
  +++ LockMethod.java   27 May 2002 12:36:17 -0000      1.35
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/LockMethod.java,v
 1.34 2002/05/17 11:54:32 juergen Exp $
  - * $Revision: 1.34 $
  - * $Date: 2002/05/17 11:54:32 $
  + * $Header: 
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/LockMethod.java,v
 1.35 2002/05/27 12:36:17 juergen Exp $
  + * $Revision: 1.35 $
  + * $Date: 2002/05/27 12:36:17 $
    *
    * ====================================================================
    *
  @@ -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.PropertyWriter;
  @@ -87,6 +86,11 @@
   import org.apache.slide.webdav.util.PropertyHelper;
   import org.apache.slide.webdav.util.XMLValue;
   
  +import org.jdom.Document;
  +import org.jdom.Element;
  +
  +import org.jdom.output.XMLOutputter;
  +
   /**
    * LOCK method.
    *
  @@ -270,155 +274,157 @@
           }
           
           if (req.getContentLength() > 0) {
  +            parseLockInfo();
  +        }
  +        else {
  +            lockType = LOCK_REFRESH;
  +        }
  +        
  +    }
  +    
  +    /**
  +     * Parses the <code>&lt;lockinfo&gt;</code> request content document.
  +     *
  +     * @throws     WebdavException  if parsing the request failed or if
  +     *                              the request is not valid.
  +     */
  +    private void parseLockInfo() throws WebdavException {
               
               lockType = LOCK_CREATION;
               
  -            Node lockInfoNode = null;
  -            
               try {
  -                Document document = parseRequestContent();
  +            retrieveRequestContent();
  +            Document document = getRequestContent();
  +            if (document == null) {
  +                throw new SAXException("Request content missing");
  +            }
                   
  -                // Get the root element of the document
  -                Element rootElement = document.getDocumentElement();
  -                lockInfoNode = rootElement;
  -            } catch (SAXException e) {
  -                resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  -                throw new WebdavException(WebdavStatus.SC_BAD_REQUEST);
  -            } catch (ParserConfigurationException e) {
  -                System.err.println(e.getMessage());
  -                resp.setStatus(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
  -                throw new WebdavException
  -                    (WebdavStatus.SC_INTERNAL_SERVER_ERROR);
  -            } catch (IOException e) {
  -                System.err.println(e.getMessage());
  -                e.printStackTrace();
  -                resp.setStatus(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
  -                throw new WebdavException
  -                    (WebdavStatus.SC_INTERNAL_SERVER_ERROR);
  +            Element lockInfoElement = document.getRootElement();
  +            if ( (lockInfoElement == null) ||
  +                    ( ! E_LOCKINFO.equals(lockInfoElement.getName()) ) ) {
  +                throw new SAXException("Request must start with 
&lt;"+E_LOCKINFO+"&gt; element");
               }
               
  -            NodeList childList = lockInfoNode.getChildNodes();
               StringWriter strWriter = null;
               PropertyWriter propertyWriter = null;
               
  -            Node lockScopeNode = null;
  -            Node lockTypeNode = null;
  -            Node lockOwnerNode = null;
  -            
  -            for (int i=0; i < childList.getLength(); i++) {
  -                Node currentNode = childList.item(i);
  -                switch (currentNode.getNodeType()) {
  -                case Node.TEXT_NODE:
  -                    break;
  -                case Node.ELEMENT_NODE:
  -                    String nodeName = currentNode.getNodeName();
  -                    if (nodeName.endsWith("lockscope")) {
  -                        lockScopeNode = currentNode;
  -                    }
  -                    if (nodeName.endsWith("locktype")) {
  -                        lockTypeNode = currentNode;
  +            Element lockScopeElement = null;
  +            Element lockTypeElement = null;
  +            Element lockOwnerElement = null;
  +            
  +            Iterator childrenIterator = lockInfoElement.getChildren().iterator();
  +            Element currentElement = null;
  +            while (childrenIterator.hasNext()) {
  +                currentElement = (Element)childrenIterator.next();
  +                if (E_LOCKSCOPE.equals(currentElement.getName())) {
  +                    lockScopeElement = currentElement;
                       }
  -                    if (nodeName.endsWith("owner")) {
  -                        lockOwnerNode = currentNode;
  +                else if (E_LOCKTYPE.equals(currentElement.getName())) {
  +                    lockTypeElement = currentElement;
                       }
  -                    break;
  +                else if (E_OWNER.equals(currentElement.getName())) {
  +                    lockOwnerElement = currentElement;
                   }
               }
               
  -            if (lockScopeNode != null) {
  -                
  -                childList = lockScopeNode.getChildNodes();
  -                for (int i=0; i < childList.getLength(); i++) {
  -                    Node currentNode = childList.item(i);
  -                    switch (currentNode.getNodeType()) {
  -                    case Node.TEXT_NODE:
  -                        break;
  -                    case Node.ELEMENT_NODE:
  -                        String tempScope = currentNode.getNodeName();
  -                        if (tempScope.indexOf(':') != -1) {
  -                            lockInfo_lockScope = "<"
  -                                + tempScope.substring(tempScope.indexOf(':')
  -                                                      + 1) + "/>";
  -                        } else {
  -                            lockInfo_lockScope = "<" + tempScope + "/>";
  +            parseLockScope(lockScopeElement);
  +            parseLockType(lockTypeElement);
  +            parseOwner(lockOwnerElement);
                           }
  -//System.out.println("Lock scope : " + lockInfo_lockScope);
  -                        break;
  +        catch (SAXException e) {
  +            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 (IOException e) {
  +            resp.setStatus(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
  +            throw new WebdavException(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
                   }
  -                
  -                if (lockInfo_lockScope == null) {
  -                    // Bad request
  -                    resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  +        catch (ParserConfigurationException e) {
  +            resp.setStatus(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
  +            throw new WebdavException(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
                   }
  -                
  -            } else {
  -                // Bad request
  -                resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
               }
               
  -            if (lockTypeNode != null) {
  +    /**
  +     * Parses the <code>&lt;lockscope&gt;</code> part of the request content
  +     * document.
  +     *
  +     * @param      lockScopeElement  the <code>&lt;lockscope&gt;</code> to parse.
  +     *
  +     * @throws     SAXException  if parsing the request failed or if
  +     *                           the request is not valid.
  +     */
  +    private void parseLockScope(Element lockScopeElement) throws SAXException {
                   
  -                childList = lockTypeNode.getChildNodes();
  -                for (int i=0; i < childList.getLength(); i++) {
  -                    Node currentNode = childList.item(i);
  -                    switch (currentNode.getNodeType()) {
  -                    case Node.TEXT_NODE:
  -                        break;
  -                    case Node.ELEMENT_NODE:
  -                        String tempType = currentNode.getNodeName();
  -                        if (tempType.indexOf(':') != -1) {
  -                            lockInfo_lockType = "<"
  -                                + tempType.substring(tempType.indexOf(':') + 1)
  -                                + "/>";
  -                        } else {
  -                            lockInfo_lockType = "<" + tempType + "/>";
  -                        }
  -//System.out.println("Lock type : " + lockInfo_lockType);
  -                        break;
  -                    }
  +        if (lockScopeElement == null) {
  +            throw new SAXException("Expected &lt;"+E_LOCKSCOPE+"&gt; element");
                   }
                   
  -                if (lockInfo_lockType == null) {
  -                    // Bad request
  -                    resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  +        List children = lockScopeElement.getChildren();
  +        if (children.size() != 1) {
  +            throw new SAXException("&lt;"+E_LOCKSCOPE+"&gt; must have exactly one 
child element");
                   }
                   
  -            } else {
  -                // Bad request
  -                resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  +        lockInfo_lockScope = ((Element)children.get(0)).getName();
               }
               
  -            if (lockOwnerNode != null) {
  +    /**
  +     * Parses the <code>&lt;locktype&gt;</code> part of the request content
  +     * document.
  +     *
  +     * @param      lockTypeElement  the <code>&lt;locktype&gt;</code> to parse.
  +     *
  +     * @throws     SAXException  if parsing the request failed or if
  +     *                           the request is not valid.
  +     */
  +    private void parseLockType(Element lockTypeElement) throws SAXException {
                   
  -                childList = lockOwnerNode.getChildNodes();
  -                for (int i=0; i < childList.getLength(); i++) {
  -                    Node currentNode = childList.item(i);
  -                    switch (currentNode.getNodeType()) {
  -                    case Node.TEXT_NODE:
  -                        break;
  -                    case Node.ELEMENT_NODE:
  -                        strWriter = new StringWriter();
  -                        propertyWriter = new PropertyWriter(strWriter, true);
  -                        propertyWriter.print(currentNode);
  -                        lockInfo_lockOwner = strWriter.toString();
  -                        break;
  +        if (lockTypeElement == null) {
  +            throw new SAXException("Expected &lt;"+E_LOCKTYPE+"&gt; element");
                       }
  +        
  +        List children = lockTypeElement.getChildren();
  +        if (children.size() != 1) {
  +            throw new SAXException("&lt;"+E_LOCKTYPE+"&gt; must have exactly one 
child element");
                   }
                   
  -                if (lockInfo_lockScope == null) {
  -                    // Bad request
  -                    resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
  +        lockInfo_lockType = ((Element)children.get(0)).getName();
                   }
                   
  -            } else {
  -                lockInfo_lockOwner = new String();
  +    /**
  +     * Parses the <code>&lt;owner&gt;</code> part of the request content
  +     * document.
  +     *
  +     * @param      ownerElement  the <code>&lt;owner&gt;</code> to parse.
  +     *
  +     * @throws     SAXException  if parsing the request failed or if
  +     *                           the request is not valid.
  +     */
  +    private void parseOwner(Element ownerElement) throws SAXException {
  +        
  +        if (ownerElement == null) {
  +            throw new SAXException("Expected &lt;"+E_OWNER+"&gt; element");
               }
               
  -        } else {
  -            lockType = LOCK_REFRESH;
  +        StringWriter stringWriter = new StringWriter();
  +        XMLOutputter xmlOutputter = new XMLOutputter();
  +        try {
  +            xmlOutputter.outputElementContent(ownerElement, stringWriter);
  +        }
  +        catch (IOException e) {
  +            // this should not happen since we do no "real" I/O but
  +            // only print to a PrintWriter
  +            e.printStackTrace();
  +        }
  +        lockInfo_lockOwner = stringWriter.toString();
  +
  +        if (lockInfo_lockOwner.length() == 0) {
  +            throw new SAXException("&lt;"+E_OWNER+"&gt; element must not be empty");
           }
  -        
       }
       
       
  @@ -488,7 +494,7 @@
                   
                   inheritance = (depth != 0);
                   boolean exclusive =
  -                    !(lockInfo_lockScope.equals("<shared/>"));
  +                        !(lockInfo_lockScope.equals(E_SHARED));
                   
                   if (lockDate == null)
                       lockDate = new Date((new Date()).getTime()
  
  
  

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

Reply via email to