BrightSide Workbench Full Report + Source Code
AttachAction.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.attach.www;
20 
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Date;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.servlet.ServletContext;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import org.turro.string.Strings;
31 import org.apache.commons.fileupload.FileItemIterator;
32 import org.apache.commons.fileupload.FileItemStream;
33 import org.apache.commons.fileupload.FileUploadException;
34 import org.apache.commons.fileupload.servlet.ServletFileUpload;
35 import org.apache.commons.fileupload.util.Streams;
36 import org.turro.action.Actions;
37 import org.turro.attach.db.AttachPU;
38 import org.turro.attach.entity.AttachContent;
39 import org.turro.attach.entity.Attachment;
40 import org.turro.auth.Authentication;
41 import org.turro.collections.KeyValueMap;
42 import org.turro.elephant.context.Application;
43 import org.turro.elephant.context.ElephantContext;
44 import org.turro.elephant.context.IConstructor;
45 import org.turro.elephant.direct.DirectContent;
46 import org.turro.elephant.direct.DirectContents;
47 import org.turro.elephant.direct.IDirectContent;
48 import org.turro.http.HttpUtil;
49 import org.turro.jpa.Dao;
50 import org.turro.log.SystemLogType;
51 import org.turro.log.SystemLogger;
52 import org.turro.marker.ElephantMarker;
53 import org.turro.plugin.contacts.IContact;
54 
59 @DirectContent(identifier="attach-action")
60 public class AttachAction implements IDirectContent {
61 
62  public static String newAttachment(String entityPath, String link, String template) {
64  if(constructor.isInRole("attach:new")) {
65  ElephantMarker marker = new ElephantMarker(constructor);
66  marker.put("action", DirectContents.createRelativeURL(getIdentifier()));
67  marker.put("entityPath", entityPath);
68  marker.put("redirect", link);
69  return marker.parse("attachment", Strings.isBlank(template) ? "newAttachment" : template);
70  }
71  return "";
72  }
73 
74  public static String delAttachment(Attachment attachment, String link) {
76  return createURL(constructor, "action=delete;id=" + attachment.getId() + ";redirect=" + HttpUtil.encodeURL(link));
77  }
78 
79  public static String createURL(IConstructor constructor, String values) {
80  String exrino = Actions.createRightNowAction(values);
82  DirectContents.DIRECT_CONTENT_PATH + getIdentifier() +
83  "?" + exrino;
84  }
85 
86  public static String getIdentifier() {
87  return AttachAction.class.getAnnotation(DirectContent.class).identifier();
88  }
89 
90  @Override
91  public boolean itsMe(String id) {
92  return getIdentifier().equals(id);
93  }
94 
95  @Override
96  public boolean myTurn(HttpServletRequest request) {
97  return DirectContents.isYourTurn(request, getIdentifier());
98  }
99 
100  @Override
101  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
102  if(ServletFileUpload.isMultipartContent(request)) {
103  try {
104  processUpload(request, response);
105  } catch (FileUploadException | IOException ex) {
106  Logger.getLogger(AttachAction.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
107  }
108  } else {
109  try {
110  processAction(request, response);
111  } catch (IOException ex) {
112  Logger.getLogger(AttachAction.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
113  }
114  }
115  }
116 
117  private void processUpload(HttpServletRequest request, HttpServletResponse response) throws FileUploadException, IOException {
119  IContact contact = Authentication.getIContact();
120  if(contact == null || !contact.isValid()) {
121  return;
122  }
123  String redirect = null, entityPath = null, comment = null;
124  ServletFileUpload upload = new ServletFileUpload();
125  upload.setHeaderEncoding(ElephantContext.getEncoding());
126  FileItemIterator iter = upload.getItemIterator(request);
127  while(iter.hasNext()) {
128  FileItemStream item = iter.next();
129  String name = item.getFieldName();
130  InputStream stream = item.openStream();
131  if(item.isFormField()) {
132  switch(name) {
133  case "redirect":
134  redirect = Streams.asString(stream, ElephantContext.getEncoding());
135  break;
136  case "entityPath":
137  entityPath = Streams.asString(stream, ElephantContext.getEncoding());
138  break;
139  case "comment":
140  comment = Streams.asString(stream, ElephantContext.getEncoding());
141  break;
142  }
143  } else if(!Strings.isBlank(entityPath)) {
144  Attachment attachment = new Attachment();
145  attachment.setModification(new Date());
146  attachment.setPath(entityPath);
147  attachment.setOwner(contact.getId());
148  attachment.setOnlyOwner(false);
149  attachment.setComment(comment);
150  attachment.setShowKey(null);
151  attachment.setPublishable(true);
152  AttachContent ac = new AttachContent();
153  ByteArrayOutputStream baos = new ByteArrayOutputStream();
154  Streams.copy(item.openStream(), baos, true);
155  ac.setFileContent(baos.toByteArray());
156  attachment.setAttachContent(ac);
157  attachment.setFileName(convertToFileName(item.getName()));
158  attachment.setFileContentType(item.getContentType());
159  attachment.setFileSize(ac.getFileContent().length);
160  attachment.setValidated(Application.getApplication().isInRole("attach:self-validate"));
161  if(attachment.getFileSize() > 0) {
162  attachment = new AttachPU().saveObject(attachment);
163  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, attachment, "uploaded", null);
164  }
165  }
166  }
167  if(!Strings.isBlank(redirect)) {
168  constructor.redirect(redirect);
169  }
170  }
171 
172  private String convertToFileName(String fileName) {
173  if(fileName.contains("/")) {
174  fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
175  }
176  return fileName;
177  }
178 
179  private void processAction(HttpServletRequest request, HttpServletResponse response) throws IOException {
180  IConstructor constructor = ElephantContext.getConstructor(request, response);
181  KeyValueMap map = Actions.getRightNowAction(constructor);
182  if(map != null) {
183  String action = map.get("action");
184  String redirect = map.get("redirect");
185  if("delete".equals(action)) {
186  Dao dao = new AttachPU();
187  Attachment attachment = dao.find(Attachment.class, map.get(Long.class, "id"));
188  if(attachment != null) {
189  dao.deleteObject(attachment);
190  }
191  }
192  if(!Strings.isBlank(redirect)) {
193  constructor.redirect(HttpUtil.decodeURL(redirect));
194  }
195  }
196  }
197 
198 }
static String createRightNowAction(String values)
Definition: Actions.java:312
static String createURL(IConstructor constructor, String values)
static String delAttachment(Attachment attachment, String link)
static String newAttachment(String entityPath, String link, String template)
boolean myTurn(HttpServletRequest request)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
static boolean isYourTurn(HttpServletRequest request, String path)
static String createRelativeURL(String id)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)