Beispiel #1
0
        /**
         * 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;
        }
Beispiel #2
0
  /**
   * Inspects the passed object for one of several specific properties and, if
   * found, returns that property as a JsonObject object. All valid response
   * objects which encapsulate a single data item (e.g. a person) must have
   * this property.
   * 
   * @param  root JsonObject to query for the presence of the specific property
   * @throws OpenSocialRequestException if property is not found _in the passed
   *         object
   * @throws JSONException
   */
  private static JsonObject getEntryObject(JsonObject root)
    {

    JsonObject entry = new JsonObject();

    if (root.Contains("data")) {
      entry = root.getJSONObject("data");
    } else if (root.Contains("entry")) {
      entry = root.getJSONObject("entry");
    } else {
      throw new OpenSocialRequestException("Entry not found");
    }

    return entry;
  }
Beispiel #3
0
  /**
   * Recursively iterates through the properties of the passed JsonObject
   * object and returns a Map of OpenSocialField objects keyed on Strings
   * representing the property values and names respectively.
   * 
   * @param  o Object-oriented representation of a JSON object which is
   *         transformed into and returned as a Map of OpenSocialField
   *         objects keyed on Strings
   * @throws JSONException
   */
  private static Dictionary<String, OpenSocialField> createObjectRepresentation(
      JsonObject o)
    {

    Dictionary<String,OpenSocialField> r = new Dictionary<String,OpenSocialField>();

    var keys = o.Names;

      foreach (string key in keys)
      {
        String property = o[key].ToString();

      if (property.Length > 0 && property[0] == '{') {
        JsonObject p = o.getJSONObject(key);
        OpenSocialField field = new OpenSocialField(true);

        field.addValue(new OpenSocialObject(createObjectRepresentation(p)));
        r.Add(key, field);
      } else if (property.Length > 0 && property[0] == '[') {
        JsonArray p = (JsonArray)o[key];
        var values = createArrayRepresentation(p);
        OpenSocialField field = new OpenSocialField(true);

        foreach(var v in values) {
          field.addValue(v);
        }

        r.Add(key, field);
      } else if (property.Length > 0) {
        OpenSocialField field = new OpenSocialField(false);
        field.addValue(unescape(property));
        r.Add(key, field);
      }      
    }

    return r;
  }
Beispiel #4
0
  /**
   * Inspects the passed object for one of several specific properties and, if
   * found, returns that property as a JsonArray object. All valid response
   * objects which contain a data collection (e.g. a collection of people)
   * must have this property.
   * 
   * @param  root JsonObject to query for the presence of the specific property
   * @throws OpenSocialRequestException if property is not found _in the passed
   *         object
   * @throws JSONException
   */
  private static JsonArray getEntryArray(JsonObject root)
    {

    JsonArray entry = new JsonArray();

    if (root.Contains("entry")) {
      entry = (JsonArray)root["entry"];
    } else if (root.Contains("data")) {
      entry = (JsonArray)root.getJSONObject("data")["list"];
    } else {
      throw new OpenSocialRequestException("Entry not found");
    }

    return entry;
  }