Search in sources :

Example 61 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class WatchListNotifier method sendEmailNotification.

/**
 * Sends the email notifying the subscriber that the updatedDocuments have been changed.
 *
 * @param subscriber user to notify
 * @param events list of events
 * @param emailTemplate email template to use
 * @param previousFireTime last time the notification was fired
 * @param context the XWiki context
 * @throws XWikiException if mail sending fails
 */
public void sendEmailNotification(String subscriber, List<WatchListEvent> events, String emailTemplate, Date previousFireTime, XWikiContext context) throws XWikiException {
    // Convert the parameters.
    List<org.xwiki.watchlist.internal.api.WatchListEvent> watchListEvents = new ArrayList<>();
    WatchListEventConverter<ActivityEvent> eventConverter = Utils.getComponent(new DefaultParameterizedType(null, WatchListEventConverter.class, ActivityEvent.class));
    for (WatchListEvent event : events) {
        List<ActivityEvent> activityEvents = event.getData();
        // Get the main event.
        org.xwiki.watchlist.internal.api.WatchListEvent watchListEvent = eventConverter.convert(activityEvents.get(0));
        // Possibly composite event.
        for (int i = 1; i < activityEvents.size(); i++) {
            org.xwiki.watchlist.internal.api.WatchListEvent associatedEvent = eventConverter.convert(activityEvents.get(i));
            watchListEvent.addEvent(associatedEvent);
        }
        // Add the converted event to the list.
        watchListEvents.add(watchListEvent);
    }
    // Delegate to the component.
    org.xwiki.watchlist.internal.api.WatchListNotifier notifier = Utils.getComponent(org.xwiki.watchlist.internal.api.WatchListNotifier.class);
    notifier.sendNotification(subscriber, watchListEvents, emailTemplate, previousFireTime);
}
Also used : ActivityEvent(com.xpn.xwiki.plugin.activitystream.api.ActivityEvent) ArrayList(java.util.ArrayList) WatchListEventConverter(org.xwiki.watchlist.internal.WatchListEventConverter) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Example 62 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class MailSenderScriptService method createMessages.

/**
 * Construct an iterator of Mime Messages by running the Component implementation of {@link
 * org.xwiki.mail.MimeMessageFactory} corresponding to the passed hint.
 *
 * @param hint the component hint of a {@link org.xwiki.mail.MimeMessageFactory} component
 * @param source the source from which to prefill the Mime Messages (depends on the implementation)
 * @param parameters an optional generic list of parameters. The supported parameters depend on the implementation
 * @return the pre-filled Mime Message iterator
 */
public Iterator<MimeMessage> createMessages(String hint, Object source, Map<String, Object> parameters) {
    Iterator<MimeMessage> result;
    try {
        MimeMessageFactory<Iterator<MimeMessage>> factory = MimeMessageFactoryProvider.get(hint, new DefaultParameterizedType(null, Iterator.class, MimeMessage.class), this.componentManagerProvider.get());
        result = factory.createMessage(source, parameters);
    } catch (Exception e) {
        // No factory found or error in constructing the message iterator, set an error
        // An error occurred, save it and return null
        setError(e);
        return null;
    }
    return result;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Iterator(java.util.Iterator) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MessagingException(javax.mail.MessagingException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 63 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class AuthenticatingIntegrationTest method registerConfiguration.

@BeforeComponent
public void registerConfiguration() throws Exception {
    Properties properties = new Properties();
    properties.setProperty("mail.smtp.starttls.enable", "true");
    // Required by GreenMail. When using XWiki with Gmail for example this is not required.
    properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    this.configuration = new TestMailSenderConfiguration(this.mail.getSmtps().getPort(), "peter", "password", properties);
    this.componentManager.registerComponent(MailSenderConfiguration.class, this.configuration);
    // Set the current wiki in the Context
    ModelContext modelContext = this.componentManager.registerMockComponent(ModelContext.class);
    when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("wiki"));
    Provider<XWikiContext> xwikiContextProvider = this.componentManager.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    when(xwikiContextProvider.get()).thenReturn(Mockito.mock(XWikiContext.class));
    this.componentManager.registerMockComponent(ExecutionContextManager.class);
    this.componentManager.registerMockComponent(Execution.class);
    this.componentManager.registerMockComponent(new DefaultParameterizedType(null, Copier.class, ExecutionContext.class));
    EnvironmentConfiguration environmentConfiguration = this.componentManager.registerMockComponent(EnvironmentConfiguration.class);
    when(environmentConfiguration.getPermanentDirectoryPath()).thenReturn(System.getProperty("java.io.tmpdir"));
}
Also used : ModelContext(org.xwiki.model.ModelContext) ExecutionContext(org.xwiki.context.ExecutionContext) EnvironmentConfiguration(org.xwiki.environment.internal.EnvironmentConfiguration) Copier(org.xwiki.mail.internal.thread.context.Copier) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) WikiReference(org.xwiki.model.reference.WikiReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) BeforeComponent(org.xwiki.test.annotation.BeforeComponent)

