173fd3df by Ean Schuessler

#6039 Support for uploads in DCS

1 parent df0c8942
......@@ -6,6 +6,7 @@ import java.io.BufferedReader;
import java.io.StringReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.File;
import java.util.Collections;
import java.util.Collection;
import java.util.Map;
......@@ -34,6 +35,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Cookie;
import javax.servlet.http.Part;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GroovyUtil;
......@@ -59,6 +61,11 @@ import org.ofbiz.service.ServiceContainer;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import groovy.lang.GroovyClassLoader;
import groovy.lang.Script;
......@@ -74,9 +81,11 @@ public class DirectControlServlet extends HttpServlet {
public static final Map<String, String> serviceURLMappings = new HashMap<String, String>();
private String sessionTokenName = "_AUTHTOKEN";
private String checkSessionService;
private ServletConfig config;
public void init(ServletConfig config) throws ServletException {
// get the mapping file for this webapp
this.config = config;
ServletContext context = config.getServletContext();
String mappingFile = context.getInitParameter("serviceURLMappings");
Debug.logInfo("Mapping file: " + mappingFile, module);
......@@ -161,13 +170,32 @@ public class DirectControlServlet extends HttpServlet {
for (CSVRecord record : records) {
List<String> row = new ArrayList<String>();
String cell = null;
Iterator<String> i=record.iterator();
Iterator<String> i=record.iterator();
while (i.hasNext()) {
row.add(i.next());
}
data.add(row);
}
context.put("data", data);
} else if (contentType != null && contentType.indexOf("multipart/form-data") != -1) {
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Configure a repository (to ensure a secure temp location is used)
ServletContext servletContext = config.getServletContext();
File repository = (File) servletContext
.getAttribute("javax.servlet.context.tempdir");
factory.setRepository(repository);
ServletFileUpload upload = new ServletFileUpload(factory);
//Map<String, FileItem> itemMap = new HashMap<String, FileItem>();
List<FileItem> items = (List<FileItem>)upload.parseRequest(request);
for (FileItem item : items) {
Debug.logInfo("PART: " + item.getFieldName(), module);
context.put(item.getFieldName(), item.getString());
}
} else {
// Check if the request is a backbone style "emulateJSON" request
if (contentType != null && contentType.indexOf("x-www-form-urlencoded") != -1 && request.getParameter("model") != null) {
......@@ -301,10 +329,10 @@ public class DirectControlServlet extends HttpServlet {
response.setContentType("text/csv");
// Find the first list
List resList = null;
List<Map> resList = null;
for (Object o : result.values()) {
if (o instanceof List) {
resList = (List) o;
resList = (List<Map>) o;
break;
}
}
......