Commit e720b343f5155075376b1ba76d90393ea5c22a78

Authored by Dancovich
1 parent f9b2d89f
Exists in master

Implementado recurso para instruir o browser a forçar um download do

conteúdo renderizado, através do comando "Content-Disposition" para
colocar a instrução "attachment".
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/FileRendererImpl.java
@@ -69,13 +69,15 @@ public class FileRendererImpl implements FileRenderer { @@ -69,13 +69,15 @@ public class FileRendererImpl implements FileRenderer {
69 private FacesContext context; 69 private FacesContext context;
70 70
71 @Override 71 @Override
72 - public void render(final byte[] byteArray, final ContentType contentType, final String fileName) { 72 + public void render(final byte[] byteArray, final ContentType contentType, final String fileName, boolean forceDownload) {
73 logger.debug("Renderizando para o arquivo " + fileName + "."); 73 logger.debug("Renderizando para o arquivo " + fileName + ".");
74 74
75 try { 75 try {
76 response.setContentType(contentType.getContentType()); 76 response.setContentType(contentType.getContentType());
77 response.setContentLength(byteArray.length); 77 response.setContentLength(byteArray.length);
78 - response.setHeader("Content-Disposition", "filename=\"" + fileName + "\""); 78 +
  79 + String forceDownloadCommand = forceDownload ? "attachment; " : "";
  80 + response.setHeader("Content-Disposition", forceDownloadCommand + "filename=\"" + fileName + "\"");
79 81
80 logger.debug("Escrevendo o arquivo " + fileName + " no response."); 82 logger.debug("Escrevendo o arquivo " + fileName + " no response.");
81 response.getOutputStream().write(byteArray, 0, byteArray.length); 83 response.getOutputStream().write(byteArray, 0, byteArray.length);
@@ -87,23 +89,38 @@ public class FileRendererImpl implements FileRenderer { @@ -87,23 +89,38 @@ public class FileRendererImpl implements FileRenderer {
87 } 89 }
88 context.responseComplete(); 90 context.responseComplete();
89 } 91 }
  92 +
  93 + @Override
  94 + public void render(final byte[] byteArray, final ContentType contentType, final String fileName) {
  95 + render(byteArray, contentType, fileName, false);
  96 + }
90 97
91 @Override 98 @Override
92 - public void render(final InputStream stream, final ContentType contentType, final String fileName) { 99 + public void render(final InputStream stream, final ContentType contentType, final String fileName, boolean forceDownload) {
93 logger.debug("Renderizando o arquivo " + fileName + "."); 100 logger.debug("Renderizando o arquivo " + fileName + ".");
94 - render(getBytes(stream), contentType, fileName); 101 + render(getBytes(stream), contentType, fileName, forceDownload);
  102 + }
  103 +
  104 + @Override
  105 + public void render(final InputStream stream, final ContentType contentType, final String fileName) {
  106 + render(stream, contentType, fileName, false);
95 } 107 }
96 108
97 @Override 109 @Override
98 - public void render(File file, ContentType contentType, String fileName) { 110 + public void render(File file, ContentType contentType, String fileName, boolean forceDownload) {
99 logger.debug("Renderizando para o arquivo " + fileName + "."); 111 logger.debug("Renderizando para o arquivo " + fileName + ".");
100 try { 112 try {
101 - render(new FileInputStream(file), contentType, fileName); 113 + render(new FileInputStream(file), contentType, fileName, forceDownload);
102 } catch (FileNotFoundException e) { 114 } catch (FileNotFoundException e) {
103 logger.info("Erro na geração do relatório. Incluíndo a exceção de erro em um FacesMessage", e); 115 logger.info("Erro na geração do relatório. Incluíndo a exceção de erro em um FacesMessage", e);
104 Faces.addMessage(e); 116 Faces.addMessage(e);
105 } 117 }
106 } 118 }
  119 +
  120 + @Override
  121 + public void render(File file, ContentType contentType, String fileName) {
  122 + render(file, contentType, fileName, false);
  123 + }
107 124
108 private byte[] getBytes(InputStream stream) { 125 private byte[] getBytes(InputStream stream) {
109 byte[] byteArray = null; 126 byte[] byteArray = null;
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/util/FileRenderer.java
@@ -53,6 +53,17 @@ public interface FileRenderer extends Serializable { @@ -53,6 +53,17 @@ public interface FileRenderer extends Serializable {
53 * @param byteArray Byte Array to be rendered. 53 * @param byteArray Byte Array to be rendered.
54 * @param contentType 54 * @param contentType
55 * @param fileName 55 * @param fileName
  56 + * @param forceDownload If true, will force a download from the browser, otherwise the browser is free to determine what will happen with the rendered content.
  57 + */
  58 + void render(final byte[] byteArray, final ContentType contentType, final String fileName, boolean forceDownload);
  59 +
  60 + /**
  61 + * Renders a byteArray for display to the user.
  62 + *
  63 + * @param byteArray Byte Array to be rendered.
  64 + * @param contentType
  65 + * @param fileName
  66 + *
56 */ 67 */
57 void render(final byte[] byteArray, final ContentType contentType, final String fileName); 68 void render(final byte[] byteArray, final ContentType contentType, final String fileName);
58 69
@@ -64,6 +75,16 @@ public interface FileRenderer extends Serializable { @@ -64,6 +75,16 @@ public interface FileRenderer extends Serializable {
64 * @param fileName 75 * @param fileName
65 */ 76 */
66 void render(final InputStream stream, final ContentType contentType, final String fileName); 77 void render(final InputStream stream, final ContentType contentType, final String fileName);
  78 +
  79 + /**
  80 + * Renders an inputStream for display to the user.
  81 + *
  82 + * @param stream
  83 + * @param contentType
  84 + * @param fileName
  85 + * @param forceDownload If true, will force a download from the browser, otherwise the browser is free to determine what will happen with the rendered content.
  86 + */
  87 + void render(final InputStream stream, final ContentType contentType, final String fileName, boolean forceDownload);
67 88
68 /** 89 /**
69 * Renders a file for display to the user. 90 * Renders a file for display to the user.
@@ -73,6 +94,16 @@ public interface FileRenderer extends Serializable { @@ -73,6 +94,16 @@ public interface FileRenderer extends Serializable {
73 * @param fileName 94 * @param fileName
74 */ 95 */
75 void render(final File file, final ContentType contentType, final String fileName); 96 void render(final File file, final ContentType contentType, final String fileName);
  97 +
  98 + /**
  99 + * Renders a file for display to the user.
  100 + *
  101 + * @param file
  102 + * @param contentType
  103 + * @param fileName
  104 + * @param forceDownload If true, will force a download from the browser, otherwise the browser is free to determine what will happen with the rendered content.
  105 + */
  106 + void render(final File file, final ContentType contentType, final String fileName, boolean forceDownload);
76 107
77 /** 108 /**
78 * File content type. 109 * File content type.