Detect invalidly mapped service names earlier, before the request body
is parsed.
Showing
1 changed file
with
34 additions
and
31 deletions
... | @@ -130,21 +130,34 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -130,21 +130,34 @@ public class DirectControlServlet extends HttpServlet { |
130 | } | 130 | } |
131 | pathInfo = pathInfo.substring(1).replaceAll(":", "."); | 131 | pathInfo = pathInfo.substring(1).replaceAll(":", "."); |
132 | 132 | ||
133 | // Determine type of request and load the context from JSON content or | 133 | // Determine the request method for service lookup and parameter filters |
134 | // parameter values accordingly | 134 | String method = request.getParameter("_method"); |
135 | if (contentType != null) { | 135 | String httpMethod = request.getMethod(); |
136 | int semi = contentType.indexOf(";"); | 136 | if (method == null) { |
137 | if (semi != -1) { | 137 | method = request.getMethod(); |
138 | contentType = contentType.substring(0, semi); | ||
139 | } | 138 | } |
139 | if (method == null) { | ||
140 | method = "GET"; | ||
140 | } | 141 | } |
141 | 142 | ||
142 | // Determine the request method for service lookup and parameter filters | 143 | // If there is a mapping for this pathInfo, run the corresponding service |
143 | String method = ""; | 144 | // otherwise, return an error |
144 | method = request.getParameter("_method"); | 145 | String serviceName = serviceURLMappings.get(pathInfo + "#" + method); |
145 | String httpMethod = request.getMethod(); | 146 | if (serviceName == null) { |
146 | if (method == null && httpMethod != null) method = httpMethod; | 147 | serviceName = serviceURLMappings.get(pathInfo); |
147 | if (method == null) method = "GET"; | 148 | if (serviceName == null) { |
149 | response.setStatus(404); | ||
150 | |||
151 | Debug.logInfo("No mapping found for " + pathInfo + "#" + method, module); | ||
152 | |||
153 | PrintWriter writer = response.getWriter(); | ||
154 | writer.println("No mapping found for URL \"" + pathInfo + "\""); | ||
155 | writer.flush(); | ||
156 | writer.close(); | ||
157 | return; | ||
158 | } | ||
159 | } | ||
160 | Debug.logInfo("Service name " + serviceName, module); | ||
148 | 161 | ||
149 | // Load context | 162 | // Load context |
150 | Map<String, Object> context = new HashMap<String, Object>(); | 163 | Map<String, Object> context = new HashMap<String, Object>(); |
... | @@ -157,6 +170,15 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -157,6 +170,15 @@ public class DirectControlServlet extends HttpServlet { |
157 | } | 170 | } |
158 | context.remove("_method"); | 171 | context.remove("_method"); |
159 | 172 | ||
173 | // Determine type of request and load the context from JSON content or | ||
174 | // parameter values accordingly | ||
175 | if (contentType != null) { | ||
176 | int semi = contentType.indexOf(";"); | ||
177 | if (semi != -1) { | ||
178 | contentType = contentType.substring(0, semi); | ||
179 | } | ||
180 | } | ||
181 | |||
160 | if ("application/json".equals(contentType)) { | 182 | if ("application/json".equals(contentType)) { |
161 | // Read request body as JSON and insert into the context | 183 | // Read request body as JSON and insert into the context |
162 | Map<String, Object> items = UtilGenerics.cast(JSON.from(UtilIO.readString(request.getReader())).toObject(Map.class)); | 184 | Map<String, Object> items = UtilGenerics.cast(JSON.from(UtilIO.readString(request.getReader())).toObject(Map.class)); |
... | @@ -211,25 +233,6 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -211,25 +233,6 @@ public class DirectControlServlet extends HttpServlet { |
211 | Delegator delegator = getDelegator(request.getServletContext()); | 233 | Delegator delegator = getDelegator(request.getServletContext()); |
212 | LocalDispatcher dispatcher = getDispatcher(request.getServletContext()); | 234 | LocalDispatcher dispatcher = getDispatcher(request.getServletContext()); |
213 | 235 | ||
214 | // If there is a mapping for this pathInfo, run the corresponding service | ||
215 | // otherwise, return an error | ||
216 | String serviceName = serviceURLMappings.get(pathInfo + "#" + method); | ||
217 | Debug.logInfo("Service name " + serviceName, module); | ||
218 | if (serviceName == null) { | ||
219 | serviceName = serviceURLMappings.get(pathInfo); | ||
220 | if (serviceName == null) { | ||
221 | response.setStatus(404); | ||
222 | |||
223 | Debug.logInfo("No mapping found for " + pathInfo + "#" + method, module); | ||
224 | |||
225 | PrintWriter writer = response.getWriter(); | ||
226 | writer.println("No mapping found for URL \"" + pathInfo + "\""); | ||
227 | writer.flush(); | ||
228 | writer.close(); | ||
229 | return; | ||
230 | } | ||
231 | } | ||
232 | |||
233 | // Check if there is an output handler | 236 | // Check if there is an output handler |
234 | String outputHandler = "JSON"; | 237 | String outputHandler = "JSON"; |
235 | if (serviceName.indexOf("|") != -1) { | 238 | if (serviceName.indexOf("|") != -1) { | ... | ... |
-
Please register or sign in to post a comment