Skip to content

Add HttpHeaders#get/setContentDisposition() - #1224

Closed
sdeleuze wants to merge 1 commit into
spring-projects:masterfrom
sdeleuze:SPR-14408
Closed

sdeleuze wants to merge 1 commit into
spring-projects:masterfrom
sdeleuze:SPR-14408

Conversation

@sdeleuze

Copy link
Copy Markdown
Contributor

As requested in SPR-14408, this PR adds a method to get the filename parameter of the Content-Disposition header.

@bclozel I have not yet included support for filename* encoded headers, because I am not sure we should support this. See this comment for more details. I will not merge this PR before your feedback.

@sdeleuze

Copy link
Copy Markdown
Contributor Author

After more reading, filename* is likely to be the right way to go for modern browsers, so I am going to update this PR with support for decoding it.

@sdeleuze

Copy link
Copy Markdown
Contributor Author

@rstoyanchev @poutsma I think this PR is ready to be reviewed by one of you before going to master.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Stream API could make this more readable, something like (not tested):

String contentDisposition = getFirst(CONTENT_DISPOSITION);
if (contentDisposition == null) {
    return null;
}
return Arrays.stream(StringUtils.tokenizeToStringArray(contentDisposition, ";"))
        .skip(1)
        .map(part -> StringUtils.tokenizeToStringArray(part, "="))
        .filter(pair -> {
            List<String> attributeNames = Arrays.asList("filename", "filename*");
            return (pair.length == 2 && attributeNames.contains(pair[0]));
        })
        .map(pair -> {
            String value = "";
            return new String[] { pair[0], value};
        })
        .map(pair -> "filename".equals(pair[0]) ? pair[1] : decodeHeaderField(pair[1]))
        .findFirst();

@sdeleuze sdeleuze Nov 4, 2016 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a private field, if testing is the only reason, which is the case as far as I can see.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I'm wondering if it's worth having a ContentDisposition representation along the lines of HttpHeaders#getContentDisposition(), such as for example.

@sdeleuze sdeleuze Nov 2, 2016 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would extend the scope of this feature, but yes I think that would be a good idea, more future proof than getContentDispositionFilename().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made both encodeHeaderFieldParam() and decodeHeaderFieldParam() private static methods in my last commit.

@sdeleuze sdeleuze changed the title Add HttpHeaders#getContentDispositionFilename() Add HttpHeaders#getContentDisposition() Nov 4, 2016
@sdeleuze
sdeleuze force-pushed the SPR-14408 branch 3 times, most recently from f6ba1ed to f6149da Compare November 4, 2016 22:28
@sdeleuze

sdeleuze commented Nov 4, 2016 •

Copy link
Copy Markdown
Contributor Author

@rstoyanchev I have pushed an updated commit with HttpHeaders#getContentDisposition(). I have implemented the support for the disposition type and name, filename, filename*and size parameters.

If we don't have date parsing according to RFC 822 already implemented, I would suggest to wait an eventual user request to implement creation-date, modification-date and read-date support (my guess is that they are not very commonly used, and in any case this is outside of the scope of the original issue).

I have keep an array based parsing instead of the Stream based one you proposed because Java 8 streams seems not really design to collect successive pairs + I think current implementation will be faster.

I have made the 2 encode and decode methods private as requested.

Could you have another look to this updated PR and send me your feedback if any?

@rstoyanchev

Copy link
Copy Markdown
Contributor

Sorry for the delay. A few last tweaks. Let's make ContentDisposition a top-level type like HttpMethod, MediaType, HttpRange. The parse logic can then be a static method in Content-Disposition following the same pattern as HttpRange. Similarly the setContentDisposition should take a ContentDisposition and the actual logic to format to a String can be in ContentDisposition, again like HttpRange. In other words let's encapsulate all the Content-Disposition related logic inside ContentDisposition and HttpHeaders will simply delegate to it.

@sdeleuze

sdeleuze commented Nov 7, 2016

Copy link
Copy Markdown
Contributor Author

No worry, thanks for your feedback, I will update the PR accordingly.

@rstoyanchev

Copy link
Copy Markdown
Contributor

Okay, feel free to commit straight to master after those changes.

@sdeleuze sdeleuze changed the title Add HttpHeaders#getContentDisposition() Add HttpHeaders#get/setContentDisposition() Nov 7, 2016
@sdeleuze
sdeleuze force-pushed the SPR-14408 branch 14 times, most recently from 8ba4cea to 055d095 Compare November 7, 2016 23:51
This commit introduces a new ContentDisposition class designed
to parse and generate Content-Disposition header value as defined
in RFC 2183. It supports the disposition type and the name,
filename (or filename* when encoded according to RFC 5987) and
size parameters.

This new class is usually used thanks to
HttpHeaders#getContentDisposition() and
HttpHeaders#setContentDisposition(ContentDisposition).

Issue: SPR-14408
@sdeleuze

sdeleuze commented Nov 8, 2016

Copy link
Copy Markdown
Contributor Author

Merged via 99a8510

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants