Skip to content

Commit

Permalink
#307, #310 (#249): for resource names, don't remove extensions anymor…
Browse files Browse the repository at this point in the history
…e. I'm not sure why this was in there in the first place, but it was causing bugs, so I just removed it completely. Good riddance.
  • Loading branch information
bbottema committed Apr 10, 2021
1 parent fa20c5e commit 7f9e408
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ private static BodyPart getBodyPartFromDatasource(final AttachmentResource attac
throws MessagingException {
final BodyPart attachmentPart = new MimeBodyPart();
// setting headers isn't working nicely using the javax mail API, so let's do that manually
final String resourceName = determineResourceName(attachmentResource, false, true);
final String fileName = determineResourceName(attachmentResource, true, false);
final String resourceName = determineResourceName(attachmentResource, true);
final String fileName = determineResourceName(attachmentResource, false);
attachmentPart.setDataHandler(new DataHandler(new NamedDataSource(fileName, attachmentResource.getDataSource())));
attachmentPart.setFileName(fileName);
final String contentType = attachmentResource.getDataSource().getContentType();
ParameterList pl = new ParameterList();
pl.set("filename", fileName);
pl.set("name", fileName);
attachmentPart.setHeader("Content-Type", contentType + pl.toString());
attachmentPart.setHeader("Content-Type", contentType + pl);
attachmentPart.setHeader("Content-ID", format("<%s>", resourceName));
attachmentPart.setDisposition(dispositionType);
return attachmentPart;
Expand All @@ -251,7 +251,7 @@ private static BodyPart getBodyPartFromDatasource(final AttachmentResource attac
/**
* Determines the right resource name and optionally attaches the correct extension to the name. The result is mime encoded.
*/
static String determineResourceName(final AttachmentResource attachmentResource, final boolean includeExtension, final boolean encodeResourceName) {
static String determineResourceName(final AttachmentResource attachmentResource, final boolean encodeResourceName) {
final String datasourceName = attachmentResource.getDataSource().getName();

String resourceName;
Expand All @@ -263,20 +263,12 @@ static String determineResourceName(final AttachmentResource attachmentResource,
} else {
resourceName = "resource" + UUID.randomUUID();
}
if (includeExtension && !valueNullOrEmpty(datasourceName)) {
if (!valueNullOrEmpty(datasourceName)) {
resourceName = possiblyAddExtension(datasourceName, resourceName);
} else if (!includeExtension && resourceName.contains(".") && resourceName.equals(datasourceName)) {
resourceName = removeExtension(resourceName);
}
return encodeResourceName ? MiscUtil.encodeText(resourceName) : resourceName;
}

@NotNull
private static String removeExtension(String resourceName) {
final String extension = resourceName.substring(resourceName.lastIndexOf("."));
return resourceName.replace(extension, "");
}

@NotNull
private static String possiblyAddExtension(final String datasourceName, String resourceName) {
@SuppressWarnings("UnnecessaryLocalVariable")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,64 +45,56 @@ public void setup() {
public void determineResourceName1()
throws IOException {
AttachmentResource resource1 = new AttachmentResource(null, getDataSource("blahblah"));
assertThat(MimeMessageHelper.determineResourceName(resource1, false, true)).isEqualTo("blahblah");
assertThat(MimeMessageHelper.determineResourceName(resource1, true, true)).isEqualTo("blahblah");
assertThat(MimeMessageHelper.determineResourceName(resource1, true)).isEqualTo("blahblah");
}

@Test
public void determineResourceName2()
throws IOException {
AttachmentResource resource2 = new AttachmentResource(null, getDataSource("blahblah.txt"));
assertThat(MimeMessageHelper.determineResourceName(resource2, false, true)).isEqualTo("blahblah");
assertThat(MimeMessageHelper.determineResourceName(resource2, true, true)).isEqualTo("blahblah.txt");
assertThat(MimeMessageHelper.determineResourceName(resource2, true)).isEqualTo("blahblah.txt");
}

@Test
public void determineResourceName3()
throws IOException {
AttachmentResource resource3 = new AttachmentResource("the resource", getDataSource(null));
assertThat(MimeMessageHelper.determineResourceName(resource3, false, true)).isEqualTo("the resource");
assertThat(MimeMessageHelper.determineResourceName(resource3, true, true)).isEqualTo("the resource");
assertThat(MimeMessageHelper.determineResourceName(resource3, true)).isEqualTo("the resource");
}

@Test
public void determineResourceName4()
throws IOException {
AttachmentResource resource4 = new AttachmentResource("the resource", getDataSource("blahblah.txt"));
assertThat(MimeMessageHelper.determineResourceName(resource4, false, true)).isEqualTo("the resource");
assertThat(MimeMessageHelper.determineResourceName(resource4, true, true)).isEqualTo("the resource.txt");
assertThat(MimeMessageHelper.determineResourceName(resource4, true)).isEqualTo("the resource.txt");
}

@Test
public void determineResourceName5()
throws IOException {
AttachmentResource resource5 = new AttachmentResource("the resource", getDataSource("blahblah"));
assertThat(MimeMessageHelper.determineResourceName(resource5, false, true)).isEqualTo("the resource");
assertThat(MimeMessageHelper.determineResourceName(resource5, true, true)).isEqualTo("the resource");
assertThat(MimeMessageHelper.determineResourceName(resource5, true)).isEqualTo("the resource");
}

@Test
public void determineResourceName6()
throws IOException {
AttachmentResource resource6 = new AttachmentResource("the resource.txt", getDataSource("blahblah.txt"));
assertThat(MimeMessageHelper.determineResourceName(resource6, false, true)).isEqualTo("the resource.txt");
assertThat(MimeMessageHelper.determineResourceName(resource6, true, true)).isEqualTo("the resource.txt");
assertThat(MimeMessageHelper.determineResourceName(resource6, true)).isEqualTo("the resource.txt");
}

@Test
public void determineResourceName7()
throws IOException {
AttachmentResource resource7 = new AttachmentResource("the resource.txt", getDataSource("blahblah"));
assertThat(MimeMessageHelper.determineResourceName(resource7, false, true)).isEqualTo("the resource.txt");
assertThat(MimeMessageHelper.determineResourceName(resource7, true, true)).isEqualTo("the resource.txt");
assertThat(MimeMessageHelper.determineResourceName(resource7, true)).isEqualTo("the resource.txt");
}

@Test
public void determineResourceName_ignoreExtensionFromResource()
throws IOException {
AttachmentResource resource7 = new AttachmentResource("the resource.txt", getDataSource("blahblah.1/www/get?id=3"));
assertThat(MimeMessageHelper.determineResourceName(resource7, false, true)).isEqualTo("the resource.txt");
assertThat(MimeMessageHelper.determineResourceName(resource7, true, true)).isEqualTo("the resource.txt");
assertThat(MimeMessageHelper.determineResourceName(resource7, true)).isEqualTo("the resource.txt");
}

private ByteArrayDataSource getDataSource(@Nullable String name)
Expand Down

0 comments on commit 7f9e408

Please sign in to comment.