Example 64 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class AuthenticatingIntegrationTest method sendTextMail.

@Test
public void sendTextMail() throws Exception {
    // Set the EC
    Execution execution = this.componentManager.getInstance(Execution.class);
    ExecutionContext executionContext = new ExecutionContext();
    XWikiContext xContext = new XWikiContext();
    xContext.setWikiId("wiki");
    executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext);
    when(execution.getContext()).thenReturn(executionContext);
    Copier<ExecutionContext> executionContextCloner = this.componentManager.getInstance(new DefaultParameterizedType(null, Copier.class, ExecutionContext.class));
    // Just return the same execution context
    when(executionContextCloner.copy(executionContext)).thenReturn(executionContext);
    // Step 1: Create a JavaMail Session
    Properties properties = this.configuration.getAllProperties();
    assertEquals("true", properties.getProperty(DefaultMailSenderConfiguration.JAVAMAIL_SMTP_AUTH));
    Session session = Session.getInstance(properties, new XWikiAuthenticator(this.configuration));
    // Step 2: Create the Message to send
    MimeMessage message = new MimeMessage(session);
    message.setSubject("subject");
    message.setRecipient(RecipientType.TO, new InternetAddress("john@doe.com"));
    // Step 3: Add the Message Body
    Multipart multipart = new MimeMultipart("mixed");
    // Add text in the body
    multipart.addBodyPart(this.defaultBodyPartFactory.create("some text here", Collections.<String, Object>singletonMap("mimetype", "text/plain")));
    message.setContent(multipart);
    // Step 4: Send the mail
    this.sender.sendAsynchronously(Arrays.asList(message), session, null);
    // Verify that the mail has been received (wait maximum 30 seconds).
    this.mail.waitForIncomingEmail(30000L, 1);
    MimeMessage[] messages = this.mail.getReceivedMessages();
    assertEquals(1, messages.length);
    assertEquals("subject", messages[0].getHeader("Subject", null));
    assertEquals("john@doe.com", messages[0].getHeader("To", null));
    assertEquals(1, ((MimeMultipart) messages[0].getContent()).getCount());
    BodyPart textBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0);
    assertEquals("text/plain", textBodyPart.getHeader("Content-Type")[0]);
    assertEquals("some text here", textBodyPart.getContent());
}
Also used : BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) XWikiAuthenticator(org.xwiki.mail.XWikiAuthenticator) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Copier(org.xwiki.mail.internal.thread.context.Copier) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Session(javax.mail.Session) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 65 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class ScriptingIntegrationTest method initialize.

@Before
public void initialize() throws Exception {
    this.scriptService = this.componentManager.getInstance(ScriptService.class, "mailsender");
    // Set the EC
    Execution execution = this.componentManager.getInstance(Execution.class);
    ExecutionContext executionContext = new ExecutionContext();
    XWikiContext xContext = new XWikiContext();
    xContext.setWikiId("wiki");
    executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext);
    execution.setContext(executionContext);
    Copier<ExecutionContext> executionContextCloner = this.componentManager.getInstance(new DefaultParameterizedType(null, Copier.class, ExecutionContext.class));
    // Just return the same execution context
    when(executionContextCloner.copy(executionContext)).thenReturn(executionContext);
    // Simulate receiving the Application Ready Event to start the mail threads
    MailSenderInitializerListener listener = this.componentManager.getInstance(EventListener.class, MailSenderInitializerListener.LISTENER_NAME);
    listener.onEvent(new ApplicationReadyEvent(), null, null);
}
Also used : ScriptService(org.xwiki.script.service.ScriptService) MailSenderScriptService(org.xwiki.mail.script.MailSenderScriptService) DefaultExecution(org.xwiki.context.internal.DefaultExecution) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) Copier(org.xwiki.mail.internal.thread.context.Copier) ApplicationReadyEvent(org.xwiki.bridge.event.ApplicationReadyEvent) XWikiContext(com.xpn.xwiki.XWikiContext) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailSenderInitializerListener(org.xwiki.mail.internal.thread.MailSenderInitializerListener) Before(org.junit.Before)

Aggregations

DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)104 Test (org.junit.Test)44 Before (org.junit.Before)32 Provider (javax.inject.Provider)27 DocumentReference (org.xwiki.model.reference.DocumentReference)24 XWikiContext (com.xpn.xwiki.XWikiContext)19 ComponentManager (org.xwiki.component.manager.ComponentManager)19 ExecutionContext (org.xwiki.context.ExecutionContext)17 ExtendedURL (org.xwiki.url.ExtendedURL)15 Execution (org.xwiki.context.Execution)14 HashMap (java.util.HashMap)13 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)13 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)12 ResourceReferenceSerializer (org.xwiki.resource.ResourceReferenceSerializer)10 VfsResourceReference (org.xwiki.vfs.VfsResourceReference)10 Properties (java.util.Properties)9 MimeBodyPartFactory (org.xwiki.mail.MimeBodyPartFactory)9 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)8 XWiki (com.xpn.xwiki.XWiki)7 File (java.io.File)7