Search in sources :

Example 36 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class SendMailConfigClassDocumentConfigurationSourceTest method getPropertyWhenNoSendMailConfigClassXObject.

@Test
public void getPropertyWhenNoSendMailConfigClassXObject() throws Exception {
    Cache<Object> cache = mock(Cache.class);
    CacheManager cacheManager = this.mocker.getInstance(CacheManager.class);
    when(cacheManager.createNewCache(any(CacheConfiguration.class))).thenReturn(cache);
    WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
    when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wiki");
    LocalDocumentReference classReference = new LocalDocumentReference("Mail", "SendMailConfigClass");
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getXObject(classReference)).thenReturn(null);
    DocumentReference documentReference = new DocumentReference("wiki", "Mail", "MailConfig");
    XWiki xwiki = mock(XWiki.class);
    when(xwiki.getDocument(eq(documentReference), any(XWikiContext.class))).thenReturn(document);
    XWikiContext xcontext = mock(XWikiContext.class);
    when(xcontext.getWiki()).thenReturn(xwiki);
    Provider<XWikiContext> xcontextProvider = this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    when(xcontextProvider.get()).thenReturn(xcontext);
    assertEquals("defaultValue", this.mocker.getComponentUnderTest().getProperty("key", "defaultValue"));
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) CacheManager(org.xwiki.cache.CacheManager) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) BaseObject(com.xpn.xwiki.objects.BaseObject) CacheConfiguration(org.xwiki.cache.config.CacheConfiguration) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) Test(org.junit.Test)

Example 37 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class SendMailConfigClassDocumentConfigurationSourceTest method getPropertyWhenSendMailConfigClassXObjectExists.

@Test
public void getPropertyWhenSendMailConfigClassXObjectExists() throws Exception {
    ConverterManager converterManager = this.mocker.getInstance(ConverterManager.class);
    when(converterManager.convert(String.class, "value")).thenReturn("value");
    Cache<Object> cache = mock(Cache.class);
    CacheManager cacheManager = this.mocker.getInstance(CacheManager.class);
    when(cacheManager.createNewCache(any(CacheConfiguration.class))).thenReturn(cache);
    WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
    when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wiki");
    LocalDocumentReference classReference = new LocalDocumentReference("Mail", "SendMailConfigClass");
    BaseProperty property = mock(BaseProperty.class);
    when(property.toText()).thenReturn("value");
    BaseObject object = mock(BaseObject.class);
    when(object.getField("key")).thenReturn(property);
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getXObject(classReference)).thenReturn(object);
    DocumentReference documentReference = new DocumentReference("wiki", "Mail", "MailConfig");
    XWiki xwiki = mock(XWiki.class);
    when(xwiki.getDocument(eq(documentReference), any(XWikiContext.class))).thenReturn(document);
    XWikiContext xcontext = mock(XWikiContext.class);
    when(xcontext.getWiki()).thenReturn(xwiki);
    Provider<XWikiContext> xcontextProvider = this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    when(xcontextProvider.get()).thenReturn(xcontext);
    assertEquals("value", this.mocker.getComponentUnderTest().getProperty("key", "defaultValue"));
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ConverterManager(org.xwiki.properties.ConverterManager) CacheManager(org.xwiki.cache.CacheManager) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty) CacheConfiguration(org.xwiki.cache.config.CacheConfiguration) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) Test(org.junit.Test)

Example 38 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class GroupMimeMessageFactory method createMessage.

@Override
public Iterator<MimeMessage> createMessage(Object groupReferenceObject, Map<String, Object> parameters) throws MessagingException {
    DocumentReference groupReference = getTypedSource(groupReferenceObject, DocumentReference.class);
    // We verify that we have both a Factory hint specified but also the source for the Factory.
    validateParameters(parameters, HINT, SOURCE);
    // Extract from the passed parameters the MimeMessageFactory to use to create a single mail
    String factoryHint = (String) parameters.get(HINT);
    MimeMessageFactory factory = getInternalMimeMessageFactory(factoryHint);
    GroupMimeMessageIterator iterator = new GroupMimeMessageIterator(groupReference, factory, parameters, this.componentManagerProvider.get());
    return iterator;
}
Also used : MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) AbstractIteratorMimeMessageFactory(org.xwiki.mail.internal.factory.AbstractIteratorMimeMessageFactory) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 39 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class AbstractTemplateMimeMessageFactory method createMessage.

