/** * Processes a JSON request. * * @param request Original JSON request * @return The JSON response. */ public JsonObject Process(JsonObject request) { JsonObject response = new JsonObject(); JsonObject requestContext = request.getJSONObject("context"); JsonArray requestedGadgets = request["gadgets"] as JsonArray; // Process all JSON first so that we don't wind up with hanging threads if // a JsonException is thrown. List <IAsyncResult> gadgets = new List <IAsyncResult>(requestedGadgets.Length); for (int i = 0, j = requestedGadgets.Length; i < j; ++i) { var context = new JsonRpcGadgetContext(requestContext, (JsonObject)requestedGadgets[i]); PreloadProcessor proc = new PreloadProcessor(CallJob); IAsyncResult result = proc.BeginInvoke(context, null, null); gadgets.Add(result); } foreach (var entry in gadgets) { try { AsyncResult result = (AsyncResult)entry; PreloadProcessor proc = (PreloadProcessor)result.AsyncDelegate; JsonObject gadget = proc.EndInvoke(result); response.Accumulate("gadgets", gadget); } catch (JsonException e) { throw new RpcException("Unable to write JSON", e); } catch (Exception ee) { if (!(ee is RpcException)) { throw new RpcException("Processing interrupted", ee); } RpcException e = (RpcException)ee; // Just one gadget failed; mark it as such. try { GadgetContext context = e.Context; JsonObject errorObj = new JsonObject(); errorObj.Put("url", context.getUrl()) .Put("moduleId", context.getModuleId()); errorObj.Accumulate("errors", e.Message); response.Accumulate("gadgets", errorObj); } catch (JsonException je) { throw new RpcException("Unable to write JSON", je); } } } return(response); }
/** * Processes a JSON request. * * @param request Original JSON request * @return The JSON response. */ public JsonObject Process(JsonObject request) { JsonObject response = new JsonObject(); JsonObject requestContext = request.getJSONObject("context"); JsonArray requestedGadgets = request["gadgets"] as JsonArray; // Process all JSON first so that we don't wind up with hanging threads if // a JsonException is thrown. List<IAsyncResult> gadgets = new List<IAsyncResult>(requestedGadgets.Length); for (int i = 0, j = requestedGadgets.Length; i < j; ++i) { var context = new JsonRpcGadgetContext(requestContext, (JsonObject)requestedGadgets[i]); PreloadProcessor proc = new PreloadProcessor(CallJob); IAsyncResult result = proc.BeginInvoke(context, null, null); gadgets.Add(result); } foreach (var entry in gadgets) { try { AsyncResult result = (AsyncResult)entry; PreloadProcessor proc = (PreloadProcessor)result.AsyncDelegate; JsonObject gadget = proc.EndInvoke(result); response.Accumulate("gadgets", gadget); } catch (JsonException e) { throw new RpcException("Unable to write JSON", e); } catch (Exception ee) { if (!(ee is RpcException)) { throw new RpcException("Processing interrupted", ee); } RpcException e = (RpcException)ee; // Just one gadget failed; mark it as such. try { GadgetContext context = e.Context; JsonObject errorObj = new JsonObject(); errorObj.Put("url", context.getUrl()) .Put("moduleId", context.getModuleId()); errorObj.Accumulate("errors", e.Message); response.Accumulate("gadgets", errorObj); } catch (JsonException je) { throw new RpcException("Unable to write JSON", je); } } } return response; }