Search in sources :

Example 26 with DavException

use of com.zimbra.cs.dav.DavException 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)

Example 27 with DavException

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

the class DavResponse method addResourceTo.

public void addResourceTo(DavContext ctxt, DavResource rs, DavContext.RequestProp props, boolean includeChildren) throws DavException {
    if (!rs.isValid()) {
        addStatus(ctxt, rs.getUri(), HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    Element top = getTop(DavElements.E_MULTISTATUS).addElement(DavElements.E_RESPONSE);
    rs.getProperty(DavElements.E_HREF).toElement(ctxt, top, false);
    Collection<QName> propNames;
    if (props.isAllProp())
        propNames = rs.getAllPropertyNames();
    else
        propNames = props.getProps();
    PropStat propstat = new PropStat();
    Map<QName, DavException> errPropMap = props.getErrProps();
    for (QName name : propNames) {
        ResourceProperty prop = rs.getProperty(name, props);
        if (errPropMap.containsKey(name)) {
            DavException ex = errPropMap.get(name);
            propstat.add(name, null, ex.getStatus());
        } else if (prop == null) {
            if (!ctxt.isBrief())
                propstat.add(name, null, HttpServletResponse.SC_NOT_FOUND);
        } else {
            propstat.add(prop);
        }
    }
    propstat.toResponse(ctxt, top, props.isNameOnly());
}
Also used : ResourceProperty(com.zimbra.cs.dav.property.ResourceProperty) DavException(com.zimbra.cs.dav.DavException) QName(org.dom4j.QName) Element(org.dom4j.Element)

Example 28 with DavException

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

the class DavServlet method getDavUrl.

public static String getDavUrl(String user) throws DavException, ServiceException {
    Provisioning prov = Provisioning.getInstance();
    Account account = prov.get(AccountBy.name, user);
    if (account == null)
        throw new DavException("unknown user " + user, HttpServletResponse.SC_NOT_FOUND, null);
    return getServiceUrl(account, DAV_PATH);
}
Also used : Account(com.zimbra.cs.account.Account) DavException(com.zimbra.cs.dav.DavException) Provisioning(com.zimbra.cs.account.Provisioning)

Example 29 with DavException

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

the class MkAddressbook method handle.

@Override
public void handle(DavContext ctxt) throws DavException, IOException {
    String user = ctxt.getUser();
    String name = ctxt.getItem();
    if (user == null || name == null)
        throw new DavException("invalid uri", HttpServletResponse.SC_FORBIDDEN, null);
    Element top = null;
    if (ctxt.hasRequestMessage()) {
        Document doc = ctxt.getRequestMessage();
        top = doc.getRootElement();
        if (!top.getName().equals(DavElements.P_MKADDRESSBOOK))
            throw new DavException("msg " + top.getName() + " not allowed in MKADDRESSBOOK", HttpServletResponse.SC_BAD_REQUEST, null);
    }
    Collection col = UrlNamespace.getCollectionAtUrl(ctxt, ctxt.getPath());
    Collection newone = col.mkCol(ctxt, name, MailItem.Type.CONTACT);
    boolean success = false;
    try {
        PropPatch.handlePropertyUpdate(ctxt, top, newone, true, MKADDRESSBOOK);
        success = true;
    } finally {
        if (!success)
            newone.delete(ctxt);
    }
    ctxt.setStatus(HttpServletResponse.SC_CREATED);
    ctxt.getResponse().addHeader(DavProtocol.HEADER_CACHE_CONTROL, DavProtocol.NO_CACHE);
}
Also used : DavException(com.zimbra.cs.dav.DavException) Element(org.dom4j.Element) Collection(com.zimbra.cs.dav.resource.Collection) Document(org.dom4j.Document)

Example 30 with DavException

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

the class MkCalendar method handle.

// valid return codes:
// 201 Created, 207 Multi-Status (403, 409, 423, 424, 507),
// 403 Forbidden, 409 Conflict, 415 Unsupported Media Type,
// 507 Insufficient Storage
@Override
public void handle(DavContext ctxt) throws DavException, IOException {
    String user = ctxt.getUser();
    String name = ctxt.getItem();
    if (user == null || name == null)
        throw new DavException("invalid uri", HttpServletResponse.SC_FORBIDDEN, null);
    Element top = null;
    if (ctxt.hasRequestMessage()) {
        Document doc = ctxt.getRequestMessage();
        top = doc.getRootElement();
        if (!top.getName().equals(DavElements.P_MKCALENDAR))
            throw new DavException("msg " + top.getName() + " not allowed in MKCALENDAR", HttpServletResponse.SC_BAD_REQUEST, null);
    }
    Collection col = UrlNamespace.getCollectionAtUrl(ctxt, ctxt.getPath());
    if (col instanceof CalendarCollection)
        throw new DavException("can't create calendar under another calendar", HttpServletResponse.SC_FORBIDDEN, null);
    Collection newone = col.mkCol(ctxt, name, MailItem.Type.APPOINTMENT);
    boolean success = false;
    try {
        PropPatch.handlePropertyUpdate(ctxt, top, newone, true, MKCALENDAR);
        success = true;
    } finally {
        if (!success)
            newone.delete(ctxt);
    }
    ctxt.setStatus(HttpServletResponse.SC_CREATED);
    ctxt.getResponse().addHeader(DavProtocol.HEADER_CACHE_CONTROL, DavProtocol.NO_CACHE);
}
Also used : CalendarCollection(com.zimbra.cs.dav.resource.CalendarCollection) DavException(com.zimbra.cs.dav.DavException) Element(org.dom4j.Element) CalendarCollection(com.zimbra.cs.dav.resource.CalendarCollection) Collection(com.zimbra.cs.dav.resource.Collection) Document(org.dom4j.Document)

Aggregations

DavException (com.zimbra.cs.dav.DavException)67 ServiceException (com.zimbra.common.service.ServiceException)27 Element (org.dom4j.Element)25 Account (com.zimbra.cs.account.Account)18 Mailbox (com.zimbra.cs.mailbox.Mailbox)18 DavResource (com.zimbra.cs.dav.resource.DavResource)15 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)15 ArrayList (java.util.ArrayList)14 Provisioning (com.zimbra.cs.account.Provisioning)11 Document (org.dom4j.Document)9 DavResponse (com.zimbra.cs.dav.service.DavResponse)8 MailItem (com.zimbra.cs.mailbox.MailItem)8 Invite (com.zimbra.cs.mailbox.calendar.Invite)7 ZMailbox (com.zimbra.client.ZMailbox)6 RequestProp (com.zimbra.cs.dav.DavContext.RequestProp)6 IOException (java.io.IOException)6 Collection (com.zimbra.cs.dav.resource.Collection)5 Folder (com.zimbra.cs.mailbox.Folder)5 CalendarCollection (com.zimbra.cs.dav.resource.CalendarCollection)4 ZProperty (com.zimbra.common.calendar.ZCalendar.ZProperty)3