commit | author | age
|
723bf7
|
1 |
/* |
U |
2 |
Dateiverwaltung - File management in your browser |
|
3 |
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de |
|
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 |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (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 |
|
41e9f0
|
19 |
package de.uhilger.filecms.web; |
7aa566
|
20 |
|
U |
21 |
import static de.uhilger.filecms.api.FileMgr.HOME_DIR_NAME; |
|
22 |
import static de.uhilger.filecms.api.FileMgr.HOME_DIR_PATH; |
|
23 |
import static de.uhilger.filecms.api.FileMgr.PUB_DIR_NAME; |
|
24 |
import static de.uhilger.filecms.api.FileMgr.PUB_DIR_PATH; |
6e70be
|
25 |
import de.uhilger.filecms.data.FileRef; |
7aa566
|
26 |
import java.io.File; |
U |
27 |
import java.io.IOException; |
|
28 |
import java.io.InputStream; |
|
29 |
import java.security.Principal; |
|
30 |
import java.util.Iterator; |
|
31 |
import java.util.List; |
|
32 |
import java.util.logging.Level; |
|
33 |
import java.util.logging.Logger; |
|
34 |
import javax.servlet.ServletContext; |
|
35 |
import javax.servlet.ServletException; |
|
36 |
import javax.servlet.http.HttpServlet; |
|
37 |
import javax.servlet.http.HttpServletRequest; |
|
38 |
import javax.servlet.http.HttpServletResponse; |
|
39 |
import org.apache.commons.fileupload.FileItem; |
|
40 |
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
|
41 |
import org.apache.commons.fileupload.servlet.FileCleanerCleanup; |
|
42 |
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
|
43 |
import org.apache.commons.io.FileCleaningTracker; |
|
44 |
|
|
45 |
/** |
|
46 |
* Objekte der Klasee UploadServlet nehmen eine Datei per HTTP Multipart Request |
|
47 |
* entgegen und speichern sie im webapps-Verzeichnis des Servlet-Containers. |
|
48 |
* |
|
49 |
* Annahme ist, dass das von javax.servlet.context.tempdir bezeichnete |
|
50 |
* Verzeichnis auf CATALINA_BASE/work/Catalina/localhost/[context] zeigt. Von |
|
51 |
* dort wird dann das webapps-Verzeichnis als CATALINA_BASE/webapps ermittelt. |
|
52 |
*/ |
|
53 |
public class UploadServlet extends HttpServlet { |
|
54 |
|
|
55 |
public static final String DESC = "file-cms uploader"; |
|
56 |
|
|
57 |
private static final Logger logger = Logger.getLogger(UploadServlet.class.getName()); |
42fece
|
58 |
|
U |
59 |
private String targetDirStr; |
7aa566
|
60 |
|
U |
61 |
/** |
|
62 |
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
|
63 |
* methods. |
|
64 |
* |
|
65 |
* @param request servlet request |
|
66 |
* @param response servlet response |
|
67 |
* @throws ServletException if a servlet-specific error occurs |
|
68 |
* @throws IOException if an I/O error occurs |
|
69 |
*/ |
|
70 |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
|
71 |
throws ServletException, IOException { |
42fece
|
72 |
|
7aa566
|
73 |
//logger.log(Level.INFO, "start"); |
U |
74 |
boolean isMultipart = ServletFileUpload.isMultipartContent(request); |
|
75 |
|
|
76 |
if (isMultipart) { |
|
77 |
//logger.log(Level.INFO, "isMultipart"); |
|
78 |
try { |
|
79 |
ServletContext servletContext = this.getServletConfig().getServletContext(); |
|
80 |
|
|
81 |
// Configure a repository (to ensure a secure temp location is used) |
|
82 |
File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir"); |
|
83 |
// CATALINA_BASE/work/Catalina/localhost/wbx |
|
84 |
|
|
85 |
// Create a factory for disk-based file items |
|
86 |
DiskFileItemFactory factory = newDiskFileItemFactory(servletContext, repository); |
|
87 |
|
|
88 |
// Create a new file upload handler |
|
89 |
ServletFileUpload upload = new ServletFileUpload(factory); |
|
90 |
|
|
91 |
// Parse the request |
|
92 |
List<FileItem> items = upload.parseRequest(request); |
|
93 |
|
|
94 |
// Process the uploaded items |
|
95 |
Iterator<FileItem> iter = items.iterator(); |
42fece
|
96 |
FileItem fileItem = null; |
7aa566
|
97 |
while (iter.hasNext()) { |
U |
98 |
FileItem item = iter.next(); |
|
99 |
|
|
100 |
if (item.isFormField()) { |
|
101 |
processFormField(item); |
|
102 |
} else { |
|
103 |
//File base = repository.getParentFile().getParentFile().getParentFile().getParentFile(); |
|
104 |
//File webapps = new File(base, "webapps"); |
|
105 |
//logger.info(webapps.getAbsolutePath()); |
42fece
|
106 |
fileItem = item; |
7aa566
|
107 |
} |
42fece
|
108 |
} |
U |
109 |
File targetDir = getTargetDir(request, targetDirStr); |
|
110 |
if(targetDir != null && fileItem != null) { |
|
111 |
logger.log(Level.FINER, "targetDir before upload: " + targetDir.getAbsolutePath()); |
|
112 |
processUploadedFile(targetDir, fileItem, true); |
7aa566
|
113 |
} |
U |
114 |
} catch (Exception ex) { |
|
115 |
logger.log(Level.SEVERE, null, ex); |
|
116 |
} |
|
117 |
} else { |
|
118 |
// kein Upload |
|
119 |
} |
|
120 |
|
|
121 |
/* |
|
122 |
response.setContentType("text/html;charset=UTF-8"); |
|
123 |
try (PrintWriter out = response.getWriter()) { |
|
124 |
out.println("<!DOCTYPE html>"); |
|
125 |
out.println("<html>"); |
|
126 |
out.println("<head>"); |
|
127 |
out.println("<title>Servlet UploadServlet</title>"); |
|
128 |
out.println("</head>"); |
|
129 |
out.println("<body>"); |
|
130 |
out.println("<h1>Servlet UploadServlet at " + request.getContextPath() + "</h1>"); |
|
131 |
out.println("</body>"); |
|
132 |
out.println("</html>"); |
|
133 |
} |
|
134 |
*/ |
|
135 |
} |
|
136 |
|
|
137 |
private File getTargetDir(HttpServletRequest request, String relPath) { |
42fece
|
138 |
File targetDir = null; |
U |
139 |
if(relPath != null) { |
|
140 |
logger.finer(relPath); |
|
141 |
String targetPath = null; |
|
142 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
|
143 |
targetPath = PUB_DIR_PATH + getUserName(request) + relPath.substring(PUB_DIR_NAME.length()); |
|
144 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
|
145 |
targetPath = HOME_DIR_PATH + getUserName(request) + relPath.substring(HOME_DIR_NAME.length()); |
|
146 |
} else { |
|
147 |
// kann eigentlich nicht sein.. |
|
148 |
} |
|
149 |
logger.finer(targetPath); |
|
150 |
targetDir = new File(getBase().getAbsolutePath(), targetPath); |
7aa566
|
151 |
} |
U |
152 |
return targetDir; |
|
153 |
} |
|
154 |
|
|
155 |
private String getUserName(HttpServletRequest request) { |
|
156 |
String userName = null; |
|
157 |
Object p = request.getUserPrincipal(); |
|
158 |
if (p instanceof Principal) { |
|
159 |
userName = ((Principal) p).getName(); |
|
160 |
} |
|
161 |
return userName; |
|
162 |
} |
|
163 |
|
|
164 |
private FileRef getBase() { |
|
165 |
FileRef base = null; |
|
166 |
Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); |
9e2964
|
167 |
if(o instanceof String) { |
U |
168 |
String baseStr = (String) o; |
|
169 |
logger.fine(baseStr); |
|
170 |
File file = new File(baseStr); |
|
171 |
base = new FileRef(file.getAbsolutePath(), file.isDirectory()); |
7aa566
|
172 |
} |
U |
173 |
return base; |
|
174 |
} |
|
175 |
|
|
176 |
private void processUploadedFile(File targetDir, FileItem item, boolean writeToFile) throws Exception { |
|
177 |
//logger.log(Level.INFO, "start"); |
|
178 |
// Process a file upload |
|
179 |
if (!item.isFormField()) { |
|
180 |
String fieldName = item.getFieldName(); |
|
181 |
String fileName = item.getName(); |
|
182 |
String contentType = item.getContentType(); |
|
183 |
boolean isInMemory = item.isInMemory(); |
|
184 |
long sizeInBytes = item.getSize(); |
|
185 |
// Process a file upload |
|
186 |
if (writeToFile) { |
|
187 |
File uploadedFile = new File(targetDir, fileName); |
891db6
|
188 |
uploadedFile.getParentFile().mkdirs(); |
42fece
|
189 |
logger.log(Level.FINER, "writeToFile " + uploadedFile.getAbsolutePath()); |
7aa566
|
190 |
item.write(uploadedFile); |
U |
191 |
} else { |
|
192 |
InputStream uploadedStream = item.getInputStream(); |
|
193 |
//... |
|
194 |
uploadedStream.close(); |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
198 |
|
|
199 |
private void processFormField(FileItem item) { |
|
200 |
//logger.log(Level.INFO, "start"); |
|
201 |
// Process a regular form field |
|
202 |
if (item.isFormField()) { |
|
203 |
String name = item.getFieldName(); |
|
204 |
String value = item.getString(); |
42fece
|
205 |
logger.log(Level.FINER, "field name: " + name + ", value: " + value); |
7aa566
|
206 |
//... |
42fece
|
207 |
if(name.equals("pfad")) { |
U |
208 |
targetDirStr = value; |
|
209 |
} |
7aa566
|
210 |
} |
U |
211 |
} |
|
212 |
|
|
213 |
public static DiskFileItemFactory newDiskFileItemFactory(ServletContext context, |
|
214 |
File repository) { |
|
215 |
FileCleaningTracker fileCleaningTracker |
|
216 |
= FileCleanerCleanup.getFileCleaningTracker(context); |
|
217 |
DiskFileItemFactory factory |
|
218 |
= new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, |
|
219 |
repository); |
|
220 |
factory.setFileCleaningTracker(fileCleaningTracker); |
|
221 |
return factory; |
|
222 |
} |
|
223 |
|
|
224 |
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
|
225 |
/** |
|
226 |
* Handles the HTTP <code>GET</code> method. |
|
227 |
* |
|
228 |
* @param request servlet request |
|
229 |
* @param response servlet response |
|
230 |
* @throws ServletException if a servlet-specific error occurs |
|
231 |
* @throws IOException if an I/O error occurs |
|
232 |
*/ |
|
233 |
@Override |
|
234 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
|
235 |
throws ServletException, IOException { |
|
236 |
processRequest(request, response); |
|
237 |
} |
|
238 |
|
|
239 |
/** |
|
240 |
* Handles the HTTP <code>POST</code> method. |
|
241 |
* |
|
242 |
* @param request servlet request |
|
243 |
* @param response servlet response |
|
244 |
* @throws ServletException if a servlet-specific error occurs |
|
245 |
* @throws IOException if an I/O error occurs |
|
246 |
*/ |
|
247 |
@Override |
|
248 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) |
|
249 |
throws ServletException, IOException { |
|
250 |
processRequest(request, response); |
|
251 |
} |
|
252 |
|
|
253 |
/** |
|
254 |
* Returns a short description of the servlet. |
|
255 |
* |
|
256 |
* @return a String containing servlet description |
|
257 |
*/ |
|
258 |
@Override |
|
259 |
public String getServletInfo() { |
|
260 |
return DESC; |
|
261 |
}// </editor-fold> |
|
262 |
|
|
263 |
} |