From 3d05702d6efa8f010f961d23d64688aeb049290c Mon Sep 17 00:00:00 2001 From: Rodrigo Turini Date: Wed, 13 Nov 2013 10:24:56 -0200 Subject: [PATCH] removing isDownloadType() method from DownloadObserver --- .../observer/download/DownloadObserver.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadObserver.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadObserver.java index ff0da8c32..436608d79 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadObserver.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/download/DownloadObserver.java @@ -21,6 +21,7 @@ import static org.slf4j.LoggerFactory.getLogger; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -60,24 +61,19 @@ public DownloadObserver(HttpServletResponse response, Result result) { this.result = result; } - public boolean isDownloadType(Class type) { - return InputStream.class.isAssignableFrom(type) || type == File.class - || Download.class.isAssignableFrom(type) || type == byte[].class; - } - public void download(@Observes MethodExecuted event) { - if (isDownloadType(event.getControllerMethod().getMethod().getReturnType())) { + Object result = event.getMethodInfo().getResult(); + Download download = resolveDownload(result); - logger.debug("Sending a file to the client"); + if (download != null) { - Object result = event.getMethodInfo().getResult(); + logger.debug("Sending a file to the client"); if (result == null && this.result.used()) return; checkNotNull(result, "You've just returned a Null Download. Consider redirecting to another page/logic"); try (OutputStream output = response.getOutputStream()) { - Download download = resolveDownload(result); download.write(response); output.flush(); } catch (IOException e) { @@ -86,7 +82,7 @@ public void download(@Observes MethodExecuted event) { } } - private Download resolveDownload(Object result) throws IOException { + private Download resolveDownload(Object result) { if (result instanceof InputStream) { return new InputStreamDownload((InputStream) result, null, null); } @@ -94,7 +90,11 @@ private Download resolveDownload(Object result) throws IOException { return new ByteArrayDownload((byte[]) result, null, null); } if (result instanceof File) { - return new FileDownload((File) result, null, null); + try { + return new FileDownload((File) result, null, null); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } } if (result instanceof Download) { return (Download) result;