-#3872 Improve handling of JSON POST requests
Showing
1 changed file
with
44 additions
and
51 deletions
... | @@ -40,6 +40,7 @@ import org.ofbiz.base.util.StringUtil; | ... | @@ -40,6 +40,7 @@ import org.ofbiz.base.util.StringUtil; |
40 | import org.ofbiz.base.util.UtilHttp; | 40 | import org.ofbiz.base.util.UtilHttp; |
41 | import org.ofbiz.base.util.UtilMisc; | 41 | import org.ofbiz.base.util.UtilMisc; |
42 | import org.ofbiz.base.util.UtilProperties; | 42 | import org.ofbiz.base.util.UtilProperties; |
43 | import org.ofbiz.base.util.UtilIO; | ||
43 | 44 | ||
44 | import org.ofbiz.entity.Delegator; | 45 | import org.ofbiz.entity.Delegator; |
45 | import org.ofbiz.entity.DelegatorFactory; | 46 | import org.ofbiz.entity.DelegatorFactory; |
... | @@ -90,62 +91,64 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -90,62 +91,64 @@ public class DirectControlServlet extends HttpServlet { |
90 | } | 91 | } |
91 | } | 92 | } |
92 | 93 | ||
93 | public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | 94 | public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
94 | doPost(request, response); | ||
95 | } | ||
96 | |||
97 | public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||
98 | String method = ""; | ||
99 | Debug.logInfo("getPathInfo: " + request.getPathInfo() + | ||
100 | " request.getContentType: " + request.getContentType(), module); | ||
101 | |||
102 | String pathInfo = request.getPathInfo(); | ||
103 | if (pathInfo == null || pathInfo.length() == 0) { | ||
104 | return; | ||
105 | } | ||
106 | pathInfo = pathInfo.substring(1).replaceAll(":", "."); | ||
107 | |||
108 | try { | 95 | try { |
109 | Delegator delegator = getDelegator(request.getServletContext()); | 96 | Debug.logInfo("getPathInfo: " + request.getPathInfo() + |
110 | LocalDispatcher dispatcher = getDispatcher(request.getServletContext()); | 97 | " request.getContentType: " + request.getContentType(), module); |
111 | 98 | ||
112 | // Build context | 99 | String pathInfo = request.getPathInfo(); |
113 | Map<String, Object> context = new HashMap<String, Object>(); | 100 | if (pathInfo == null || pathInfo.length() == 0) { |
114 | String paramList = ""; | 101 | return; |
115 | for (Iterator<String> k = request.getParameterMap().keySet().iterator(); k.hasNext(); ) { | ||
116 | paramList += " " + k.next(); | ||
117 | } | 102 | } |
103 | pathInfo = pathInfo.substring(1).replaceAll(":", "."); | ||
118 | 104 | ||
119 | Debug.logInfo("PARAMETERS: " + paramList, module); | 105 | // Determine type of request and load the context from JSON content or |
120 | // Handle if this is a form post | 106 | // parameter values accordingly |
121 | String contentType = request.getContentType(); | 107 | String contentType = request.getContentType(); |
108 | |||
109 | // Determine the request method for service lookup and parameter filters | ||
110 | String method = ""; | ||
122 | method = request.getParameter("_method"); | 111 | method = request.getParameter("_method"); |
123 | String httpMethod = request.getMethod(); | 112 | String httpMethod = request.getMethod(); |
124 | if (method == null && httpMethod != null) method = httpMethod; | 113 | if (method == null && httpMethod != null) method = httpMethod; |
125 | Debug.logInfo("Method: HTTP(" + httpMethod + ") embedded(" + method + ")", module); | ||
126 | if (method == null) method = "GET"; | 114 | if (method == null) method = "GET"; |
127 | // If this is a backbone PUT/DELETE emulated call then handle it | 115 | Debug.logInfo("Method: HTTP(" + httpMethod + ") embedded(" + method + ")", module); |
128 | if (request.getParameter("model") != null) { | 116 | |
129 | // if (!"CALL".equals(method)) context.put("_method", method); | 117 | // Load context |
130 | JSON json = new JSON(new StringReader(request.getParameter("model"))); | 118 | Map<String, Object> context = new HashMap<String, Object>(); |
119 | if ("application/json".equals(contentType)) { | ||
120 | // Read request body as JSON and insert into the context | ||
121 | JSON json = new JSON(new InputStreamReader(request.getInputStream())); | ||
131 | Map<String,Object> items = json.JSONObject(); | 122 | Map<String,Object> items = json.JSONObject(); |
132 | for (String key : items.keySet()) { | 123 | for (String key : items.keySet()) { |
133 | if (!"sessionId".equals(key) && !"_method".equals(key)) { | 124 | context.put(key, items.get(key)); |
134 | context.put(key, items.get(key)); | ||
135 | Debug.logInfo(">parameter '" + key + "'=" + items.get(key), module); | ||
136 | } | ||
137 | } | 125 | } |
138 | } else { | 126 | } else { |
139 | for (Enumeration<String> params = request.getParameterNames(); params.hasMoreElements();) { | 127 | // Check if the request is a backbone style "emulateJSON" request |
140 | String param = params.nextElement(); | 128 | if (request.getContentType() == "application/x-www-form-urlencoded" && |
141 | Object[] values = request.getParameterValues(param); | 129 | request.getParameter("model") != null) { |
142 | if (!"sessionId".equals(param) && !"_method".equals(param)) { | 130 | JSON json = new JSON(new StringReader(request.getParameter("model"))); |
143 | context.put(param, values.length == 1 ? values[0] : values); | 131 | Map<String,Object> items = json.JSONObject(); |
144 | Debug.logInfo("!parameter '" + param + "'=" + context.get(param), module); | 132 | for (String key : items.keySet()) { |
133 | if (!"sessionId".equals(key)) { | ||
134 | context.put(key, items.get(key)); | ||
135 | } | ||
136 | } | ||
137 | } else { | ||
138 | // Directly copy request parameters into context. | ||
139 | for (Enumeration<String> params = request.getParameterNames(); params.hasMoreElements();) { | ||
140 | String param = params.nextElement(); | ||
141 | Object[] values = request.getParameterValues(param); | ||
142 | if (!"sessionId".equals(param) && !"_method".equals(param)) { | ||
143 | context.put(param, values.length == 1 ? values[0] : values); | ||
144 | } | ||
145 | } | 145 | } |
146 | } | 146 | } |
147 | } | 147 | } |
148 | 148 | ||
149 | Delegator delegator = getDelegator(request.getServletContext()); | ||
150 | LocalDispatcher dispatcher = getDispatcher(request.getServletContext()); | ||
151 | |||
149 | // If there is a mapping for this pathInfo, run the corresponding service | 152 | // If there is a mapping for this pathInfo, run the corresponding service |
150 | // otherwise, return an error | 153 | // otherwise, return an error |
151 | String serviceName = serviceURLMappings.get(pathInfo + "#" + method); | 154 | String serviceName = serviceURLMappings.get(pathInfo + "#" + method); |
... | @@ -153,7 +156,7 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -153,7 +156,7 @@ public class DirectControlServlet extends HttpServlet { |
153 | if (serviceName == null) { | 156 | if (serviceName == null) { |
154 | serviceName = serviceURLMappings.get(pathInfo); | 157 | serviceName = serviceURLMappings.get(pathInfo); |
155 | if (serviceName == null) { | 158 | if (serviceName == null) { |
156 | response.setStatus(400); | 159 | response.setStatus(404); |
157 | 160 | ||
158 | Debug.logInfo("No mapping found for " + pathInfo + "#" + method, module); | 161 | Debug.logInfo("No mapping found for " + pathInfo + "#" + method, module); |
159 | 162 | ||
... | @@ -200,17 +203,8 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -200,17 +203,8 @@ public class DirectControlServlet extends HttpServlet { |
200 | Locale locale = UtilHttp.getLocale(request); | 203 | Locale locale = UtilHttp.getLocale(request); |
201 | TimeZone timeZone = UtilHttp.getTimeZone(request); | 204 | TimeZone timeZone = UtilHttp.getTimeZone(request); |
202 | 205 | ||
203 | /* | ||
204 | DispatchContext dctx = dispatcher.getDispatchContext(); | ||
205 | ModelService model = dctx.getModelService(serviceName); | ||
206 | for (ModelParam modelParam: model.getInModelParamList()) { | ||
207 | } | ||
208 | */ | ||
209 | |||
210 | Map<String, Object> result = dispatcher.runSync(serviceName, context); | 206 | Map<String, Object> result = dispatcher.runSync(serviceName, context); |
211 | 207 | ||
212 | // System.err.println("RESULT:" + result); | ||
213 | |||
214 | result.remove("responseMessage"); | 208 | result.remove("responseMessage"); |
215 | 209 | ||
216 | if (result.get("errorMessage") != null) { | 210 | if (result.get("errorMessage") != null) { |
... | @@ -225,12 +219,11 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -225,12 +219,11 @@ public class DirectControlServlet extends HttpServlet { |
225 | String jsonStr = json.toString(); | 219 | String jsonStr = json.toString(); |
226 | response.setContentLength(jsonStr.getBytes("UTF8").length); | 220 | response.setContentLength(jsonStr.getBytes("UTF8").length); |
227 | writer.write(jsonStr); | 221 | writer.write(jsonStr); |
228 | // Debug.logWarning("JSON result [%s]", module, jsonStr); | ||
229 | 222 | ||
230 | writer.flush(); | 223 | writer.flush(); |
231 | writer.close(); | 224 | writer.close(); |
232 | } catch (Throwable t) { | 225 | } catch (Throwable t) { |
233 | response.setStatus(400); | 226 | response.setStatus(500); |
234 | PrintWriter writer = response.getWriter(); | 227 | PrintWriter writer = response.getWriter(); |
235 | Debug.logInfo("Exception processing request", module); | 228 | Debug.logInfo("Exception processing request", module); |
236 | Debug.logInfo(t, module); | 229 | Debug.logInfo(t, module); | ... | ... |
-
Please register or sign in to post a comment