19 package org.turro.attach.www;
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;
62 public static String
newAttachment(String entityPath, String link, String
template) {
64 if(constructor.
isInRole(
"attach:new")) {
67 marker.
put(
"entityPath", entityPath);
68 marker.
put(
"redirect", link);
69 return marker.
parse(
"attachment", Strings.isBlank(
template) ?
"newAttachment" :
template);
76 return createURL(constructor,
"action=delete;id=" + attachment.
getId() +
";redirect=" + HttpUtil.encodeURL(link));
91 public boolean itsMe(String
id) {
92 return getIdentifier().equals(
id);
96 public boolean myTurn(HttpServletRequest request) {
101 public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
102 if(ServletFileUpload.isMultipartContent(request)) {
104 processUpload(request, response);
105 }
catch (FileUploadException | IOException ex) {
110 processAction(request, response);
111 }
catch (IOException ex) {
117 private void processUpload(HttpServletRequest request, HttpServletResponse response)
throws FileUploadException, IOException {
120 if(contact ==
null || !contact.
isValid()) {
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()) {
134 redirect = Streams.asString(stream, ElephantContext.getEncoding());
137 entityPath = Streams.asString(stream, ElephantContext.getEncoding());
140 comment = Streams.asString(stream, ElephantContext.getEncoding());
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);
167 if(!Strings.isBlank(redirect)) {
172 private String convertToFileName(String fileName) {
173 if(fileName.contains(
"/")) {
174 fileName = fileName.substring(fileName.lastIndexOf(
"/") + 1);
179 private void processAction(HttpServletRequest request, HttpServletResponse response)
throws IOException {
180 IConstructor constructor = ElephantContext.getConstructor(request, response);
181 KeyValueMap map = Actions.getRightNowAction(constructor);
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);
192 if(!Strings.isBlank(redirect)) {
193 constructor.redirect(HttpUtil.decodeURL(redirect));
static String createRightNowAction(String values)
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 String getIdentifier()
static IContact getIContact()
IConstructor getConstructor()
static Application getApplication()
static String getRootWebPath()
static String logMsg(String msg)
static boolean isYourTurn(HttpServletRequest request, String path)
static final String DIRECT_CONTENT_PATH
static String createRelativeURL(String id)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
void redirect(String path)
boolean isInRole(String role)