Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
  * regarding copyright ownership.  The ASF licenses this file
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
package org.apache.shindig.gadgets;
Handles converting HttpResponse objects to the format expected by the makeRequest javascript.
public class FetchResponseUtils {

Convert a response to a JSON object. The returned JSON object contains the following values: id: the id of the response rc: integer response code body: string response body headers: object, keys are header names, values are lists of header values The returned object is guaranteed to be mutable.

response the response body
id the response id, or null if not needed
body string to use as the body of the response.
a JSONObject representation of the response body.
  public static Map<StringObjectgetResponseAsJson(HttpResponse responseString id,
      String body) {
    Map<StringObjectresp = Maps.newHashMap();
    if (id != null) {
    Map<StringCollection<String>> headers = Maps.newHashMap();
    if (headers.size() > 0) {
    // Merge in additional response data
    for (Map.Entry<StringStringentry : response.getMetadata().entrySet()) {
      resp.put(entry.getKey(), entry.getValue());
    return resp;
  private static void addHeaders(Map<StringCollection<String>> headersHttpResponse response,
      String name) {
    Collection<Stringvalues = response.getHeaders(name);
    if (!values.isEmpty()) {
      headers.put(name.toLowerCase(), values);
New to GrepCode? Check out our FAQ X