@SuppressWarnings("unchecked")
    protected void doLock(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String path = getRelativePath(req);

        // Check if webdav is set to read only
        if (m_readOnly) {

            resp.setStatus(CmsWebdavStatus.SC_FORBIDDEN);

            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(Messages.LOG_WEBDAV_READ_ONLY_0));
            }

            return;
        }

        // Check if resource is locked
        if (isLocked(req)) {

            resp.setStatus(CmsWebdavStatus.SC_LOCKED);

            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(Messages.LOG_ITEM_LOCKED_1, path));
            }

            return;
        }

        CmsRepositoryLockInfo lock = new CmsRepositoryLockInfo();

        // Parsing depth header
        String depthStr = req.getHeader(HEADER_DEPTH);
        if (depthStr == null) {
            lock.setDepth(CmsRepositoryLockInfo.DEPTH_INFINITY_VALUE);
        } else {
            if (depthStr.equals("0")) {
                lock.setDepth(0);
            } else {
                lock.setDepth(CmsRepositoryLockInfo.DEPTH_INFINITY_VALUE);
            }
        }

        // Parsing timeout header
        int lockDuration = CmsRepositoryLockInfo.TIMEOUT_INFINITE_VALUE;
        lock.setExpiresAt(System.currentTimeMillis() + (lockDuration * 1000));

        int lockRequestType = LOCK_CREATION;

        Element lockInfoNode = null;

        try {
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(new InputSource(req.getInputStream()));

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

        if (lockInfoNode != null) {

            // Reading lock information
            Iterator<Element> iter = lockInfoNode.elementIterator();

            Element lockScopeNode = null;
            Element lockTypeNode = null;
            Element lockOwnerNode = null;

            while (iter.hasNext()) {
                Element currentElem = iter.next();
                switch (currentElem.getNodeType()) {
                    case Node.TEXT_NODE:
                        break;
                    case Node.ELEMENT_NODE:
                        String nodeName = currentElem.getName();
                        if (nodeName.endsWith(TAG_LOCKSCOPE)) {
                            lockScopeNode = currentElem;
                        }
                        if (nodeName.endsWith(TAG_LOCKTYPE)) {
                            lockTypeNode = currentElem;
                        }
                        if (nodeName.endsWith(TAG_OWNER)) {
                            lockOwnerNode = currentElem;
                        }
                        break;
                    default:
                        break;
                }
            }

            if (lockScopeNode != null) {

                iter = lockScopeNode.elementIterator();
                while (iter.hasNext()) {
                    Element currentElem = iter.next();
                    switch (currentElem.getNodeType()) {
                        case Node.TEXT_NODE:
                            break;
                        case Node.ELEMENT_NODE:
                            String tempScope = currentElem.getName();
                            if (tempScope.indexOf(':') != -1) {
                                lock.setScope(tempScope.substring(tempScope.indexOf(':') + 1));
                            } else {
                                lock.setScope(tempScope);
                            }
                            break;
                        default:
                            break;
                    }
                }

                if (lock.getScope() == null) {

                    // Bad request
                    resp.setStatus(CmsWebdavStatus.SC_BAD_REQUEST);
                }

            } else {

                // Bad request
                resp.setStatus(CmsWebdavStatus.SC_BAD_REQUEST);
            }

            if (lockTypeNode != null) {

                iter = lockTypeNode.elementIterator();
                while (iter.hasNext()) {
                    Element currentElem = iter.next();
                    switch (currentElem.getNodeType()) {
                        case Node.TEXT_NODE:
                            break;
                        case Node.ELEMENT_NODE:
                            String tempType = currentElem.getName();
                            if (tempType.indexOf(':') != -1) {
                                lock.setType(tempType.substring(tempType.indexOf(':') + 1));
                            } else {
                                lock.setType(tempType);
                            }
                            break;
                        default:
                            break;
                    }
                }

                if (lock.getType() == null) {

                    // Bad request
                    resp.setStatus(CmsWebdavStatus.SC_BAD_REQUEST);
                }

            } else {

                // Bad request
                resp.setStatus(CmsWebdavStatus.SC_BAD_REQUEST);
            }

            if (lockOwnerNode != null) {

                iter = lockOwnerNode.elementIterator();
                while (iter.hasNext()) {
                    Element currentElem = iter.next();
                    switch (currentElem.getNodeType()) {
                        case Node.TEXT_NODE:
                            lock.setOwner(lock.getOwner() + currentElem.getStringValue());
                            break;
                        case Node.ELEMENT_NODE:
                            lock.setOwner(lock.getOwner() + currentElem.getStringValue());
                            break;
                        default:
                            break;
                    }
                }

                if (lock.getOwner() == null) {

                    // Bad request
                    resp.setStatus(CmsWebdavStatus.SC_BAD_REQUEST);
                }

            } else {
                lock.setOwner("");
            }

        }

        lock.setPath(path);
        lock.setUsername(m_username);

        if (lockRequestType == LOCK_REFRESH) {

            CmsRepositoryLockInfo currentLock = m_session.getLock(path);
            if (currentLock == null) {
                lockRequestType = LOCK_CREATION;
            }
        }

        if (lockRequestType == LOCK_CREATION) {

            try {

                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_LOCK_ITEM_1, lock.getOwner()));
                }

                boolean result = m_session.lock(path, lock);
                if (result) {

                    // Add the Lock-Token header as by RFC 2518 8.10.1
                    // - only do this for newly created locks
                    resp.addHeader(HEADER_LOCKTOKEN, "<opaquelocktoken:" + generateLockToken(req, lock) + ">");

                    if (LOG.isDebugEnabled()) {
                        LOG.debug(Messages.get().getBundle().key(Messages.LOG_LOCK_ITEM_FAILED_0));
                    }

                } else {

                    resp.setStatus(CmsWebdavStatus.SC_LOCKED);

                    if (LOG.isDebugEnabled()) {
                        LOG.debug(Messages.get().getBundle().key(Messages.LOG_LOCK_ITEM_SUCCESS_0));
                    }

                    return;
                }
            } catch (CmsVfsResourceNotFoundException rnfex) {
                resp.setStatus(CmsWebdavStatus.SC_NOT_FOUND);

                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_ITEM_NOT_FOUND_1, path));
                }

                return;
            } catch (CmsSecurityException sex) {
                resp.setStatus(CmsWebdavStatus.SC_FORBIDDEN);

                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_NO_PERMISSION_0));
                }

                return;
            } catch (CmsException ex) {
                resp.setStatus(CmsWebdavStatus.SC_INTERNAL_SERVER_ERROR);

                if (LOG.isErrorEnabled()) {
                    LOG.error(Messages.get().getBundle().key(Messages.LOG_REPOSITORY_ERROR_2, "LOCK", path), ex);
                }

                return;
            }
        }

        // Set the status, then generate the XML response containing
        // the lock information
        Document doc = DocumentHelper.createDocument();
        Element propElem = doc.addElement(new QName(TAG_PROP, Namespace.get(DEFAULT_NAMESPACE)));

        Element lockElem = addElement(propElem, TAG_LOCKDISCOVERY);
        addLockElement(lock, lockElem, generateLockToken(req, lock));

        resp.setStatus(CmsWebdavStatus.SC_OK);
        resp.setContentType("text/xml; charset=UTF-8");

        Writer writer = resp.getWriter();
        doc.write(writer);
        writer.close();
    }
