You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@shindig.apache.org by "Craig McClanahan (JIRA)" <ji...@apache.org> on 2010/09/30 02:32:35 UTC

[jira] Created: (SHINDIG-1436) Shindig does not support returning a JSON array when format 'json' is selected on an osapi.http.get request.

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.


[jira] Resolved: (SHINDIG-1436) Shindig does not support returning a JSON array when format 'json' is selected on an osapi.http.get request.

Posted by "Henry Saputra (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/SHINDIG-1436?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henry Saputra resolved SHINDIG-1436.
------------------------------------

    Fix Version/s: 3.0.0
       Resolution: Fixed

Fixed with svn revision 1003169:

svn commit: r1003169 - in /shindig/trunk/java/gadgets/src: main/java/org/apache/shindig/gadgets/servlet/HttpRequestHandler.java test/java/org/apache/shindig/gadgets/servlet/HttpRequestHandlerTest.java

> 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
>            Assignee: Henry Saputra
>             Fix For: 3.0.0
>
>
> 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.


[jira] Commented: (SHINDIG-1436) Shindig does not support returning a JSON array when format 'json' is selected on an osapi.http.get request.

Posted by "Craig McClanahan (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/SHINDIG-1436?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12916397#action_12916397 ] 

Craig McClanahan commented on SHINDIG-1436:
-------------------------------------------

Yep, that proposed fix definitely works for me.  As an extra advantage, it will help on regular JSON objects where the body might have spurious whitespace before the initial "{" character.


> 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.


[jira] Assigned: (SHINDIG-1436) Shindig does not support returning a JSON array when format 'json' is selected on an osapi.http.get request.

Posted by "Henry Saputra (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/SHINDIG-1436?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henry Saputra reassigned SHINDIG-1436:
--------------------------------------

    Assignee: Henry Saputra

> 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
>            Assignee: Henry Saputra
>
> 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.


[jira] Commented: (SHINDIG-1436) Shindig does not support returning a JSON array when format 'json' is selected on an osapi.http.get request.

Posted by "Henry Saputra (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/SHINDIG-1436?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12916396#action_12916396 ] 

Henry Saputra commented on SHINDIG-1436:
----------------------------------------

Please review proposed fix at http://codereview.appspot.com/2323043/

> 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.