View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.client;
20  
21  import java.io.UnsupportedEncodingException;
22  import java.nio.charset.UnsupportedCharsetException;
23  import java.util.List;
24  
25  import org.eclipse.jetty.client.api.ContentResponse;
26  import org.eclipse.jetty.client.api.Response;
27  import org.eclipse.jetty.http.HttpFields;
28  import org.eclipse.jetty.http.HttpVersion;
29  
30  public class HttpContentResponse implements ContentResponse
31  {
32      private final Response response;
33      private final byte[] content;
34      private final String encoding;
35  
36      public HttpContentResponse(Response response, byte[] content, String encoding)
37      {
38          this.response = response;
39          this.content = content;
40          this.encoding = encoding;
41      }
42  
43      @Override
44      public long getConversationID()
45      {
46          return response.getConversationID();
47      }
48  
49      @Override
50      public <T extends ResponseListener> List<T> getListeners(Class<T> listenerClass)
51      {
52          return response.getListeners(listenerClass);
53      }
54  
55      @Override
56      public HttpVersion getVersion()
57      {
58          return response.getVersion();
59      }
60  
61      @Override
62      public int getStatus()
63      {
64          return response.getStatus();
65      }
66  
67      @Override
68      public String getReason()
69      {
70          return response.getReason();
71      }
72  
73      @Override
74      public HttpFields getHeaders()
75      {
76          return response.getHeaders();
77      }
78  
79      @Override
80      public boolean abort(Throwable cause)
81      {
82          return response.abort(cause);
83      }
84  
85      @Override
86      public byte[] getContent()
87      {
88          return content;
89      }
90  
91      @Override
92      public String getContentAsString()
93      {
94          String encoding = this.encoding;
95          try
96          {
97              return new String(getContent(), encoding == null ? "UTF-8" : encoding);
98          }
99          catch (UnsupportedEncodingException e)
100         {
101             throw new UnsupportedCharsetException(encoding);
102         }
103     }
104 
105     @Override
106     public String toString()
107     {
108         return String.format("%s[%s %d %s - %d bytes]",
109                 HttpContentResponse.class.getSimpleName(),
110                 getVersion(),
111                 getStatus(),
112                 getReason(),
113                 getContent().length);
114     }
115 }