Search in sources :

Example 6 with XMLWriter

use of org.apache.catalina.util.XMLWriter in project tomcat70 by apache.

the class WebdavStatus method sendReport.

/**
 * Send a multistatus element containing a complete error report to the
 * client.
 *
 * @param req Servlet request
 * @param resp Servlet response
 * @param errorList List of error to be displayed
 */
private void sendReport(HttpServletRequest req, HttpServletResponse resp, Hashtable<String, Integer> errorList) throws IOException {
    resp.setStatus(WebdavStatus.SC_MULTI_STATUS);
    String absoluteUri = req.getRequestURI();
    String relativePath = getRelativePath(req);
    XMLWriter generatedXML = new XMLWriter();
    generatedXML.writeXMLHeader();
    generatedXML.writeElement("D", DEFAULT_NAMESPACE, "multistatus", XMLWriter.OPENING);
    Enumeration<String> pathList = errorList.keys();
    while (pathList.hasMoreElements()) {
        String errorPath = pathList.nextElement();
        int errorCode = errorList.get(errorPath).intValue();
        generatedXML.writeElement("D", "response", XMLWriter.OPENING);
        generatedXML.writeElement("D", "href", XMLWriter.OPENING);
        String toAppend = errorPath.substring(relativePath.length());
        if (!toAppend.startsWith("/"))
            toAppend = "/" + toAppend;
        generatedXML.writeText(absoluteUri + toAppend);
        generatedXML.writeElement("D", "href", XMLWriter.CLOSING);
        generatedXML.writeElement("D", "status", XMLWriter.OPENING);
        generatedXML.writeText("HTTP/1.1 " + errorCode + " " + WebdavStatus.getStatusText(errorCode));
        generatedXML.writeElement("D", "status", XMLWriter.CLOSING);
        generatedXML.writeElement("D", "response", XMLWriter.CLOSING);
    }
    generatedXML.writeElement("D", "multistatus", XMLWriter.CLOSING);
    Writer writer = resp.getWriter();
    writer.write(generatedXML.toString());
    writer.close();
}
Also used : XMLWriter(org.apache.catalina.util.XMLWriter) XMLWriter(org.apache.catalina.util.XMLWriter) DOMWriter(org.apache.catalina.util.DOMWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 7 with XMLWriter

use of org.apache.catalina.util.XMLWriter in project tomcat70 by apache.

the class WebdavStatus method doPropfind.

/**
 * PROPFIND Method.
 */
protected void doPropfind(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (!listings) {
        // Get allowed methods
        StringBuilder methodsAllowed = determineMethodsAllowed(resources, req);
        resp.addHeader("Allow", methodsAllowed.toString());
        resp.sendError(WebdavStatus.SC_METHOD_NOT_ALLOWED);
        return;
    }
    String path = getRelativePath(req);
    if (path.endsWith("/"))
        path = path.substring(0, path.length() - 1);
    // Properties which are to be displayed.
    Vector<String> properties = null;
    // Propfind depth
    int depth = maxDepth;
    // Propfind type
    int type = FIND_ALL_PROP;
    String depthStr = req.getHeader("Depth");
    if (depthStr == null) {
        depth = maxDepth;
    } else {
        if (depthStr.equals("0")) {
            depth = 0;
        } else if (depthStr.equals("1")) {
            depth = 1;
        } else if (depthStr.equals("infinity")) {
            depth = maxDepth;
        }
    }
    Node propNode = null;
    if (req.getContentLength() > 0) {
        DocumentBuilder documentBuilder = getDocumentBuilder();
        try {
            Document document = documentBuilder.parse(new InputSource(req.getInputStream()));
            // Get the root element of the document
            Element rootElement = document.getDocumentElement();
            NodeList childList = rootElement.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:
                        if (currentNode.getNodeName().endsWith("prop")) {
                            type = FIND_BY_PROPERTY;
                            propNode = currentNode;
                        }
                        if (currentNode.getNodeName().endsWith("propname")) {
                            type = FIND_PROPERTY_NAMES;
                        }
                        if (currentNode.getNodeName().endsWith("allprop")) {
                            type = FIND_ALL_PROP;
                        }
                        break;
                }
            }
        } catch (SAXException e) {
            // Something went wrong - bad request
            resp.sendError(WebdavStatus.SC_BAD_REQUEST);
            return;
        } catch (IOException e) {
            // Something went wrong - bad request
            resp.sendError(WebdavStatus.SC_BAD_REQUEST);
            return;
        }
    }
    if (type == FIND_BY_PROPERTY) {
        properties = new Vector<String>();
        // propNode must be non-null if type == FIND_BY_PROPERTY
        @SuppressWarnings("null") NodeList childList = propNode.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 nodeName = currentNode.getNodeName();
                    String propertyName = null;
                    if (nodeName.indexOf(':') != -1) {
                        propertyName = nodeName.substring(nodeName.indexOf(':') + 1);
                    } else {
                        propertyName = nodeName;
                    }
                    // href is a live property which is handled differently
                    properties.addElement(propertyName);
                    break;
            }
        }
    }
    boolean exists = true;
    Object object = null;
    try {
        object = resources.lookup(path);
    } catch (NamingException e) {
        exists = false;
        int slash = path.lastIndexOf('/');
        if (slash != -1) {
            String parentPath = path.substring(0, slash);
            Vector<String> currentLockNullResources = lockNullResources.get(parentPath);
            if (currentLockNullResources != null) {
                Enumeration<String> lockNullResourcesList = currentLockNullResources.elements();
                while (lockNullResourcesList.hasMoreElements()) {
                    String lockNullPath = lockNullResourcesList.nextElement();
                    if (lockNullPath.equals(path)) {
                        resp.setStatus(WebdavStatus.SC_MULTI_STATUS);
                        resp.setContentType("text/xml; charset=UTF-8");
                        // Create multistatus object
                        XMLWriter generatedXML = new XMLWriter(resp.getWriter());
                        generatedXML.writeXMLHeader();
                        generatedXML.writeElement("D", DEFAULT_NAMESPACE, "multistatus", XMLWriter.OPENING);
                        parseLockNullProperties(req, generatedXML, lockNullPath, type, properties);
                        generatedXML.writeElement("D", "multistatus", XMLWriter.CLOSING);
                        generatedXML.sendData();
                        return;
                    }
                }
            }
        }
    }
    if (!exists) {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND, path);
        return;
    }
    resp.setStatus(WebdavStatus.SC_MULTI_STATUS);
    resp.setContentType("text/xml; charset=UTF-8");
    // Create multistatus object
    XMLWriter generatedXML = new XMLWriter(resp.getWriter());
    generatedXML.writeXMLHeader();
    generatedXML.writeElement("D", DEFAULT_NAMESPACE, "multistatus", XMLWriter.OPENING);
    if (depth == 0) {
        parseProperties(req, generatedXML, path, type, properties);
    } else {
        // The stack always contains the object of the current level
        Stack<String> stack = new Stack<String>();
        stack.push(path);
        // Stack of the objects one level below
        Stack<String> stackBelow = new Stack<String>();
        while ((!stack.isEmpty()) && (depth >= 0)) {
            String currentPath = stack.pop();
            parseProperties(req, generatedXML, currentPath, type, properties);
            try {
                object = resources.lookup(currentPath);
            } catch (NamingException e) {
                continue;
            }
            if ((object instanceof DirContext) && (depth > 0)) {
                try {
                    NamingEnumeration<NameClassPair> enumeration = resources.list(currentPath);
                    while (enumeration.hasMoreElements()) {
                        NameClassPair ncPair = enumeration.nextElement();
                        String newPath = currentPath;
                        if (!(newPath.endsWith("/")))
                            newPath += "/";
                        newPath += ncPair.getName();
                        stackBelow.push(newPath);
                    }
                } catch (NamingException e) {
                    resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path);
                    return;
                }
                // Displaying the lock-null resources present in that
                // collection
                String lockPath = currentPath;
                if (lockPath.endsWith("/"))
                    lockPath = lockPath.substring(0, lockPath.length() - 1);
                Vector<String> currentLockNullResources = lockNullResources.get(lockPath);
                if (currentLockNullResources != null) {
                    Enumeration<String> lockNullResourcesList = currentLockNullResources.elements();
                    while (lockNullResourcesList.hasMoreElements()) {
                        String lockNullPath = lockNullResourcesList.nextElement();
                        parseLockNullProperties(req, generatedXML, lockNullPath, type, properties);
                    }
                }
            }
            if (stack.isEmpty()) {
                depth--;
                stack = stackBelow;
                stackBelow = new Stack<String>();
            }
            generatedXML.sendData();
        }
    }
    generatedXML.writeElement("D", "multistatus", XMLWriter.CLOSING);
    generatedXML.sendData();
}
Also used : InputSource(org.xml.sax.InputSource) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) DirContext(javax.naming.directory.DirContext) Document(org.w3c.dom.Document) XMLWriter(org.apache.catalina.util.XMLWriter) SAXException(org.xml.sax.SAXException) NamingException(javax.naming.NamingException) Vector(java.util.Vector) Enumeration(java.util.Enumeration) NamingEnumeration(javax.naming.NamingEnumeration) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) Stack(java.util.Stack) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NameClassPair(javax.naming.NameClassPair)

