0f8e94b0 by Ean Schuessler

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

2 parents d7b2fc8d cf441efb
......@@ -40,6 +40,7 @@ import org.ofbiz.base.util.StringUtil;
import org.ofbiz.base.util.UtilHttp;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilIO;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.DelegatorFactory;
......@@ -90,12 +91,8 @@ public class DirectControlServlet extends HttpServlet {
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = "";
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Debug.logInfo("getPathInfo: " + request.getPathInfo() +
" request.getContentType: " + request.getContentType(), module);
......@@ -105,47 +102,53 @@ public class DirectControlServlet extends HttpServlet {
}
pathInfo = pathInfo.substring(1).replaceAll(":", ".");
try {
Delegator delegator = getDelegator(request.getServletContext());
LocalDispatcher dispatcher = getDispatcher(request.getServletContext());
// Build context
Map<String, Object> context = new HashMap<String, Object>();
String paramList = "";
for (Iterator<String> k = request.getParameterMap().keySet().iterator(); k.hasNext(); ) {
paramList += " " + k.next();
}
Debug.logInfo("PARAMETERS: " + paramList, module);
// Handle if this is a form post
// Determine type of request and load the context from JSON content or
// parameter values accordingly
String contentType = request.getContentType();
// Determine the request method for service lookup and parameter filters
String method = "";
method = request.getParameter("_method");
String httpMethod = request.getMethod();
if (method == null && httpMethod != null) method = httpMethod;
Debug.logInfo("Method: HTTP(" + httpMethod + ") embedded(" + method + ")", module);
if (method == null) method = "GET";
// If this is a backbone PUT/DELETE emulated call then handle it
if (request.getParameter("model") != null) {
// if (!"CALL".equals(method)) context.put("_method", method);
Debug.logInfo("Method: HTTP(" + httpMethod + ") embedded(" + method + ")", module);
// Load context
Map<String, Object> context = new HashMap<String, Object>();
if ("application/json".equals(contentType)) {
// Read request body as JSON and insert into the context
JSON json = new JSON(new InputStreamReader(request.getInputStream()));
Map<String,Object> items = json.JSONObject();
for (String key : items.keySet()) {
context.put(key, items.get(key));
}
} else {
// Check if the request is a backbone style "emulateJSON" request
if (request.getContentType() == "application/x-www-form-urlencoded" &&
request.getParameter("model") != null) {
JSON json = new JSON(new StringReader(request.getParameter("model")));
Map<String,Object> items = json.JSONObject();
for (String key : items.keySet()) {
if (!"sessionId".equals(key) && !"_method".equals(key)) {
if (!"sessionId".equals(key)) {
context.put(key, items.get(key));
Debug.logInfo(">parameter '" + key + "'=" + items.get(key), module);
}
}
} else {
// Directly copy request parameters into context.
for (Enumeration<String> params = request.getParameterNames(); params.hasMoreElements();) {
String param = params.nextElement();
Object[] values = request.getParameterValues(param);
if (!"sessionId".equals(param) && !"_method".equals(param)) {
context.put(param, values.length == 1 ? values[0] : values);
Debug.logInfo("!parameter '" + param + "'=" + context.get(param), module);
}
}
}
}
Delegator delegator = getDelegator(request.getServletContext());
LocalDispatcher dispatcher = getDispatcher(request.getServletContext());
// If there is a mapping for this pathInfo, run the corresponding service
// otherwise, return an error
String serviceName = serviceURLMappings.get(pathInfo + "#" + method);
......@@ -153,7 +156,7 @@ public class DirectControlServlet extends HttpServlet {
if (serviceName == null) {
serviceName = serviceURLMappings.get(pathInfo);
if (serviceName == null) {
response.setStatus(400);
response.setStatus(404);
Debug.logInfo("No mapping found for " + pathInfo + "#" + method, module);
......@@ -200,17 +203,8 @@ public class DirectControlServlet extends HttpServlet {
Locale locale = UtilHttp.getLocale(request);
TimeZone timeZone = UtilHttp.getTimeZone(request);
/*
DispatchContext dctx = dispatcher.getDispatchContext();
ModelService model = dctx.getModelService(serviceName);
for (ModelParam modelParam: model.getInModelParamList()) {
}
*/
Map<String, Object> result = dispatcher.runSync(serviceName, context);
// System.err.println("RESULT:" + result);
result.remove("responseMessage");
if (result.get("errorMessage") != null) {
......@@ -225,12 +219,11 @@ public class DirectControlServlet extends HttpServlet {
String jsonStr = json.toString();
response.setContentLength(jsonStr.getBytes("UTF8").length);
writer.write(jsonStr);
// Debug.logWarning("JSON result [%s]", module, jsonStr);
writer.flush();
writer.close();
} catch (Throwable t) {
response.setStatus(400);
response.setStatus(500);
PrintWriter writer = response.getWriter();
Debug.logInfo("Exception processing request", module);
Debug.logInfo(t, module);
......