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);
}
}
}
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);
}
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);
}
}
}
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);
}
}
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());
}
}
Aggregations