PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks-client/src/main/java/com/google/sitebricks/client/WebResponseImpl.java

http://github.com/dhanji/sitebricks
Java | 93 lines | 68 code | 14 blank | 11 comment | 6 complexity | d223d82995b52843813c37f93f412d7a MD5 | raw file
 1package com.google.sitebricks.client;
 2
 3import com.google.inject.Injector;
 4import com.google.inject.TypeLiteral;
 5import com.ning.http.client.Response;
 6import net.jcip.annotations.NotThreadSafe;
 7
 8import java.io.IOException;
 9import java.io.InputStream;
10import java.util.HashMap;
11import java.util.List;
12import java.util.Map;
13import java.util.logging.Logger;
14
15/**
16 * @author Dhanji R. Prasanna (dhanji@gmail.com)
17 * @author Jeanfrancois Arcand (jfarcand@apache.org)
18 */
19@NotThreadSafe
20class WebResponseImpl implements WebResponse {
21
22  private final Injector injector;
23  private final Response response;
24
25  // memo field
26  private Map<String, String> headers;
27
28  public WebResponseImpl(Injector injector, Response response) {
29    this.injector = injector;
30    this.response = response;
31  }
32
33  public Map<String, String> getHeaders() {
34    if (null != this.headers)
35      return this.headers;
36
37    // translate from ahc http client headers
38    final Map<String, String> headers = new HashMap<String, String>();
39    for (Map.Entry<String, List<String>> header : response.getHeaders().entrySet()) {
40      for (String value : header.getValue()) {
41        headers.put(header.getKey(), value);
42      }
43    }
44
45    return this.headers = headers;
46  }
47
48  public <T> ResponseTransportBuilder<T> to(final Class<T> data) {
49    TypeLiteral<T> typeLiteral = TypeLiteral.get(data);
50    return to(typeLiteral);
51  }
52
53  @Override
54  public <T> ResponseTransportBuilder<T> to(final TypeLiteral<T> data) {
55    return new ResponseTransportBuilder<T>() {
56      public T using(Class<? extends Transport> transportKey) {
57        InputStream in = null;
58        try {
59          in = response.getResponseBodyAsStream();
60
61          return injector.getInstance(transportKey).in(in, data);
62        } catch (IOException e) {
63          throw new TransportException(e);
64          //
65          // ugly stream closing here, to abstract it away from user code
66          //
67        } finally {
68          try {
69            if (null != in)
70              in.close();
71          } catch (IOException e) {
72            // strange, unrecoverable error =(
73            Logger.getLogger(WebResponseImpl.class.getName()).severe("Could not close input stream to in-memory byte array: " + e);
74          }
75        }
76      }
77    };
78  }
79
80  public int status() {
81    return response.getStatusCode();
82  }
83
84  @Override
85  public String toString() {
86    try {
87      return response.getResponseBody();
88    } catch (IOException e) {
89      // TODO
90      return "";
91    }
92  }
93}