Search in sources :

Example 46 with DavException

use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.

the class UrlNamespace method getCollectionAtUrl.

/* Returns Collection at the specified URL. */
public static Collection getCollectionAtUrl(DavContext ctxt, String url) throws DavException {
    UrlComponents uc = parseUrl(url);
    int lastPos = uc.path.length() - 1;
    if (uc.path.endsWith("/"))
        lastPos--;
    int index = uc.path.lastIndexOf('/', lastPos);
    String path;
    if (index == -1)
        path = "/";
    else
        path = uc.path.substring(0, index);
    String user = uc.user;
    if (user == null)
        user = ctxt.getUser();
    DavResource rsc = getResourceAt(new DavContext(ctxt, path), user, path);
    if (rsc instanceof Collection)
        return (Collection) rsc;
    throw new DavException("invalid uri", HttpServletResponse.SC_NOT_FOUND, null);
}
Also used : DavException(com.zimbra.cs.dav.DavException) DavContext(com.zimbra.cs.dav.DavContext) Mountpoint(com.zimbra.cs.mailbox.Mountpoint)

Example 47 with DavException

use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.

the class UrlNamespace method getResourceAtUrl.

/* Returns DavResource at the specified URL. */
public static DavResource getResourceAtUrl(DavContext ctxt, String url) throws DavException {
    if (url.indexOf(PRINCIPALS_PATH) >= 0)
        return getPrincipalAtUrl(ctxt, url);
    UrlComponents uc = parseUrl(url);
    if (uc.user == null || uc.path == null)
        throw new DavException("invalid uri", HttpServletResponse.SC_NOT_FOUND, null);
    DavResource rs = getResourceAt(ctxt, uc.user, uc.path);
    if (rs != null)
        rs.mUri = uc.path;
    return rs;
}
Also used : DavException(com.zimbra.cs.dav.DavException)

Example 48 with DavException

use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.

the class MailItemResource method getMailbox.

protected Mailbox getMailbox(DavContext ctxt) throws ServiceException, DavException {
    Provisioning prov = Provisioning.getInstance();
    Account account = prov.get(AccountBy.id, mOwnerId);
    if (account == null) {
        // Anti-account name harvesting.
        ZimbraLog.dav.info("Failing GET of mailbox for item resource - no such account '%s'", mOwnerId);
        throw new DavException("Request denied", HttpServletResponse.SC_NOT_FOUND, null);
    }
    return MailboxManager.getInstance().getMailboxByAccount(account);
}
Also used : Account(com.zimbra.cs.account.Account) DavException(com.zimbra.cs.dav.DavException) Provisioning(com.zimbra.cs.account.Provisioning)

Example 49 with DavException

use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.

the class CalDavDataImport method applyRemoteItem.

