BrightSide Workbench Full Report + Source Code
AttachDownload.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.attach.www;
20 
21 import com.lowagie.text.DocumentException;
22 import java.io.IOException;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URLEncoder;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import javax.servlet.ServletContext;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import org.amic.util.string.Cipher;
33 import org.turro.attach.db.AttachPU;
34 import org.turro.attach.entity.Attachment;
35 import org.turro.auth.Authentication;
36 import org.turro.elephant.context.ElephantContext;
37 import org.turro.elephant.context.IConstructor;
38 import org.turro.elephant.direct.DirectContent;
39 import org.turro.elephant.direct.DirectContents;
40 import org.turro.elephant.direct.IDirectContent;
41 import org.turro.log.SystemLogType;
42 import org.turro.log.SystemLogger;
43 
48 @DirectContent(identifier="attachment-show")
49 public class AttachDownload implements IDirectContent {
50 
51  public static String createURL(IConstructor constructor, Attachment attachment) {
52  Map<String, String> downKeys = (Map<String, String>) constructor.getSessionAttribute("downKeys");
53  if(downKeys == null) {
54  downKeys = new HashMap<>();
55  }
56  String key = Cipher.digest(attachment.getId() + "", 40);
57  downKeys.put(key, attachment.getId() + "");
58  constructor.setSessionAttribute("downKeys", downKeys);
59  return ElephantContext.getRootWebPath() + DirectContents.DIRECT_CONTENT_PATH + getIdentifier() + "?id=" + key;
60  }
61 
62  public static String createROURL(IConstructor constructor, Attachment attachment) {
63  if("pdf".equalsIgnoreCase(attachment.getFileExtension())) {
64  try {
65  Map<String, String> downKeys = (Map<String, String>) constructor.getSessionAttribute("downKeys");
66  if(downKeys == null) {
67  downKeys = new HashMap<>();
68  }
69  String key = Cipher.digest(attachment.getId() + "", 40);
70  downKeys.put(key, attachment.getId() + "");
71  constructor.setSessionAttribute("downKeys", downKeys);
73  "/_internal/js/pdfjs/web/roviewer.html?file=" +
74  ElephantContext.getRootWebPath() + DirectContents.DIRECT_CONTENT_PATH + getIdentifier() + "?id" + URLEncoder.encode("=", "UTF-8") + key;
75  } catch (UnsupportedEncodingException ex) {
76  Logger.getLogger(AttachDownload.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
77  return null;
78  }
79  } else {
80  return createURL(constructor, attachment);
81  }
82  }
83 
84  public static String getIdentifier() {
85  return AttachDownload.class.getAnnotation(DirectContent.class).identifier();
86  }
87 
88  @Override
89  public boolean itsMe(String id) {
90  return getIdentifier().equals(id);
91  }
92 
93  @Override
94  public boolean myTurn(HttpServletRequest request) {
95  return DirectContents.isYourTurn(request, getIdentifier());
96  }
97 
98  @Override
99  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
100  Map downKeys = (Map) request.getSession().getAttribute("downKeys");
101  if(downKeys != null) {
102  String value = (String) downKeys.get(request.getParameter("id"));
103  if(value != null) {
104  Attachment attachment = new AttachPU().find(Attachment.class, Long.parseLong(value));
105  if(attachment != null) {
106  try {
107  String contentType = attachment.getFileContentType();
108  String fileName = attachment.getFileName();
109 
110  response.setHeader("pragma", "no-cache");
111  response.setHeader("Cache-control", "no-cache, no-store, must-revalidate");
112  response.setHeader("Expires", "01 Apr 1995 01:10:10 GMT");
113 
114  if(contentType!=null) {
115  response.setContentType(contentType);
116  }
117 
118  if(fileName != null) {
119  fileName = fileName.substring(fileName.lastIndexOf('\\')+1);
120  fileName = fileName.substring(fileName.lastIndexOf('/')+1);
121 
122  StringBuilder contentDisposition = new StringBuilder();
123 
124  contentDisposition.append("filename=\"");
125  contentDisposition.append(fileName);
126  contentDisposition.append("\"");
127 
128  response.setHeader("Content-Disposition", contentDisposition.toString());
129  response.setHeader("Content-Lenght", attachment.getFileSize() + "");
130  }
131 
132  byte[] bytes = attachment.getContentModified(Authentication.getIContact());//LoadContent.getContent(attachment);
133  if (bytes != null) {
134  response.getOutputStream().write(bytes);
135  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/attachment/" + attachment.getId(), "downloaded", null);
136  }
137  } catch (IOException | DocumentException ex) {
138  Logger.getLogger(AttachDownload.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
139  }
140  }
141  }
142  }
143  }
144 
145 }
byte[] getContentModified(IContact contact)
static String createROURL(IConstructor constructor, Attachment attachment)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
boolean myTurn(HttpServletRequest request)
static String createURL(IConstructor constructor, Attachment attachment)
static boolean isYourTurn(HttpServletRequest request, String path)
static ISystemLogger getInstance()
void setSessionAttribute(String key, Object value)
void doLog(SystemLogType type, Object entity, String comment, Serializable data)