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