Skip to content

Add content disposition setter to ResponseEntity #33343

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

Closed
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
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -68,6 +68,7 @@
*
* @author Arjen Poutsma
* @author Brian Clozel
* @author Ricardo Figueroa
* @author Sebastien Deleuze
* @since 3.0.2
* @param <T> the body type
Expand Down Expand Up @@ -501,6 +502,15 @@ public interface BodyBuilder extends HeadersBuilder<BodyBuilder> {
*/
BodyBuilder contentType(MediaType contentType);

/**
* Set the {@linkplain ContentDisposition content disposition} of the body, as specified by the
* {@code Content-Disposition} header.
* @param contentDisposition the content disposition
* @return this builder
* @see HttpHeaders#setContentDisposition(ContentDisposition)
*/
BodyBuilder contentDisposition(ContentDisposition contentDisposition);

/**
* Set the body of the response entity and returns it.
* @param <T> the type of the body
Expand Down Expand Up @@ -567,6 +577,12 @@ public BodyBuilder contentType(MediaType contentType) {
return this;
}

@Override
public BodyBuilder contentDisposition(ContentDisposition contentDisposition) {
this.headers.setContentDisposition(contentDisposition);

Choose a reason for hiding this comment

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

This looks well written, Just a small comment
Consider adding a null check for the contentDisposition parameter. If null values are not allowed, you could throw an IllegalArgumentException

return this;
}

@Override
public BodyBuilder eTag(@Nullable String etag) {
if (etag != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
/**
* @author Arjen Poutsma
* @author Marcel Overdijk
* @author Ricardo Figueroa
* @author Kazuki Shimizu
* @author Sebastien Deleuze
*/
Expand Down Expand Up @@ -188,14 +189,19 @@ void headers() {
URI location = URI.create("location");
long contentLength = 67890;
MediaType contentType = MediaType.TEXT_PLAIN;
ContentDisposition contentDisposition = ContentDisposition.
attachment().
filename("filename.jpg").
build();

ResponseEntity<Void> responseEntity = ResponseEntity.ok().
allow(HttpMethod.GET).
lastModified(12345L).
location(location).
contentLength(contentLength).
contentType(contentType).
headers(headers -> assertThat(headers).hasSize(5)).
contentDisposition(contentDisposition).
headers(headers -> assertThat(headers).hasSize(6)).
build();

assertThat(responseEntity).isNotNull();
Expand All @@ -207,6 +213,7 @@ void headers() {
assertThat(responseHeaders.getFirst(HttpHeaders.LOCATION)).isEqualTo(location.toASCIIString());
assertThat(responseHeaders.getFirst(HttpHeaders.CONTENT_LENGTH)).isEqualTo(String.valueOf(contentLength));
assertThat(responseHeaders.getFirst(HttpHeaders.CONTENT_TYPE)).isEqualTo(contentType.toString());
assertThat(responseHeaders.getFirst(HttpHeaders.CONTENT_DISPOSITION)).isEqualTo("attachment; filename=\"filename.jpg\"");

assertThat(responseEntity.getBody()).isNull();
}
Expand Down