cd8ca01e by Adam Heath

Merge branch 'BF-4378' of /home/git/repositories/brainfood/ofbiz-directcontrolservlet

2 parents ef4b37ae 8e6015ca
...@@ -55,6 +55,9 @@ import org.ofbiz.service.LocalDispatcher; ...@@ -55,6 +55,9 @@ import org.ofbiz.service.LocalDispatcher;
55 import org.ofbiz.service.ModelService; 55 import org.ofbiz.service.ModelService;
56 import org.ofbiz.service.ServiceContainer; 56 import org.ofbiz.service.ServiceContainer;
57 57
58 import org.apache.commons.csv.CSVFormat;
59 import org.apache.commons.csv.CSVRecord;
60
58 import groovy.lang.GroovyClassLoader; 61 import groovy.lang.GroovyClassLoader;
59 import groovy.lang.Script; 62 import groovy.lang.Script;
60 63
...@@ -139,11 +142,31 @@ public class DirectControlServlet extends HttpServlet { ...@@ -139,11 +142,31 @@ public class DirectControlServlet extends HttpServlet {
139 for (String key : items.keySet()) { 142 for (String key : items.keySet()) {
140 context.put(key, items.get(key)); 143 context.put(key, items.get(key));
141 } 144 }
145 } else if ("text/csv".equals(contentType)) {
146 // Directly copy request parameters into context.
147 for (Enumeration<String> params = request.getParameterNames(); params.hasMoreElements();) {
148 String param = params.nextElement();
149 Object[] values = request.getParameterValues(param);
150 if (!"sessionId".equals(param) && !"_method".equals(param)) {
151 context.put(param, values.length == 1 ? values[0] : values);
152 }
153 }
154
155 Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(request.getReader());
156 List<List<String>> data = new ArrayList<List<String>>();
157 for (CSVRecord record : records) {
158 List<String> row = new ArrayList<String>();
159 String cell = null;
160 Iterator<String> i=record.iterator();
161 while (i.hasNext()) {
162 row.add(i.next());
163 }
164 data.add(row);
165 }
166 context.put("data", data);
142 } else { 167 } else {
143 // Check if the request is a backbone style "emulateJSON" request 168 // Check if the request is a backbone style "emulateJSON" request
144 if (contentType != null && 169 if (contentType != null && contentType.indexOf("x-www-form-urlencoded") != -1 && request.getParameter("model") != null) {
145 contentType.indexOf("x-www-form-urlencoded") != -1 &&
146 request.getParameter("model") != null) {
147 Debug.logInfo("MODEL: " + request.getParameter("model"), module); 170 Debug.logInfo("MODEL: " + request.getParameter("model"), module);
148 JSON json = new JSON(new StringReader(request.getParameter("model"))); 171 JSON json = new JSON(new StringReader(request.getParameter("model")));
149 Map<String,Object> items = json.JSONObject(); 172 Map<String,Object> items = json.JSONObject();
...@@ -254,7 +277,6 @@ public class DirectControlServlet extends HttpServlet { ...@@ -254,7 +277,6 @@ public class DirectControlServlet extends HttpServlet {
254 String jsonStr = json.toString(); 277 String jsonStr = json.toString();
255 response.setContentLength(jsonStr.getBytes("UTF8").length); 278 response.setContentLength(jsonStr.getBytes("UTF8").length);
256 writer.write(jsonStr); 279 writer.write(jsonStr);
257
258 writer.flush(); 280 writer.flush();
259 writer.close(); 281 writer.close();
260 } catch (Throwable t) { 282 } catch (Throwable t) {
......