Shindig does not support returning a JSON array when format 'json' is selected 
on an osapi.http.get request.
------------------------------------------------------------------------------------------------------------

                 Key: SHINDIG-1436
                 URL: https://issues.apache.org/jira/browse/SHINDIG-1436
             Project: Shindig
          Issue Type: Bug
          Components: Java
    Affects Versions: 2.0.0
            Reporter: Craig McClanahan


I am trying to use an osapi.http.get request to a JSON service that returns a 
JSON array with a collection of objects, rather than just a single object.

    osapi.http.get({
        href : 'http://www.example.com/customers',  // Returns an array of 
customer objects, so the first character in the response will be '['
        format : 'json',
        authz : 'none'
    }).execute(function(response) {
        ...
    });

Even though I can contact this service successfully with other clients, trying 
to access it via osapi.http.get with Shindig results in a status 406 response 
with message "Response not valid JSON" from the transformBody() method in 
HttpRequestHandler.java.  In turn, this is because the logic in this method 
tries to create a single JSON object out of the response text:


  /** Format a response as JSON, including additional JSON inserted by chained 
content fetchers. */
  protected Object transformBody(HttpApiRequest request, HttpResponse results)
      throws GadgetException {
    String body = results.getResponseAsString();
    if ("feed".equalsIgnoreCase(request.format)) {
      return processFeed(request, body);
    } else if ("json".equalsIgnoreCase(request.format)) {
      try {
        return new JSONObject(body);
      } catch (JSONException e) {
        // TODO: include data block with invalid JSON
        throw new ProtocolException(HttpServletResponse.SC_NOT_ACCEPTABLE, 
"Response not valid JSON", e);
      }
    }
    
    return body;
  }

The call to the JSONObject constructor will fail because the body text starts 
with "[" (because it is a JSON array), not "{".

This behavior is inconsistent with my read of the OpenSocial 1.0 
specification[1], which states that the "content" element of the response may 
contain either a JSON object or a JSON array:

    If @format is "json", the parsed JSON object or array of the response.

The proposed solution would be to check for the first non-whitespace character, 
and return new JSONArray(body) if it is a '[' character, instead of '{'.

[1] 
http://opensocial-resources.googlecode.com/svn/spec/1.0/Core-Data.xml#rfc.section.2.9


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to