private MailItem applyRemoteItem(RemoteItem remoteItem, Folder where) throws ServiceException, IOException {
    if (!(remoteItem instanceof RemoteCalendarItem)) {
        ZimbraLog.datasource.warn("applyRemoteItem: not a calendar item: %s", remoteItem);
        return null;
    }
    RemoteCalendarItem item = (RemoteCalendarItem) remoteItem;
    DataSource ds = getDataSource();
    DataSourceItem dsItem = DbDataSource.getReverseMapping(ds, item.href);
    OperationContext octxt = new OperationContext(mbox);
    MailItem mi = null;
    boolean isStale = false;
    boolean isCreate = false;
    if (dsItem.md == null && item.status != Status.deleted) {
        dsItem.md = new Metadata();
        dsItem.md.put(METADATA_KEY_TYPE, METADATA_TYPE_APPOINTMENT);
    }
    if (dsItem.itemId == 0) {
        isStale = true;
        isCreate = true;
    } else {
        String etag = dsItem.md.get(METADATA_KEY_ETAG, null);
        try {
            mi = mbox.getItemById(octxt, dsItem.itemId, MailItem.Type.UNKNOWN);
        } catch (MailServiceException.NoSuchItemException se) {
            ZimbraLog.datasource.warn("applyRemoteItem: calendar item not found: ", remoteItem);
        }
        if (item.etag == null) {
            ZimbraLog.datasource.warn("No Etag returned for item %s", item.href);
            isStale = true;
        } else if (etag == null) {
            ZimbraLog.datasource.warn("Empty etag for item %d", dsItem.itemId);
            isStale = true;
        } else {
            isStale = !item.etag.equals(etag);
        }
        if (mi == null)
            isStale = true;
    }
    if (item.status == Status.deleted) {
        ZimbraLog.datasource.debug("Deleting appointment %s", item.href);
        try {
            mi = mbox.getItemById(octxt, item.itemId, MailItem.Type.UNKNOWN);
        } catch (NoSuchItemException se) {
            mi = null;
        }
        try {
            mbox.delete(octxt, item.itemId, MailItem.Type.UNKNOWN);
        } catch (ServiceException se) {
            ZimbraLog.datasource.warn("Error deleting remotely deleted item %d (%s)", item.itemId, dsItem.remoteId);
        }
    } else if (isStale) {
        ZimbraLog.datasource.debug("Updating stale appointment %s", item.href);
        ZCalendar.ZVCalendar vcalendar;
        SetCalendarItemData main = new SetCalendarItemData();
        SetCalendarItemData[] exceptions = null;
        CalDavClient client = null;
        try {
            client = getClient();
        } catch (DavException e) {
            throw ServiceException.FAILURE("error creating CalDAV client", e);
        }
        Appointment appt = client.getCalendarData(new Appointment(item.href, item.etag));
        if (appt.data == null) {
            ZimbraLog.datasource.warn("No appointment found at " + item.href);
            return null;
        }
        dsItem.md.put(METADATA_KEY_ETAG, appt.etag);
        try {
            vcalendar = ZCalendar.ZCalendarBuilder.build(appt.data);
            List<Invite> invites = Invite.createFromCalendar(mbox.getAccount(), null, vcalendar, true);
            if (invites.size() > 1)
                exceptions = new SetCalendarItemData[invites.size() - 1];
            int pos = 0;
            boolean first = true;
            for (Invite i : invites) {
                if (first) {
                    main.invite = i;
                    first = false;
                } else {
                    SetCalendarItemData scid = new SetCalendarItemData();
                    scid.invite = i;
                    exceptions[pos++] = scid;
                }
            }
        } catch (Exception e) {
            ZimbraLog.datasource.warn("Error parsing appointment ", e);
            return null;
        }
        mi = mbox.setCalendarItem(octxt, where.getId(), 0, null, main, exceptions, null, CalendarItem.NEXT_ALARM_KEEP_CURRENT);
        dsItem.itemId = mi.getId();
        dsItem.folderId = mi.getFolderId();
        if (isCreate) {
            DbDataSource.addMapping(ds, dsItem);
        } else {
            DbDataSource.updateMapping(ds, dsItem);
        }
    } else {
        ZimbraLog.datasource.debug("Appointment up to date %s", item.href);
        try {
            mi = mbox.getItemById(octxt, dsItem.itemId, MailItem.Type.UNKNOWN);
        } catch (NoSuchItemException se) {
            // item not found.  delete the mapping so it can be downloaded again if needed.
            ArrayList<Integer> deletedIds = new ArrayList<Integer>();
            deletedIds.add(dsItem.itemId);
            DbDataSource.deleteMappings(ds, deletedIds);
        }
    }
    return mi;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Appointment(com.zimbra.cs.dav.client.CalDavClient.Appointment) DavException(com.zimbra.cs.dav.DavException) Metadata(com.zimbra.cs.mailbox.Metadata) ArrayList(java.util.ArrayList) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) CalDavClient(com.zimbra.cs.dav.client.CalDavClient) ServiceException(com.zimbra.common.service.ServiceException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) IOException(java.io.IOException) DavException(com.zimbra.cs.dav.DavException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DbDataSource(com.zimbra.cs.db.DbDataSource) DataSource(com.zimbra.cs.account.DataSource) SetCalendarItemData(com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData) MailItem(com.zimbra.cs.mailbox.MailItem) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ArrayList(java.util.ArrayList) List(java.util.List) DataSourceItem(com.zimbra.cs.db.DbDataSource.DataSourceItem) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 50 with DavException

use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.

the class CalDavClient method login.

public void login(String defaultPrincipalUrl) throws IOException, DavException {
    String principalUrl = getCurrentUserPrincipal();
    if (principalUrl == null) {
        principalUrl = defaultPrincipalUrl;
    }
    DavRequest propfind = DavRequest.PROPFIND(principalUrl);
    propfind.addRequestProp(DavElements.E_DISPLAYNAME);
    propfind.addRequestProp(DavElements.E_CALENDAR_HOME_SET);
    propfind.addRequestProp(DavElements.E_SCHEDULE_INBOX_URL);
    propfind.addRequestProp(DavElements.E_SCHEDULE_OUTBOX_URL);
    Collection<DavObject> response = sendMultiResponseRequest(propfind);
    if (response.size() != 1) {
        throw new DavException(String.format("invalid response to propfind on principal url '%s'", principalUrl), null);
    }
    DavObject resp = response.iterator().next();
    mCalendarHomeSet = new HashSet<String>();
    Element homeSet = resp.getProperty(DavElements.E_CALENDAR_HOME_SET);
    if (homeSet != null) {
        for (Object href : homeSet.elements(DavElements.E_HREF)) {
            String hrefVal = ((Element) href).getText();
            mCalendarHomeSet.add(hrefVal);
        }
    }
    if (mCalendarHomeSet.isEmpty()) {
        throw new DavException("dav response from principal url does not contain calendar-home-set", null);
    }
    Element elem = resp.getProperty(DavElements.E_SCHEDULE_INBOX_URL);
    if (elem != null && elem.element(DavElements.E_HREF) != null) {
        mScheduleInbox = elem.element(DavElements.E_HREF).getText();
    }
    elem = resp.getProperty(DavElements.E_SCHEDULE_OUTBOX_URL);
    if (elem != null && elem.element(DavElements.E_HREF) != null) {
        mScheduleOutbox = elem.element(DavElements.E_HREF).getText();
    }
}
Also used : DavException(com.zimbra.cs.dav.DavException) Element(org.dom4j.Element)

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