Example usage for javax.naming.directory DirContext createSubcontext

List of usage examples for javax.naming.directory DirContext createSubcontext

Introduction

In this page you can find the example usage for javax.naming.directory DirContext createSubcontext.

Prototype

public Context createSubcontext(Name name) throws NamingException;

Source Link

Document

Creates and binds a new context.

Usage

From source file:com.concursive.connect.web.webdav.servlets.WebdavServlet.java

/**
 * Copy a collection./*w w  w  .j a v a 2s .co m*/
 *
 * @param resources Resources implementation to be used
 * @param errorList Hashtable containing the list of errors which occurred
 *                  during the copy operation
 * @param source    Path of the resource to be copied
 * @param dest      Destination path
 * @return Description of the Return Value
 */
private boolean copyResource(DirContext resources, Hashtable errorList, String source, String dest) {

    if (debug > 1) {
        System.out.println("Copy: " + source + " To: " + dest);
    }

    Object object = null;
    try {
        object = resources.lookup(source);
    } catch (NamingException e) {
    }

    if (object instanceof DirContext) {

        try {
            resources.createSubcontext(dest);
        } catch (NamingException e) {
            errorList.put(dest, new Integer(WebdavStatus.SC_CONFLICT));
            return false;
        }

        try {
            NamingEnumeration enum1 = resources.list(source);
            while (enum1.hasMoreElements()) {
                NameClassPair ncPair = (NameClassPair) enum1.nextElement();
                String childDest = dest;
                if (!childDest.equals("/")) {
                    childDest += "/";
                }
                childDest += ncPair.getName();
                String childSrc = source;
                if (!childSrc.equals("/")) {
                    childSrc += "/";
                }
                childSrc += ncPair.getName();
                copyResource(resources, errorList, childSrc, childDest);
            }
        } catch (NamingException e) {
            errorList.put(dest, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
            return false;
        }

    } else {

        if (object instanceof Resource) {
            try {
                resources.bind(dest, object);
            } catch (NamingException e) {
                errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
                return false;
            }
        } else {
            errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
            return false;
        }

    }

    return true;
}

From source file:com.concursive.connect.web.webdav.servlets.WebdavServlet.java

/**
 * MKCOL Method.//from ww  w.  j a v  a 2s  .co m
 *
 * @param req  Description of the Parameter
 * @param resp Description of the Parameter
 * @throws javax.servlet.ServletException Description of the Exception
 * @throws java.io.IOException            Description of the Exception
 */
protected void doMkcol(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    if (readOnly) {
        resp.sendError(WebdavStatus.SC_FORBIDDEN);
        return;
    }

    if (isLocked(req)) {
        resp.sendError(WebdavStatus.SC_LOCKED);
        return;
    }

    String path = getRelativePath(req);

    if ((path.toUpperCase().startsWith("/WEB-INF")) || (path.toUpperCase().startsWith("/META-INF"))) {
        resp.sendError(WebdavStatus.SC_FORBIDDEN);
        return;
    }

    // Retrieve the resources
    DirContext resources = getResources();

    if (resources == null) {
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }

    boolean exists = true;
    Object object = null;
    try {
        object = resources.lookup(path);
    } catch (NamingException e) {
        exists = false;
    }

    // Can't create a collection if a resource already exists at the given
    // path
    if (exists) {
        // Get allowed methods
        StringBuffer methodsAllowed = determineMethodsAllowed(resources, req);

        resp.addHeader("Allow", methodsAllowed.toString());

        resp.sendError(WebdavStatus.SC_METHOD_NOT_ALLOWED);
        return;
    }

    if (req.getInputStream().available() > 0) {
        DocumentBuilder documentBuilder = getDocumentBuilder();
        try {
            Document document = documentBuilder.parse(new InputSource(req.getInputStream()));
            // TODO : Process this request body
            resp.sendError(WebdavStatus.SC_NOT_IMPLEMENTED);
            return;
        } catch (SAXException saxe) {
            // Parse error - assume invalid content
            resp.sendError(WebdavStatus.SC_BAD_REQUEST);
            return;
        }
    }

    boolean result = true;
    try {
        resources.createSubcontext(path);
    } catch (NamingException e) {
        result = false;
    }

    if (!result) {
        resp.sendError(WebdavStatus.SC_CONFLICT, WebdavStatus.getStatusText(WebdavStatus.SC_CONFLICT));
    } else {
        resp.setStatus(WebdavStatus.SC_CREATED);
        // Removing any lock-null resource which would be present
        lockNullResources.remove(path);
    }

}