Search in sources :

Example 1 with LockMgr

use of com.zimbra.cs.dav.LockMgr in project zm-mailbox by Zimbra.

the class Lock method handle.

@Override
public void handle(DavContext ctxt) throws DavException, IOException, ServiceException {
    LockMgr lockmgr = LockMgr.getInstance();
    LockMgr.Lock lock = null;
    if (ctxt.hasRequestMessage()) {
        DavContext.Depth depth = ctxt.getDepth();
        if (depth == Depth.one)
            throw new DavException("invalid depth", HttpServletResponse.SC_BAD_REQUEST, null);
        String d = (depth == Depth.zero) ? "0" : depth.toString();
        LockMgr.LockScope scope = LockScope.shared;
        LockMgr.LockType type = LockType.write;
        Document req = ctxt.getRequestMessage();
        Element top = req.getRootElement();
        if (!top.getName().equals(DavElements.P_LOCKINFO))
            throw new DavException("msg " + top.getName() + " not allowed in LOCK", HttpServletResponse.SC_BAD_REQUEST, null);
        Element e = top.element(DavElements.E_LOCKSCOPE);
        @SuppressWarnings("unchecked") List<Element> ls = e.elements();
        for (Element v : ls) {
            if (v.getQName().equals(DavElements.E_EXCLUSIVE))
                scope = LockScope.exclusive;
            else if (v.getQName().equals(DavElements.E_SHARED))
                scope = LockScope.shared;
            else
                throw new DavException("unrecognized scope element " + v.toString(), HttpServletResponse.SC_BAD_REQUEST, null);
        }
        e = top.element(DavElements.E_LOCKTYPE);
        @SuppressWarnings("unchecked") List<Element> lt = e.elements();
        for (Element v : lt) {
            if (v.getQName().equals(DavElements.E_WRITE))
                type = LockType.write;
            else
                throw new DavException("unrecognized type element " + v.toString(), HttpServletResponse.SC_BAD_REQUEST, null);
        }
        String owner;
        e = top.element(DavElements.E_OWNER);
        if (e != null && e.elementIterator(DavElements.E_HREF).hasNext()) {
            Element ownerElem = (Element) e.elementIterator(DavElements.E_HREF).next();
            owner = ownerElem.getText();
        } else {
            owner = ctxt.getAuthAccount().getName();
        }
        lock = lockmgr.createLock(ctxt, owner, ctxt.getUri(), type, scope, d);
        ctxt.getResponse().addHeader(DavProtocol.HEADER_LOCK_TOKEN, lock.toLockTokenHeader());
    } else {
        // refresh lock
        String token = ctxt.getRequest().getHeader(DavProtocol.HEADER_IF);
        if (token == null) {
            throw new DavException("no request body", HttpServletResponse.SC_BAD_REQUEST, null);
        }
        token = token.trim();
        int len = token.length();
        if (token.charAt(0) == '(' && token.charAt(len - 1) == ')') {
            token = token.substring(1, len - 1);
        }
        List<LockMgr.Lock> locks = lockmgr.getLocks(ctxt.getUri());
        for (LockMgr.Lock l : locks) {
            if (l.token.equals(LockMgr.Lock.parseLockTokenHeader(token))) {
                l.extendExpiration();
                lock = l;
                break;
            }
        }
        if (lock == null) {
            throw new DavException("Lock does not exist", HttpServletResponse.SC_PRECONDITION_FAILED, null);
        }
    }
    ctxt.getDavResponse().addProperty(ctxt, new LockDiscovery(lock));
    ctxt.setStatus(HttpServletResponse.SC_OK);
    sendResponse(ctxt);
}
Also used : LockMgr(com.zimbra.cs.dav.LockMgr) DavException(com.zimbra.cs.dav.DavException) Element(org.dom4j.Element) Depth(com.zimbra.cs.dav.DavContext.Depth) Document(org.dom4j.Document) LockDiscovery(com.zimbra.cs.dav.property.LockDiscovery) LockScope(com.zimbra.cs.dav.LockMgr.LockScope) LockType(com.zimbra.cs.dav.LockMgr.LockType) DavContext(com.zimbra.cs.dav.DavContext)

Aggregations

DavContext (com.zimbra.cs.dav.DavContext)1 Depth (com.zimbra.cs.dav.DavContext.Depth)1 DavException (com.zimbra.cs.dav.DavException)1 LockMgr (com.zimbra.cs.dav.LockMgr)1 LockScope (com.zimbra.cs.dav.LockMgr.LockScope)1 LockType (com.zimbra.cs.dav.LockMgr.LockType)1 LockDiscovery (com.zimbra.cs.dav.property.LockDiscovery)1 Document (org.dom4j.Document)1 Element (org.dom4j.Element)1