d8530866 by Ean Schuessler

Return dates as standard strings. #4298

1 parent 27a74df2
...@@ -12,6 +12,8 @@ import java.util.Map; ...@@ -12,6 +12,8 @@ import java.util.Map;
12 import java.net.URL; 12 import java.net.URL;
13 import java.net.MalformedURLException; 13 import java.net.MalformedURLException;
14 import java.util.Set; 14 import java.util.Set;
15 import java.util.Date;
16 import java.util.List;
15 import java.util.concurrent.CopyOnWriteArraySet; 17 import java.util.concurrent.CopyOnWriteArraySet;
16 import java.util.HashMap; 18 import java.util.HashMap;
17 import java.util.HashSet; 19 import java.util.HashSet;
...@@ -19,6 +21,7 @@ import java.util.Enumeration; ...@@ -19,6 +21,7 @@ import java.util.Enumeration;
19 import java.util.Locale; 21 import java.util.Locale;
20 import java.util.TimeZone; 22 import java.util.TimeZone;
21 import java.util.Iterator; 23 import java.util.Iterator;
24 import java.sql.Timestamp;
22 25
23 import javolution.util.FastList; 26 import javolution.util.FastList;
24 27
...@@ -48,7 +51,9 @@ import org.ofbiz.entity.GenericValue; ...@@ -48,7 +51,9 @@ import org.ofbiz.entity.GenericValue;
48 import org.ofbiz.entity.util.EntityUtil; 51 import org.ofbiz.entity.util.EntityUtil;
49 import org.ofbiz.entity.condition.EntityOperator; 52 import org.ofbiz.entity.condition.EntityOperator;
50 import org.ofbiz.entity.condition.EntityCondition; 53 import org.ofbiz.entity.condition.EntityCondition;
54 import org.ofbiz.service.DispatchContext;
51 import org.ofbiz.service.LocalDispatcher; 55 import org.ofbiz.service.LocalDispatcher;
56 import org.ofbiz.service.ModelService;
52 import org.ofbiz.service.ServiceContainer; 57 import org.ofbiz.service.ServiceContainer;
53 58
54 import groovy.lang.GroovyClassLoader; 59 import groovy.lang.GroovyClassLoader;
...@@ -59,6 +64,8 @@ import org.ofbiz.base.json.JSON; ...@@ -59,6 +64,8 @@ import org.ofbiz.base.json.JSON;
59 import org.ofbiz.base.json.ParseException; 64 import org.ofbiz.base.json.ParseException;
60 65
61 import net.sf.json.JSONObject; 66 import net.sf.json.JSONObject;
67 import net.sf.json.JsonConfig;
68 import net.sf.json.processors.JsonValueProcessor;
62 69
63 public class DirectControlServlet extends HttpServlet { 70 public class DirectControlServlet extends HttpServlet {
64 public static final String module = DirectControlServlet.class.getName(); 71 public static final String module = DirectControlServlet.class.getName();
...@@ -218,10 +225,17 @@ public class DirectControlServlet extends HttpServlet { ...@@ -218,10 +225,17 @@ public class DirectControlServlet extends HttpServlet {
218 225
219 Debug.logInfo("USERLOGIN " + context.get("userLogin") + " AUTHTOKEN " + authToken, module); 226 Debug.logInfo("USERLOGIN " + context.get("userLogin") + " AUTHTOKEN " + authToken, module);
220 227
228 DispatchContext dctx = dispatcher.getDispatchContext();
229 ModelService model = dctx.getModelService(serviceName);
230
221 // some needed info for when running the service 231 // some needed info for when running the service
222 Locale locale = UtilHttp.getLocale(request); 232 Locale locale = UtilHttp.getLocale(request);
223 TimeZone timeZone = UtilHttp.getTimeZone(request); 233 TimeZone timeZone = UtilHttp.getTimeZone(request);
224 234
235 List<Object> errorMessages = FastList.newInstance();
236
237 context = model.makeValid(context, ModelService.IN_PARAM, true, errorMessages, timeZone, locale);
238
225 Map<String, Object> result = dispatcher.runSync(serviceName, context); 239 Map<String, Object> result = dispatcher.runSync(serviceName, context);
226 240
227 result.remove("responseMessage"); 241 result.remove("responseMessage");
...@@ -234,7 +248,10 @@ public class DirectControlServlet extends HttpServlet { ...@@ -234,7 +248,10 @@ public class DirectControlServlet extends HttpServlet {
234 248
235 PrintWriter writer = response.getWriter(); 249 PrintWriter writer = response.getWriter();
236 250
237 JSONObject json = JSONObject.fromObject(result); 251 JsonConfig jsonConfig = new JsonConfig();
252 jsonConfig.registerJsonValueProcessor(Date.class, new ISODateValueProcessor());
253 jsonConfig.registerJsonValueProcessor(Timestamp.class, new ISODateValueProcessor());
254 JSONObject json = JSONObject.fromObject(result, jsonConfig);
238 String jsonStr = json.toString(); 255 String jsonStr = json.toString();
239 response.setContentLength(jsonStr.getBytes("UTF8").length); 256 response.setContentLength(jsonStr.getBytes("UTF8").length);
240 writer.write(jsonStr); 257 writer.write(jsonStr);
...@@ -305,3 +322,23 @@ public class DirectControlServlet extends HttpServlet { ...@@ -305,3 +322,23 @@ public class DirectControlServlet extends HttpServlet {
305 return delegator; 322 return delegator;
306 } 323 }
307 } 324 }
325
326 class ISODateValueProcessor implements JsonValueProcessor {
327 public static final String module = ISODateValueProcessor.class.getName();
328
329 public ISODateValueProcessor() {
330 }
331
332 public Object processArrayValue( Object value, JsonConfig jsonConfig ) {
333 return value;
334 }
335
336 public Object processObjectValue( String key, Object value, JsonConfig jsonConfig ) {
337 return process(value, jsonConfig);
338 }
339
340 private Object process( Object value, JsonConfig jsonConfig ) {
341 String newValue = value.toString();
342 return newValue;
343 }
344 }
......