Skip to content

Commit 1320cd1

Browse files
authored
Merge pull request #50 from kosinal/master
New NamedDataSource, to allow for renaming datasources
2 parents e9495dc + f54794d commit 1320cd1

File tree

3 files changed

+179
-5
lines changed

3 files changed

+179
-5
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.simplejavamail.mailer.internal.datasource;
2+
3+
import javax.activation.DataSource;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.OutputStream;
7+
8+
/**
9+
* Data source used to fix bug, when user try to use different name for file in {@link javax.activation.FileDataSource},
10+
* than the actual name is.
11+
*
12+
* @author Lukas Kosina
13+
* @see DataSource
14+
* @see javax.activation.FileDataSource
15+
*/
16+
public class NamedDataSource implements DataSource {
17+
18+
/**
19+
* Original data source used for attachment
20+
*/
21+
private final DataSource dataSource;
22+
/**
23+
* The new name, which will be applied as email attachment
24+
*/
25+
private final String name;
26+
27+
/**
28+
* Constructor. Used for wrapping data source in parameter. Method {@link NamedDataSource#getName()} will
29+
* use original name of the data source
30+
*
31+
* @param dataSource wrapped data source
32+
*/
33+
public NamedDataSource(DataSource dataSource) {
34+
this.dataSource = dataSource;
35+
this.name = null;
36+
}
37+
38+
/**
39+
* Constructor. Used for wrapping data source in parameter. Method {@link NamedDataSource#getName()} will
40+
* not use the original name, but it will use the name in the parameter instead.
41+
*
42+
* @param dataSource wrapped data source
43+
* @param name new name of data source
44+
*/
45+
public NamedDataSource(String name, DataSource dataSource) {
46+
this.dataSource = dataSource;
47+
this.name = name;
48+
}
49+
50+
/**
51+
* Return the input stream from {@link #dataSource}
52+
*
53+
* @return input stream
54+
* @throws IOException if exception occurs during getting input stream from {@link #dataSource}
55+
*/
56+
@Override
57+
public InputStream getInputStream() throws IOException {
58+
return dataSource.getInputStream();
59+
}
60+
61+
/**
62+
* Return the output stream from {@link #dataSource}
63+
*
64+
* @return output stream
65+
* @throws IOException if exception occurs during getting output stream from {@link #dataSource}
66+
*/
67+
@Override
68+
public OutputStream getOutputStream() throws IOException {
69+
return dataSource.getOutputStream();
70+
}
71+
72+
/**
73+
* Return the original content type from {@link #dataSource}
74+
*
75+
* @return content type of data source
76+
*/
77+
@Override
78+
public String getContentType() {
79+
return dataSource.getContentType();
80+
}
81+
82+
/**
83+
* If parameter {@link #name} is set, then return the value of parameter name. Otherwise, return the name of the
84+
* {@link #dataSource}
85+
*
86+
* @return name of data source
87+
*/
88+
@Override
89+
public String getName() {
90+
return name != null ? name : dataSource.getName();
91+
}
92+
}

src/main/java/org/simplejavamail/mailer/internal/mailsender/MimeMessageHelper.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.simplejavamail.email.AttachmentResource;
88
import org.simplejavamail.email.Email;
99
import org.simplejavamail.email.Recipient;
10+
import org.simplejavamail.mailer.internal.datasource.NamedDataSource;
1011

1112
import javax.activation.DataHandler;
1213
import javax.activation.DataSource;
@@ -29,15 +30,15 @@
2930
*/
3031
public final class MimeMessageHelper {
3132

32-
private MimeMessageHelper() {
33-
34-
}
35-
3633
/**
3734
* Encoding used for setting body text, email address, headers, reply-to fields etc. ({@link StandardCharsets#UTF_8}).
3835
*/
3936
private static final String CHARACTER_ENCODING = StandardCharsets.UTF_8.name();
4037

38+
private MimeMessageHelper() {
39+
40+
}
41+
4142
/**
4243
* Creates a new {@link MimeMessage} instance coupled to a specific {@link Session} instance and prepares it in the email structure, so that it
4344
* can be filled and send.
@@ -207,7 +208,7 @@ private static BodyPart getBodyPartFromDatasource(final AttachmentResource attac
207208
// setting headers isn't working nicely using the javax mail API, so let's do that manually
208209
String resourceName = determineResourceName(attachmentResource, false);
209210
String fileName = determineResourceName(attachmentResource, true);
210-
attachmentPart.setDataHandler(new DataHandler(attachmentResource.getDataSource()));
211+
attachmentPart.setDataHandler(new DataHandler(new NamedDataSource(fileName, attachmentResource.getDataSource())));
211212
attachmentPart.setFileName(fileName);
212213
String contentType = attachmentResource.getDataSource().getContentType();
213214
attachmentPart.setHeader("Content-Type", contentType + "; filename=" + fileName + "; name=" + resourceName);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.simplejavamail.mailer.internal.datasource;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.mockito.Mock;
7+
import org.mockito.runners.MockitoJUnitRunner;
8+
9+
import javax.activation.DataSource;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
import static org.mockito.Mockito.*;
13+
14+
@RunWith(MockitoJUnitRunner.class)
15+
public class NamedDataSourceTest {
16+
17+
@Mock
18+
private DataSource dataSource;
19+
20+
@Before
21+
public void setUp() throws Exception {
22+
when(dataSource.getName()).thenReturn("testName");
23+
}
24+
25+
@Test
26+
public void renameWillWork() throws Exception {
27+
DataSource testDataSource = new NamedDataSource("newName", dataSource);
28+
assertThat(testDataSource.getName()).isEqualTo("newName");
29+
verifyZeroInteractions(dataSource);
30+
}
31+
32+
@Test
33+
public void originalNameWillWork() throws Exception {
34+
DataSource testDataSource = new NamedDataSource(dataSource);
35+
assertThat(testDataSource.getName()).isEqualTo("testName");
36+
verify(dataSource).getName();
37+
}
38+
39+
@Test
40+
public void inputStreamWillBeTheSame1() throws Exception {
41+
DataSource testDataSource = new NamedDataSource("newName", dataSource);
42+
testDataSource.getInputStream();
43+
verify(dataSource).getInputStream();
44+
}
45+
46+
@Test
47+
public void inputStreamWillBeTheSame2() throws Exception {
48+
DataSource testDataSource = new NamedDataSource(dataSource);
49+
testDataSource.getInputStream();
50+
verify(dataSource).getInputStream();
51+
}
52+
53+
@Test
54+
public void outputStreamWillBeTheSame1() throws Exception {
55+
DataSource testDataSource = new NamedDataSource("newName", dataSource);
56+
testDataSource.getOutputStream();
57+
verify(dataSource).getOutputStream();
58+
}
59+
60+
@Test
61+
public void outputStreamWillBeTheSame2() throws Exception {
62+
DataSource testDataSource = new NamedDataSource(dataSource);
63+
testDataSource.getOutputStream();
64+
verify(dataSource).getOutputStream();
65+
}
66+
67+
@Test
68+
public void contentTypeStreamWillBeTheSame1() throws Exception {
69+
DataSource testDataSource = new NamedDataSource("newName", dataSource);
70+
testDataSource.getContentType();
71+
verify(dataSource).getContentType();
72+
}
73+
74+
@Test
75+
public void contentTypeStreamWillBeTheSame2() throws Exception {
76+
DataSource testDataSource = new NamedDataSource(dataSource);
77+
testDataSource.getContentType();
78+
verify(dataSource).getContentType();
79+
}
80+
81+
}

0 commit comments

Comments
 (0)