NGINXUploadProcessor.java
5.62 KB
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
package com.brainfood.servlet;
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.ofbiz.base.util.HttpClient;
public class NGINXUploadProcessor extends HttpServlet {
class ProgressRecord {
boolean complete = false;
long bytesRead;
long contentLength;
int items;
public ProgressRecord(long bytesRead, long contentLength, int items) {
this.bytesRead = bytesRead;
this.contentLength = contentLength;
this.items = items;
}
}
private static final long serialVersionUID = 1L;
private ServletConfig config;
private static final ConcurrentHashMap<String, ProgressRecord> progress = new ConcurrentHashMap<String, ProgressRecord>();
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return this.config;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
@Override
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (request.getRequestURI().contains("progress")) {
PrintWriter out = new PrintWriter(response.getWriter());
String xProgressId = request.getHeader("X-Progress-ID");
System.err.println("QUERY PROGRESS: [" + xProgressId + "] - " + progress.get(xProgressId));
if (xProgressId != null && progress.get(xProgressId) != null) {
ProgressRecord record = progress.get(xProgressId);
out.println("({ \"state\" : \""+ (record.complete ? "done" : "uploading") +"\", \"received\" : " +
record.bytesRead + ", \"size\" : " + record.contentLength + " });");
} else {
out.println("({ \"error\" : \"Invalid X-Progress-ID\" });");
}
out.close();
} else {
String xProgressId = request.getParameter("X-Progress-ID");
System.err.println("STORE: [" + xProgressId + "]");
progress.put(xProgressId, new ProgressRecord(-1, 0, 1));
if (ServletFileUpload.isMultipartContent(request)) {
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Configure a repository (to ensure a secure temp location is used)
ServletContext servletContext = this.getServletConfig()
.getServletContext();
File repository = (File) servletContext
.getAttribute("javax.servlet.context.tempdir");
factory.setRepository(repository);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
PrintWriter out = new PrintWriter(response.getWriter());
try {
Map<String, FileItem> itemMap = new HashMap<String, FileItem>();
class UploadProgressListener implements ProgressListener {
String xProgressId;
Map<String, ProgressRecord> progress;
public UploadProgressListener(String xProgressId, Map<String, ProgressRecord> progress) {
this.xProgressId = xProgressId;
this.progress = progress;
}
@Override
public void update(long sent, long length, int items) {
System.err.println("PROGRESS: " + sent + " " + length + " " + items);
progress.put(xProgressId, new ProgressRecord(sent, length, items));
}
};
ProgressListener listener = (ProgressListener) new UploadProgressListener(xProgressId, this.progress);
upload.setProgressListener(listener);
List<FileItem> items = (List<FileItem>)upload.parseRequest(request);
for (FileItem item : items) {
itemMap.put(item.getFieldName(), item);
}
HashMap parameters = new HashMap();
parameters.put("file.path", ((DiskFileItem)itemMap.get("file")).getStoreLocation().getPath());
parameters.put("X-Progress-ID", xProgressId);
String token = itemMap.get("token").getString();
parameters.put("token", token);
HttpClient http = new HttpClient("http://localhost:8080/bfp/control/upload", parameters);
String pResponse = http.post();
System.err.println("FINISHED:" + pResponse);
out.print(pResponse);
progress.get(xProgressId).complete = true;
} catch (Exception ex) {
ex.printStackTrace();
}
out.close();
} else {
PrintWriter out = new PrintWriter(response.getWriter());
out.println("Must be a multipart upload");
out.close();
}
}
}
}