-
Notifications
You must be signed in to change notification settings - Fork 15
/
FileServlet.java
251 lines (206 loc) · 7.42 KB
/
FileServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright 2010 Vodafone Group Services Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onesocialweb.openfire.web;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.jivesoftware.admin.AuthCheckFilter;
import org.jivesoftware.util.JiveGlobals;
import org.onesocialweb.openfire.exception.AuthenticationException;
import org.onesocialweb.openfire.exception.InvalidParameterValueException;
import org.onesocialweb.openfire.exception.MissingParameterException;
import org.onesocialweb.openfire.model.FileEntry;
import org.xmpp.packet.JID;
@SuppressWarnings("serial")
public class FileServlet extends HttpServlet {
private static final long NOTIFICATION_THRESHOLD = 256000;
private static int BUFFSIZE = 64000;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final PrintWriter out = response.getWriter();
try {
processGet(request, response);
} catch (MissingParameterException e) {
out.println("Missing paramter: " + e.getParameter());
} catch (FileNotFoundException e) {
out.println("File not found: " + e.getMessage());
}
catch (Exception e) {
out.println("Exception occured: " + e.getMessage());
e.printStackTrace(out);
}
out.flush();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
final PrintWriter out = response.getWriter();
try {
processPost(request, response);
} catch (Exception e) {
out.println("Exception occured: " + e.getMessage());
e.printStackTrace(out);
}
out.flush();
}
private void processPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
final PrintWriter out = response.getWriter();
// 1- Bind this request to a XMPP JID
final JID user = getAuthenticatedUser(request);
if (user == null) {
throw new AuthenticationException();
}
// 2- Process the file
final UploadManager uploadManager = UploadManager.getInstance();
// Files larger than a threshold should be stored on disk in temporary
// storage place
final DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(256000);
factory.setRepository(getTempFolder());
// Prepare the upload parser and set a max size to enforce
final ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(512000000);
// Get the request ID
final String requestId = request.getParameter("requestId");
// Create a progress listener
ProgressListener progressListener = new ProgressListener() {
private long lastNotification = 0;
public void update(long pBytesRead, long pContentLength, int pItems) {
if (lastNotification == 0 || (pBytesRead - lastNotification) > NOTIFICATION_THRESHOLD) {
lastNotification = pBytesRead;
UploadManager.getInstance().updateProgress(user, pBytesRead, pContentLength, requestId);
}
}
};
upload.setProgressListener(progressListener);
// Process the upload
List items = upload.parseRequest(request);
for (Object objItem : items) {
FileItem item = (FileItem) objItem;
if (!item.isFormField()) {
String fileID = UUID.randomUUID().toString();
File target = new File(getUploadFolder(), fileID);
item.write(target);
UploadManager.getInstance().commitFile(user, target, item.getName(), requestId);
break; // Store only one file
}
}
}
private void processGet(HttpServletRequest request, HttpServletResponse response) throws MissingParameterException, IOException, InvalidParameterValueException {
// Validate the request token
// TODO
// Process the parameters
String fileId = request.getParameter("fileId");
if (fileId == null || fileId.isEmpty()) {
throw new MissingParameterException("fileId");
}
// Get the file entry
FileEntry fileEntry = UploadManager.getInstance().getFile(fileId);
if (fileEntry == null) {
throw new FileNotFoundException(fileId);
}
// Open the file
File file = new File(getUploadFolder(), fileEntry.getId());
if (!file.exists()) {
throw new FileNotFoundException(fileEntry.getName());
}
// Process the file
String pSize = request.getParameter("size");
if (pSize != null) {
}
DataInputStream is = new DataInputStream(new FileInputStream(file));
// Send the headers
response.setContentType(fileEntry.getType());
response.setContentLength((int) fileEntry.getSize());
//response.setHeader("Content-Disposition", "attachment; filename=\"" + fileEntry.getName() + "\"");
// Stream the file
final byte[] bbuf = new byte[BUFFSIZE];
final OutputStream os = response.getOutputStream();
int length = 0;
while ((is != null) && ((length = is.read(bbuf)) != -1)) {
os.write(bbuf, 0, length);
}
is.close();
os.flush();
os.close();
}
private JID getAuthenticatedUser(HttpServletRequest request) throws MissingParameterException {
// Fetch the parameters
String jid = request.getParameter("jid");
String signature = request.getParameter("signature");
// Validate
if (jid == null) {
throw new MissingParameterException("jid");
}
if (signature == null) {
throw new MissingParameterException("signature");
}
// Validate the session
try {
if (SessionValidator.getInstance().validateSession(jid, signature)) {
return new JID(jid);
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
// Exclude this servlet from requering the user to login
AuthCheckFilter.addExclude("osw-openfire-plugin");
AuthCheckFilter.addExclude("osw-openfire-plugin/file/");
AuthCheckFilter.addExclude("osw-openfire-plugin/form.html");
}
private File getTempFolder() {
final String tempPath = JiveGlobals.getProperty("onesocialweb.path.temp");
if (tempPath != null) {
File tempFolder = new File(tempPath);
if (tempFolder.exists() && tempFolder.canWrite()) {
return tempFolder;
}
}
return null;
}
private File getUploadFolder() {
final String uploadPath = JiveGlobals.getProperty("onesocialweb.path.upload");
if (uploadPath != null) {
File uploadFolder = new File(uploadPath);
if (uploadFolder.exists() && uploadFolder.canWrite()) {
return uploadFolder;
}
}
return null;
}
}