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();
}
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);
}
}
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);
}
}
}
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);
}
}
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;
}
Aggregations