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