use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.
the class PropPatch method getSetsAndRemoves.
/**
*
* @param top
* @param isCreate - any "remove" elements will be ignored (should they be an error?)
* @param method - used for logging to identify what HTTP method we are doing
* @return pair of lists of elements under "set" and those under "remove" in the request
* @throws DavException
* @throws IOException
*/
public static Pair<List<Element>, List<Element>> getSetsAndRemoves(Element top, boolean isCreate, String method) throws DavException, IOException {
List<Element> set = Lists.newArrayList();
List<Element> remove = Lists.newArrayList();
if (top == null) {
return null;
}
for (Object obj : top.elements()) {
if (!(obj instanceof Element)) {
continue;
}
Element e = (Element) obj;
String nodeName = e.getName();
boolean isSet;
if (nodeName.equals(DavElements.P_SET)) {
isSet = true;
} else if (nodeName.equals(DavElements.P_REMOVE)) {
if (isCreate) {
continue;
}
isSet = false;
} else {
continue;
}
e = e.element(DavElements.E_PROP);
if (e == null) {
throw new DavException("missing <D:prop> in " + method, HttpServletResponse.SC_BAD_REQUEST, null);
}
for (Object propObj : e.elements()) {
if (propObj instanceof Element) {
Element propElem = (Element) propObj;
if (isSet) {
set.add(propElem);
} else {
remove.add(propElem);
}
}
}
}
return new Pair<List<Element>, List<Element>>(set, remove);
}
use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.
the class PropPatch method processSetsAndRemoves.
/**
* @param setElems - list of elements under "set" in the request. Must NOT be null
* @param removeElems - list of elements under "remove" in the request. Must NOT be null
* @return Pair - first is list of elements representing properties to set. second is names of
* properties to remove.
*/
public static Pair<List<Element>, List<QName>> processSetsAndRemoves(DavContext ctxt, DavResource resource, List<Element> setElems, List<Element> removeElems, boolean isCreate) throws DavException, IOException {
List<Element> set = Lists.newArrayList();
List<QName> remove = Lists.newArrayList();
RequestProp rp = new RequestProp(true);
ctxt.setResponseProp(rp);
for (Element propElem : setElems) {
QName propName = propElem.getQName();
ResourceProperty prop = resource.getProperty(propName);
if (prop == null || !prop.isProtected()) {
set.add(propElem);
rp.addProp(propElem);
} else if (isCreate && prop.isAllowSetOnCreate()) {
set.add(propElem);
} else {
rp.addPropError(propName, new DavException.CannotModifyProtectedProperty(propName));
}
}
for (Element propElem : removeElems) {
QName propName = propElem.getQName();
ResourceProperty prop = resource.getProperty(propName);
if (prop == null || !prop.isProtected()) {
remove.add(propName);
rp.addProp(propElem);
} else {
rp.addPropError(propName, new DavException.CannotModifyProtectedProperty(propName));
}
}
return new Pair<List<Element>, List<QName>>(set, remove);
}
use of com.zimbra.cs.dav.DavException 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.DavException in project zm-mailbox by Zimbra.
the class CalendarQuery method handleCalendarItem.
private void handleCalendarItem(QueryContext ctxt, DavResource calItem) {
if (!(calItem instanceof CalendarObject))
return;
try {
CalendarObject calobj = (CalendarObject) calItem;
if (!calobj.match(ctxt.componentFilter)) {
return;
}
DavResponse resp = ctxt.davCtxt.getDavResponse();
if (ctxt.expandRange != null) {
calobj.expand(ctxt.expandRange);
}
resp.addResource(ctxt.davCtxt, calItem, ctxt.props, false);
} catch (DavException de) {
ZimbraLog.dav.error("can't get calendar item data", de);
}
}
use of com.zimbra.cs.dav.DavException 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);
}
Aggregations