19 package org.turro.dossier.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.attach.db.AttachPU;
37 import org.turro.attach.entity.AttachContent;
38 import org.turro.attach.entity.Attachment;
39 import org.turro.attach.www.AttachCtrl;
40 import org.turro.auth.Authentication;
41 import org.turro.contacts.Contact;
42 import org.turro.describeit.DescribeItUtil;
43 import org.turro.dossier.db.DossierPU;
44 import org.turro.dossier.dossier.DossierWrapper;
45 import org.turro.dossier.entity.Dossier;
46 import org.turro.elephant.context.Application;
47 import org.turro.elephant.context.ElephantContext;
48 import org.turro.elephant.context.IConstructor;
49 import org.turro.elephant.direct.DirectContent;
50 import org.turro.elephant.direct.DirectContents;
51 import org.turro.elephant.direct.IDirectContent;
52 import org.turro.html.HTMLEntities;
53 import org.turro.jpa.Dao;
54 import org.turro.log.SystemLogType;
55 import org.turro.log.SystemLogger;
56 import org.turro.marker.ElephantMarker;
57 import org.turro.plugin.contacts.IContact;
63 @DirectContent(identifier=
"edit-dossier")
66 public static String
editDossier(String dossierId, String link, String
template) {
67 Long
id = Long.valueOf(dossierId);
69 return editDossier(dossier, link,
template);
79 marker.
put(
"redirect", link);
81 marker.
put(
"dossier", dossier);
86 return marker.
parse(
"dossier", Strings.isBlank(
template) ?
"editDossier" :
template);
97 public boolean itsMe(String
id) {
98 return getIdentifier().equals(
id);
102 public boolean myTurn(HttpServletRequest request) {
107 public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
108 if(ServletFileUpload.isMultipartContent(request)) {
110 processInformation(request, response);
111 }
catch (FileUploadException | IOException ex) {
117 private void processInformation(HttpServletRequest request, HttpServletResponse response)
throws FileUploadException, IOException {
121 if(contact ==
null || !contact.
isValid()) {
124 Long dossierId =
null;
125 String redirect =
null, comment =
null, webDefault =
null;
126 ServletFileUpload upload =
new ServletFileUpload();
127 upload.setHeaderEncoding(ElephantContext.getEncoding());
128 FileItemIterator iter = upload.getItemIterator(request);
129 while(iter.hasNext()) {
130 FileItemStream item = iter.next();
131 String name = item.getFieldName();
132 InputStream stream = item.openStream();
133 if(item.isFormField()) {
136 dossierId = Long.valueOf(Streams.asString(stream));
137 dossier = dao.find(Dossier.class, dossierId);
140 webDefault = Streams.asString(stream, ElephantContext.getEncoding());
143 redirect = Streams.asString(stream, ElephantContext.getEncoding());
146 comment = Streams.asString(stream, ElephantContext.getEncoding());
147 comment = HTMLEntities.plainWhenPossible(comment);
150 }
else if(dossier !=
null) {
151 Attachment attachment =
new Attachment();
152 attachment.setModification(
new Date());
153 attachment.setPath(
"/dossier/" + dossier.
getId());
154 attachment.setOwner(contact.
getId());
155 attachment.setOnlyOwner(
false);
156 attachment.setComment(comment);
157 attachment.setShowKey(
null);
158 attachment.setPublishable(
true);
159 AttachContent ac =
new AttachContent();
160 ByteArrayOutputStream baos =
new ByteArrayOutputStream();
161 Streams.copy(item.openStream(), baos,
true);
162 ac.setFileContent(baos.toByteArray());
163 attachment.setAttachContent(ac);
164 attachment.setFileName(convertToFileName(item.getName()));
165 attachment.setFileContentType(item.getContentType());
166 attachment.setFileSize(ac.getFileContent().length);
167 attachment.setValidated(Application.getApplication().isInRole(
"attach:self-validate"));
168 if(attachment.getFileSize() > 0) {
169 attachment =
new AttachPU().saveObject(attachment);
170 SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, attachment,
"uploaded",
null);
174 if(dossier !=
null && !Strings.isBlank(webDefault)) {
175 DescribeItUtil.addDescription(dossier, DescribeItUtil.DEFAULT_ID, webDefault, (Contact) contact.
getContact(),
null);
177 if(!Strings.isBlank(redirect)) {
178 if(redirect.endsWith(
"=0")) {
179 redirect = redirect.substring(0, redirect.length() - 1) + dossier.
getId();
181 response.sendRedirect(redirect);
185 private String convertToFileName(String fileName) {
186 if(fileName.contains(
"/")) {
187 fileName = fileName.substring(fileName.lastIndexOf(
"/") + 1);
String parseAttachments()
void setPublicOnly(boolean publicOnly)
void setEntityPath(String entityPath)
static IContact getIContact()
static String descriptionString(String id, Object entity)
static final String DEFAULT_ID
static String getObjectPath(Object object)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
static String getIdentifier()
boolean myTurn(HttpServletRequest request)
static String editDossier(Dossier dossier, String link, String template)
static String editDossier(String dossierId, String link, String template)
IConstructor getConstructor()
static Application getApplication()
static String logMsg(String msg)
static boolean isYourTurn(HttpServletRequest request, String path)
static String createRelativeURL(String id)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)