All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.sitebricks.client.WebResponseImpl Maven / Gradle / Ivy

package com.google.sitebricks.client;

import com.google.inject.Injector;
import com.ning.http.client.Response;
import net.jcip.annotations.NotThreadSafe;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author Dhanji R. Prasanna ([email protected])
 * @author Jeanfrancois Arcand ([email protected])
 */
@NotThreadSafe
class WebResponseImpl implements WebResponse {

  private final Injector injector;
  private final Response response;

  // memo field
  private Map headers;

  public WebResponseImpl(Injector injector, Response response) {
    this.injector = injector;
    this.response = response;
  }

  public Map getHeaders() {
    if (null != this.headers)
      return this.headers;

    // translate from ahc http client headers
    final Map headers = new HashMap();
    for (Map.Entry> header : response.getHeaders().entrySet()) {
      for (String value : header.getValue()) {
        headers.put(header.getKey(), value);
      }
    }

    return this.headers = headers;
  }

  public  ResponseTransportBuilder to(final Class data) {
    return new ResponseTransportBuilder() {
      public T using(Class transportKey) {
        InputStream in = null;
        try {
          in = response.getResponseBodyAsStream();

          return injector.getInstance(transportKey).in(in, data);
        } catch (IOException e) {
          throw new TransportException(e);
          //
          // ugly stream closing here, to abstract it away from user code
          //
        } finally {
          try {
            if (null != in)
              in.close();
          } catch (IOException e) {
            // strange, unrecoverable error =(
            Logger.getLogger(WebResponseImpl.class.getName()).severe("Could not close input stream to in-memory byte array: " + e);
          }
        }
      }
    };
  }

  public int status() {
    return response.getStatusCode();
  }

  @Override
  public String toString() {
    try {
      return response.getResponseBody();
    } catch (IOException e) {
      // TODO
      return "";
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy