Search in sources :

Example 11 with DavResource

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

the class AddressbookMultiget method handle.

@Override
public void handle(DavContext ctxt) throws ServiceException, DavException {
    Element query = ctxt.getRequestMessage().getRootElement();
    if (!query.getQName().equals(DavElements.CardDav.E_ADDRESSBOOK_MULTIGET))
        throw new DavException("msg " + query.getName() + " is not addressbook-multiget", HttpServletResponse.SC_BAD_REQUEST, null);
    DavResponse resp = ctxt.getDavResponse();
    DavResource reqResource = ctxt.getRequestedResource();
    if (!(reqResource instanceof AddressbookCollection))
        throw new DavException("requested resource is not an addressbook collection", HttpServletResponse.SC_BAD_REQUEST, null);
    RequestProp reqProp = ctxt.getRequestProp();
    for (Object obj : query.elements(DavElements.E_HREF)) {
        if (obj instanceof Element) {
            String href = ((Element) obj).getText();
            URI uri = URI.create(href);
            String[] fragments = HttpUtil.getPathFragments(uri);
            if (uri.getPath().toLowerCase().endsWith(AddressObject.VCARD_EXTENSION)) {
                // double encode the last fragment
                fragments[fragments.length - 1] = HttpUtil.urlEscapeIncludingSlash(fragments[fragments.length - 1]);
            }
            uri = HttpUtil.getUriFromFragments(fragments, uri.getQuery(), true, false);
            href = uri.getPath();
            DavResource rs = UrlNamespace.getResourceAtUrl(ctxt, href);
            if (rs != null)
                resp.addResource(ctxt, rs, reqProp, false);
        }
    }
}
Also used : AddressbookCollection(com.zimbra.cs.dav.resource.AddressbookCollection) DavResource(com.zimbra.cs.dav.resource.DavResource) DavException(com.zimbra.cs.dav.DavException) DavResponse(com.zimbra.cs.dav.service.DavResponse) RequestProp(com.zimbra.cs.dav.DavContext.RequestProp) Element(org.dom4j.Element) AddressObject(com.zimbra.cs.dav.resource.AddressObject) URI(java.net.URI)

Example 12 with DavResource

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

the class ExpandProperty method expandProperties.

/**
     * @param rs - the requested resource
     * @param elem - specification of what should be expanded - either the top level {@code <DAV:expand-property>}
     *               element or a descendant {@code <DAV:property>} element
     * @param resp - the target {@code <DAV:response>} element
     */
private void expandProperties(DavContext ctxt, DavResource rs, Element elem, Element resp) {
    rs.getProperty(DavElements.E_HREF).toElement(ctxt, resp, false);
    @SuppressWarnings("rawtypes") Iterator iter = elem.elementIterator(DavElements.E_PROPERTY);
    PropStat propstat = new PropStat();
    while (iter.hasNext()) {
        Element property = (Element) iter.next();
        Prop p = new Prop(property);
        ResourceProperty rp = rs.getProperty(p.getQName());
        if (rp == null) {
            if (!ctxt.isBrief())
                propstat.add(p.getQName(), null, HttpServletResponse.SC_NOT_FOUND);
        } else {
            @SuppressWarnings("rawtypes") Iterator subProps = property.elementIterator();
            if (subProps.hasNext()) {
                PropStat sub = new PropStat();
                sub.add(rp);
                Element subElem = DocumentHelper.createElement(DavElements.E_RESPONSE);
                sub.toResponse(ctxt, subElem, false);
                @SuppressWarnings("rawtypes") Iterator subPropstats = subElem.elementIterator(DavElements.E_PROPSTAT);
                while (subPropstats.hasNext()) {
                    Element subPropstat = (Element) subPropstats.next();
                    Element status = subPropstat.element(DavElements.E_STATUS);
                    if (!status.getText().equals(DavResponse.sStatusTextMap.get(HttpServletResponse.SC_OK)))
                        continue;
                    Element prop = subPropstat.element(DavElements.E_PROP);
                    if (prop == null)
                        continue;
                    prop = prop.element(p.getQName());
                    if (prop == null)
                        continue;
                    @SuppressWarnings("rawtypes") Iterator hrefs = prop.elementIterator(DavElements.E_HREF);
                    if (!hrefs.hasNext()) {
                        // need to say which property, even if the list is empty
                        propstat.add(rp);
                    } else {
                        while (hrefs.hasNext()) {
                            Element href = (Element) hrefs.next();
                            String url = href.getText();
                            if (url == null)
                                continue;
                            try {
                                url = URLDecoder.decode(url, "UTF-8");
                            } catch (UnsupportedEncodingException e) {
                                ZimbraLog.dav.warn("can't decode url %s", url, e);
                            }
                            try {
                                DavResource target = UrlNamespace.getResourceAtUrl(ctxt, url);
                                Element targetElem = DocumentHelper.createElement(DavElements.E_RESPONSE);
                                expandProperties(ctxt, target, property, targetElem);
                                propstat.add(rp.getName(), targetElem);
                            } catch (DavException e) {
                                ZimbraLog.dav.warn("can't find resource for " + url, e);
                            }
                        }
                    }
                }
            } else {
                propstat.add(rp);
            }
        }
    }
    propstat.toResponse(ctxt, resp, false);
}
Also used : ResourceProperty(com.zimbra.cs.dav.property.ResourceProperty) DavResource(com.zimbra.cs.dav.resource.DavResource) DavException(com.zimbra.cs.dav.DavException) PropStat(com.zimbra.cs.dav.service.DavResponse.PropStat) Element(org.dom4j.Element) Iterator(java.util.Iterator) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 13 with DavResource

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

the class Get method handle.

@Override
public void handle(DavContext ctxt) throws DavException, IOException, ServiceException {
    DavResource resource = ctxt.getRequestedResource();
    HttpServletResponse resp = ctxt.getResponse();
    String contentType = resource.getContentType(ctxt);
    if (contentType != null) {
        ContentType ct = new ContentType(contentType);
        if (ct.getParameter(MimeConstants.P_CHARSET) == null)
            ct.setParameter(MimeConstants.P_CHARSET, MimeConstants.P_CHARSET_UTF8);
        resp.setContentType(ct.toString());
    }
    if (resource.hasEtag()) {
        ctxt.getResponse().setHeader(DavProtocol.HEADER_ETAG, resource.getEtag());
        ctxt.getResponse().setHeader(ETagHeaderFilter.ZIMBRA_ETAG_HEADER, resource.getEtag());
    }
    // resp.setContentLength(resource.getContentLength());
    if (!returnContent() || !resource.hasContent(ctxt))
        return;
    resp.setHeader("Content-Disposition", "attachment");
    ByteUtil.copy(resource.getContent(ctxt), true, ctxt.getResponse().getOutputStream(), false);
    resp.setStatus(ctxt.getStatus());
    ctxt.responseSent();
    if (ZimbraLog.dav.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder("Response for DAV GET ").append(ctxt.getUri()).append("\n");
        if (contentType != null && contentType.startsWith("text")) {
            DavServlet.addResponseHeaderLoggingInfo(resp, sb);
            if (ZimbraLog.dav.isTraceEnabled()) {
                sb.append(new String(ByteUtil.getContent(resource.getContent(ctxt), 0), "UTF-8"));
            }
            ZimbraLog.dav.debug(sb);
        }
    }
}
Also used : DavResource(com.zimbra.cs.dav.resource.DavResource) ContentType(com.zimbra.common.mime.ContentType) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 14 with DavResource

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

the class Move method checkPrecondition.

@Override
public void checkPrecondition(DavContext ctxt) throws DavException, ServiceException {
    super.checkPrecondition(ctxt);
    DavResource rs = ctxt.getRequestedResource();
    if (!(rs instanceof MailItemResource))
        throw new DavException("cannot move", HttpServletResponse.SC_BAD_REQUEST, null);
    col = ctxt.getDestinationCollection();
    mir = (MailItemResource) rs;
    if (!mir.isCollection()) {
        Collection srcCollection = ctxt.getRequestedParentCollection();
        if (srcCollection.getDefaultView() != MailItem.Type.UNKNOWN && srcCollection.getDefaultView() != col.getDefaultView())
            throw new DavException("cannot move to incompatible collection", HttpServletResponse.SC_FORBIDDEN, null);
    } else {
        // allow moving of collections of type document or unknown only.
        if (!(((Collection) mir).getDefaultView() == MailItem.Type.DOCUMENT || ((Collection) mir).getDefaultView() == MailItem.Type.UNKNOWN))
            throw new DavException("cannot move non-document collection", HttpServletResponse.SC_FORBIDDEN, null);
        // do not allow moving of collection if destination type is not document or unknown.            
        if (!(col.getDefaultView() == MailItem.Type.UNKNOWN || col.getDefaultView() == MailItem.Type.DOCUMENT))
            throw new DavException("cannot move to incompatible collection", HttpServletResponse.SC_FORBIDDEN, null);
    }
}
Also used : DavResource(com.zimbra.cs.dav.resource.DavResource) MailItemResource(com.zimbra.cs.dav.resource.MailItemResource) DavException(com.zimbra.cs.dav.DavException) Collection(com.zimbra.cs.dav.resource.Collection)

Example 15 with DavResource

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

the class Put 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());
    DavResource rs = col.createItem(ctxt, name);
    if (rs.isNewlyCreated())
        ctxt.setStatus(HttpServletResponse.SC_CREATED);
    else
        ctxt.setStatus(HttpServletResponse.SC_NO_CONTENT);
    if (rs.hasEtag()) {
        ctxt.getResponse().setHeader(DavProtocol.HEADER_ETAG, rs.getEtag());
        ctxt.getResponse().setHeader(ETagHeaderFilter.ZIMBRA_ETAG_HEADER, rs.getEtag());
    }
}
Also used : DavResource(com.zimbra.cs.dav.resource.DavResource) DavException(com.zimbra.cs.dav.DavException) Collection(com.zimbra.cs.dav.resource.Collection)

Aggregations

DavResource (com.zimbra.cs.dav.resource.DavResource)25 DavException (com.zimbra.cs.dav.DavException)15 Element (org.dom4j.Element)12 DavResponse (com.zimbra.cs.dav.service.DavResponse)9 RequestProp (com.zimbra.cs.dav.DavContext.RequestProp)8 ArrayList (java.util.ArrayList)7 ServiceException (com.zimbra.common.service.ServiceException)3 CalendarCollection (com.zimbra.cs.dav.resource.CalendarCollection)3 Collection (com.zimbra.cs.dav.resource.Collection)3 MailItemResource (com.zimbra.cs.dav.resource.MailItemResource)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Document (org.dom4j.Document)3 Account (com.zimbra.cs.account.Account)2 Provisioning (com.zimbra.cs.account.Provisioning)2 TimeRange (com.zimbra.cs.dav.caldav.Range.TimeRange)2 Ace (com.zimbra.cs.dav.property.Acl.Ace)2 AddressObject (com.zimbra.cs.dav.resource.AddressObject)2 AddressbookCollection (com.zimbra.cs.dav.resource.AddressbookCollection)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2