Skip to content

Commit 443e839

Browse files
committed
XWIKI-19869: Improve user property storage
- Provide a new API for sending text based email - Provide a migration and a listener for informing users about it
1 parent 9c577ba commit 443e839

File tree

14 files changed

+1044
-12
lines changed

14 files changed

+1044
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* See the NOTICE file distributed with this work for additional
3+
* information regarding copyright ownership.
4+
*
5+
* This is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU Lesser General Public License as
7+
* published by the Free Software Foundation; either version 2.1 of
8+
* the License, or (at your option) any later version.
9+
*
10+
* This software is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this software; if not, write to the Free
17+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19+
*/
20+
package org.xwiki.mail.internal.factory.text;
21+
22+
import java.util.Map;
23+
24+
import javax.inject.Inject;
25+
import javax.inject.Named;
26+
import javax.inject.Singleton;
27+
import javax.mail.Address;
28+
import javax.mail.Message;
29+
import javax.mail.MessagingException;
30+
import javax.mail.Multipart;
31+
import javax.mail.internet.MimeMessage;
32+
import javax.mail.internet.MimeMultipart;
33+
34+
import org.xwiki.component.annotation.Component;
35+
import org.xwiki.mail.ExtendedMimeMessage;
36+
import org.xwiki.mail.MimeBodyPartFactory;
37+
import org.xwiki.mail.MimeMessageFactory;
38+
import org.xwiki.mail.internal.factory.AbstractMimeMessageFactory;
39+
import org.xwiki.properties.ConverterManager;
40+
41+
/**
42+
* A basic {@link MimeMessageFactory} which is taken a {@link String} as source which represents the content of the
43+
* message.
44+
*
45+
* @version $Id$
46+
* @since 14.6RC1
47+
* @since 14.4.3
48+
* @since 13.10.8
49+
*/
50+
@Component
51+
@Singleton
52+
@Named("text")
53+
public class TextMimeMessageFactory extends AbstractMimeMessageFactory<MimeMessage>
54+
{
55+
@Inject
56+
private ConverterManager converterManager;
57+
58+
@Inject
59+
private MimeBodyPartFactory<String> mimeBodyPartFactory;
60+
61+
@Override
62+
public MimeMessage createMessage(Object source, Map<String, Object> parameters) throws MessagingException
63+
{
64+
// This whole code has been inspired by the implementation of
65+
// org.xwiki.mail.internal.factory.template.AbstractTemplateMimeMessageFactory
66+
67+
// Note: We don't create a Session here ATM since it's not required. The returned MimeMessage will be
68+
// given a valid Session when it's deserialized from the mail content store for sending.
69+
ExtendedMimeMessage message = new ExtendedMimeMessage();
70+
71+
// Handle optional "from" address.
72+
Address from = this.converterManager.convert(Address.class, parameters.get("from"));
73+
if (from != null) {
74+
message.setFrom(from);
75+
}
76+
77+
// Handle optional "to", "cc" and "bcc" addresses.
78+
setRecipient(message, Message.RecipientType.TO, parameters.get("to"));
79+
setRecipient(message, Message.RecipientType.CC, parameters.get("cc"));
80+
setRecipient(message, Message.RecipientType.BCC, parameters.get("bcc"));
81+
82+
// Handle optional "type" parameter to set the mail type
83+
// Set the Message type if passed in parameters
84+
String type = (String) parameters.get("type");
85+
if (type != null) {
86+
message.setType(type);
87+
}
88+
89+
// Handle the subject. Get it from the parameters.
90+
String subject = (String) parameters.get("subject");
91+
message.setSubject(subject);
92+
93+
// Add a default body part taken from the template.
94+
Multipart multipart = new MimeMultipart("mixed");
95+
multipart.addBodyPart(this.mimeBodyPartFactory.create((String) source, parameters));
96+
message.setContent(multipart);
97+
98+
return message;
99+
}
100+
101+
private void setRecipient(MimeMessage message, Message.RecipientType type, Object value)
102+
throws MessagingException
103+
{
104+
Address[] addresses = this.converterManager.convert(Address[].class, value);
105+
if (addresses != null) {
106+
message.setRecipients(type, addresses);
107+
}
108+
}
109+
}

