Search in sources :

Example 1 with ResourceTypeProperty

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

the class MkCol 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_NOT_ACCEPTABLE, null);
    Collection col = UrlNamespace.getCollectionAtUrl(ctxt, ctxt.getPath());
    Pair<List<Element>, List<Element>> elemPair = null;
    if (ctxt.hasRequestMessage()) {
        Document req = ctxt.getRequestMessage();
        Element top = req.getRootElement();
        if (!top.getName().equals(DavElements.P_MKCOL)) {
            throw new DavException("body " + top.getName() + " not allowed in MKCOL", HttpServletResponse.SC_BAD_REQUEST, null);
        }
        elemPair = PropPatch.getSetsAndRemoves(top, true, MKCOL);
    }
    if (elemPair == null) {
        col.mkCol(ctxt, name);
        ctxt.setStatus(HttpServletResponse.SC_CREATED);
    } else {
        Element resourcetypeElem = null;
        MailItem.Type view = null;
        List<Element> set = elemPair.getFirst();
        Iterator<Element> setIter = set.iterator();
        while (setIter.hasNext()) {
            Element elem = setIter.next();
            String propName = elem.getName();
            if (propName.equals(DavElements.P_RESOURCETYPE)) {
                ResourceTypeProperty prop = new ResourceTypeProperty(elem);
                if (prop.isAddressBook()) {
                    view = MailItem.Type.CONTACT;
                } else if (prop.isCalendar()) {
                    view = MailItem.Type.APPOINTMENT;
                } else if (!prop.isCollection()) {
                    throw new DavException("resourcetype must include collection for MKCOL", HttpServletResponse.SC_BAD_REQUEST, null);
                }
                resourcetypeElem = elem;
                setIter.remove();
                break;
            }
        }
        col = col.mkCol(ctxt, name, view);
        Pair<List<Element>, List<QName>> pair = PropPatch.processSetsAndRemoves(ctxt, col, elemPair.getFirst(), elemPair.getSecond(), true);
        col.patchProperties(ctxt, pair.getFirst(), pair.getSecond());
        ctxt.getResponseProp().addProp(resourcetypeElem);
        ctxt.setStatus(HttpServletResponse.SC_CREATED);
        DavResponse resp = ctxt.getDavResponse();
        resp.addResource(ctxt, col, ctxt.getResponseProp(), false);
        sendResponse(ctxt);
    }
}
Also used : DavException(com.zimbra.cs.dav.DavException) Element(org.dom4j.Element) Document(org.dom4j.Document) MailItem(com.zimbra.cs.mailbox.MailItem) DavResponse(com.zimbra.cs.dav.service.DavResponse) Collection(com.zimbra.cs.dav.resource.Collection) List(java.util.List) ResourceTypeProperty(com.zimbra.cs.dav.property.ResourceTypeProperty)

Aggregations

DavException (com.zimbra.cs.dav.DavException)1 ResourceTypeProperty (com.zimbra.cs.dav.property.ResourceTypeProperty)1 Collection (com.zimbra.cs.dav.resource.Collection)1 DavResponse (com.zimbra.cs.dav.service.DavResponse)1 MailItem (com.zimbra.cs.mailbox.MailItem)1 List (java.util.List)1 Document (org.dom4j.Document)1 Element (org.dom4j.Element)1