Search in sources :

Example 11 with NotificationType

use of org.bedework.caldav.util.notifications.NotificationType in project bw-calendar-engine by Bedework.

the class Notifications method getMatching.

@Override
public List<NotificationType> getMatching(final QName type) throws CalFacadeException {
    final List<NotificationType> res = new ArrayList<>();
    final BwCalendar ncol = getCols().getSpecial(BwCalendar.calTypeNotifications, true);
    if (ncol == null) {
        return res;
    }
    final Collection<BwResource> rsrc = getSvc().getResourcesHandler().getAll(ncol.getPath());
    if (Util.isEmpty(rsrc)) {
        return res;
    }
    if (rsrc.size() > 100) {
        warn("Large resource collection for " + ncol.getPath());
    }
    for (final BwResource r : rsrc) {
        if (type != null) {
            final NotificationInfo ni = NotificationType.fromContentType(r.getContentType());
            if ((ni == null) || !type.equals(ni.type)) {
                continue;
            }
        }
        final NotificationType nt = makeNotification(r);
        if (nt != null) {
            res.add(nt);
        }
    }
    return res;
}
Also used : NotificationInfo(org.bedework.caldav.util.notifications.NotificationType.NotificationInfo) NotificationType(org.bedework.caldav.util.notifications.NotificationType) BwResource(org.bedework.calfacade.BwResource) ArrayList(java.util.ArrayList) BwCalendar(org.bedework.calfacade.BwCalendar)

Example 12 with NotificationType

use of org.bedework.caldav.util.notifications.NotificationType in project bw-calendar-engine by Bedework.

the class Notifications method makeNotification.

private NotificationType makeNotification(final BwResource rsrc) throws CalFacadeException {
    getSvc().getResourcesHandler().getContent(rsrc);
    final BwResourceContent bwrc = rsrc.getContent();
    if (bwrc == null) {
        return null;
    }
    final Blob b = bwrc.getValue();
    if (b == null) {
        return null;
    }
    try {
        final InputStream is = b.getBinaryStream();
        final NotificationType note = Parser.fromXml(is);
        if (note != null) {
            note.setName(rsrc.getName());
            note.getNotification().setEncoding(rsrc.getEncoding());
        }
        return note;
    } catch (final Throwable t) {
        if (debug) {
            error(t);
        }
        error("Unable to parse notification " + rsrc.getColPath() + " " + rsrc.getName());
        return null;
    }
}
Also used : Blob(java.sql.Blob) InputStream(java.io.InputStream) NotificationType(org.bedework.caldav.util.notifications.NotificationType) BwResourceContent(org.bedework.calfacade.BwResourceContent)

Example 13 with NotificationType

use of org.bedework.caldav.util.notifications.NotificationType in project bw-calendar-engine by Bedework.

the class Sharing method share.

