4985f30a by Ean Schuessler

Added classes to track upload progress.

1 parent cda98983
......@@ -3,17 +3,37 @@ 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 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 {
HashMap<String, ProgressRecord> progress;
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 final HashMap<String, ProgressRecord> progress = new HashMap<String, ProgressRecord>();
@Override
public void destroy() {
......@@ -23,7 +43,7 @@ public class NGINXUploadProcessor extends HttpServlet {
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
return this.config;
}
@Override
......@@ -34,12 +54,17 @@ public class NGINXUploadProcessor extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
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());
out.println("({ \"state\" : \"uploading\", \"received\" : 21216, \"size\" : 379686 });");
out.close();
} else {
if (ServletFileUpload.isMultipartContent(request)) {
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
......@@ -57,8 +82,41 @@ public class NGINXUploadProcessor extends HttpServlet {
// Parse the request
PrintWriter out = new PrintWriter(response.getWriter());
try {
List<?> items = upload.parseRequest(request);
out.println(items);
Map<String, FileItem> itemMap = new HashMap<String, FileItem>();
String xProgressId = request.getParameter("X-Progress-ID");
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("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();
out.print(pResponse);
} catch (Exception ex) {
ex.printStackTrace();
}
......@@ -69,4 +127,5 @@ public class NGINXUploadProcessor extends HttpServlet {
out.close();
}
}
}
}
......