@Override
public MimeMessage createMessage(Object templateReferenceObject, Map<String, Object> parameters) throws MessagingException {
    DocumentReference templateReference = getTypedSource(templateReferenceObject, DocumentReference.class);
    // Note: We don't create a Session here ATM since it's not required. The returned MimeMessage will be
    // given a valid Session when it's deserialized from the mail content store for sending.
    ExtendedMimeMessage message = new ExtendedMimeMessage();
    // Handle optional "from" address.
    Address from = this.converterManager.convert(Address.class, parameters.get("from"));
    if (from != null) {
        message.setFrom(from);
    }
    // Handle optional "to", "cc" and "bcc" addresses.
    setRecipient(message, Message.RecipientType.TO, parameters.get("to"));
    setRecipient(message, Message.RecipientType.CC, parameters.get("cc"));
    setRecipient(message, Message.RecipientType.BCC, parameters.get("bcc"));
    // Handle optional "type" parameter to set the mail type
    // Set the Message type if passed in parameters
    String type = (String) parameters.get("type");
    if (type != null) {
        message.setType(type);
    }
    // Handle the subject. Get it from the template
    Map<String, Object> velocityVariables = (Map<String, Object>) parameters.get("velocityVariables");
    Object localeValue = parameters.get("language");
    String subject = getTemplateManager().evaluate(templateReference, "subject", velocityVariables, localeValue);
    message.setSubject(subject);
    // Add a default body part taken from the template.
    Multipart multipart = new MimeMultipart("mixed");
    multipart.addBodyPart(getMimeBodyPartFactory().create(templateReference, parameters));
    message.setContent(multipart);
    return message;
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Address(javax.mail.Address) MimeMultipart(javax.mail.internet.MimeMultipart) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 40 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class UsersMimeMessageIterator method createMessageInternal.

@Override
protected ExtendedMimeMessage createMessageInternal() throws MessagingException {
    ExtendedMimeMessage mimeMessage;
    DocumentReference userReference = users.get(this.position);
    // If the user has no email address then return a null Mime Message so that it's skipped
    Object emailObject = this.documentAccessBridge.getProperty(userReference, new DocumentReference(userReference.getWikiReference().getName(), "XWiki", "XWikiUsers"), "email");
    if (emailObject != null) {
        String email = emailObject.toString();
        Map<String, Object> parameters = (Map<String, Object>) this.parameters.get("parameters");
        mimeMessage = ExtendedMimeMessage.wrap(this.factory.createMessage(this.parameters.get("source"), parameters));
        mimeMessage.addRecipient(Message.RecipientType.TO, InternetAddress.parse(email)[0]);
    } else {
        getLogger().warn("User [{}] has no email defined. Email has not been sent to that user.", userReference);
        mimeMessage = null;
    }
    return mimeMessage;
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

DocumentReference (org.xwiki.model.reference.DocumentReference)1324 Test (org.junit.Test)711 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)482 BaseObject (com.xpn.xwiki.objects.BaseObject)250 XWikiContext (com.xpn.xwiki.XWikiContext)186 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)157 ArrayList (java.util.ArrayList)128 WikiReference (org.xwiki.model.reference.WikiReference)127 XWikiException (com.xpn.xwiki.XWikiException)121 EntityReference (org.xwiki.model.reference.EntityReference)113 SpaceReference (org.xwiki.model.reference.SpaceReference)96 XWiki (com.xpn.xwiki.XWiki)82 HashMap (java.util.HashMap)54 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)52 Expectations (org.jmock.Expectations)50 Before (org.junit.Before)50 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)46 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)45 AttachmentReference (org.xwiki.model.reference.AttachmentReference)44 Date (java.util.Date)42