Example 8 with XMLWriter

use of org.apache.catalina.util.XMLWriter in project tomcat by apache.

the class WebdavStatus method sendReport.

/**
 * Send a multistatus element containing a complete error report to the
 * client.
 *
 * @param req Servlet request
 * @param resp Servlet response
 * @param errorList List of error to be displayed
 *
 * @throws IOException If an IO error occurs
 */
private void sendReport(HttpServletRequest req, HttpServletResponse resp, Hashtable<String, Integer> errorList) throws IOException {
    resp.setStatus(WebdavStatus.SC_MULTI_STATUS);
    XMLWriter generatedXML = new XMLWriter();
    generatedXML.writeXMLHeader();
    generatedXML.writeElement("D", DEFAULT_NAMESPACE, "multistatus", XMLWriter.OPENING);
    Enumeration<String> pathList = errorList.keys();
    while (pathList.hasMoreElements()) {
        String errorPath = pathList.nextElement();
        int errorCode = errorList.get(errorPath).intValue();
        generatedXML.writeElement("D", "response", XMLWriter.OPENING);
        generatedXML.writeElement("D", "href", XMLWriter.OPENING);
        generatedXML.writeText(getServletContext().getContextPath() + errorPath);
        generatedXML.writeElement("D", "href", XMLWriter.CLOSING);
        generatedXML.writeElement("D", "status", XMLWriter.OPENING);
        generatedXML.writeText("HTTP/1.1 " + errorCode + " ");
        generatedXML.writeElement("D", "status", XMLWriter.CLOSING);
        generatedXML.writeElement("D", "response", XMLWriter.CLOSING);
    }
    generatedXML.writeElement("D", "multistatus", XMLWriter.CLOSING);
    Writer writer = resp.getWriter();
    writer.write(generatedXML.toString());
    writer.close();
}
Also used : XMLWriter(org.apache.catalina.util.XMLWriter) XMLWriter(org.apache.catalina.util.XMLWriter) DOMWriter(org.apache.catalina.util.DOMWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 9 with XMLWriter

use of org.apache.catalina.util.XMLWriter in project tomcat by apache.

the class WebdavStatus method doPropfind.

/**
 * PROPFIND Method.
 * @param req The Servlet request
 * @param resp The Servlet response
 * @throws ServletException If an error occurs
 * @throws IOException If an IO error occurs
 */
protected void doPropfind(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (!listings) {
        sendNotAllowed(req, resp);
        return;
    }
    String path = getRelativePath(req);
    if (path.length() > 1 && path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }
    // Properties which are to be displayed.
    Vector<String> properties = null;
    // Propfind depth
    int depth = maxDepth;
    // Propfind type
    int type = FIND_ALL_PROP;
    String depthStr = req.getHeader("Depth");
    if (depthStr == null) {
        depth = maxDepth;
    } else {
        if (depthStr.equals("0")) {
            depth = 0;
        } else if (depthStr.equals("1")) {
            depth = 1;
        } else if (depthStr.equals("infinity")) {
            depth = maxDepth;
        }
    }
    Node propNode = null;
    if (req.getContentLengthLong() > 0) {
        DocumentBuilder documentBuilder = getDocumentBuilder();
        try {
            Document document = documentBuilder.parse(new InputSource(req.getInputStream()));
            // Get the root element of the document
            Element rootElement = document.getDocumentElement();
            NodeList childList = rootElement.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:
                        if (currentNode.getNodeName().endsWith("prop")) {
                            type = FIND_BY_PROPERTY;
                            propNode = currentNode;
                        }
                        if (currentNode.getNodeName().endsWith("propname")) {
                            type = FIND_PROPERTY_NAMES;
                        }
                        if (currentNode.getNodeName().endsWith("allprop")) {
                            type = FIND_ALL_PROP;
                        }
                        break;
                }
            }
        } catch (SAXException | IOException e) {
            // Something went wrong - bad request
            resp.sendError(WebdavStatus.SC_BAD_REQUEST);
            return;
        }
    }
    if (type == FIND_BY_PROPERTY) {
        properties = new Vector<>();
        // propNode must be non-null if type == FIND_BY_PROPERTY
        @SuppressWarnings("null") NodeList childList = propNode.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 nodeName = currentNode.getNodeName();
                    String propertyName = null;
                    if (nodeName.indexOf(':') != -1) {
                        propertyName = nodeName.substring(nodeName.indexOf(':') + 1);
                    } else {
                        propertyName = nodeName;
                    }
                    // href is a live property which is handled differently
                    properties.addElement(propertyName);
                    break;
            }
        }
    }
    WebResource resource = resources.getResource(path);
    if (!resource.exists()) {
        int slash = path.lastIndexOf('/');
        if (slash != -1) {
            String parentPath = path.substring(0, slash);
            Vector<String> currentLockNullResources = lockNullResources.get(parentPath);
            if (currentLockNullResources != null) {
                Enumeration<String> lockNullResourcesList = currentLockNullResources.elements();
                while (lockNullResourcesList.hasMoreElements()) {
                    String lockNullPath = lockNullResourcesList.nextElement();
                    if (lockNullPath.equals(path)) {
                        resp.setStatus(WebdavStatus.SC_MULTI_STATUS);
                        resp.setContentType("text/xml; charset=UTF-8");
                        // Create multistatus object
                        XMLWriter generatedXML = new XMLWriter(resp.getWriter());
                        generatedXML.writeXMLHeader();
                        generatedXML.writeElement("D", DEFAULT_NAMESPACE, "multistatus", XMLWriter.OPENING);
                        parseLockNullProperties(req, generatedXML, lockNullPath, type, properties);
                        generatedXML.writeElement("D", "multistatus", XMLWriter.CLOSING);
                        generatedXML.sendData();
                        return;
                    }
                }
            }
        }
    }
    if (!resource.exists()) {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    resp.setStatus(WebdavStatus.SC_MULTI_STATUS);
    resp.setContentType("text/xml; charset=UTF-8");
    // Create multistatus object
    XMLWriter generatedXML = new XMLWriter(resp.getWriter());
    generatedXML.writeXMLHeader();
    generatedXML.writeElement("D", DEFAULT_NAMESPACE, "multistatus", XMLWriter.OPENING);
    if (depth == 0) {
        parseProperties(req, generatedXML, path, type, properties);
    } else {
        // The stack always contains the object of the current level
        Stack<String> stack = new Stack<>();
        stack.push(path);
        // Stack of the objects one level below
        Stack<String> stackBelow = new Stack<>();
        while ((!stack.isEmpty()) && (depth >= 0)) {
            String currentPath = stack.pop();
            parseProperties(req, generatedXML, currentPath, type, properties);
            resource = resources.getResource(currentPath);
            if (resource.isDirectory() && (depth > 0)) {
                String[] entries = resources.list(currentPath);
                for (String entry : entries) {
                    String newPath = currentPath;
                    if (!(newPath.endsWith("/"))) {
                        newPath += "/";
                    }
                    newPath += entry;
                    stackBelow.push(newPath);
                }
                // Displaying the lock-null resources present in that
                // collection
                String lockPath = currentPath;
                if (lockPath.endsWith("/")) {
                    lockPath = lockPath.substring(0, lockPath.length() - 1);
                }
                Vector<String> currentLockNullResources = lockNullResources.get(lockPath);
                if (currentLockNullResources != null) {
                    Enumeration<String> lockNullResourcesList = currentLockNullResources.elements();
                    while (lockNullResourcesList.hasMoreElements()) {
                        String lockNullPath = lockNullResourcesList.nextElement();
                        parseLockNullProperties(req, generatedXML, lockNullPath, type, properties);
                    }
                }
            }
            if (stack.isEmpty()) {
                depth--;
                stack = stackBelow;
                stackBelow = new Stack<>();
            }
            generatedXML.sendData();
        }
    }
    generatedXML.writeElement("D", "multistatus", XMLWriter.CLOSING);
    generatedXML.sendData();
}
Also used : InputSource(org.xml.sax.InputSource) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) WebResource(org.apache.catalina.WebResource) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLWriter(org.apache.catalina.util.XMLWriter) SAXException(org.xml.sax.SAXException) Stack(java.util.Stack) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Aggregations

XMLWriter (org.apache.catalina.util.XMLWriter)9 IOException (java.io.IOException)6 StringWriter (java.io.StringWriter)6 Writer (java.io.Writer)6 DocumentBuilder (javax.xml.parsers.DocumentBuilder)6 DOMWriter (org.apache.catalina.util.DOMWriter)6 Document (org.w3c.dom.Document)6 Element (org.w3c.dom.Element)6 Node (org.w3c.dom.Node)6 NodeList (org.w3c.dom.NodeList)6 InputSource (org.xml.sax.InputSource)6 SAXException (org.xml.sax.SAXException)6 Vector (java.util.Vector)5 NamingException (javax.naming.NamingException)4 DirContext (javax.naming.directory.DirContext)4 Stack (java.util.Stack)3 Enumeration (java.util.Enumeration)2 NameClassPair (javax.naming.NameClassPair)2 NamingEnumeration (javax.naming.NamingEnumeration)2 WebResource (org.apache.catalina.WebResource)2