Package javax.naming.directory

Examples of javax.naming.directory.DirContext


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

        // Check if operation allowed by OLAT VFS security callback
        DirContext resources = getResources(req);
        VFSDirContext vfsContext = (VFSDirContext) resources;
        String destinationPath = req.getHeader("Destination");
        if (!vfsContext.canWrite(destinationPath)) {
          resp.sendError(WebdavStatus.SC_FORBIDDEN);
          return;         
View Full Code Here


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

        // Check if operation allowed by OLAT VFS security callback
        DirContext resources = getResources(req);
        VFSDirContext vfsContext = (VFSDirContext) resources;
        String path = getRelativePath(req);
        if (!vfsContext.canRename(path)) {
          resp.sendError(WebdavStatus.SC_FORBIDDEN);
          return;         
View Full Code Here

            resp.sendError(WebdavStatus.SC_FORBIDDEN);
            return;
        }
       
        // Check if operation allowed by OLAT VFS security callback
        DirContext resources = getResources(req);
        VFSDirContext vfsContext = (VFSDirContext) resources;
        String path = getRelativePath(req);
        if (!vfsContext.canWrite(path)) {
          resp.sendError(WebdavStatus.SC_FORBIDDEN);
          return;         
        }


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

        LockInfo lock = new LockInfo();

        // Parsing lock request

        // Parsing depth header

        String depthStr = req.getHeader("Depth");

        if (depthStr == null) {
            lock.depth = INFINITY;
        } else {
            if (depthStr.equals("0")) {
                lock.depth = 0;
            } else {
                lock.depth = INFINITY;
            }
        }

        // Parsing timeout header

        int lockDuration = DEFAULT_TIMEOUT;
        String lockDurationStr = req.getHeader("Timeout");
        if (lockDurationStr == null) {
            lockDuration = DEFAULT_TIMEOUT;
        } else {
            if (lockDurationStr.startsWith("Second-")) {
                lockDuration =
                    (new Integer(lockDurationStr.substring(7))).intValue();
            } else {
                if (lockDurationStr.equalsIgnoreCase("infinity")) {
                    lockDuration = MAX_TIMEOUT;
                } else {
                    try {
                        lockDuration =
                            (new Integer(lockDurationStr)).intValue();
                    } catch (NumberFormatException e) {
                        lockDuration = MAX_TIMEOUT;
                    }
                }
            }
            if (lockDuration == 0) {
                lockDuration = DEFAULT_TIMEOUT;
            }
            if (lockDuration > MAX_TIMEOUT) {
                lockDuration = MAX_TIMEOUT;
            }
        }
        lock.expiresAt = System.currentTimeMillis() + (lockDuration * 1000);

        int lockRequestType = LOCK_CREATION;

        Node lockInfoNode = null;

        DocumentBuilder documentBuilder = getDocumentBuilder();

        try {
            Document document = documentBuilder.parse(new InputSource
                (req.getInputStream()));

            // Get the root element of the document
            Element rootElement = document.getDocumentElement();
            lockInfoNode = rootElement;
        } catch(Exception e) {
            lockRequestType = LOCK_REFRESH;
        }

        if (lockInfoNode != null) {

            // Reading lock information

            NodeList childList = lockInfoNode.getChildNodes();
            StringWriter strWriter = null;
            DOMWriter domWriter = 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;
                    }
                    if (nodeName.endsWith("owner")) {
                        lockOwnerNode = currentNode;
                    }
                    break;
                }
            }

            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) {
                            lock.scope = tempScope.substring
                                (tempScope.indexOf(':') + 1);
                        } else {
                            lock.scope = tempScope;
                        }
                        break;
                    }
                }

                if (lock.scope == null) {
                    // Bad request
                    resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
                }

            } else {
                // Bad request
                resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
            }

            if (lockTypeNode != null) {

                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) {
                            lock.type =
                                tempType.substring(tempType.indexOf(':') + 1);
                        } else {
                            lock.type = tempType;
                        }
                        break;
                    }
                }

                if (lock.type == null) {
                    // Bad request
                    resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
                }

            } else {
                // Bad request
                resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
            }

            if (lockOwnerNode != null) {

                childList = lockOwnerNode.getChildNodes();
                for (int i=0; i < childList.getLength(); i++) {
                    Node currentNode = childList.item(i);
                    switch (currentNode.getNodeType()) {
                    case Node.TEXT_NODE:
                        lock.owner += currentNode.getNodeValue();
                        break;
                    case Node.ELEMENT_NODE:
                        strWriter = new StringWriter();
                        domWriter = new DOMWriter(strWriter, true);
                        domWriter.print(currentNode);
                        lock.owner += strWriter.toString();
                        break;
                    }
                }

                if (lock.owner == null) {
                    // Bad request
                    resp.setStatus(WebdavStatus.SC_BAD_REQUEST);
                }

            } else {
                lock.owner = new String();
            }

        }

        lock.path = path;

        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;
        }

        Enumeration locksList = null;

        if (lockRequestType == LOCK_CREATION) {

            // Generating lock id
            String lockTokenStr = req.getServletPath() + "-" + lock.type + "-"
                + lock.scope + "-" + req.getUserPrincipal() + "-"
                + lock.depth + "-" + lock.owner + "-" + lock.tokens + "-"
                + lock.expiresAt + "-" + System.currentTimeMillis() + "-"
                + secret;
            String lockToken =
                md5Encoder.encode(md5Helper.digest(lockTokenStr.getBytes()));

            if ( (exists) && (object instanceof DirContext) &&
                 (lock.depth == INFINITY) ) {

                // Locking a collection (and all its member resources)

                // Checking if a child resource of this collection is
                // already locked
                Vector lockPaths = new Vector();
                locksList = collectionLocks.elements();
                while (locksList.hasMoreElements()) {
                    LockInfo currentLock = (LockInfo) locksList.nextElement();
                    if (currentLock.hasExpired()) {
                        resourceLocks.remove(currentLock.path);
                        continue;
                    }
                    if ( (currentLock.path.startsWith(lock.path)) &&
                         ((currentLock.isExclusive()) ||
                          (lock.isExclusive())) ) {
                        // A child collection of this collection is locked
                        lockPaths.addElement(currentLock.path);
                    }
                }
                locksList = resourceLocks.elements();
                while (locksList.hasMoreElements()) {
                    LockInfo currentLock = (LockInfo) locksList.nextElement();
                    if (currentLock.hasExpired()) {
                        resourceLocks.remove(currentLock.path);
                        continue;
                    }
                    if ( (currentLock.path.startsWith(lock.path)) &&
                         ((currentLock.isExclusive()) ||
                          (lock.isExclusive())) ) {
                        // A child resource of this collection is locked
                        lockPaths.addElement(currentLock.path);
                    }
                }

                if (!lockPaths.isEmpty()) {

                    // One of the child paths was locked
                    // We generate a multistatus error report

                    Enumeration lockPathsList = lockPaths.elements();

                    resp.setStatus(WebdavStatus.SC_CONFLICT);

                    XMLWriter generatedXML = new XMLWriter();
                    generatedXML.writeXMLHeader();

                    generatedXML.writeElement
                        (null, "multistatus" + generateNamespaceDeclarations(),
                         XMLWriter.OPENING);

                    while (lockPathsList.hasMoreElements()) {
                        generatedXML.writeElement(null, "response",
                                                  XMLWriter.OPENING);
                        generatedXML.writeElement(null, "href",
                                                  XMLWriter.OPENING);
                        generatedXML
                            .writeText((String) lockPathsList.nextElement());
                        generatedXML.writeElement(null, "href",
                                                  XMLWriter.CLOSING);
                        generatedXML.writeElement(null, "status",
                                                  XMLWriter.OPENING);
                        generatedXML
                            .writeText("HTTP/1.1 " + WebdavStatus.SC_LOCKED
                                       + " " + WebdavStatus
                                       .getStatusText(WebdavStatus.SC_LOCKED));
                        generatedXML.writeElement(null, "status",
                                                  XMLWriter.CLOSING);

                        generatedXML.writeElement(null, "response",
                                                  XMLWriter.CLOSING);
                    }

                    generatedXML.writeElement(null, "multistatus",
                                          XMLWriter.CLOSING);

                    Writer writer = resp.getWriter();
                    writer.write(generatedXML.toString());
                    writer.close();

                    return;

                }

                boolean addLock = true;

                // Checking if there is already a shared lock on this path
                locksList = collectionLocks.elements();
                while (locksList.hasMoreElements()) {

                    LockInfo currentLock = (LockInfo) locksList.nextElement();
                    if (currentLock.path.equals(lock.path)) {

                        if (currentLock.isExclusive()) {
                            resp.sendError(WebdavStatus.SC_LOCKED);
                            return;
                        } else {
                            if (lock.isExclusive()) {
                                resp.sendError(WebdavStatus.SC_LOCKED);
                                return;
                            }
                        }

                        currentLock.tokens.addElement(lockToken);
                        lock = currentLock;
                        addLock = false;

                    }

                }

                if (addLock) {
                    lock.tokens.addElement(lockToken);
                    collectionLocks.addElement(lock);
                }

            } else {

                // Locking a single resource

                // Retrieving an already existing lock on that resource
                LockInfo presentLock = (LockInfo) resourceLocks.get(lock.path);
                if (presentLock != null) {

                    if ((presentLock.isExclusive()) || (lock.isExclusive())) {
                        // If either lock is exclusive, the lock can't be
                        // granted
                        resp.sendError(WebdavStatus.SC_PRECONDITION_FAILED);
                        return;
                    } else {
                        presentLock.tokens.addElement(lockToken);
                        lock = presentLock;
                    }

                } else {

                    lock.tokens.addElement(lockToken);
                    resourceLocks.put(lock.path, lock);

                    // Checking if a resource exists at this path
                    exists = true;
                    try {
                        object = resources.lookup(path);
                    } catch (NamingException e) {
                        exists = false;
                    }
                    if (!exists) {

View Full Code Here

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

        // Check if operation allowed by OLAT VFS security callback
        DirContext resources = getResources(req);
        VFSDirContext vfsContext = (VFSDirContext) resources;
        String path = getRelativePath(req);
        if (!vfsContext.canWrite(path)) {
          resp.sendError(WebdavStatus.SC_FORBIDDEN);
          return;         
View Full Code Here

        }

        // Overwriting the destination

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

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

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

        if (object instanceof VFSItem) {
View Full Code Here

            resp.sendError(WebdavStatus.SC_LOCKED);
            return false;
        }

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

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

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

        if (!exists) {
            resp.sendError(WebdavStatus.SC_NOT_FOUND);
            return false;
        }

        // do not delete junctions attached to VirtualFileSystems ...
        if (object instanceof VFSDirContext) {
          if (((VFSDirContext)object).getVirtualDocBase().canDelete() != VFSConstants.YES) {
            resp.sendError(WebdavStatus.SC_METHOD_NOT_ALLOWED);
            return false;
          }
        }
       
        boolean collection = (object instanceof DirContext);

        if (!collection) {
            try {
                resources.unbind(path);
            } catch (NamingException e) {
                resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
                return false;
            }
        } else {

            Hashtable errorList = new Hashtable();

            deleteCollection(req, resources, path, errorList);
            try {
                resources.unbind(path);
            } catch (NamingException e) {
                errorList.put(path, new Integer
                    (WebdavStatus.SC_INTERNAL_SERVER_ERROR));
            }
View Full Code Here

      }
    }

    try {
      if ((attrs != null) && (ctx instanceof DirContext)) {
        DirContext dirctx = (DirContext)ctx;
        param = parameters[0];
        if (param instanceof AnyName) {
          dirctx.bind((Name)param.toObject(), value, attrs);
        } else {
          dirctx.bind(param.toString(), value, attrs);
        }
        return this;
      }

      param = parameters[0];
View Full Code Here

      }
    }

    try {
      if ((attrs != null) && (ctx instanceof DirContext)) {
        DirContext dirctx = (DirContext)ctx;
        param = parameters[0];
        if (param instanceof AnyName) {
          dirctx.rebind((Name)param.toObject(), value, attrs);
        } else {
          dirctx.rebind(param.toString(), value, attrs);
        }
        return this;
      }

      param = parameters[0];
View Full Code Here

    }

    try {
   
      if ((attrs != null) && (ctx instanceof DirContext)) {
        DirContext dirctx = (DirContext)ctx;
        param = parameters[0];
        if (param instanceof AnyName) {
          return new AnyNamingContext(
            dirctx.createSubcontext((Name)param.toObject(), attrs));
        } else {
          return new AnyNamingContext(
            dirctx.createSubcontext(param.toString(), attrs));
        }
      }

      param = parameters[0];
      if (param instanceof AnyName) {
View Full Code Here

  {
    Context ctx = getContext();
    if (!(ctx instanceof DirContext)) {
      return UNDEFINED;
    }
    DirContext dirctx = (DirContext)ctx;
   
    int nparams = parameters.length;
    if (nparams > 2) {
      nparams = 2;
    }
   
    try {
      switch(nparams) {
      case 2:
        {
          Any param = parameters[0];
          String[] attributes = AnyUtils.toStringArray(parameters[1]);
          if (param instanceof AnyName) {
            return new AnyAttributes(dirctx.getAttributes((Name)param.toObject(), attributes));
          } else {
            return new AnyAttributes(dirctx.getAttributes(parameters[0].toString(), attributes));
          }
        }

      case 1:
        {
          Any param = parameters[0];
          if (param instanceof AnyName) {
            return new AnyAttributes(dirctx.getAttributes((Name)param.toObject()));
          } else {
            return new AnyAttributes(dirctx.getAttributes(param.toString()));
          }
        }
       
      case 0:
        return new AnyAttributes(dirctx.getAttributes(""));
      }
      throw context.BadParameter("Parameter combination not supported");
    } catch (NamingException e) {
      throw context.exception(e);
    }
View Full Code Here

TOP

Related Classes of javax.naming.directory.DirContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.