Make ProgressRecord fields final, fix tabs
BFP-1784
Showing
1 changed file
with
61 additions
and
59 deletions
... | @@ -18,48 +18,49 @@ import org.apache.commons.fileupload.servlet.ServletFileUpload; | ... | @@ -18,48 +18,49 @@ import org.apache.commons.fileupload.servlet.ServletFileUpload; |
18 | import org.ofbiz.base.util.HttpClient; | 18 | import org.ofbiz.base.util.HttpClient; |
19 | 19 | ||
20 | public class NGINXUploadProcessor extends HttpServlet { | 20 | public class NGINXUploadProcessor extends HttpServlet { |
21 | class ProgressRecord { | 21 | class ProgressRecord { |
22 | boolean complete = false; | 22 | final boolean complete; |
23 | long bytesRead; | 23 | final long bytesRead; |
24 | long contentLength; | 24 | final long contentLength; |
25 | int items; | 25 | final int items; |
26 | 26 | ||
27 | public ProgressRecord(long bytesRead, long contentLength, int items) { | 27 | public ProgressRecord(long bytesRead, long contentLength, int items, boolean complete) { |
28 | this.bytesRead = bytesRead; | 28 | this.bytesRead = bytesRead; |
29 | this.contentLength = contentLength; | 29 | this.contentLength = contentLength; |
30 | this.items = items; | 30 | this.items = items; |
31 | } | 31 | this.complete = complete; |
32 | } | 32 | } |
33 | } | ||
33 | 34 | ||
34 | private static final long serialVersionUID = 1L; | 35 | private static final long serialVersionUID = 1L; |
35 | private ServletConfig config; | 36 | private ServletConfig config; |
36 | private static final ConcurrentHashMap<String, ProgressRecord> progress = new ConcurrentHashMap<String, ProgressRecord>(); | 37 | private static final ConcurrentHashMap<String, ProgressRecord> progress = new ConcurrentHashMap<String, ProgressRecord>(); |
37 | 38 | ||
38 | @Override | 39 | @Override |
39 | public void destroy() { | 40 | public void destroy() { |
40 | // TODO Auto-generated method stub | 41 | // TODO Auto-generated method stub |
41 | } | 42 | } |
42 | 43 | ||
43 | @Override | 44 | @Override |
44 | public ServletConfig getServletConfig() { | 45 | public ServletConfig getServletConfig() { |
45 | // TODO Auto-generated method stub | 46 | // TODO Auto-generated method stub |
46 | return this.config; | 47 | return this.config; |
47 | } | 48 | } |
48 | 49 | ||
49 | @Override | 50 | @Override |
50 | public String getServletInfo() { | 51 | public String getServletInfo() { |
51 | // TODO Auto-generated method stub | 52 | // TODO Auto-generated method stub |
52 | return null; | 53 | return null; |
53 | } | 54 | } |
54 | 55 | ||
55 | @Override | 56 | @Override |
56 | public void init(ServletConfig config) throws ServletException { | 57 | public void init(ServletConfig config) throws ServletException { |
57 | this.config = config; | 58 | this.config = config; |
58 | } | 59 | } |
59 | 60 | ||
60 | @Override | 61 | @Override |
61 | public void service(HttpServletRequest request, HttpServletResponse response) | 62 | public void service(HttpServletRequest request, HttpServletResponse response) |
62 | throws ServletException, IOException { | 63 | throws ServletException, IOException { |
63 | if (request.getRequestURI().contains("progress")) { | 64 | if (request.getRequestURI().contains("progress")) { |
64 | PrintWriter out = new PrintWriter(response.getWriter()); | 65 | PrintWriter out = new PrintWriter(response.getWriter()); |
65 | String xProgressId = request.getHeader("X-Progress-ID"); | 66 | String xProgressId = request.getHeader("X-Progress-ID"); |
... | @@ -67,7 +68,7 @@ public class NGINXUploadProcessor extends HttpServlet { | ... | @@ -67,7 +68,7 @@ public class NGINXUploadProcessor extends HttpServlet { |
67 | if (xProgressId != null && progress.get(xProgressId) != null) { | 68 | if (xProgressId != null && progress.get(xProgressId) != null) { |
68 | ProgressRecord record = progress.get(xProgressId); | 69 | ProgressRecord record = progress.get(xProgressId); |
69 | out.println("({ \"state\" : \""+ (record.complete ? "done" : "uploading") +"\", \"received\" : " + | 70 | out.println("({ \"state\" : \""+ (record.complete ? "done" : "uploading") +"\", \"received\" : " + |
70 | record.bytesRead + ", \"size\" : " + record.contentLength + " });"); | 71 | record.bytesRead + ", \"size\" : " + record.contentLength + " });"); |
71 | } else { | 72 | } else { |
72 | out.println("({ \"error\" : \"Invalid X-Progress-ID\" });"); | 73 | out.println("({ \"error\" : \"Invalid X-Progress-ID\" });"); |
73 | } | 74 | } |
... | @@ -75,7 +76,7 @@ public class NGINXUploadProcessor extends HttpServlet { | ... | @@ -75,7 +76,7 @@ public class NGINXUploadProcessor extends HttpServlet { |
75 | } else { | 76 | } else { |
76 | String xProgressId = request.getParameter("X-Progress-ID"); | 77 | String xProgressId = request.getParameter("X-Progress-ID"); |
77 | System.err.println("STORE: [" + xProgressId + "]"); | 78 | System.err.println("STORE: [" + xProgressId + "]"); |
78 | progress.put(xProgressId, new ProgressRecord(-1, 0, 1)); | 79 | progress.put(xProgressId, new ProgressRecord(-1, 0, 1, false)); |
79 | 80 | ||
80 | if (ServletFileUpload.isMultipartContent(request)) { | 81 | if (ServletFileUpload.isMultipartContent(request)) { |
81 | // Create a factory for disk-based file items | 82 | // Create a factory for disk-based file items |
... | @@ -94,29 +95,29 @@ public class NGINXUploadProcessor extends HttpServlet { | ... | @@ -94,29 +95,29 @@ public class NGINXUploadProcessor extends HttpServlet { |
94 | // Parse the request | 95 | // Parse the request |
95 | PrintWriter out = new PrintWriter(response.getWriter()); | 96 | PrintWriter out = new PrintWriter(response.getWriter()); |
96 | try { | 97 | try { |
97 | Map<String, FileItem> itemMap = new HashMap<String, FileItem>(); | 98 | Map<String, FileItem> itemMap = new HashMap<String, FileItem>(); |
98 | 99 | ||
99 | class UploadProgressListener implements ProgressListener { | 100 | class UploadProgressListener implements ProgressListener { |
100 | String xProgressId; | 101 | String xProgressId; |
101 | Map<String, ProgressRecord> progress; | 102 | Map<String, ProgressRecord> progress; |
102 | 103 | ||
103 | public UploadProgressListener(String xProgressId, Map<String, ProgressRecord> progress) { | 104 | public UploadProgressListener(String xProgressId, Map<String, ProgressRecord> progress) { |
104 | this.xProgressId = xProgressId; | 105 | this.xProgressId = xProgressId; |
105 | this.progress = progress; | 106 | this.progress = progress; |
106 | } | 107 | } |
107 | @Override | 108 | @Override |
108 | public void update(long sent, long length, int items) { | 109 | public void update(long bytesRead, long contentLength, int items) { |
109 | System.err.println("PROGRESS: " + sent + " " + length + " " + items); | 110 | System.err.println("PROGRESS: " + bytesRead + " " + contentLength + " " + items); |
110 | progress.put(xProgressId, new ProgressRecord(sent, length, items)); | 111 | progress.put(xProgressId, new ProgressRecord(bytesRead, contentLength, items, false)); |
111 | } | 112 | } |
112 | }; | 113 | }; |
113 | 114 | ||
114 | ProgressListener listener = (ProgressListener) new UploadProgressListener(xProgressId, this.progress); | 115 | ProgressListener listener = (ProgressListener) new UploadProgressListener(xProgressId, this.progress); |
115 | upload.setProgressListener(listener); | 116 | upload.setProgressListener(listener); |
116 | List<FileItem> items = (List<FileItem>)upload.parseRequest(request); | 117 | List<FileItem> items = (List<FileItem>)upload.parseRequest(request); |
117 | 118 | ||
118 | for (FileItem item : items) { | 119 | for (FileItem item : items) { |
119 | itemMap.put(item.getFieldName(), item); | 120 | itemMap.put(item.getFieldName(), item); |
120 | } | 121 | } |
121 | 122 | ||
122 | HashMap parameters = new HashMap(); | 123 | HashMap parameters = new HashMap(); |
... | @@ -130,7 +131,8 @@ public class NGINXUploadProcessor extends HttpServlet { | ... | @@ -130,7 +131,8 @@ public class NGINXUploadProcessor extends HttpServlet { |
130 | System.err.println("FINISHED:" + pResponse); | 131 | System.err.println("FINISHED:" + pResponse); |
131 | out.print(pResponse); | 132 | out.print(pResponse); |
132 | 133 | ||
133 | progress.get(xProgressId).complete = true; | 134 | ProgressRecord oldRecord = progress.get(xProgressId); |
135 | progress.put(xProgressId, new ProgressRecord(oldRecord.bytesRead, oldRecord.contentLength, oldRecord.items, true)); | ||
134 | } catch (Exception ex) { | 136 | } catch (Exception ex) { |
135 | ex.printStackTrace(); | 137 | ex.printStackTrace(); |
136 | } | 138 | } |
... | @@ -141,5 +143,5 @@ public class NGINXUploadProcessor extends HttpServlet { | ... | @@ -141,5 +143,5 @@ public class NGINXUploadProcessor extends HttpServlet { |
141 | out.close(); | 143 | out.close(); |
142 | } | 144 | } |
143 | } | 145 | } |
144 | } | 146 | } |
145 | } | 147 | } | ... | ... |
-
Please register or sign in to post a comment