Search in sources :

Example 16 with Execution

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);
    }
}
Also used : AttachmentAddedEvent(com.xpn.xwiki.internal.event.AttachmentAddedEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) ActivityStreamException(com.xpn.xwiki.plugin.activitystream.api.ActivityStreamException) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) CommentUpdatedEvent(com.xpn.xwiki.internal.event.CommentUpdatedEvent) RemoteObservationManagerContext(org.xwiki.observation.remote.RemoteObservationManagerContext) CommentDeletedEvent(com.xpn.xwiki.internal.event.CommentDeletedEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) CommentAddedEvent(com.xpn.xwiki.internal.event.CommentAddedEvent) Execution(org.xwiki.context.Execution) AnnotationAddedEvent(org.xwiki.annotation.event.AnnotationAddedEvent) AttachmentUpdatedEvent(com.xpn.xwiki.internal.event.AttachmentUpdatedEvent) ObservationContext(org.xwiki.observation.ObservationContext) AnnotationDeletedEvent(org.xwiki.annotation.event.AnnotationDeletedEvent) AttachmentDeletedEvent(com.xpn.xwiki.internal.event.AttachmentDeletedEvent)

Example 17 with Execution

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());
    }
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) BeanDescriptor(org.xwiki.properties.BeanDescriptor) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DashboardMacroParameters(org.xwiki.rendering.macro.dashboard.DashboardMacroParameters) BeanManager(org.xwiki.properties.BeanManager) Test(org.junit.Test)

Example 18 with Execution

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"));
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) BeanDescriptor(org.xwiki.properties.BeanDescriptor) DashboardRenderer(org.xwiki.rendering.macro.dashboard.DashboardRenderer) GadgetRenderer(org.xwiki.rendering.macro.dashboard.GadgetRenderer) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DashboardMacroParameters(org.xwiki.rendering.macro.dashboard.DashboardMacroParameters) BeanManager(org.xwiki.properties.BeanManager) Test(org.junit.Test)

Example 19 with Execution

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());
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DocumentReferenceResolver(org.xwiki.model.reference.DocumentReferenceResolver) HashMap(java.util.HashMap) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) Properties(java.util.Properties) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) EntityReference(org.xwiki.model.reference.EntityReference) ComponentManager(org.xwiki.component.manager.ComponentManager) BaseObject(com.xpn.xwiki.objects.BaseObject) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(javax.mail.Session) Test(org.junit.Test)

Example 20 with Execution

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);
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) ComponentManager(org.xwiki.component.manager.ComponentManager) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Provider(javax.inject.Provider) Before(org.junit.Before)

Aggregations

Execution (org.xwiki.context.Execution)82 ExecutionContext (org.xwiki.context.ExecutionContext)58 Before (org.junit.Before)36 XWikiContext (com.xpn.xwiki.XWikiContext)30 Test (org.junit.Test)19 DocumentReference (org.xwiki.model.reference.DocumentReference)17 XWiki (com.xpn.xwiki.XWiki)13 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)13 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)11 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)10 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)8 ComponentManager (org.xwiki.component.manager.ComponentManager)8 HashMap (java.util.HashMap)7 Expectations (org.jmock.Expectations)7 VelocityContext (org.apache.velocity.VelocityContext)5 ExecutionContextManager (org.xwiki.context.ExecutionContextManager)5 BaseObject (com.xpn.xwiki.objects.BaseObject)4 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)4 XDOM (org.xwiki.rendering.block.XDOM)4 AuthorizationManager (org.xwiki.security.authorization.AuthorizationManager)4