/** 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; }
/** 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 { body = body.trim(); if(body.length() > 0 && body.charAt(0) == '[') { return new JSONArray(body); } else { 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; }
/** 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 { body = body.trim(); if(body.charAt(0) == '[') { return new JSONArray(body); } else { 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; }