Skip to content

Commit

Permalink
Helper method in Response for setting filename #344
Browse files Browse the repository at this point in the history
  • Loading branch information
decebals committed Jan 22, 2017
1 parent 0e00c99 commit 089fe04
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions pippo-core/src/main/java/ro/pippo/core/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@ public Response header(String name, Date value) {
return this;
}

/**
* Sets a filename header. It's a shortcut for {@code header(HttpConstants.Header.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")}.
*
* @param filename
* @return
*/
public Response filenameHeader(String filename) {
if (filename != null && !filename.isEmpty()) {
header(HttpConstants.Header.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"");
} else {
header(HttpConstants.Header.CONTENT_DISPOSITION, "attachment; filename=\"\"");
}

return this;
}

/**
* Returns a header value, if set in the Response.
*
Expand Down Expand Up @@ -958,11 +974,7 @@ public void file(String filename, InputStream input) {
}

if (isHeaderEmpty(HttpConstants.Header.CONTENT_DISPOSITION)) {
if (filename != null && !filename.isEmpty()) {
header(HttpConstants.Header.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"");
} else {
header(HttpConstants.Header.CONTENT_DISPOSITION, "attachment; filename=\"\"");
}
filenameHeader(filename);
}

finalizeResponse();
Expand Down

0 comments on commit 089fe04

Please sign in to comment.