707be16d by Jerry Pommer

Merge branch 'BFP-1783' of /home/git/repositories/brainfood/java-nginx-upload-processor

2 parents 4afee607 74e33972
...@@ -6,6 +6,7 @@ import java.io.PrintWriter; ...@@ -6,6 +6,7 @@ import java.io.PrintWriter;
6 import java.util.HashMap; 6 import java.util.HashMap;
7 import java.util.List; 7 import java.util.List;
8 import java.util.Map; 8 import java.util.Map;
9 import java.util.concurrent.ConcurrentHashMap;
9 10
10 import javax.servlet.*; 11 import javax.servlet.*;
11 import javax.servlet.http.*; 12 import javax.servlet.http.*;
...@@ -16,10 +17,8 @@ import org.apache.commons.fileupload.disk.DiskFileItem; ...@@ -16,10 +17,8 @@ import org.apache.commons.fileupload.disk.DiskFileItem;
16 import org.apache.commons.fileupload.servlet.ServletFileUpload; 17 import org.apache.commons.fileupload.servlet.ServletFileUpload;
17 import org.ofbiz.base.util.HttpClient; 18 import org.ofbiz.base.util.HttpClient;
18 19
19
20 public class NGINXUploadProcessor extends HttpServlet { 20 public class NGINXUploadProcessor extends HttpServlet {
21 class ProgressRecord { 21 class ProgressRecord {
22 HashMap<String, ProgressRecord> progress;
23 long bytesRead; 22 long bytesRead;
24 long contentLength; 23 long contentLength;
25 int items; 24 int items;
...@@ -33,7 +32,7 @@ public class NGINXUploadProcessor extends HttpServlet { ...@@ -33,7 +32,7 @@ public class NGINXUploadProcessor extends HttpServlet {
33 32
34 private static final long serialVersionUID = 1L; 33 private static final long serialVersionUID = 1L;
35 private ServletConfig config; 34 private ServletConfig config;
36 private final HashMap<String, ProgressRecord> progress = new HashMap<String, ProgressRecord>(); 35 private static final ConcurrentHashMap<String, ProgressRecord> progress = new ConcurrentHashMap<String, ProgressRecord>();
37 36
38 @Override 37 @Override
39 public void destroy() { 38 public void destroy() {
...@@ -63,7 +62,7 @@ public class NGINXUploadProcessor extends HttpServlet { ...@@ -63,7 +62,7 @@ public class NGINXUploadProcessor extends HttpServlet {
63 if (request.getRequestURI().contains("progress")) { 62 if (request.getRequestURI().contains("progress")) {
64 PrintWriter out = new PrintWriter(response.getWriter()); 63 PrintWriter out = new PrintWriter(response.getWriter());
65 String xProgressId = request.getHeader("X-Progress-ID"); 64 String xProgressId = request.getHeader("X-Progress-ID");
66 System.err.println("PROGRESS: " + xProgressId); 65 System.err.println("QUERY PROGRESS: [" + xProgressId + "] - " + progress.get(xProgressId));
67 if (xProgressId != null && progress.get(xProgressId) != null) { 66 if (xProgressId != null && progress.get(xProgressId) != null) {
68 ProgressRecord record = progress.get(xProgressId); 67 ProgressRecord record = progress.get(xProgressId);
69 String status = "done"; 68 String status = "done";
...@@ -77,6 +76,10 @@ public class NGINXUploadProcessor extends HttpServlet { ...@@ -77,6 +76,10 @@ public class NGINXUploadProcessor extends HttpServlet {
77 } 76 }
78 out.close(); 77 out.close();
79 } else { 78 } else {
79 String xProgressId = request.getParameter("X-Progress-ID");
80 System.err.println("STORE: [" + xProgressId + "]");
81 progress.put(xProgressId, new ProgressRecord(-1, 0, 1));
82
80 if (ServletFileUpload.isMultipartContent(request)) { 83 if (ServletFileUpload.isMultipartContent(request)) {
81 // Create a factory for disk-based file items 84 // Create a factory for disk-based file items
82 DiskFileItemFactory factory = new DiskFileItemFactory(); 85 DiskFileItemFactory factory = new DiskFileItemFactory();
...@@ -95,8 +98,6 @@ public class NGINXUploadProcessor extends HttpServlet { ...@@ -95,8 +98,6 @@ public class NGINXUploadProcessor extends HttpServlet {
95 PrintWriter out = new PrintWriter(response.getWriter()); 98 PrintWriter out = new PrintWriter(response.getWriter());
96 try { 99 try {
97 Map<String, FileItem> itemMap = new HashMap<String, FileItem>(); 100 Map<String, FileItem> itemMap = new HashMap<String, FileItem>();
98 String xProgressId = request.getParameter("X-Progress-ID");
99 System.err.println("STORE: " + xProgressId);
100 101
101 class UploadProgressListener implements ProgressListener { 102 class UploadProgressListener implements ProgressListener {
102 String xProgressId; 103 String xProgressId;
...@@ -114,23 +115,22 @@ public class NGINXUploadProcessor extends HttpServlet { ...@@ -114,23 +115,22 @@ public class NGINXUploadProcessor extends HttpServlet {
114 }; 115 };
115 116
116 ProgressListener listener = (ProgressListener) new UploadProgressListener(xProgressId, this.progress); 117 ProgressListener listener = (ProgressListener) new UploadProgressListener(xProgressId, this.progress);
117
118 progress.put(xProgressId, new ProgressRecord(-1, 0, 1));
119
120 upload.setProgressListener(listener); 118 upload.setProgressListener(listener);
121 List<FileItem> items = (List<FileItem>)upload.parseRequest(request); 119 List<FileItem> items = (List<FileItem>)upload.parseRequest(request);
120
122 for (FileItem item : items) { 121 for (FileItem item : items) {
123 itemMap.put(item.getFieldName(), item); 122 itemMap.put(item.getFieldName(), item);
124 } 123 }
125 124
126 HashMap parameters = new HashMap(); 125 HashMap parameters = new HashMap();
127 parameters.put("path", ((DiskFileItem)itemMap.get("file")).getStoreLocation().getPath()); 126 parameters.put("file.path", ((DiskFileItem)itemMap.get("file")).getStoreLocation().getPath());
128 parameters.put("X-Progress-ID", xProgressId); 127 parameters.put("X-Progress-ID", xProgressId);
129 String token = itemMap.get("token").getString(); 128 String token = itemMap.get("token").getString();
130 parameters.put("token", token); 129 parameters.put("token", token);
131 130
132 HttpClient http = new HttpClient("http://localhost:8080/bfp/control/upload", parameters); 131 HttpClient http = new HttpClient("http://localhost:8080/bfp/control/upload", parameters);
133 String pResponse = http.post(); 132 String pResponse = http.post();
133 System.err.println("FINISHED:" + pResponse);
134 out.print(pResponse); 134 out.print(pResponse);
135 } catch (Exception ex) { 135 } catch (Exception ex) {
136 ex.printStackTrace(); 136 ex.printStackTrace();
......