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