Search in sources :

Example 1 with WatchListException

use of org.xwiki.watchlist.internal.api.WatchListException in project xwiki-platform by xwiki.

the class DefaultWatchListNotifier method sendNotification.

@Override
public void sendNotification(Collection<String> subscribers, List<WatchListEvent> events, Map<String, Object> notificationData) throws WatchListException {
    try {
        // FIXME: Temporary, until we move to all references.
        List<DocumentReference> subscriberReferences = getSubscriberReferences(subscribers);
        // Source
        Map<String, Object> source = new HashMap<>();
        source.put(EventsAndSubscribersSource.SUBSCRIBERS_PARAMETER, subscriberReferences);
        source.put(EventsAndSubscribersSource.EVENTS_PARAMETER, events);
        // Parameters
        Map<String, Object> parameters = new HashMap<>();
        parameters.put(WatchListEventMimeMessageFactory.HINT_PARAMETER, "template");
        parameters.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, notificationData.get(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER));
        parameters.put(WatchListEventMimeMessageFactory.SKIP_CONTEXT_USER_PARAMETER, notificationData.get(WatchListEventMimeMessageFactory.SKIP_CONTEXT_USER_PARAMETER));
        parameters.put(WatchListEventMimeMessageFactory.ATTACH_AUTHOR_AVATARS_PARAMETER, notificationData.get(WatchListEventMimeMessageFactory.ATTACH_AUTHOR_AVATARS_PARAMETER));
        Map<String, Object> templateFactoryParameters = getTemplateFactoryParameters(notificationData);
        parameters.put(WatchListEventMimeMessageFactory.PARAMETERS_PARAMETER, templateFactoryParameters);
        // Create the message iterator and the other mail sender parameters.
        Iterator<MimeMessage> messageIterator = messageFactory.createMessage(source, parameters);
        Session session = this.sessionFactory.create(Collections.<String, String>emptyMap());
        MailListener mailListener = mailListenerProvider.get();
        // Pass it to the message sender to send it asynchronously.
        // FIXME: !? There must be a better way instead of using IteratorIterable.
        mailSender.sendAsynchronously(new IteratorIterable<MimeMessage>(messageIterator), session, mailListener);
    } catch (Exception e) {
        throw new WatchListException(String.format("Failed to send notification to subscribers [%s]", subscribers), e);
    }
}
Also used : MailListener(org.xwiki.mail.MailListener) HashMap(java.util.HashMap) WatchListException(org.xwiki.watchlist.internal.api.WatchListException) XWikiException(com.xpn.xwiki.XWikiException) WatchListException(org.xwiki.watchlist.internal.api.WatchListException) MimeMessage(javax.mail.internet.MimeMessage) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(javax.mail.Session)

Example 2 with WatchListException

use of org.xwiki.watchlist.internal.api.WatchListException in project xwiki-platform by xwiki.

the class DefaultWatchListNotifier method sendNotification.

@Override
@Deprecated
public void sendNotification(String subscriber, List<WatchListEvent> events, String templateDocument, Date previousFireTime) throws XWikiException {
    Map<String, Object> notificationData = new HashMap<>();
    notificationData.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, templateDocument);
    notificationData.put(PREVIOUS_FIRE_TIME_VARIABLE, previousFireTime);
    try {
        this.sendNotification(Arrays.asList(subscriber), events, notificationData);
    } catch (WatchListException e) {
        throw new XWikiException("", e);
    }
}
Also used : HashMap(java.util.HashMap) WatchListException(org.xwiki.watchlist.internal.api.WatchListException) XWikiException(com.xpn.xwiki.XWikiException)

Example 3 with WatchListException

use of org.xwiki.watchlist.internal.api.WatchListException in project xwiki-platform by xwiki.

the class DefaultWatchListEventHTMLDiffExtractor method getHTMLDiff.

@Override
public String getHTMLDiff(WatchListEvent event) throws WatchListException {
    XWikiContext context = contextProvider.get();
    StringBuffer result = new StringBuffer();
    try {
        DiffPluginApi diff = (DiffPluginApi) context.getWiki().getPluginApi("diff", context);
        XWikiDocument d2 = context.getWiki().getDocument(event.getDocumentReference(), context);
        if (event.getType().equals(WatchListEventType.CREATE)) {
            d2 = context.getWiki().getDocument(d2, INITIAL_DOCUMENT_VERSION, context);
        }
        XWikiDocument d1 = context.getWiki().getDocument(d2, event.getPreviousVersion(), context);
        List<AttachmentDiff> attachDiffs = d2.getAttachmentDiff(d1, d2, context);
        List<List<ObjectDiff>> objectDiffs = d2.getObjectDiff(d1, d2, context);
        List<List<ObjectDiff>> classDiffs = d2.getClassDiff(d1, d2, context);
        List<MetaDataDiff> metaDiffs = d2.getMetaDataDiff(d1, d2, context);
        if (!d1.getContent().equals(d2.getContent())) {
            Div contentDiv = createDiffDiv("contentDiff");
            String contentDiff = diff.getDifferencesAsHTML(d1.getContent(), d2.getContent(), false);
            contentDiv.addElement(contentDiff);
            result.append(contentDiv);
        }
        for (AttachmentDiff aDiff : attachDiffs) {
            Div attachmentDiv = createDiffDiv("attachmentDiff");
            attachmentDiv.addElement(HTML_IMG_ATTACHMENT_PREFIX + HTML_IMG_PLACEHOLDER_SUFFIX);
            attachmentDiv.addElement(aDiff.toString());
            result.append(attachmentDiv);
        }
        result.append(getObjectsHTMLDiff(objectDiffs, false, event.getFullName(), diff));
        result.append(getObjectsHTMLDiff(classDiffs, true, event.getFullName(), diff));
        for (MetaDataDiff mDiff : metaDiffs) {
            Div metaDiv = createDiffDiv("metaDiff");
            metaDiv.addElement(HTML_IMG_METADATA_PREFIX + HTML_IMG_PLACEHOLDER_SUFFIX);
            metaDiv.addElement(mDiff.toString());
            result.append(metaDiv);
        }
        return result.toString();
    } catch (Exception e) {
        throw new WatchListException(String.format("Failed to compute HTML diff for event type [%s] on [%s]", event.getType(), event.getPrefixedFullName()), e);
    }
}
Also used : MetaDataDiff(com.xpn.xwiki.doc.MetaDataDiff) DiffPluginApi(com.xpn.xwiki.plugin.diff.DiffPluginApi) WatchListException(org.xwiki.watchlist.internal.api.WatchListException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) WatchListException(org.xwiki.watchlist.internal.api.WatchListException) Div(org.apache.ecs.html.Div) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) List(java.util.List) AttachmentDiff(com.xpn.xwiki.doc.AttachmentDiff)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)3 WatchListException (org.xwiki.watchlist.internal.api.WatchListException)3 HashMap (java.util.HashMap)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 AttachmentDiff (com.xpn.xwiki.doc.AttachmentDiff)1 MetaDataDiff (com.xpn.xwiki.doc.MetaDataDiff)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 DiffPluginApi (com.xpn.xwiki.plugin.diff.DiffPluginApi)1 List (java.util.List)1 Session (javax.mail.Session)1 MimeMessage (javax.mail.internet.MimeMessage)1 Div (org.apache.ecs.html.Div)1 MailListener (org.xwiki.mail.MailListener)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1