19 package org.turro.elephant.download;
21 import java.io.IOException;
22 import java.util.HashMap;
24 import java.util.function.Predicate;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.activation.MimetypesFileTypeMap;
28 import javax.servlet.ServletContext;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import org.amic.util.string.Cipher;
32 import org.apache.commons.io.IOUtils;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.context.IConstructor;
35 import org.turro.elephant.direct.DirectContent;
36 import org.turro.elephant.direct.DirectContents;
37 import org.turro.elephant.direct.IDirectContent;
38 import org.turro.log.SystemLogger;
39 import org.turro.util.IdGenerator;
48 private static final String CONTENT_DOWNLOAD_KEY =
"contentDownKeys";
49 public static final MimetypesFileTypeMap MIME_TYPES_FILE_MAP =
new MimetypesFileTypeMap();
52 Map<String, ContentStream> downKeys = (Map<String, ContentStream>) constructor.
getSessionAttribute(CONTENT_DOWNLOAD_KEY);
53 if(downKeys ==
null) {
54 downKeys =
new HashMap<>();
56 String key = Cipher.digest(IdGenerator.generateHex() +
"", 40);
57 downKeys.put(key, onDownload);
67 public boolean itsMe(String
id) {
68 return getIdentifier().equals(
id);
72 public boolean myTurn(HttpServletRequest request) {
77 public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
78 Map<String, Predicate<ContentStream>> downKeys = (Map<String, Predicate<ContentStream>>) request.getSession().getAttribute(CONTENT_DOWNLOAD_KEY);
79 if(downKeys !=
null) {
81 if(onDownload !=
null) {
84 String contentType = MIME_TYPES_FILE_MAP.getContentType(fileName);
86 response.setHeader(
"pragma",
"no-cache");
87 response.setHeader(
"Cache-control",
"no-cache, no-store, must-revalidate");
88 response.setHeader(
"Expires",
"01 Apr 1995 01:10:10 GMT");
90 if(contentType!=
null) {
91 response.setContentType(contentType);
94 if(fileName !=
null) {
95 fileName = fileName.substring(fileName.lastIndexOf(
'\\')+1);
96 fileName = fileName.substring(fileName.lastIndexOf(
'/')+1);
98 StringBuilder contentDisposition =
new StringBuilder();
100 contentDisposition.append(
"filename=\"");
101 contentDisposition.append(fileName);
102 contentDisposition.append(
"\"");
104 response.setHeader(
"Content-Disposition", contentDisposition.toString());
105 response.setHeader(
"Content-Lenght", onDownload.
getFileSize() +
"");
108 IOUtils.copy(onDownload.
getInputStream(), response.getOutputStream());
114 }
catch (IOException ex) {
static String getRelativePath(String path)
static String getRootWebPath()
static String logMsg(String msg)
static boolean isYourTurn(HttpServletRequest request, String path)
static final String DIRECT_CONTENT_PATH
static String getIdentifier()
boolean myTurn(HttpServletRequest request)
static String createURL(IConstructor constructor, ContentStream onDownload)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
InputStream getInputStream()
static ILogWrapper info()
void setSessionAttribute(String key, Object value)
Object getSessionAttribute(String key)
ILogWrapper comment(String comment)
ILogWrapper entityPath(String path)
ILogWrapper entityName(String name)