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.http;
 
 
 import  com.google.common.collect.ImmutableList;
 import  com.google.common.collect.Lists;
 import  com.google.common.collect.Maps;
 import  com.google.common.collect.Multimap;
 
 
 import java.util.List;
 import java.util.Map;


Constructs HttpResponse objects.
 
 public class HttpResponseBuilder {
   private int httpStatusCode = .;
   private final Multimap<StringStringheaders = HttpResponse.newHeaderMultimap();
   private byte[] responseBytes = .;
   private final Map<StringStringmetadata = Maps.newHashMap();
 
   public HttpResponseBuilder() {}
 
   public HttpResponseBuilder(HttpResponseBuilder builder) {
      = builder.httpStatusCode;
     .putAll(builder.headers);
     .putAll(builder.metadata);
      = builder.responseBytes;
   }
 
   public HttpResponseBuilder(HttpResponse response) {
      = response.getHttpStatusCode();
 
     .putAll(response.getHeaders());
 
     .putAll(response.getMetadata());
      = response.getResponseAsBytes();
   }

  

Returns:
A new HttpResponse.
 
   public HttpResponse create() {
     return new HttpResponse(this);
   }

  

Parameters:
body The response string. Converted to UTF-8 bytes and copied when set.
 
      = CharsetUtil.getUtf8Bytes(body);
     return this;
   }

  

Parameters:
responseBytes The response body. Copied when set.
 
   public HttpResponseBuilder setResponse(byte[] responseBytes) {
     if (responseBytes == null) {
       responseBytes = .;
     }
     this. = new byte[responseBytes.length];
     System.arraycopy(responseBytes, 0, this., 0, responseBytes.length);
     return this;
   }

  

Parameters:
httpStatusCode The HTTP response status, defined on HttpResponse.
 
   public HttpResponseBuilder setHttpStatusCode(int httpStatusCode) {
     this. = httpStatusCode;
     return this;
   }

  
Add a single header to the response. If a value for the given name is already set, a second value is added. If you wish to overwrite any possible values for a header, use setHeader(String, String).
  public HttpResponseBuilder addHeader(String nameString value) {
    if (name != null) {
      .put(namevalue);
    }
    return this;
  }

  
Sets a single header value, overwriting any previously set headers with the same name.
  public HttpResponseBuilder setHeader(String nameString value) {
    if (name != null) {
      .replaceValues(name, Lists.newArrayList(value));
    }
    return this;
  }

  
Adds an entire map of headers to the response.
  public HttpResponseBuilder addHeaders(Map<StringStringheaders) {
    for (Map.Entry<String,Stringentry : headers.entrySet()) {
      this..put(entry.getKey(), entry.getValue());
    }
    return this;
  }

  
Adds all headers in the provided multimap to the response.
  public HttpResponseBuilder addAllHeaders(Map<String, ? extends List<String>> headers) {
    for (Map.Entry<String,? extends List<String>> entry : headers.entrySet()) {
      this..putAll(entry.getKey(), entry.getValue());
    }
    return this;
  }

  
Remove all headers with the given name from the response.

Returns:
Any values that were removed from the response.
  public Collection<StringremoveHeader(String name) {
    return .removeAll(name);
  }

  

Parameters:
cacheTtl The time to live for this response, in seconds.
  public HttpResponseBuilder setCacheTtl(int cacheTtl) {
    .removeAll("Pragma");
    .removeAll("Expires");
    .replaceValues("Cache-Control", ImmutableList.of("public,max-age=" + cacheTtl));
    return this;
  }

  

Parameters:
expirationTime The expiration time for this response, in milliseconds since the Unix epoch.
  public HttpResponseBuilder setExpirationTime(long expirationTime) {
    .removeAll("Cache-Control");
    .removeAll("Pragma");
    .put("Expires", DateUtil.formatRfc1123Date(expirationTime));
    return this;
  }

  
Sets cache-control headers indicating the response is not cacheable.
  private final List<StringNO_CACHE_HEADER = ImmutableList.of("no-cache");
    .replaceValues("Cache-Control");
    .replaceValues("Pragma");
    .removeAll("Expires");
    return this;
  }

  
Adds a new piece of metadata to the response.
  public HttpResponseBuilder setMetadata(String keyString value) {
    .put(keyvalue);
    return this;
  }

  
Merges the given Map of metadata into the existing metadata.
  public HttpResponseBuilder setMetadata(Map<StringStringmetadata) {
    this..putAll(metadata);
    return this;
  }
  Multimap<StringStringgetHeaders() {
    return ;
  }
    return ;
  }
  byte[] getResponse() {
    return ;
  }
  public int getHttpStatusCode() {
    return ;
  }
New to GrepCode? Check out our FAQ X