78d782c7 by Ean Schuessler

Process _method correctly

Method was not being set on a POST and was being populated as a
parameter into the context. Now that there is mapping for the
method type there is no reason to put it into the context.
1 parent 7eca963e
......@@ -66,14 +66,14 @@ public class DirectControlServlet extends HttpServlet {
// get the mapping file for this webapp
ServletContext context = config.getServletContext();
String mappingFile = context.getInitParameter("serviceURLMappings");
Debug.logError("Mapping file: " + mappingFile, module);
Debug.logInfo("Mapping file: " + mappingFile, module);
if (context.getInitParameter("sessionTokenName") != null) {
sessionTokenName = context.getInitParameter("sessionTokenName");
}
if (mappingFile == null) {
Debug.logError("No mapping file configured", module);
Debug.logInfo("No mapping file configured", module);
} else {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(context.getResourceAsStream(mappingFile)));
......@@ -83,7 +83,7 @@ public class DirectControlServlet extends HttpServlet {
serviceURLMappings.put(confItem[0], confItem[1]);
}
} catch (IOException ex) {
Debug.logError("Could not read mapping file " + mappingFile, module);
Debug.logInfo("Could not read mapping file " + mappingFile, module);
throw new ServletException("Could not read mapping file " + mappingFile);
}
}
......@@ -95,8 +95,8 @@ public class DirectControlServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = "";
Debug.logInfo("request.getPathInfo: " + request.getPathInfo(), module);
Debug.logInfo("request.getContentType: " + request.getContentType(), module);
Debug.logInfo("getPathInfo: " + request.getPathInfo() +
" request.getContentType: " + request.getContentType(), module);
String pathInfo = request.getPathInfo();
if (pathInfo == null || pathInfo.length() == 0) {
......@@ -116,42 +116,46 @@ public class DirectControlServlet extends HttpServlet {
String contentType = request.getContentType();
if (contentType != null && contentType.indexOf("application/x-www-form-urlencoded") != -1) {
method = request.getParameter("_method");
if (method == null) method = "POST";
// If this is a backbone PUT/DELETE emulated call then handle it
Debug.logInfo("Method: " + method, module);
if ("PUT".equals(method) || "DELETE".equals(method) || "CALL".equals(method)) {
if (!"CALL".equals(method)) context.put("_method", method);
if ("PUT".equals(method) || "DELETE".equals(method) || "CALL".equals(method) || "POST".equals(method)) {
// if (!"CALL".equals(method)) context.put("_method", method);
JSON json = new JSON(new StringReader(request.getParameter("model")));
Map<String,Object> items = json.JSONObject();
authToken = (String) items.get("sessionId");
for (String key : items.keySet()) {
if (!"sessionId".equals(key)) {
if (!"sessionId".equals(key) && !"_method".equals(key)) {
context.put(key, items.get(key));
Debug.logInfo("parameter '" + key + "'=" + items.get(key), module);
}
}
} else {
} else {
authToken = request.getParameter("sessionId");
for (Enumeration<String> params = request.getParameterNames(); params.hasMoreElements();) {
String param = params.nextElement();
Object[] values = request.getParameterValues(param);
if (!"sessionId".equals(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);
}
}
}
} else {
Debug.logError("Unsupported form encoding", module);
Debug.logInfo("Unsupported form encoding", module);
}
// If there is a mapping for this pathInfo, run the corresponding service
// otherwise, return an error
String serviceName = serviceURLMappings.get(pathInfo + "#" + method);
Debug.logInfo("Service name " + serviceName, module);
if (serviceName == null) {
serviceName = serviceURLMappings.get(pathInfo);
if (serviceName == null) {
response.setStatus(400);
Debug.logInfo("No mapping found for " + pathInfo + "#" + method, module);
PrintWriter writer = response.getWriter();
writer.println("No mapping found for URL \"" + pathInfo + "\"");
writer.flush();
......@@ -160,6 +164,8 @@ public class DirectControlServlet extends HttpServlet {
}
}
Debug.logInfo("Service name" +serviceName + " mapped for " + pathInfo + "#" + method, module);
// If the sessionId parameter is set, attempt to look up the corresponding
// UserLogin and apply it to the service context
if (authToken != null) {
......@@ -187,6 +193,13 @@ 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);
......@@ -234,20 +247,20 @@ public class DirectControlServlet extends HttpServlet {
/** This method only sets up a dispatcher for the current webapp and passed in delegator, it does not save it to the ServletContext or anywhere else, just returns it */
public static LocalDispatcher makeWebappDispatcher(ServletContext servletContext, Delegator delegator) {
if (delegator == null) {
Debug.logError("[ContextFilter.init] ERROR: delegator not defined.", module);
Debug.logInfo("[ContextFilter.init] ERROR: delegator not defined.", module);
return null;
}
// get the unique name of this dispatcher
String dispatcherName = servletContext.getInitParameter("localDispatcherName");
if (dispatcherName == null) {
Debug.logError("No localDispatcherName specified in the web.xml file", module);
Debug.logInfo("No localDispatcherName specified in the web.xml file", module);
dispatcherName = delegator.getDelegatorName();
}
LocalDispatcher dispatcher = ServiceContainer.getLocalDispatcher(dispatcherName, delegator);
if (dispatcher == null) {
Debug.logError("[ContextFilter.init] ERROR: dispatcher could not be initialized.", module);
Debug.logInfo("[ContextFilter.init] ERROR: dispatcher could not be initialized.", module);
}
return dispatcher;
......@@ -265,7 +278,7 @@ public class DirectControlServlet extends HttpServlet {
delegator = DelegatorFactory.getDelegator(delegatorName);
servletContext.setAttribute("delegator", delegator);
if (delegator == null) {
Debug.logError("[ContextFilter.init] ERROR: delegator factory returned null for delegatorName \"" + delegatorName + "\"", module);
Debug.logInfo("[ContextFilter.init] ERROR: delegator factory returned null for delegatorName \"" + delegatorName + "\"", module);
}
}
return delegator;
......