Search in sources :

Example 11 with DavException

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);
        }
    }
}
Also used : Account(com.zimbra.cs.account.Account) ServiceException(com.zimbra.common.service.ServiceException) DavException(com.zimbra.cs.dav.DavException) Provisioning(com.zimbra.cs.account.Provisioning) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 12 with DavException

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);
    }
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DavException(com.zimbra.cs.dav.DavException) Document(com.zimbra.cs.mailbox.Document) FileUploadServlet(com.zimbra.cs.service.FileUploadServlet) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)

Example 13 with DavException

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;
}
Also used : ZCalendar(com.zimbra.common.calendar.ZCalendar) ServiceException(com.zimbra.common.service.ServiceException) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) DavException(com.zimbra.cs.dav.DavException) InputStream(java.io.InputStream) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 14 with DavException

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);
}
Also used : Ace(com.zimbra.cs.dav.property.Acl.Ace) DavResource(com.zimbra.cs.dav.resource.DavResource) MailItemResource(com.zimbra.cs.dav.resource.MailItemResource) DavException(com.zimbra.cs.dav.DavException) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Document(org.dom4j.Document)

Example 15 with DavException

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");
    }
}
Also used : FreeBusy(com.zimbra.cs.fb.FreeBusy) DavException(com.zimbra.cs.dav.DavException) ParsedDuration(com.zimbra.common.calendar.ParsedDuration) FreeBusyQuery(com.zimbra.cs.fb.FreeBusyQuery) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) ParseException(java.text.ParseException) Date(java.util.Date)

Aggregations

DavException (com.zimbra.cs.dav.DavException)67 ServiceException (com.zimbra.common.service.ServiceException)27 Element (org.dom4j.Element)25 Account (com.zimbra.cs.account.Account)18 Mailbox (com.zimbra.cs.mailbox.Mailbox)18 DavResource (com.zimbra.cs.dav.resource.DavResource)15 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)15 ArrayList (java.util.ArrayList)14 Provisioning (com.zimbra.cs.account.Provisioning)11 Document (org.dom4j.Document)9 DavResponse (com.zimbra.cs.dav.service.DavResponse)8 MailItem (com.zimbra.cs.mailbox.MailItem)8 Invite (com.zimbra.cs.mailbox.calendar.Invite)7 ZMailbox (com.zimbra.client.ZMailbox)6 RequestProp (com.zimbra.cs.dav.DavContext.RequestProp)6 IOException (java.io.IOException)6 Collection (com.zimbra.cs.dav.resource.Collection)5 Folder (com.zimbra.cs.mailbox.Folder)5 CalendarCollection (com.zimbra.cs.dav.resource.CalendarCollection)4 ZProperty (com.zimbra.common.calendar.ZCalendar.ZProperty)3