use of org.xwiki.context.Execution in project xwiki-platform by xwiki.
the class ActivityStreamImpl method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
// Do not record some ignored events
ObservationContext observationContext = Utils.getComponent(ObservationContext.class);
if (observationContext.isIn(IGNORED_EVENTS)) {
return;
}
XWikiDocument currentDoc = (XWikiDocument) source;
XWikiDocument originalDoc = currentDoc.getOriginalDocument();
XWikiContext context = (XWikiContext) data;
String wiki = context.getWikiId();
String msgPrefix = "activitystream.event.";
String streamName = getStreamName(currentDoc.getSpace(), context);
// If we haven't found a stream to store the event or if both currentDoc and originalDoc are null: exit
if (streamName == null) {
return;
}
Execution executionContext = Utils.getComponent(Execution.class);
// Take events into account only once in a cluster
if (!Utils.getComponent(RemoteObservationManagerContext.class).isRemoteState() && !executionContext.getContext().hasProperty(AbstractEventStreamEvent.EVENT_LOOP_CONTEXT_LOCK_PROPERTY)) {
executionContext.getContext().setProperty(AbstractEventStreamEvent.EVENT_LOOP_CONTEXT_LOCK_PROPERTY, true);
String eventType;
String displayTitle;
String additionalIdentifier = null;
if (event instanceof DocumentCreatedEvent) {
eventType = ActivityEventType.CREATE;
displayTitle = currentDoc.getRenderedTitle(context);
} else if (event instanceof DocumentUpdatedEvent) {
eventType = ActivityEventType.UPDATE;
displayTitle = originalDoc.getRenderedTitle(context);
} else if (event instanceof DocumentDeletedEvent) {
eventType = ActivityEventType.DELETE;
displayTitle = originalDoc.getRenderedTitle(context);
// When we receive a DELETE event, the given document is blank and does not have version & hidden tag
// properly set.
currentDoc.setVersion(originalDoc.getVersion());
currentDoc.setHidden(originalDoc.isHidden());
} else if (event instanceof CommentAddedEvent) {
eventType = ActivityEventType.ADD_COMMENT;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((CommentAddedEvent) event).getIdentifier();
} else if (event instanceof CommentDeletedEvent) {
eventType = ActivityEventType.DELETE_COMMENT;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((CommentDeletedEvent) event).getIdentifier();
} else if (event instanceof CommentUpdatedEvent) {
eventType = ActivityEventType.UPDATE_COMMENT;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((CommentUpdatedEvent) event).getIdentifier();
} else if (event instanceof AttachmentAddedEvent) {
eventType = ActivityEventType.ADD_ATTACHMENT;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((AttachmentAddedEvent) event).getName();
} else if (event instanceof AttachmentDeletedEvent) {
eventType = ActivityEventType.DELETE_ATTACHMENT;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((AttachmentDeletedEvent) event).getName();
} else if (event instanceof AttachmentUpdatedEvent) {
eventType = ActivityEventType.UPDATE_ATTACHMENT;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((AttachmentUpdatedEvent) event).getName();
} else if (event instanceof AnnotationAddedEvent) {
eventType = ActivityEventType.ADD_ANNOTATION;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((AnnotationAddedEvent) event).getIdentifier();
} else if (event instanceof AnnotationDeletedEvent) {
eventType = ActivityEventType.DELETE_ANNOTATION;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((AnnotationDeletedEvent) event).getIdentifier();
} else {
// update annotation
eventType = ActivityEventType.UPDATE_ANNOTATION;
displayTitle = currentDoc.getRenderedTitle(context);
additionalIdentifier = ((AnnotationUpdatedEvent) event).getIdentifier();
}
List<String> params = new ArrayList<String>();
params.add(displayTitle);
if (additionalIdentifier != null) {
params.add(additionalIdentifier);
}
try {
addDocumentActivityEvent(streamName, currentDoc, eventType, msgPrefix + eventType, params, context);
} catch (ActivityStreamException e) {
LOGGER.error("Exception while trying to add a document activity event, updated document: [" + wiki + ":" + currentDoc + "]");
}
executionContext.getContext().removeProperty(AbstractEventStreamEvent.EVENT_LOOP_CONTEXT_LOCK_PROPERTY);
}
}
use of org.xwiki.context.Execution in project xwiki-platform by xwiki.
the class DashboardMacroTest method executeWhenInsideDashboardMacro.
@Test
public void executeWhenInsideDashboardMacro() throws Exception {
BeanManager beanManager = this.mocker.getInstance(BeanManager.class);
BeanDescriptor descriptor = mock(BeanDescriptor.class);
when(beanManager.getBeanDescriptor(any())).thenReturn(descriptor);
when(descriptor.getProperties()).thenReturn(Collections.emptyList());
Execution execution = this.mocker.getInstance(Execution.class);
ExecutionContext ec = new ExecutionContext();
when(execution.getContext()).thenReturn(ec);
ec.setProperty("dashboardMacroCalls", 1);
DashboardMacroParameters parameters = new DashboardMacroParameters();
MacroTransformationContext macroContext = new MacroTransformationContext();
try {
this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
fail("Exception should have been raised here");
} catch (MacroExecutionException expected) {
assertEquals("Dashboard macro recursion detected. Don't call the Dashboard macro inside of itself...", expected.getMessage());
}
}
use of org.xwiki.context.Execution in project xwiki-platform by xwiki.
the class DashboardMacroTest method executeWhenNotInsideDashboardMacro.
@Test
public void executeWhenNotInsideDashboardMacro() throws Exception {
BeanManager beanManager = this.mocker.getInstance(BeanManager.class);
BeanDescriptor descriptor = mock(BeanDescriptor.class);
when(beanManager.getBeanDescriptor(any())).thenReturn(descriptor);
when(descriptor.getProperties()).thenReturn(Collections.emptyList());
DashboardRenderer renderer = this.mocker.registerMockComponent(DashboardRenderer.class, "columns");
GadgetRenderer gadgetRenderer = this.mocker.registerMockComponent(GadgetRenderer.class);
Execution execution = this.mocker.getInstance(Execution.class);
ExecutionContext ec = new ExecutionContext();
when(execution.getContext()).thenReturn(ec);
DashboardMacroParameters parameters = new DashboardMacroParameters();
MacroTransformationContext macroContext = new MacroTransformationContext();
this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
// We verify that the counter ends up at 0 so that calls to subsequent dashboard macros can succeed.
assertEquals(0, ec.getProperty("dashboardMacroCalls"));
}
use of org.xwiki.context.Execution in project xwiki-platform by xwiki.
the class GroupMimeMessageIteratorTest method createMessage.
@Test
public void createMessage() throws Exception {
DocumentReference groupReference = new DocumentReference("xwiki", "XWiki", "Marketing");
DocumentReference userReference1 = new DocumentReference("xwiki", "XWiki", "JohnDoe");
DocumentReference userReference2 = new DocumentReference("xwiki", "XWiki", "JaneDoe");
DocumentReference userReference3 = new DocumentReference("xwiki", "XWiki", "JonnieDoe");
Session session = Session.getInstance(new Properties());
MimeMessageFactory<MimeMessage> factory = new MimeMessageFactory<MimeMessage>() {
@Override
public MimeMessage createMessage(Object source, Map parameters) throws MessagingException {
return new ExtendedMimeMessage();
}
};
Map<String, Object> parameters = new HashMap<>();
parameters.put("parameters", Collections.EMPTY_MAP);
parameters.put("session", session);
DocumentAccessBridge accessBridge = mock(DocumentAccessBridge.class);
when(accessBridge.getProperty(eq(groupReference), any(), eq(0), eq("member"))).thenReturn("XWiki.JohnDoe");
when(accessBridge.getProperty(eq(groupReference), any(), eq(1), eq("member"))).thenReturn("XWiki.JaneDoe");
when(accessBridge.getProperty(eq(groupReference), any(), eq(2), eq("member"))).thenReturn("XWiki.JonnieDoe");
when(accessBridge.getProperty(eq(userReference1), any(), eq("email"))).thenReturn("john@doe.com");
when(accessBridge.getProperty(eq(userReference2), any(), eq("email"))).thenReturn("jane@doe.com");
when(accessBridge.getProperty(eq(userReference3), any(), eq("email"))).thenReturn("jannie@doe.com");
Execution execution = mock(Execution.class);
ExecutionContext executionContext = mock(ExecutionContext.class);
when(execution.getContext()).thenReturn(executionContext);
XWikiContext xwikiContext = mock(XWikiContext.class);
when(executionContext.getProperty("xwikicontext")).thenReturn(xwikiContext);
XWiki xwiki = mock(XWiki.class);
when(xwikiContext.getWiki()).thenReturn(xwiki);
XWikiDocument document = mock(XWikiDocument.class);
when(xwiki.getDocument(eq(groupReference), eq(xwikiContext))).thenReturn(document);
BaseObject object = mock(BaseObject.class);
when(document.getXObjects(any(EntityReference.class))).thenReturn(Arrays.asList(object, object, object));
DocumentReferenceResolver<String> resolver = (DocumentReferenceResolver<String>) mock(DocumentReferenceResolver.class);
when(resolver.resolve("XWiki.JohnDoe")).thenReturn(userReference1);
when(resolver.resolve("XWiki.JaneDoe")).thenReturn(userReference2);
when(resolver.resolve("XWiki.JonnieDoe")).thenReturn(userReference3);
ComponentManager componentManager = mock(ComponentManager.class);
when(componentManager.getInstance(eq(DocumentAccessBridge.class))).thenReturn(accessBridge);
when(componentManager.getInstance(eq(Execution.class))).thenReturn(execution);
when(componentManager.getInstance(eq(DocumentReferenceResolver.TYPE_STRING), eq("current"))).thenReturn(resolver);
GroupMimeMessageIterator iterator = new GroupMimeMessageIterator(groupReference, factory, parameters, componentManager);
assertTrue(iterator.hasNext());
MimeMessage message1 = iterator.next();
assertArrayEquals(message1.getRecipients(Message.RecipientType.TO), InternetAddress.parse("john@doe.com"));
assertTrue(iterator.hasNext());
MimeMessage message2 = iterator.next();
assertArrayEquals(message2.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jane@doe.com"));
assertTrue(iterator.hasNext());
MimeMessage message3 = iterator.next();
assertArrayEquals(message3.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jannie@doe.com"));
assertFalse(iterator.hasNext());
}
use of org.xwiki.context.Execution in project xwiki-platform by xwiki.
the class MailStorageScriptServiceTest method setUp.
@Before
public void setUp() throws Exception {
Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
when(componentManagerProvider.get()).thenReturn(this.mocker);
Execution execution = this.mocker.getInstance(Execution.class);
ExecutionContext executionContext = new ExecutionContext();
when(execution.getContext()).thenReturn(executionContext);
}
Aggregations