Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

MimeMessageHelper addInline with ByteArrayResource fail with null filename #33527

Closed
blink38 opened this issue Sep 12, 2024 · 3 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@blink38
Copy link

blink38 commented Sep 12, 2024

Affects: spring-context-support-6.1.12


I'm creating a mail using MimeMessageHelper. I want to add inline file using ByteArrayResource.

The function used is :

public void addInline(String contentId, Resource resource) throws MessagingException {
		Assert.notNull(resource, "Resource must not be null");
		String contentType = getFileTypeMap().getContentType(resource.getFilename());
		addInline(contentId, resource, contentType);
	}

The problem is that org.springframework.core.io.ByteArrayResource extends org.springframework.core.io.AbstractResource which implements getFilename() returning null

       /**
	 * This implementation always returns {@code null},
	 * assuming that this resource type does not have a filename.
	 */
	@Override
	@Nullable
	public String getFilename() {
		return null;
	}

So

String contentType = getFileTypeMap().getContentType(resource.getFilename());

throw a java.lang.NullPointerException because jakarta.activation.MimetypesFileTypeMap.getContentType(String filename) code start with :

 public synchronized String getContentType(String filename) {
        int dot_pos = filename.lastIndexOf("."); // period index

(jakarta.activation-api.2.1.3)

The thrown trace is

Caused by: java.lang.NullPointerException: Cannot invoke "String.lastIndexOf(String)" because "filename" is null
	at jakarta.activation.MimetypesFileTypeMap.getContentType(MimetypesFileTypeMap.java:361)
	at org.springframework.mail.javamail.ConfigurableMimeFileTypeMap.getContentType(ConfigurableMimeFileTypeMap.java:180)
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 12, 2024
@bclozel
Copy link
Member

bclozel commented Sep 12, 2024

I think this is the expected behavior here, since you are providing a Resource implementation that doesn't provide a filename. Can you choose a different Resource implementation or create your own?

@bclozel bclozel added status: waiting-for-feedback We need additional information before we can continue in: core Issues in core modules (aop, beans, core, context, expression) labels Sep 12, 2024
@blink38
Copy link
Author

blink38 commented Sep 13, 2024

I managed to work around creating my own resource implementation. A simple solution is to create a class extending ByteArrayResource which override getFilename().

But, I think it should be great the developer to be advertise of this behaviour when writing code (maybe a note in the addInline comment) instead of discover it when debugging a NullPointerException.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Sep 13, 2024
@bclozel bclozel self-assigned this Sep 13, 2024
@bclozel bclozel added type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Sep 13, 2024
@bclozel bclozel added this to the 6.2.0-RC2 milestone Sep 13, 2024
@bclozel bclozel added type: enhancement A general enhancement and removed type: documentation A documentation task labels Sep 13, 2024
@bclozel bclozel modified the milestones: 6.2.0-RC2, 6.1.14 Sep 13, 2024
@bclozel bclozel added type: bug A general bug and removed type: enhancement A general enhancement labels Sep 13, 2024
@bclozel
Copy link
Member

bclozel commented Sep 13, 2024

In the end I have processed it as a bug, since we should fall back to "application/octet-stream" (as it is the case already in other places) instead of failing with a NPE. Thanks for your report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants