I am processing file inside my method - basically preparing it for the download.
But I decided to split actions for setting headers and reading bytes. As Java does not support method declaration inside methods, I decided to go with class inside method.
public static String downloadFile(final String fileBody, HttpServletResponse response) {
class FileDownloader {
private static final String HEADER_KEY = "Content-Disposition";
private static final String HEADER_VALUE = "attachment; filename=\"%s\"";
private static final String TEMPLATE_FILE_NAME = "filename.html";
private void setHeader(HttpServletResponse response) {
response.setHeader(HEADER_KEY, String.format(HEADER_VALUE, TEMPLATE_FILE_NAME));
}
public void processResponse(HttpServletResponse response) throws IOException {
byte[] bytes = fileBody.getBytes();
IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream());
setHeader(response);
}
}
try {
new FileDownloader().processResponse(response);
return StringUtils.EMPTY;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
But I am not sure whether the idea I've come up with is good or bad. Basically there is no reason for method separation because they are not doing a lot of work.