@ SuppressWarnings ( "unchecked" ) protected void doLock ( HttpServletRequest req , HttpServletResponse resp ) throws IOException { String path = getRelativePath ( req ) ; if ( m_readOnly ) { resp . setStatus ( CmsWebdavStatus . SC_FORBIDDEN ) ; if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( Messages . get ( ) . getBundle ( ) . key ( Messages . LOG_WEBDAV_READ_ONLY_0 ) ) ; } return ; } if ( isLocked ( req ) ) { resp . setStatus ( CmsWebdavStatus . SC_LOCKED ) ; if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( Messages . get ( ) . getBundle ( ) . key ( Messages . LOG_ITEM_LOCKED_1 , path ) ) ; } return ; } CmsRepositoryLockInfo lock = new CmsRepositoryLockInfo ( ) ; String depthStr = req . getHeader ( HEADER_DEPTH ) ; if ( depthStr == null ) { lock . setDepth ( CmsRepositoryLockInfo . DEPTH_INFINITY_VALUE ) ; } else { if ( depthStr . equals ( "0" ) ) { lock . setDepth ( 0 ) ; } else { lock . setDepth ( CmsRepositoryLockInfo . DEPTH_INFINITY_VALUE ) ; } } int lockDuration = CmsRepositoryLockInfo . TIMEOUT_INFINITE_VALUE ; lock . setExpiresAt ( System . currentTimeMillis ( ) + ( lockDuration * 1000 ) ) ; int lockRequestType = LOCK_CREATION ; Element lockInfoNode = null ; try { SAXReader saxReader = new SAXReader ( ) ; Document document = saxReader . read ( new InputSource ( req . getInputStream ( ) ) ) ; Element rootElement = document . getRootElement ( ) ; lockInfoNode = rootElement ; } catch ( Exception e ) { lockRequestType = LOCK_REFRESH ; } if ( lockInfoNode != null ) { Iterator < Element > iter = lockInfoNode . elementIterator ( ) ; Element lockScopeNode = null ; Element lockTypeNode = null ; Element lockOwnerNode = null ; while ( iter . hasNext ( ) ) { Element currentElem = iter . next ( ) ; switch ( currentElem . getNodeType ( ) ) { case Node . TEXT_NODE : break ; case Node . ELEMENT_NODE : String nodeName = currentElem . getName ( ) ; if ( nodeName . endsWith ( TAG_LOCKSCOPE ) ) { lockScopeNode = currentElem ; } if ( nodeName . endsWith ( TAG_LOCKTYPE ) ) { lockTypeNode = currentElem ; } if ( nodeName . endsWith ( TAG_OWNER ) ) { lockOwnerNode = currentElem ; } break ; default : break ; } } if ( lockScopeNode != null ) { iter = lockScopeNode . elementIterator ( ) ; while ( iter . hasNext ( ) ) { Element currentElem = iter . next ( ) ; switch ( currentElem . getNodeType ( ) ) { case Node . TEXT_NODE : break ; case Node . ELEMENT_NODE : String tempScope = currentElem . getName ( ) ; if ( tempScope . indexOf ( ':' ) != - 1 ) { lock . setScope ( tempScope . substring ( tempScope . indexOf ( ':' ) + 1 ) ) ; } else { lock . setScope ( tempScope ) ; } break ; default : break ; } } if ( lock . getScope ( ) == null ) { resp . setStatus ( CmsWebdavStatus . SC_BAD_REQUEST ) ; } } else { resp . setStatus ( CmsWebdavStatus . SC_BAD_REQUEST ) ; } if ( lockTypeNode != null ) { iter = lockTypeNode . elementIterator ( ) ; while ( iter . hasNext ( ) ) { Element currentElem = iter . next ( ) ; switch ( currentElem . getNodeType ( ) ) { case Node . TEXT_NODE : break ; case Node . ELEMENT_NODE : String tempType = currentElem . getName ( ) ; if ( tempType . indexOf ( ':' ) != - 1 ) { lock . setType ( tempType . substring ( tempType . indexOf ( ':' ) + 1 ) ) ; } else { lock . setType ( tempType ) ; } break ; default : break ; } } if ( lock . getType ( ) == null ) { resp . setStatus ( CmsWebdavStatus . SC_BAD_REQUEST ) ; } } else { resp . setStatus ( CmsWebdavStatus . SC_BAD_REQUEST ) ; } if ( lockOwnerNode != null ) { iter = lockOwnerNode . elementIterator ( ) ; while ( iter . hasNext ( ) ) { Element currentElem = iter . next ( ) ; switch ( currentElem . getNodeType ( ) ) { case Node . TEXT_NODE : lock . setOwner ( lock . getOwner ( ) + currentElem . getStringValue ( ) ) ; break ; case Node . ELEMENT_NODE : lock . setOwner ( lock . getOwner ( ) + currentElem . getStringValue ( ) ) ; break ; default : break ; } } if ( lock . getOwner ( ) == null ) { resp . setStatus ( CmsWebdavStatus . SC_BAD_REQUEST ) ; } } else { lock . setOwner ( "" ) ; } } lock . setPath ( path ) ; lock . setUsername ( m_username ) ; if ( lockRequestType == LOCK_REFRESH ) { CmsRepositoryLockInfo currentLock = m_session . getLock ( path ) ; if ( currentLock == null ) { lockRequestType = LOCK_CREATION ; } } if ( lockRequestType == LOCK_CREATION ) { try { if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( Messages . get ( ) . getBundle ( ) . key ( Messages . LOG_LOCK_ITEM_1 , lock . getOwner ( ) ) ) ; } boolean result = m_session . lock ( path , lock ) ; if ( result ) { resp . addHeader ( HEADER_LOCKTOKEN , "<opaquelocktoken:" + generateLockToken ( req , lock ) + ">" ) ; if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( Messages . get ( ) . getBundle ( ) . key ( Messages . LOG_LOCK_ITEM_FAILED_0 ) ) ; } } else { resp . setStatus ( CmsWebdavStatus . SC_LOCKED ) ; if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( Messages . get ( ) . getBundle ( ) . key ( Messages . LOG_LOCK_ITEM_SUCCESS_0 ) ) ; } return ; } } catch ( CmsVfsResourceNotFoundException rnfex ) { resp . setStatus ( CmsWebdavStatus . SC_NOT_FOUND ) ; if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( Messages . get ( ) . getBundle ( ) . key ( Messages . LOG_ITEM_NOT_FOUND_1 , path ) ) ; } return ; } catch ( CmsSecurityException sex ) { resp . setStatus ( CmsWebdavStatus . SC_FORBIDDEN ) ; if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( Messages . get ( ) . getBundle ( ) . key ( Messages . LOG_NO_PERMISSION_0 ) ) ; } return ; } catch ( CmsException ex ) { resp . setStatus ( CmsWebdavStatus . SC_INTERNAL_SERVER_ERROR ) ; if ( LOG . isErrorEnabled ( ) ) { LOG . error ( Messages . get ( ) . getBundle ( ) . key ( Messages . LOG_REPOSITORY_ERROR_2 , "LOCK" , path ) , ex ) ; } return ; } } Document doc = DocumentHelper . createDocument ( ) ; Element propElem = doc . addElement ( new QName ( TAG_PROP , Namespace . get ( DEFAULT_NAMESPACE ) ) ) ; Element lockElem = addElement ( propElem , TAG_LOCKDISCOVERY ) ; addLockElement ( lock , lockElem , generateLockToken ( req , lock ) ) ; resp . setStatus ( CmsWebdavStatus . SC_OK ) ; resp . setContentType ( "text/xml; charset=UTF-8" ) ; Writer writer = resp . getWriter ( ) ; doc . write ( writer ) ; writer . close ( ) ; }
Process a LOCK WebDAV request for the specified resource.<p>

@param req the servlet request we are processing
@param resp the servlet response we are creating

@throws IOException if an input/output error occurs