75a3aa43 by Adam Heath

Detect invalidly mapped service names earlier, before the request body

is parsed.
1 parent 498612c9
......@@ -130,21 +130,34 @@ public class DirectControlServlet extends HttpServlet {
}
pathInfo = pathInfo.substring(1).replaceAll(":", ".");
// Determine type of request and load the context from JSON content or
// parameter values accordingly
if (contentType != null) {
int semi = contentType.indexOf(";");
if (semi != -1) {
contentType = contentType.substring(0, semi);
// Determine the request method for service lookup and parameter filters
String method = request.getParameter("_method");
String httpMethod = request.getMethod();
if (method == null) {
method = request.getMethod();
}
if (method == null) {
method = "GET";
}
// 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;
if (method == null) method = "GET";
// If there is a mapping for this pathInfo, run the corresponding service
// otherwise, return an error
String serviceName = serviceURLMappings.get(pathInfo + "#" + method);
if (serviceName == null) {
serviceName = serviceURLMappings.get(pathInfo);
if (serviceName == null) {
response.setStatus(404);
Debug.logInfo("No mapping found for " + pathInfo + "#" + method, module);
PrintWriter writer = response.getWriter();
writer.println("No mapping found for URL \"" + pathInfo + "\"");
writer.flush();
writer.close();
return;
}
}
Debug.logInfo("Service name " + serviceName, module);
// Load context
Map<String, Object> context = new HashMap<String, Object>();
......@@ -157,6 +170,15 @@ public class DirectControlServlet extends HttpServlet {
}
context.remove("_method");
// Determine type of request and load the context from JSON content or
// parameter values accordingly
if (contentType != null) {
int semi = contentType.indexOf(";");
if (semi != -1) {
contentType = contentType.substring(0, semi);
}
}
if ("application/json".equals(contentType)) {
// Read request body as JSON and insert into the context
Map<String, Object> items = UtilGenerics.cast(JSON.from(UtilIO.readString(request.getReader())).toObject(Map.class));
......@@ -211,25 +233,6 @@ public class DirectControlServlet extends HttpServlet {
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);
Debug.logInfo("Service name " + serviceName, module);
if (serviceName == null) {
serviceName = serviceURLMappings.get(pathInfo);
if (serviceName == null) {
response.setStatus(404);
Debug.logInfo("No mapping found for " + pathInfo + "#" + method, module);
PrintWriter writer = response.getWriter();
writer.println("No mapping found for URL \"" + pathInfo + "\"");
writer.flush();
writer.close();
return;
}
}
// Check if there is an output handler
String outputHandler = "JSON";
if (serviceName.indexOf("|") != -1) {
......