4985f30a by Ean Schuessler

Added classes to track upload progress.

1 parent cda98983
...@@ -3,17 +3,37 @@ package com.brainfood.servlet; ...@@ -3,17 +3,37 @@ package com.brainfood.servlet;
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.io.File; 4 import java.io.File;
5 import java.io.PrintWriter; 5 import java.io.PrintWriter;
6 import java.util.HashMap;
6 import java.util.List; 7 import java.util.List;
8 import java.util.Map;
7 9
8 import javax.servlet.*; 10 import javax.servlet.*;
9 import javax.servlet.http.*; 11 import javax.servlet.http.*;
10 12
11 import org.apache.commons.fileupload.*; 13 import org.apache.commons.fileupload.*;
12 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 14 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
15 import org.apache.commons.fileupload.disk.DiskFileItem;
13 import org.apache.commons.fileupload.servlet.ServletFileUpload; 16 import org.apache.commons.fileupload.servlet.ServletFileUpload;
17 import org.ofbiz.base.util.HttpClient;
18
14 19
15 public class NGINXUploadProcessor extends HttpServlet { 20 public class NGINXUploadProcessor extends HttpServlet {
21 class ProgressRecord {
22 HashMap<String, ProgressRecord> progress;
23 long bytesRead;
24 long contentLength;
25 int items;
26
27 public ProgressRecord(long bytesRead, long contentLength, int items) {
28 this.bytesRead = bytesRead;
29 this.contentLength = contentLength;
30 this.items = items;
31 }
32 }
33
16 private static final long serialVersionUID = 1L; 34 private static final long serialVersionUID = 1L;
35 private ServletConfig config;
36 private final HashMap<String, ProgressRecord> progress = new HashMap<String, ProgressRecord>();
17 37
18 @Override 38 @Override
19 public void destroy() { 39 public void destroy() {
...@@ -23,7 +43,7 @@ public class NGINXUploadProcessor extends HttpServlet { ...@@ -23,7 +43,7 @@ public class NGINXUploadProcessor extends HttpServlet {
23 @Override 43 @Override
24 public ServletConfig getServletConfig() { 44 public ServletConfig getServletConfig() {
25 // TODO Auto-generated method stub 45 // TODO Auto-generated method stub
26 return null; 46 return this.config;
27 } 47 }
28 48
29 @Override 49 @Override
...@@ -34,12 +54,17 @@ public class NGINXUploadProcessor extends HttpServlet { ...@@ -34,12 +54,17 @@ public class NGINXUploadProcessor extends HttpServlet {
34 54
35 @Override 55 @Override
36 public void init(ServletConfig config) throws ServletException { 56 public void init(ServletConfig config) throws ServletException {
37 // TODO Auto-generated method stub 57 this.config = config;
38 } 58 }
39 59
40 @Override 60 @Override
41 public void service(HttpServletRequest request, HttpServletResponse response) 61 public void service(HttpServletRequest request, HttpServletResponse response)
42 throws ServletException, IOException { 62 throws ServletException, IOException {
63 if (request.getRequestURI().contains("progress")) {
64 PrintWriter out = new PrintWriter(response.getWriter());
65 out.println("({ \"state\" : \"uploading\", \"received\" : 21216, \"size\" : 379686 });");
66 out.close();
67 } else {
43 if (ServletFileUpload.isMultipartContent(request)) { 68 if (ServletFileUpload.isMultipartContent(request)) {
44 // Create a factory for disk-based file items 69 // Create a factory for disk-based file items
45 DiskFileItemFactory factory = new DiskFileItemFactory(); 70 DiskFileItemFactory factory = new DiskFileItemFactory();
...@@ -57,8 +82,41 @@ public class NGINXUploadProcessor extends HttpServlet { ...@@ -57,8 +82,41 @@ public class NGINXUploadProcessor extends HttpServlet {
57 // Parse the request 82 // Parse the request
58 PrintWriter out = new PrintWriter(response.getWriter()); 83 PrintWriter out = new PrintWriter(response.getWriter());
59 try { 84 try {
60 List<?> items = upload.parseRequest(request); 85 Map<String, FileItem> itemMap = new HashMap<String, FileItem>();
61 out.println(items); 86 String xProgressId = request.getParameter("X-Progress-ID");
87
88 class UploadProgressListener implements ProgressListener {
89 String xProgressId;
90 Map<String, ProgressRecord> progress;
91
92 public UploadProgressListener(String xProgressId, Map<String, ProgressRecord> progress) {
93 this.xProgressId = xProgressId;
94 this.progress = progress;
95 }
96 @Override
97 public void update(long sent, long length, int items) {
98 System.err.println("PROGRESS: " + sent + " " + length + " " + items);
99 progress.put(xProgressId, new ProgressRecord(sent, length, items));
100 }
101 };
102
103 ProgressListener listener = (ProgressListener) new UploadProgressListener(xProgressId, this.progress);
104
105 upload.setProgressListener(listener);
106 List<FileItem> items = (List<FileItem>)upload.parseRequest(request);
107 for (FileItem item : items) {
108 itemMap.put(item.getFieldName(), item);
109 }
110
111 HashMap parameters = new HashMap();
112 parameters.put("path", ((DiskFileItem)itemMap.get("file")).getStoreLocation().getPath());
113 parameters.put("X-Progress-ID", xProgressId);
114 String token = itemMap.get("token").getString();
115 parameters.put("token", token);
116
117 HttpClient http = new HttpClient("http://localhost:8080/bfp/control/upload", parameters);
118 String pResponse = http.post();
119 out.print(pResponse);
62 } catch (Exception ex) { 120 } catch (Exception ex) {
63 ex.printStackTrace(); 121 ex.printStackTrace();
64 } 122 }
...@@ -69,4 +127,5 @@ public class NGINXUploadProcessor extends HttpServlet { ...@@ -69,4 +127,5 @@ public class NGINXUploadProcessor extends HttpServlet {
69 out.close(); 127 out.close();
70 } 128 }
71 } 129 }
130 }
72 } 131 }
......