Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

New NamedDataSource #50

Merged
merged 1 commit into from
Jul 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.simplejavamail.mailer.internal.datasource;

import javax.activation.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
* Data source used to fix bug, when user try to use different name for file in {@link javax.activation.FileDataSource},
* than the actual name is.
*
* @author Lukas Kosina
* @see DataSource
* @see javax.activation.FileDataSource
*/
public class NamedDataSource implements DataSource {

/**
* Original data source used for attachment
*/
private final DataSource dataSource;
/**
* The new name, which will be applied as email attachment
*/
private final String name;

/**
* Constructor. Used for wrapping data source in parameter. Method {@link NamedDataSource#getName()} will
* use original name of the data source
*
* @param dataSource wrapped data source
*/
public NamedDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.name = null;
}

/**
* Constructor. Used for wrapping data source in parameter. Method {@link NamedDataSource#getName()} will
* not use the original name, but it will use the name in the parameter instead.
*
* @param dataSource wrapped data source
* @param name new name of data source
*/
public NamedDataSource(String name, DataSource dataSource) {
this.dataSource = dataSource;
this.name = name;
}

/**
* Return the input stream from {@link #dataSource}
*
* @return input stream
* @throws IOException if exception occurs during getting input stream from {@link #dataSource}
*/
@Override
public InputStream getInputStream() throws IOException {
return dataSource.getInputStream();
}

/**
* Return the output stream from {@link #dataSource}
*
* @return output stream
* @throws IOException if exception occurs during getting output stream from {@link #dataSource}
*/
@Override
public OutputStream getOutputStream() throws IOException {
return dataSource.getOutputStream();
}

/**
* Return the original content type from {@link #dataSource}
*
* @return content type of data source
*/
@Override
public String getContentType() {
return dataSource.getContentType();
}

/**
* If parameter {@link #name} is set, then return the value of parameter name. Otherwise, return the name of the
* {@link #dataSource}
*
* @return name of data source
*/
@Override
public String getName() {
return name != null ? name : dataSource.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.simplejavamail.email.AttachmentResource;
import org.simplejavamail.email.Email;
import org.simplejavamail.email.Recipient;
import org.simplejavamail.mailer.internal.datasource.NamedDataSource;

import javax.activation.DataHandler;
import javax.activation.DataSource;
Expand All @@ -29,15 +30,15 @@
*/
public final class MimeMessageHelper {

private MimeMessageHelper() {

}

/**
* Encoding used for setting body text, email address, headers, reply-to fields etc. ({@link StandardCharsets#UTF_8}).
*/
private static final String CHARACTER_ENCODING = StandardCharsets.UTF_8.name();

private MimeMessageHelper() {

}

/**
* Creates a new {@link MimeMessage} instance coupled to a specific {@link Session} instance and prepares it in the email structure, so that it
* can be filled and send.
Expand Down Expand Up @@ -207,7 +208,7 @@ private static BodyPart getBodyPartFromDatasource(final AttachmentResource attac
// setting headers isn't working nicely using the javax mail API, so let's do that manually
String resourceName = determineResourceName(attachmentResource, false);
String fileName = determineResourceName(attachmentResource, true);
attachmentPart.setDataHandler(new DataHandler(attachmentResource.getDataSource()));
attachmentPart.setDataHandler(new DataHandler(new NamedDataSource(fileName, attachmentResource.getDataSource())));
attachmentPart.setFileName(fileName);
String contentType = attachmentResource.getDataSource().getContentType();
attachmentPart.setHeader("Content-Type", contentType + "; filename=" + fileName + "; name=" + resourceName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.simplejavamail.mailer.internal.datasource;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import javax.activation.DataSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class NamedDataSourceTest {

@Mock
private DataSource dataSource;

@Before
public void setUp() throws Exception {
when(dataSource.getName()).thenReturn("testName");
}

@Test
public void renameWillWork() throws Exception {
DataSource testDataSource = new NamedDataSource("newName", dataSource);
assertThat(testDataSource.getName()).isEqualTo("newName");
verifyZeroInteractions(dataSource);
}

@Test
public void originalNameWillWork() throws Exception {
DataSource testDataSource = new NamedDataSource(dataSource);
assertThat(testDataSource.getName()).isEqualTo("testName");
verify(dataSource).getName();
}

@Test
public void inputStreamWillBeTheSame1() throws Exception {
DataSource testDataSource = new NamedDataSource("newName", dataSource);
testDataSource.getInputStream();
verify(dataSource).getInputStream();
}

@Test
public void inputStreamWillBeTheSame2() throws Exception {
DataSource testDataSource = new NamedDataSource(dataSource);
testDataSource.getInputStream();
verify(dataSource).getInputStream();
}

@Test
public void outputStreamWillBeTheSame1() throws Exception {
DataSource testDataSource = new NamedDataSource("newName", dataSource);
testDataSource.getOutputStream();
verify(dataSource).getOutputStream();
}

@Test
public void outputStreamWillBeTheSame2() throws Exception {
DataSource testDataSource = new NamedDataSource(dataSource);
testDataSource.getOutputStream();
verify(dataSource).getOutputStream();
}

@Test
public void contentTypeStreamWillBeTheSame1() throws Exception {
DataSource testDataSource = new NamedDataSource("newName", dataSource);
testDataSource.getContentType();
verify(dataSource).getContentType();
}

@Test
public void contentTypeStreamWillBeTheSame2() throws Exception {
DataSource testDataSource = new NamedDataSource(dataSource);
testDataSource.getContentType();
verify(dataSource).getContentType();
}

}