-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removing isDownloadType() method from DownloadObserver
- Loading branch information
Showing
1 changed file
with
11 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lucascs
Member
|
||
} | ||
if (result instanceof Download) { | ||
return (Download) result; | ||
|
👎 consider wrapping this exception in something more specific.