Skip to content

Commit

Permalink
removing isDownloadType() method from DownloadObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
Turini committed Nov 13, 2013
1 parent d72adcd commit 3d05702
Showing 1 changed file with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -86,15 +82,19 @@ 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);
}
if (result instanceof byte[]) {
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);
}

This comment has been minimized.

Copy link
@lucascs

lucascs Nov 13, 2013

Member

👎 consider wrapping this exception in something more specific.

This comment has been minimized.

Copy link
@Turini

Turini Nov 13, 2013

Author Member

what do you suggest?

This comment has been minimized.

Copy link
@lucascs

lucascs Nov 13, 2013

Member

If the observer method can throw an arbitrary exception, just propagate ( throws IOException )

This comment has been minimized.

Copy link
@Turini

Turini Nov 13, 2013

Author Member

done here 13383e2

}
if (result instanceof Download) {
return (Download) result;
Expand Down

0 comments on commit 3d05702

Please sign in to comment.