View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.StandardCharsets;
23  import java.nio.charset.UnsupportedCharsetException;
24  import java.util.List;
25  
26  import org.eclipse.jetty.client.api.ContentResponse;
27  import org.eclipse.jetty.client.api.Request;
28  import org.eclipse.jetty.client.api.Response;
29  import org.eclipse.jetty.http.HttpFields;
30  import org.eclipse.jetty.http.HttpVersion;
31  
32  public class HttpContentResponse implements ContentResponse
33  {
34      private final Response response;
35      private final byte[] content;
36      private final String mediaType;
37      private final String encoding;
38  
39      public HttpContentResponse(Response response, byte[] content, String mediaType, String encoding)
40      {
41          this.response = response;
42          this.content = content;
43          this.mediaType = mediaType;
44          this.encoding = encoding;
45      }
46  
47      @Override
48      public Request getRequest()
49      {
50          return response.getRequest();
51      }
52  
53      @Override
54      public <T extends ResponseListener> List<T> getListeners(Class<T> listenerClass)
55      {
56          return response.getListeners(listenerClass);
57      }
58  
59      @Override
60      public HttpVersion getVersion()
61      {
62          return response.getVersion();
63      }
64  
65      @Override
66      public int getStatus()
67      {
68          return response.getStatus();
69      }
70  
71      @Override
72      public String getReason()
73      {
74          return response.getReason();
75      }
76  
77      @Override
78      public HttpFields getHeaders()
79      {
80          return response.getHeaders();
81      }
82  
83      @Override
84      public boolean abort(Throwable cause)
85      {
86          return response.abort(cause);
87      }
88  
89      @Override
90      public String getMediaType()
91      {
92          return mediaType;
93      }
94  
95      @Override
96      public String getEncoding()
97      {
98          return encoding;
99      }
100 
101     @Override
102     public byte[] getContent()
103     {
104         return content;
105     }
106 
107     @Override
108     public String getContentAsString()
109     {
110         String encoding = this.encoding;
111         if (encoding == null)
112         {
113             return new String(getContent(), StandardCharsets.UTF_8);
114         }
115         else
116         {
117             try
118             {
119                 return new String(getContent(), encoding);
120             }
121             catch (UnsupportedEncodingException e)
122             {
123                 throw new UnsupportedCharsetException(encoding);
124             }
125         }
126     }
127 
128     @Override
129     public String toString()
130     {
131         return String.format("%s[%s %d %s - %d bytes]",
132                 HttpContentResponse.class.getSimpleName(),
133                 getVersion(),
134                 getStatus(),
135                 getReason(),
136                 getContent().length);
137     }
138 }