BrightSide Workbench Full Report + Source Code
ContentDownload.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.download;
20 
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Map;
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;
40 
45 @DirectContent(identifier="content-download")
46 public class ContentDownload implements IDirectContent {
47 
48  private static final String CONTENT_DOWNLOAD_KEY = "contentDownKeys";
49  public static final MimetypesFileTypeMap MIME_TYPES_FILE_MAP = new MimetypesFileTypeMap();
50 
51  public static String createURL(IConstructor constructor, ContentStream onDownload) {
52  Map<String, ContentStream> downKeys = (Map<String, ContentStream>) constructor.getSessionAttribute(CONTENT_DOWNLOAD_KEY);
53  if(downKeys == null) {
54  downKeys = new HashMap<>();
55  }
56  String key = Cipher.digest(IdGenerator.generateHex() + "", 40);
57  downKeys.put(key, onDownload);
58  constructor.setSessionAttribute(CONTENT_DOWNLOAD_KEY, downKeys);
59  return ElephantContext.getRootWebPath() + DirectContents.DIRECT_CONTENT_PATH + getIdentifier() + "?id=" + key;
60  }
61 
62  public static String getIdentifier() {
63  return ContentDownload.class.getAnnotation(DirectContent.class).identifier();
64  }
65 
66  @Override
67  public boolean itsMe(String id) {
68  return getIdentifier().equals(id);
69  }
70 
71  @Override
72  public boolean myTurn(HttpServletRequest request) {
73  return DirectContents.isYourTurn(request, getIdentifier());
74  }
75 
76  @Override
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) {
80  ContentStream onDownload = (ContentStream) downKeys.get(request.getParameter("id"));
81  if(onDownload != null) {
82  try {
83  String fileName = onDownload.getFileName();
84  String contentType = MIME_TYPES_FILE_MAP.getContentType(fileName);
85 
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");
89 
90  if(contentType!=null) {
91  response.setContentType(contentType);
92  }
93 
94  if(fileName != null) {
95  fileName = fileName.substring(fileName.lastIndexOf('\\')+1);
96  fileName = fileName.substring(fileName.lastIndexOf('/')+1);
97 
98  StringBuilder contentDisposition = new StringBuilder();
99 
100  contentDisposition.append("filename=\"");
101  contentDisposition.append(fileName);
102  contentDisposition.append("\"");
103 
104  response.setHeader("Content-Disposition", contentDisposition.toString());
105  response.setHeader("Content-Lenght", onDownload.getFileSize() + "");
106  }
107 
108  IOUtils.copy(onDownload.getInputStream(), response.getOutputStream());
109 
112  .entityName(fileName).comment("downloaded")
113  .log();
114  } catch (IOException ex) {
115  Logger.getLogger(ContentDownload.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
116  }
117  }
118  }
119  }
120 
121 }
static boolean isYourTurn(HttpServletRequest request, String path)
boolean myTurn(HttpServletRequest request)
static String createURL(IConstructor constructor, ContentStream onDownload)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
static ILogWrapper info()
void setSessionAttribute(String key, Object value)
ILogWrapper comment(String comment)
ILogWrapper entityPath(String path)
ILogWrapper entityName(String name)