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
  *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.shindig.gadgets;
import  com.google.common.collect.Maps;
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.

Parameters:
response the response body
id the response id, or null if not needed
body string to use as the body of the response.
Returns:
a JSONObject representation of the response body.
  public static Map<StringObjectgetResponseAsJson(HttpResponse responseString id,
      String body) {
    Map<StringObjectresp = Maps.newHashMap();
    if (id != null) {
      resp.put("id"id);
    }
    resp.put("rc"response.getHttpStatusCode());
    resp.put("body"body);
    Map<StringCollection<String>> headers = Maps.newHashMap();
    addHeaders(headersresponse"set-cookie");
    addHeaders(headersresponse"location");
    if (headers.size() > 0) {
      resp.put("headers"headers);
    }
    // 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