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,39 +54,78 @@ public class NGINXUploadProcessor extends HttpServlet { ...@@ -34,39 +54,78 @@ 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 {
43 if (ServletFileUpload.isMultipartContent(request)) { 63 if (request.getRequestURI().contains("progress")) {
44 // Create a factory for disk-based file items 64 PrintWriter out = new PrintWriter(response.getWriter());
45 DiskFileItemFactory factory = new DiskFileItemFactory(); 65 out.println("({ \"state\" : \"uploading\", \"received\" : 21216, \"size\" : 379686 });");
66 out.close();
67 } else {
68 if (ServletFileUpload.isMultipartContent(request)) {
69 // Create a factory for disk-based file items
70 DiskFileItemFactory factory = new DiskFileItemFactory();
46 71
47 // Configure a repository (to ensure a secure temp location is used) 72 // Configure a repository (to ensure a secure temp location is used)
48 ServletContext servletContext = this.getServletConfig() 73 ServletContext servletContext = this.getServletConfig()
49 .getServletContext(); 74 .getServletContext();
50 File repository = (File) servletContext 75 File repository = (File) servletContext
51 .getAttribute("javax.servlet.context.tempdir"); 76 .getAttribute("javax.servlet.context.tempdir");
52 factory.setRepository(repository); 77 factory.setRepository(repository);
53 78
54 // Create a new file upload handler 79 // Create a new file upload handler
55 ServletFileUpload upload = new ServletFileUpload(factory); 80 ServletFileUpload upload = new ServletFileUpload(factory);
56 81
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");
62 } catch (Exception ex) { 87
63 ex.printStackTrace(); 88 class UploadProgressListener implements ProgressListener {
64 } 89 String xProgressId;
65 out.close(); 90 Map<String, ProgressRecord> progress;
66 } else { 91
67 PrintWriter out = new PrintWriter(response.getWriter()); 92 public UploadProgressListener(String xProgressId, Map<String, ProgressRecord> progress) {
68 out.println("Must be a multipart upload"); 93 this.xProgressId = xProgressId;
69 out.close(); 94 this.progress = progress;
70 } 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);
120 } catch (Exception ex) {
121 ex.printStackTrace();
122 }
123 out.close();
124 } else {
125 PrintWriter out = new PrintWriter(response.getWriter());
126 out.println("Must be a multipart upload");
127 out.close();
128 }
129 }
71 } 130 }
72 } 131 }
......