Search in sources :

Example 11 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class AbstractMimeMessageIterator method computeNext.

/**
 * Compute the message that will be sent to the next user in the iterator.
 */
protected void computeNext() {
    this.currentEvents = Collections.emptyList();
    this.currentUserEmail = null;
    while ((this.currentEvents.isEmpty() || currentUserEmail == null) && this.userIterator.hasNext()) {
        this.currentUser = this.userIterator.next();
        try {
            this.currentUserEmail = new InternetAddress(getUserEmail(this.currentUser));
        } catch (AddressException e) {
            // The user has not written a valid email
            continue;
        }
        try {
            // TODO: in a next version, it will be import to paginate these results and to send several emails
            // if there is too much content
            this.currentEvents = retrieveCompositeEventList(currentUser);
        } catch (NotificationException e) {
            logger.error(ERROR_MESSAGE, this.currentUser, e);
        }
    }
    this.hasNext = currentUserEmail != null && !this.currentEvents.isEmpty();
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException) NotificationException(org.xwiki.notifications.NotificationException)

Example 12 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class EmailTemplateRenderer method executeTemplate.

/**
 * Execute a template.
 *
 * @param event composite event to render
 * @param userId id of the user who will receive the email
 * @param template the template to use
 * @param syntax syntax of the template and of the output
 * @return the rendered template
 * @throws NotificationException if something wrong happens
 */
public Block executeTemplate(CompositeEvent event, String userId, Template template, Syntax syntax) throws NotificationException {
    XWikiContext context = contextProvider.get();
    XWikiURLFactory originalURLFactory = context.getURLFactory();
    ScriptContext scriptContext = scriptContextManager.getScriptContext();
    try {
        // Bind the event to some variable in the velocity context
        scriptContext.setAttribute(EVENT_BINDING_NAME, event, ScriptContext.ENGINE_SCOPE);
        scriptContext.setAttribute(USER_BINDING_NAME, userId, ScriptContext.ENGINE_SCOPE);
        // Use the external URL factory to generate full URLs
        context.setURLFactory(new ExternalServletURLFactory(context));
        // Set the given syntax in the rendering context
        if (renderingContext instanceof MutableRenderingContext) {
            ((MutableRenderingContext) renderingContext).push(null, null, syntax, null, false, syntax);
        }
        // Render the template or fallback to the default one
        return templateManager.execute(template);
    } catch (Exception e) {
        throw new NotificationException("Failed to render the notification.", e);
    } finally {
        // Cleaning the rendering context
        if (renderingContext instanceof MutableRenderingContext) {
            ((MutableRenderingContext) renderingContext).pop();
        }
        // Cleaning the URL factory
        context.setURLFactory(originalURLFactory);
        // Cleaning the velocity context
        scriptContext.removeAttribute(EVENT_BINDING_NAME, ScriptContext.ENGINE_SCOPE);
        scriptContext.removeAttribute(USER_BINDING_NAME, ScriptContext.ENGINE_SCOPE);
    }
}
Also used : XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) ExternalServletURLFactory(com.xpn.xwiki.web.ExternalServletURLFactory) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) ScriptContext(javax.script.ScriptContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) NotificationException(org.xwiki.notifications.NotificationException)

Example 13 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class AutomaticWatchModeListener method documentModifiedHandler.

/**
 * Automatically watch modified document depending on the configuration.
 *
 * @param event the observation event we check for a deleted document event
 * @param currentDoc document version after event occurred
 * @param context the XWiki context
 */