xwiki-platform-core/xwiki-platform-mail/xwiki-platform-mail-send/xwiki-platform-mail-send-default/src/main/resources/META-INF/components.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ org.xwiki.mail.internal.factory.template.DefaultAttachmentConverter
3333
org.xwiki.mail.internal.factory.files.SerializedFilesMimeMessageFactory
3434
org.xwiki.mail.internal.factory.group.GroupMimeMessageFactory
3535
org.xwiki.mail.internal.factory.usersandgroups.UsersAndGroupsMimeMessageFactory
36+
org.xwiki.mail.internal.factory.text.TextMimeMessageFactory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* See the NOTICE file distributed with this work for additional
3+
* information regarding copyright ownership.
4+
*
5+
* This is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU Lesser General Public License as
7+
* published by the Free Software Foundation; either version 2.1 of
8+
* the License, or (at your option) any later version.
9+
*
10+
* This software is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this software; if not, write to the Free
17+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19+
*/
20+
package org.xwiki.mail.internal.factory.text;
21+
22+
import java.io.IOException;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
import javax.mail.Address;
27+
import javax.mail.Message;
28+
import javax.mail.MessagingException;
29+
import javax.mail.Multipart;
30+
import javax.mail.internet.InternetAddress;
31+
import javax.mail.internet.MimeBodyPart;
32+
import javax.mail.internet.MimeMessage;
33+
import javax.mail.internet.MimeMultipart;
34+
35+
import org.junit.jupiter.api.Test;
36+
import org.xwiki.mail.ExtendedMimeMessage;
37+
import org.xwiki.mail.MimeBodyPartFactory;
38+
import org.xwiki.properties.ConverterManager;
39+
import org.xwiki.test.junit5.mockito.ComponentTest;
40+
import org.xwiki.test.junit5.mockito.InjectMockComponents;
41+
import org.xwiki.test.junit5.mockito.MockComponent;
42+
43+
import static org.junit.jupiter.api.Assertions.*;
44+
import static org.mockito.Mockito.mock;
45+
import static org.mockito.Mockito.when;
46+
47+
/**
48+
* Tests for {@link TextMimeMessageFactory}.
49+
*
50+
* @version $Id$
51+
*/
52+
@ComponentTest
53+
class TextMimeMessageFactoryTest
54+
{
55+
@InjectMockComponents
56+
private TextMimeMessageFactory textMimeMessageFactory;
57+
58+
@MockComponent
59+
private ConverterManager converterManager;
60+
61+
@MockComponent
62+
private MimeBodyPartFactory<String> mimeBodyPartFactory;
63+
64+
@Test
65+
void createMessage() throws MessagingException, IOException
66+
{
67+
String source = "Some mail content";
68+
Map<String, Object> parameters = new HashMap<>();
69+
parameters.put("to", "[email protected]");
70+
parameters.put("from", "[email protected]");
71+
parameters.put("type", "text");
72+
parameters.put("subject", "important email");
73+
74+
Address from = new InternetAddress("[email protected]");
75+
Address to = new InternetAddress("[email protected]");
76+
77+
when(this.converterManager.convert(Address[].class, "[email protected]")).thenReturn(new Address[] {to});
78+
when(this.converterManager.convert(Address.class, "[email protected]")).thenReturn(from);
79+
80+
MimeBodyPart bodyPart = mock(MimeBodyPart.class);
81+
when(this.mimeBodyPartFactory.create(source, parameters)).thenReturn(bodyPart);
82+
83+
ExtendedMimeMessage expectedMessage = new ExtendedMimeMessage();
84+
expectedMessage.setFrom(from);
85+
expectedMessage.setRecipient(Message.RecipientType.TO, to);
86+
expectedMessage.setSubject("important email");
87+
expectedMessage.setType("text");
88+
Multipart multipart = new MimeMultipart("mixed");
89+
multipart.addBodyPart(bodyPart);
90+
expectedMessage.setContent(multipart);
91+
92+
MimeMessage message = this.textMimeMessageFactory.createMessage(source, parameters);
93+
assertEquals(to, message.getRecipients(Message.RecipientType.TO)[0]);
94+
assertEquals(from, message.getFrom()[0]);
95+
assertEquals("important email", message.getSubject());
96+
assertEquals(bodyPart, ((Multipart) message.getContent()).getBodyPart(0));
97+
}
98+
}

0 commit comments

Comments
 (0)