use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.
the class CalendarCollection method handlePost.
@Override
public void handlePost(DavContext ctxt) throws DavException, IOException, ServiceException {
Provisioning prov = Provisioning.getInstance();
DavResource rs = null;
try {
String user = ctxt.getUser();
Account account = prov.get(AccountBy.name, user);
if (account == null) {
// Anti-account name harvesting.
ZimbraLog.dav.info("Failing POST to Calendar - no such account '%s'", user);
throw new DavException("Request denied", HttpServletResponse.SC_NOT_FOUND, null);
}
List<Invite> invites = uploadToInvites(ctxt, account);
String uid = findEventUid(invites);
rs = createItemFromInvites(ctxt, account, uid + ".ics", invites, false);
if (rs.isNewlyCreated()) {
ctxt.getResponse().setHeader("Location", rs.getHref());
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());
}
} catch (ServiceException e) {
if (e.getCode().equals(ServiceException.FORBIDDEN)) {
throw new DavException(e.getMessage(), HttpServletResponse.SC_FORBIDDEN, e);
} else {
throw new DavException("cannot create icalendar item", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
}
}
}
use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.
the class Collection method createItem.
// create Document at the URI
public DavResource createItem(DavContext ctxt, String name) throws DavException, IOException {
Mailbox mbox = null;
try {
mbox = getMailbox(ctxt);
} catch (ServiceException e) {
throw new DavException("cannot get mailbox", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
}
FileUploadServlet.Upload upload = ctxt.getUpload();
String ctype = upload.getContentType();
if (ctype != null && (ctype.startsWith(DavProtocol.VCARD_CONTENT_TYPE) || ctype.startsWith(MimeConstants.CT_TEXT_VCARD_LEGACY) || ctype.startsWith(MimeConstants.CT_TEXT_VCARD_LEGACY2))) {
// vCard MIME types such as text/x-vcard or text/directory.
return createVCard(ctxt, name);
}
String author = ctxt.getAuthAccount().getName();
try {
// add a revision if the resource already exists
MailItem item = mbox.getItemByPath(ctxt.getOperationContext(), ctxt.getPath());
if (item.getType() != MailItem.Type.DOCUMENT && item.getType() != MailItem.Type.WIKI) {
throw new DavException("no DAV resource for " + item.getType(), HttpServletResponse.SC_NOT_ACCEPTABLE, null);
}
Document doc = mbox.addDocumentRevision(ctxt.getOperationContext(), item.getId(), author, name, null, upload.getInputStream());
return new Notebook(ctxt, doc);
} catch (ServiceException e) {
if (!(e instanceof NoSuchItemException))
throw new DavException("cannot get item ", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
}
// create
try {
Document doc = mbox.createDocument(ctxt.getOperationContext(), mId, name, ctype, author, null, upload.getInputStream());
Notebook notebook = new Notebook(ctxt, doc);
notebook.mNewlyCreated = true;
return notebook;
} catch (ServiceException se) {
throw new DavException("cannot create ", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, se);
}
}
use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.
the class CalendarCollection method uploadToInvites.
private List<Invite> uploadToInvites(DavContext ctxt, Account account) throws DavException, IOException {
Upload upload = ctxt.getUpload();
String contentType = upload.getContentType();
if (!contentType.startsWith(MimeConstants.CT_TEXT_CALENDAR)) {
throw new DavException.InvalidData(DavElements.E_SUPPORTED_CALENDAR_DATA, String.format("Incorrect Content-Type '%s', expected '%s'", contentType, MimeConstants.CT_TEXT_CALENDAR));
}
if (upload.getSize() <= 0) {
throw new DavException.InvalidData(DavElements.E_VALID_CALENDAR_DATA, "empty request");
}
List<Invite> invites;
try (InputStream is = ctxt.getUpload().getInputStream()) {
ZCalendar.ZVCalendar vcalendar = ZCalendar.ZCalendarBuilder.build(is, MimeConstants.P_CHARSET_UTF8);
// Apple iCal fixup
CalDavUtils.removeAttendeeForOrganizer(vcalendar);
if (ctxt.isIcalClient()) {
// Apple iCal fixup for todos
CalDavUtils.adjustPercentCompleteForToDos(vcalendar);
}
invites = Invite.createFromCalendar(account, findSummary(vcalendar), vcalendar, true);
} catch (ServiceException se) {
throw new DavException.InvalidData(DavElements.E_VALID_CALENDAR_DATA, String.format("Problem parsing %s data - %s", MimeConstants.CT_TEXT_CALENDAR, se.getMessage()));
}
return invites;
}
use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.
the class Acl method handle.
public void handle(DavContext ctxt) throws DavException, IOException, ServiceException {
DavResource rs = ctxt.getRequestedResource();
if (!rs.isCollection() || !(rs instanceof MailItemResource))
throw new DavException("acl not implemented for non-collection resource", HttpServletResponse.SC_NOT_IMPLEMENTED);
if (!ctxt.hasRequestMessage())
throw new DavException("empty request", HttpServletResponse.SC_BAD_REQUEST);
Document reqMsg = ctxt.getRequestMessage();
Element acl = reqMsg.getRootElement();
if (!acl.getQName().equals(DavElements.E_ACL))
throw new DavException("request does not start with acl element", HttpServletResponse.SC_BAD_REQUEST);
List<Element> aceElements = acl.elements(DavElements.E_ACE);
ArrayList<Ace> aceList = new ArrayList<Ace>();
for (Element ace : aceElements) aceList.add(new Ace(ace));
MailItemResource mir = (MailItemResource) rs;
mir.setAce(ctxt, aceList);
}
use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.
the class ScheduleOutbox method handleFreebusyRequest.
private void handleFreebusyRequest(DavContext ctxt, ZComponent vfreebusy, String originator, String rcpt, Element resp) throws DavException, ServiceException {
ZProperty dtstartProp = vfreebusy.getProperty(ICalTok.DTSTART);
ZProperty dtendProp = vfreebusy.getProperty(ICalTok.DTEND);
ZProperty durationProp = vfreebusy.getProperty(ICalTok.DURATION);
if (dtstartProp == null || dtendProp == null && durationProp == null)
throw new DavException("missing dtstart or dtend/duration in the schedule request", HttpServletResponse.SC_BAD_REQUEST, null);
long start, end;
try {
ParsedDateTime startTime = ParsedDateTime.parseUtcOnly(dtstartProp.getValue());
start = startTime.getUtcTime();
if (dtendProp != null) {
end = ParsedDateTime.parseUtcOnly(dtendProp.getValue()).getUtcTime();
} else {
ParsedDuration dur = ParsedDuration.parse(durationProp.getValue());
ParsedDateTime endTime = startTime.add(dur);
end = endTime.getUtcTime();
}
} catch (ParseException pe) {
throw new DavException("can't parse date", HttpServletResponse.SC_BAD_REQUEST, pe);
}
ZimbraLog.dav.debug("rcpt: " + rcpt + ", start: " + new Date(start) + ", end: " + new Date(end));
FreeBusy fb = null;
if (ctxt.isFreebusyEnabled()) {
FreeBusyQuery fbQuery = new FreeBusyQuery(ctxt.getRequest(), ctxt.getAuthAccount(), start, end, null);
fbQuery.addEmailAddress(getAddressFromPrincipalURL(rcpt), FreeBusyQuery.CALENDAR_FOLDER_ALL);
java.util.Collection<FreeBusy> fbResult = fbQuery.getResults();
if (fbResult.size() > 0)
fb = fbResult.iterator().next();
}
if (fb != null) {
String fbMsg = fb.toVCalendar(FreeBusy.Method.REPLY, originator, rcpt, null);
resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
resp.addElement(DavElements.E_REQUEST_STATUS).setText("2.0;Success");
resp.addElement(DavElements.E_CALENDAR_DATA).setText(fbMsg);
} else {
resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
resp.addElement(DavElements.E_REQUEST_STATUS).setText("5.3;No f/b for the user");
}
}
Aggregations