private void documentModifiedHandler(Event event, XWikiDocument currentDoc, XWikiContext context) {
    DocumentReference userReference = currentDoc.getAuthorReference();
    // But the user can still manually mark its page as watched
    if (userReference == null || !context.getWiki().exists(userReference, context) || userReference.equals(currentDoc.getDocumentReference())) {
        return;
    }
    // Determine if the current event should be registered, based on the user's preferences.
    boolean register = shouldRegister(event, currentDoc, userReference);
    if (register) {
        try {
            watchedEntitiesManager.watchEntity(factory.createWatchedLocationReference(currentDoc.getDocumentReference()), userReference);
        } catch (NotificationException e) {
            logger.warn("Failed to watch document [{}] for user [{}]", currentDoc.getDocumentReference(), userReference, e);
        }
    }
}
Also used : NotificationException(org.xwiki.notifications.NotificationException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 14 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class DefaultModelBridge method savePropertyInHiddenDocument.

@Override
public void savePropertyInHiddenDocument(BaseObjectReference objectReference, String property, Object value) throws NotificationException {
    try {
        XWikiContext xcontext = contextProvider.get();
        DocumentReference documentReference = (DocumentReference) objectReference.getParent();
        XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
        doc.setHidden(true);
        BaseObject obj = doc.getObject(entityReferenceSerializer.serialize(objectReference.getXClassReference()), true, xcontext);
        if (obj != null) {
            // Set the value
            obj.set(property, value, xcontext);
            // Prevent version changes
            doc.setMetaDataDirty(false);
            doc.setContentDirty(false);
            // Save
            xcontext.getWiki().saveDocument(doc, String.format("Property [%s] set.", property), xcontext);
        }
    } catch (XWikiException e) {
        throw new NotificationException(String.format("Failed to update the object [%s].", objectReference), e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 15 with NotificationException

use of org.xwiki.notifications.NotificationException in project xwiki-platform by xwiki.

the class DefaultNotificationRSSManager method renderFeed.

@Override
public SyndFeed renderFeed(List<CompositeEvent> events) {
    SyndFeed feed = new SyndFeedImpl();
    // Define the general properties of the rss
    feed.setFeedType("rss_2.0");
    feed.setTitle(this.contextualLocalizationManager.getTranslationPlain("notifications.rss.feedTitle"));
    // Set the RSS feed link to the service generating the feed
    feed.setLink(this.modelBridge.getDocumentURL(new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), Arrays.asList("XWiki", "Notifications", "Code"), "NotificationRSSService"), "get", "outputSyntax=plain"));
    // Set the feed description
    feed.setDescription(this.contextualLocalizationManager.getTranslationPlain("notifications.rss.feedDescription"));
    // Add every given CompositeEvent entry to the rss
    List<SyndEntry> entries = new ArrayList<>();
    for (CompositeEvent event : events) {
        try {
            NotificationRSSRenderer renderer = this.getRenderer(event);
            if (renderer != null) {
                entries.add(renderer.renderNotification(event));
            } else {
                entries.add(defaultNotificationRSSRenderer.renderNotification(event));
            }
        } catch (NotificationException e) {
            this.logger.warn("Unable to render RSS entry for CompositeEvent [{}] : [{}]", event, ExceptionUtils.getRootCauseMessage(e));
        }
    }
    feed.setEntries(entries);
    return feed;
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) NotificationRSSRenderer(org.xwiki.notifications.notifiers.rss.NotificationRSSRenderer) ArrayList(java.util.ArrayList) NotificationException(org.xwiki.notifications.NotificationException) SyndFeedImpl(com.rometools.rome.feed.synd.SyndFeedImpl) CompositeEvent(org.xwiki.notifications.CompositeEvent) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

NotificationException (org.xwiki.notifications.NotificationException)24 DocumentReference (org.xwiki.model.reference.DocumentReference)14 XWikiContext (com.xpn.xwiki.XWikiContext)10 XWikiException (com.xpn.xwiki.XWikiException)9 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)9 XWiki (com.xpn.xwiki.XWiki)8 BaseObject (com.xpn.xwiki.objects.BaseObject)8 ArrayList (java.util.ArrayList)6 List (java.util.List)4 NotificationFormat (org.xwiki.notifications.NotificationFormat)4 NotificationFilterPreference (org.xwiki.notifications.filters.NotificationFilterPreference)4 NotificationPreference (org.xwiki.notifications.preferences.NotificationPreference)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 NotificationFilter (org.xwiki.notifications.filters.NotificationFilter)3 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)2 Map (java.util.Map)2 CompositeEvent (org.xwiki.notifications.CompositeEvent)2