From 62857776073ebd734e40a722c41b36d074a0bd7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Thu, 11 Sep 2014 01:28:12 -0300 Subject: [PATCH 01/10] Initial support for DownloadBuilder --- .../observer/download/DownloadBuilder.java | 161 ++++++++++++++++++ .../observer/download/FileDownload.java | 2 +- .../observer/download/FileDownloadTest.java | 47 ++++- 3 files changed, 200 insertions(+), 10 deletions(-) create mode 100644 vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java new file mode 100644 index 000000000..3802429af --- /dev/null +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java @@ -0,0 +1,161 @@ +package br.com.caelum.vraptor.observer.download; + +import static com.google.common.base.Objects.firstNonNull; +import static java.util.Objects.requireNonNull; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.InputStream; + +import javax.enterprise.inject.Vetoed; + +/** + * A Builder to create a proper instance for {@link Download} class. + * + * @author Otávio S Garcia + * @since 4.1.0 + */ +@Vetoed +public final class DownloadBuilder { + + /** + * Creates an instance for build a {@link FileDownload}.
+ * + * Download download = DownloadBuilder.of(myFile) + * .withFileName("resume.txt") // optional, default is file.getName() + * .withContentType("text/plain") // optional, null will sent as octet/stream + * .downloadable() // optional, default is inline content + * .build(); + * + */ + public static FileDownloadBuilder of(File file) { + return new FileDownloadBuilder(file); + } + + /** + * Creates an instance for build a {@link InputStreamDownload}.
+ * + * Download download = DownloadBuilder.of(myInputStream) + * .withFileName("resume.txt") // optional + * .withContentType("text/plain") // optional, null will sent as octet/stream + * .downloadable() // optional, default is inline content + * .withSize(100L) // optional + * .build(); + * + */ + public static InputStreamDownloadBuilder of(InputStream input) { + return new InputStreamDownloadBuilder(input); + } + + /** + * Creates an instance for build a {@link ByteArrayDownload}.
+ * + * Download download = DownloadBuilder.of(myFile) + * .withFileName("resume.txt") // optional + * .withContentType("text/plain") // optional, null will sent as octet/stream + * .downloadable() // optional, default is inline content + * .build(); + * + */ + public static ByteArrayDownloadBuilder of(byte[] input) { + return new ByteArrayDownloadBuilder(input); + } + + public static class FileDownloadBuilder { + private final File file; + private String fileName; + private String contentType; + private boolean doDownload; + + public FileDownloadBuilder(File file) { + this.file = requireNonNull(file, "File can't be null"); + } + + public FileDownloadBuilder withFileName(String fileName) { + this.fileName = fileName; + return this; + } + + public FileDownloadBuilder withContentType(String contentType) { + this.contentType = contentType; + return this; + } + + public FileDownloadBuilder downloadable() { + this.doDownload = true; + return this; + } + + public FileDownload build() + throws FileNotFoundException { + fileName = firstNonNull(fileName, file.getName()); + return new FileDownload(file, contentType, fileName, doDownload); + } + } + + public static class InputStreamDownloadBuilder { + private final InputStream input; + private String fileName; + private String contentType; + private long size; + private boolean doDownload; + + public InputStreamDownloadBuilder(InputStream input) { + this.input = requireNonNull(input, "InputStream can't be null"); + } + + public InputStreamDownloadBuilder withFileName(String fileName) { + this.fileName = fileName; + return this; + } + + public InputStreamDownloadBuilder withContentType(String contentType) { + this.contentType = contentType; + return this; + } + + public InputStreamDownloadBuilder withSize(long size) { + this.size = size; + return this; + } + + public InputStreamDownloadBuilder downloadable() { + this.doDownload = true; + return this; + } + + public InputStreamDownload build() { + return new InputStreamDownload(input, contentType, fileName, doDownload, size); + } + } + + public static class ByteArrayDownloadBuilder { + private final byte[] buff; + private String fileName; + private String contentType; + private boolean doDownload; + + public ByteArrayDownloadBuilder(byte[] buff) { + this.buff = requireNonNull(buff, "byte[] can't be null"); + } + + public ByteArrayDownloadBuilder withFileName(String fileName) { + this.fileName = fileName; + return this; + } + + public ByteArrayDownloadBuilder withContentType(String contentType) { + this.contentType = contentType; + return this; + } + + public ByteArrayDownloadBuilder downloadable() { + this.doDownload = true; + return this; + } + + public ByteArrayDownload build() { + return new ByteArrayDownload(buff, contentType, fileName, doDownload); + } + } +} \ No newline at end of file diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/FileDownload.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/FileDownload.java index 32bde8af2..647fce938 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/FileDownload.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/FileDownload.java @@ -67,7 +67,7 @@ public void write(HttpServletResponse response) throws IOException { private File checkFile(File file) throws FileNotFoundException { if (!file.exists()) { - throw new FileNotFoundException("File " + file.getName() + "doesn't exists"); + throw new FileNotFoundException("File " + file.getName() + " doesn't exists"); } return file; diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/FileDownloadTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/FileDownloadTest.java index fa9421ddb..af5ade3cc 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/FileDownloadTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/FileDownloadTest.java @@ -22,6 +22,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @@ -29,16 +30,22 @@ import javax.servlet.WriteListener; import javax.servlet.http.HttpServletResponse; -import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import br.com.caelum.vraptor.observer.download.FileDownload; - public class FileDownloadTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + private File file; private byte[] bytes; private @Mock HttpServletResponse response; @@ -52,7 +59,7 @@ public void setUp() throws Exception { bytes = new byte[] { (byte) 0 }; outputStream = new ByteArrayOutputStream(); - file = File.createTempFile("test", "vraptor"); + file = folder.newFile(); try (FileOutputStream fileStream = new FileOutputStream(file)) { fileStream.write(bytes); @@ -77,11 +84,6 @@ public void setWriteListener(WriteListener writeListener) { when(response.getOutputStream()).thenReturn(socketStream); } - @After - public void tearDown() { - file.delete(); - } - @Test public void shouldFlushWholeFileToHttpResponse() throws IOException { FileDownload fd = new FileDownload(file, "type"); @@ -99,4 +101,31 @@ public void shouldUseHeadersToHttpResponse() throws IOException { verify(response, times(1)).setHeader("Content-disposition", "inline; filename=x.txt"); assertArrayEquals(bytes, outputStream.toByteArray()); } + + @Test + public void builderShouldThrowsExceptionIfFileDoesntExists() throws Exception { + thrown.expect(FileNotFoundException.class); + thrown.expectMessage("File exists doesn't exists"); + + DownloadBuilder.of(new File("/path/that/doesnt/exists")).build(); + } + + @Test + public void builderShouldUseNameArgument() throws Exception { + Download fd = DownloadBuilder.of(file).withFileName("file.txt") + .withContentType("text/plain").downloadable().build(); + fd.write(response); + + verify(response).setHeader("Content-Length", String.valueOf(file.length())); + verify(response).setHeader("Content-disposition", "attachment; filename=file.txt"); + } + + @Test + public void builderShouldUseFileNameWhenNameNotPresent() throws Exception { + Download fd = DownloadBuilder.of(file).withContentType("text/plain").build(); + fd.write(response); + + verify(response).setHeader("Content-Length", String.valueOf(file.length())); + verify(response).setHeader("Content-disposition", "inline; filename=" + file.getName()); + } } From 282273978f0c8855cbc89ede8884dea8dc7b046e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Thu, 11 Sep 2014 10:07:02 -0300 Subject: [PATCH 02/10] Changing the file name to make code better to understand --- .../caelum/vraptor/observer/download/FileDownloadTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/FileDownloadTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/FileDownloadTest.java index af5ade3cc..4edaf11a4 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/FileDownloadTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/FileDownloadTest.java @@ -105,9 +105,9 @@ public void shouldUseHeadersToHttpResponse() throws IOException { @Test public void builderShouldThrowsExceptionIfFileDoesntExists() throws Exception { thrown.expect(FileNotFoundException.class); - thrown.expectMessage("File exists doesn't exists"); + thrown.expectMessage("File picture.jpg doesn't exists"); - DownloadBuilder.of(new File("/path/that/doesnt/exists")).build(); + DownloadBuilder.of(new File("/path/that/doesnt/exists/picture.jpg")).build(); } @Test From e3a19283fb997d669c80ff6a6dbf867819ebd2c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Thu, 11 Sep 2014 10:09:13 -0300 Subject: [PATCH 03/10] Using abstract builder to extract common code (thanks @Turini) --- .../observer/download/DownloadBuilder.java | 78 ++++++------------- 1 file changed, 22 insertions(+), 56 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java index 3802429af..b87fe3d38 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java @@ -61,101 +61,67 @@ public static ByteArrayDownloadBuilder of(byte[] input) { return new ByteArrayDownloadBuilder(input); } - public static class FileDownloadBuilder { - private final File file; - private String fileName; - private String contentType; - private boolean doDownload; - - public FileDownloadBuilder(File file) { - this.file = requireNonNull(file, "File can't be null"); - } + public static abstract class AbstractDownloadBuilder { + protected String fileName; + protected String contentType; + protected boolean doDownload; - public FileDownloadBuilder withFileName(String fileName) { + public T withFileName(String fileName) { this.fileName = fileName; - return this; + return (T) this; } - public FileDownloadBuilder withContentType(String contentType) { + public T withContentType(String contentType) { this.contentType = contentType; - return this; + return (T) this; } - public FileDownloadBuilder downloadable() { + public T downloadable() { this.doDownload = true; - return this; + return (T) this; + } + } + + public static class FileDownloadBuilder extends AbstractDownloadBuilder { + private final File file; + + public FileDownloadBuilder(File file) { + this.file = requireNonNull(file, "File can't be null"); } - public FileDownload build() - throws FileNotFoundException { + public FileDownload build() throws FileNotFoundException { fileName = firstNonNull(fileName, file.getName()); return new FileDownload(file, contentType, fileName, doDownload); } } - public static class InputStreamDownloadBuilder { + public static class InputStreamDownloadBuilder extends AbstractDownloadBuilder { private final InputStream input; - private String fileName; - private String contentType; private long size; - private boolean doDownload; public InputStreamDownloadBuilder(InputStream input) { this.input = requireNonNull(input, "InputStream can't be null"); } - public InputStreamDownloadBuilder withFileName(String fileName) { - this.fileName = fileName; - return this; - } - - public InputStreamDownloadBuilder withContentType(String contentType) { - this.contentType = contentType; - return this; - } - public InputStreamDownloadBuilder withSize(long size) { this.size = size; return this; } - public InputStreamDownloadBuilder downloadable() { - this.doDownload = true; - return this; - } - public InputStreamDownload build() { return new InputStreamDownload(input, contentType, fileName, doDownload, size); } } - public static class ByteArrayDownloadBuilder { + public static class ByteArrayDownloadBuilder extends AbstractDownloadBuilder { private final byte[] buff; - private String fileName; - private String contentType; - private boolean doDownload; public ByteArrayDownloadBuilder(byte[] buff) { this.buff = requireNonNull(buff, "byte[] can't be null"); } - public ByteArrayDownloadBuilder withFileName(String fileName) { - this.fileName = fileName; - return this; - } - - public ByteArrayDownloadBuilder withContentType(String contentType) { - this.contentType = contentType; - return this; - } - - public ByteArrayDownloadBuilder downloadable() { - this.doDownload = true; - return this; - } - public ByteArrayDownload build() { return new ByteArrayDownload(buff, contentType, fileName, doDownload); } } -} \ No newline at end of file +} From 389dcde63f759f310a81a59c95cac86d79616645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Thu, 11 Sep 2014 12:00:31 -0300 Subject: [PATCH 04/10] Adding better javadoc code --- .../observer/download/DownloadBuilder.java | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java index b87fe3d38..9ab46f4d2 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java @@ -20,12 +20,15 @@ public final class DownloadBuilder { /** * Creates an instance for build a {@link FileDownload}.
+ * * - * Download download = DownloadBuilder.of(myFile) - * .withFileName("resume.txt") // optional, default is file.getName() - * .withContentType("text/plain") // optional, null will sent as octet/stream - * .downloadable() // optional, default is inline content - * .build(); + * Download download = DownloadBuilder.of(myFile) + * .withFileName("resume.txt") // optional, default is file.getName() + * .withContentType("text/plain") // optional, null will sent as octet/stream + * .downloadable() // optional, default is inline content + * .build(); + * @param file The input file. + * @throws NullPointerException If the {@code file} argument is {@code null} * */ public static FileDownloadBuilder of(File file) { @@ -34,14 +37,17 @@ public static FileDownloadBuilder of(File file) { /** * Creates an instance for build a {@link InputStreamDownload}.
+ * * - * Download download = DownloadBuilder.of(myInputStream) - * .withFileName("resume.txt") // optional - * .withContentType("text/plain") // optional, null will sent as octet/stream - * .downloadable() // optional, default is inline content - * .withSize(100L) // optional - * .build(); + * Download download = DownloadBuilder.of(myInputStream) + * .withFileName("resume.txt") // optional + * .withContentType("text/plain") // optional, null will sent as octet/stream + * .downloadable() // optional, default is inline content + * .withSize(100L) // optional + * .build(); * + * @param file The input InputStream to process. + * @throws NullPointerException If the {@code input} argument is {@code null} */ public static InputStreamDownloadBuilder of(InputStream input) { return new InputStreamDownloadBuilder(input); @@ -49,13 +55,16 @@ public static InputStreamDownloadBuilder of(InputStream input) { /** * Creates an instance for build a {@link ByteArrayDownload}.
+ * * - * Download download = DownloadBuilder.of(myFile) - * .withFileName("resume.txt") // optional - * .withContentType("text/plain") // optional, null will sent as octet/stream - * .downloadable() // optional, default is inline content - * .build(); + * Download download = DownloadBuilder.of(myFile) + * .withFileName("resume.txt") // optional + * .withContentType("text/plain") // optional, null will sent as octet/stream + * .downloadable() // optional, default is inline content + * .build(); * + * @param file The input byte array. + * @throws NullPointerException If the {@code input} argument is {@code null} */ public static ByteArrayDownloadBuilder of(byte[] input) { return new ByteArrayDownloadBuilder(input); From 37899c2bd854b200cf0de27515b576471ef5ad4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Thu, 11 Sep 2014 12:01:18 -0300 Subject: [PATCH 05/10] Fixing javadoc code --- .../caelum/vraptor/observer/download/DownloadBuilder.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java index 9ab46f4d2..2e818bf3f 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadBuilder.java @@ -27,9 +27,10 @@ public final class DownloadBuilder { * .withContentType("text/plain") // optional, null will sent as octet/stream * .downloadable() // optional, default is inline content * .build(); + * + * * @param file The input file. * @throws NullPointerException If the {@code file} argument is {@code null} - * */ public static FileDownloadBuilder of(File file) { return new FileDownloadBuilder(file); @@ -46,6 +47,7 @@ public static FileDownloadBuilder of(File file) { * .withSize(100L) // optional * .build(); * + * * @param file The input InputStream to process. * @throws NullPointerException If the {@code input} argument is {@code null} */ @@ -63,6 +65,7 @@ public static InputStreamDownloadBuilder of(InputStream input) { * .downloadable() // optional, default is inline content * .build(); * + * * @param file The input byte array. * @throws NullPointerException If the {@code input} argument is {@code null} */ From c7f2bd1eb40b86f8f2ec805813ff146537e81586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Fri, 12 Sep 2014 15:23:51 -0300 Subject: [PATCH 06/10] Adding test for byte array --- .../download/ByteArrayDownloadTest.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/ByteArrayDownloadTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/ByteArrayDownloadTest.java index 7e1d738f2..44196f509 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/ByteArrayDownloadTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/ByteArrayDownloadTest.java @@ -20,6 +20,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -28,14 +29,17 @@ import javax.servlet.http.HttpServletResponse; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import br.com.caelum.vraptor.observer.download.ByteArrayDownload; - public class ByteArrayDownloadTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + private byte[] bytes; private @Mock HttpServletResponse response; private ServletOutputStream socketStream; @@ -45,7 +49,7 @@ public class ByteArrayDownloadTest { public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - bytes = new byte[] { (byte) 0 }; + bytes = new byte[] { (byte) 0x0 }; this.outputStream = new ByteArrayOutputStream(); this.socketStream = new ServletOutputStream() { @@ -83,4 +87,21 @@ public void shouldUseHeadersToHttpResponse() throws IOException { verify(response, times(1)).setHeader("Content-type", "type"); assertArrayEquals(bytes, outputStream.toByteArray()); } + + @Test + public void builderShouldThrowsExceptionIfInputDataIsNull() throws Exception { + thrown.expect(NullPointerException.class); + + DownloadBuilder.of((ByteArrayInputStream) null).build(); + } + + @Test + public void testConstructWithDownloadBuilder() throws Exception { + Download fd = DownloadBuilder.of(bytes).withFileName("file.txt") + .withContentType("text/plain").downloadable().build(); + fd.write(response); + + verify(response).setHeader("Content-Length", String.valueOf(bytes.length)); + verify(response).setHeader("Content-disposition", "attachment; filename=file.txt"); + } } From f2cc3c8afc72ae92389e0459ec07ddf3c7f1946f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Fri, 12 Sep 2014 15:26:38 -0300 Subject: [PATCH 07/10] Adding tests for InputStream download --- .../download/InputStreamDownloadTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/InputStreamDownloadTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/InputStreamDownloadTest.java index 967f23b52..a61da6da4 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/InputStreamDownloadTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/InputStreamDownloadTest.java @@ -29,7 +29,9 @@ import javax.servlet.http.HttpServletResponse; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -37,6 +39,9 @@ public class InputStreamDownloadTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + private InputStream inputStream; private byte[] bytes; private @Mock HttpServletResponse response; @@ -87,4 +92,21 @@ public void shouldUseHeadersToHttpResponse() throws IOException { assertArrayEquals(bytes, outputStream.toByteArray()); } + @Test + public void builderShouldThrowsExceptionIfInputStreamIsNull() throws Exception { + thrown.expect(NullPointerException.class); + + DownloadBuilder.of((InputStream) null).build(); + } + + @Test + public void testConstructWithDownloadBuilder() throws Exception { + Download fd = DownloadBuilder.of(inputStream).withFileName("file.txt") + .withSize(bytes.length) + .withContentType("text/plain").downloadable().build(); + fd.write(response); + + verify(response).setHeader("Content-Length", String.valueOf(bytes.length)); + verify(response).setHeader("Content-disposition", "attachment; filename=file.txt"); + } } From 27b87ac9c039806e855665d90b9413a95b58d80c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Fri, 12 Sep 2014 15:28:47 -0300 Subject: [PATCH 08/10] Fixing typo: from ByteArrayInputStream to byte[] --- .../caelum/vraptor/observer/download/ByteArrayDownloadTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/ByteArrayDownloadTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/ByteArrayDownloadTest.java index 44196f509..4977063d0 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/ByteArrayDownloadTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/download/ByteArrayDownloadTest.java @@ -92,7 +92,7 @@ public void shouldUseHeadersToHttpResponse() throws IOException { public void builderShouldThrowsExceptionIfInputDataIsNull() throws Exception { thrown.expect(NullPointerException.class); - DownloadBuilder.of((ByteArrayInputStream) null).build(); + DownloadBuilder.of((byte[]) null).build(); } @Test From f849948d73db49cb616e5a9ab80416a923a621cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Fri, 12 Sep 2014 15:37:32 -0300 Subject: [PATCH 09/10] Creating english docs for DownloadBuilder --- .../content/en/docs/download-and-upload.html | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vraptor-site/content/en/docs/download-and-upload.html b/vraptor-site/content/en/docs/download-and-upload.html index ec437419f..55e25df8d 100644 --- a/vraptor-site/content/en/docs/download-and-upload.html +++ b/vraptor-site/content/en/docs/download-and-upload.html @@ -59,6 +59,18 @@ } ~~~ +##Using DownloadBuilder + +`DownloadBuilder` is a class to help you to create instances for `Download`, using a fluent interface. To create a `FileDownload` you can write this code: + +~~~ +#!java +FileDownload download = DownloadBuilder.of(myFile) + .withFileName("resume.txt") // optional, default is File.getName() + .withContentType("text/plain") // optional, null will not sent + .downloadable() // optional, default is inline content + .build(); +~~~ ##Upload From 7b4d4fbd5db9c581b933a25e62a2f78a78b8ad40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ot=C3=A1vio=20Garcia?= Date: Fri, 12 Sep 2014 15:39:58 -0300 Subject: [PATCH 10/10] Updating portuguese docs about DownloadBuilder --- vraptor-site/content/pt/docs/download-e-upload.html | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vraptor-site/content/pt/docs/download-e-upload.html b/vraptor-site/content/pt/docs/download-e-upload.html index 8e8395d42..128b536af 100644 --- a/vraptor-site/content/pt/docs/download-e-upload.html +++ b/vraptor-site/content/pt/docs/download-e-upload.html @@ -60,6 +60,18 @@ } ~~~ +##Using DownloadBuilder + +`DownloadBuilder` é uma classe útil para ajudar você a criar instâncias da classe `Download`, usando uma interface fluente. Para criar uma instância de um `FileDownload` você pode escrever o seguinte código: + +~~~ +#!java +FileDownload download = DownloadBuilder.of(meuArquivo) + .withFileName("curriculo.txt") // opcional, o padrão é File.getName() + .withContentType("text/plain") // optional, não será enviado se nulo + .downloadable() // opcional, o padrão é inline content + .build(); +~~~ ##Upload