@Override
public ShareResultType share(final BwCalendar col, final ShareType share) throws CalFacadeException {
    if (!col.getCanAlias()) {
        throw new CalFacadeForbidden("Cannot share");
    }
    final ShareResultType sr = new ShareResultType();
    final List<String> removePrincipalHrefs = new ArrayList<>();
    final List<AddPrincipal> addPrincipals = new ArrayList<>();
    final String calAddr = principalToCaladdr(getPrincipal());
    final InviteType invite = getInviteStatus(col);
    if (invite.getOrganizer() == null) {
        final OrganizerType org = new OrganizerType();
        org.setHref(calAddr);
        invite.setOrganizer(org);
    }
    final List<InviteNotificationType> notifications = new ArrayList<>();
    boolean addedSharee = false;
    boolean removedSharee = false;
    /* If there are any removal elements in the invite, remove those
     * sharees. We'll flag hrefs as bad if they are not actually sharees.
     *
     * If we do remove a sharee we'll add notifications to the list
     * to send later.
     */
    for (final RemoveType rem : share.getRemove()) {
        final InviteNotificationType n = doRemove(col, rem, calAddr, invite);
        if (n != null) {
            removedSharee = true;
            if ((n.getPreviousStatus() != null) && !n.getPreviousStatus().equals(declineStatus)) {
                // We don't notify if the user had declined
                notifications.add(n);
            }
            sr.addGood(rem.getHref());
            removePrincipalHrefs.add(rem.getHref());
        } else {
            sr.addBad(rem.getHref());
        }
    }
    /* Now deal with the added sharees if there are any.
     */
    for (final SetType set : share.getSet()) {
        final InviteNotificationType n = doSet(col, set, addPrincipals, calAddr, invite);
        if (n != null) {
            addedSharee = true;
            notifications.add(n);
            sr.addGood(set.getHref());
        } else {
            sr.addBad(set.getHref());
        }
    }
    if (!addedSharee && !removedSharee) {
        // Nothing changed
        return sr;
    }
    /* Send any invitations and update the sharing status.
     * If it's a removal and the current status is not
     * accepted then just delete the current invitation
     */
    final Notifications notify = (Notifications) getSvc().getNotificationsHandler();
    sendNotifications: for (final InviteNotificationType in : notifications) {
        final Sharee sh = getSharee(in.getHref());
        final boolean remove = in.getInviteStatus().equals(removeStatus);
        final List<NotificationType> notes = notify.getMatching(in.getHref(), AppleServerTags.inviteNotification);
        if (!Util.isEmpty(notes)) {
            for (final NotificationType n : notes) {
                final InviteNotificationType nin = (InviteNotificationType) n.getNotification();
                if (!nin.getHostUrl().equals(in.getHostUrl())) {
                    continue;
                }
                if (remove) {
                    if (nin.getInviteStatus().equals(noresponseStatus)) {
                        notify.remove(sh.pr, n);
                        continue sendNotifications;
                    }
                } else {
                    notify.remove(sh.pr, n);
                }
            }
        }
        final NotificationType note = new NotificationType();
        note.setDtstamp(new DtStamp(new DateTime(true)).getValue());
        note.setNotification(in);
        notify.send(sh.pr, note);
        /* Add the invite to the set of properties associated with this collection
       * We give it a name consisting of the inviteNotification tag + uid.
       */
        final QName qn = new QName(AppleServerTags.inviteNotification.getNamespaceURI(), AppleServerTags.inviteNotification.getLocalPart() + in.getUid());
        try {
            col.setProperty(NamespaceAbbrevs.prefixed(qn), in.toXml());
        } catch (final CalFacadeException cfe) {
            throw cfe;
        } catch (final Throwable t) {
            throw new CalFacadeException(t);
        }
    }
    if (addedSharee && !col.getShared()) {
        // Mark the collection as shared
        col.setShared(true);
    }
    try {
        col.setQproperty(AppleServerTags.invite, invite.toXml());
        getCols().update(col);
        for (final String principalHref : removePrincipalHrefs) {
            removeAccess(col, principalHref);
        }
        for (final AddPrincipal ap : addPrincipals) {
            setAccess(col, ap);
        }
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    }
    return sr;
}
Also used : ShareResultType(org.bedework.caldav.util.sharing.ShareResultType) InviteNotificationType(org.bedework.caldav.util.sharing.InviteNotificationType) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) CalFacadeForbidden(org.bedework.calfacade.exc.CalFacadeForbidden) OrganizerType(org.bedework.caldav.util.sharing.OrganizerType) DateTime(net.fortuna.ical4j.model.DateTime) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) RemoveType(org.bedework.caldav.util.sharing.RemoveType) DtStamp(net.fortuna.ical4j.model.property.DtStamp) SetType(org.bedework.caldav.util.sharing.SetType) NotificationType(org.bedework.caldav.util.notifications.NotificationType) InviteNotificationType(org.bedework.caldav.util.sharing.InviteNotificationType) InviteType(org.bedework.caldav.util.sharing.InviteType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 14 with NotificationType

use of org.bedework.caldav.util.notifications.NotificationType in project bw-calendar-engine by Bedework.

the class Sharing method updateSharingStatus.

/* ====================================================================
   *                   Private methods
   * ==================================================================== */
/* This requires updating the shared calendar to reflect the accept/decline
   * status
   */
private boolean updateSharingStatus(final String sharerHref, final String path, final InviteReplyType reply, final Holder<AccessType> access) throws CalFacadeException {
    pushPrincipal(sharerHref);
    try {
        final BwCalendar col = getCols().get(path);
        if (col == null) {
            // Bad hosturl?
            throw new CalFacadeForbidden(CalFacadeException.shareTargetNotFound);
        }
        /* See if we have an outstanding invite for this user */
        final QName qn = new QName(AppleServerTags.inviteNotification.getNamespaceURI(), AppleServerTags.inviteNotification.getLocalPart() + reply.getInReplyTo());
        final String pname = NamespaceAbbrevs.prefixed(qn);
        final String xmlInvite = col.getProperty(pname);
        if (xmlInvite == null) {
            // No invite
            if (debug) {
                trace("No invite notification on collection with name: " + pname);
            }
            throw new CalFacadeForbidden(CalFacadeException.noInvite);
        }
        /* Remove the invite */
        col.setProperty(pname, null);
        /* Get the invite property and locate and update this sharee */
        final InviteType invite = getInviteStatus(col);
        UserType uentry = null;
        final String invitee = getSvc().getDirectories().normalizeCua(reply.getHref());
        if (invite != null) {
            uentry = invite.finduser(invitee);
        }
        if (uentry == null) {
            if (debug) {
                trace("Cannot find invitee: " + invitee);
            }
            throw new CalFacadeForbidden(CalFacadeException.noInviteeInUsers);
        }
        if (reply.testAccepted()) {
            uentry.setInviteStatus(AppleServerTags.inviteAccepted);
        } else {
            uentry.setInviteStatus(AppleServerTags.inviteDeclined);
        }
        access.value = uentry.getAccess();
        col.setProperty(NamespaceAbbrevs.prefixed(AppleServerTags.invite), invite.toXml());
        getCols().update(col);
        /* Now send the sharer the reply as a notification */
        final NotificationType note = new NotificationType();
        note.setDtstamp(new DtStamp(new DateTime(true)).getValue());
        final InviteReplyType irt = (InviteReplyType) reply.clone();
        note.setNotification(irt);
        /* Fill in the summary (the sharer's summary) on the reply. */
        irt.setSummary(reply.getSummary());
        getSvc().getNotificationsHandler().add(note);
        return irt.testAccepted();
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    } finally {
        popPrincipal();
    }
}
Also used : DtStamp(net.fortuna.ical4j.model.property.DtStamp) QName(javax.xml.namespace.QName) NotificationType(org.bedework.caldav.util.notifications.NotificationType) InviteNotificationType(org.bedework.caldav.util.sharing.InviteNotificationType) InviteType(org.bedework.caldav.util.sharing.InviteType) InviteReplyType(org.bedework.caldav.util.sharing.InviteReplyType) CalFacadeForbidden(org.bedework.calfacade.exc.CalFacadeForbidden) BwCalendar(org.bedework.calfacade.BwCalendar) UserType(org.bedework.caldav.util.sharing.UserType) DateTime(net.fortuna.ical4j.model.DateTime) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Aggregations

NotificationType (org.bedework.caldav.util.notifications.NotificationType)14 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)6 InviteNotificationType (org.bedework.caldav.util.sharing.InviteNotificationType)4 DateTime (net.fortuna.ical4j.model.DateTime)3 DtStamp (net.fortuna.ical4j.model.property.DtStamp)3 BaseNotificationType (org.bedework.caldav.util.notifications.BaseNotificationType)3 AdminNotificationType (org.bedework.caldav.util.notifications.admin.AdminNotificationType)3 ApprovalResponseNotificationType (org.bedework.caldav.util.notifications.admin.ApprovalResponseNotificationType)3 AwaitingApprovalNotificationType (org.bedework.caldav.util.notifications.admin.AwaitingApprovalNotificationType)3 SuggestBaseNotificationType (org.bedework.caldav.util.notifications.suggest.SuggestBaseNotificationType)3 SuggestNotificationType (org.bedework.caldav.util.notifications.suggest.SuggestNotificationType)3 SuggestResponseNotificationType (org.bedework.caldav.util.notifications.suggest.SuggestResponseNotificationType)3 InviteType (org.bedework.caldav.util.sharing.InviteType)3 UserType (org.bedework.caldav.util.sharing.UserType)3 BwCalendar (org.bedework.calfacade.BwCalendar)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 QName (javax.xml.namespace.QName)2 ResourceChangeType (org.bedework.caldav.util.notifications.ResourceChangeType)2 InviteReplyType (org.bedework.caldav.util.sharing.InviteReplyType)2