1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.Response;
28 import org.eclipse.jetty.http.HttpFields;
29 import org.eclipse.jetty.http.HttpVersion;
30
31 public class HttpContentResponse implements ContentResponse
32 {
33 private final Response response;
34 private final byte[] content;
35 private final String encoding;
36
37 public HttpContentResponse(Response response, byte[] content, String encoding)
38 {
39 this.response = response;
40 this.content = content;
41 this.encoding = encoding;
42 }
43
44 @Override
45 public long getConversationID()
46 {
47 return response.getConversationID();
48 }
49
50 @Override
51 public <T extends ResponseListener> List<T> getListeners(Class<T> listenerClass)
52 {
53 return response.getListeners(listenerClass);
54 }
55
56 @Override
57 public HttpVersion getVersion()
58 {
59 return response.getVersion();
60 }
61
62 @Override
63 public int getStatus()
64 {
65 return response.getStatus();
66 }
67
68 @Override
69 public String getReason()
70 {
71 return response.getReason();
72 }
73
74 @Override
75 public HttpFields getHeaders()
76 {
77 return response.getHeaders();
78 }
79
80 @Override
81 public boolean abort(Throwable cause)
82 {
83 return response.abort(cause);
84 }
85
86 @Override
87 public byte[] getContent()
88 {
89 return content;
90 }
91
92 @Override
93 public String getContentAsString()
94 {
95 String encoding = this.encoding;
96 if (encoding == null)
97 {
98 return new String(getContent(), StandardCharsets.UTF_8);
99 }
100 else
101 {
102 try
103 {
104 return new String(getContent(), encoding);
105 }
106 catch (UnsupportedEncodingException e)
107 {
108 throw new UnsupportedCharsetException(encoding);
109 }
110 }
111 }
112
113 @Override
114 public String toString()
115 {
116 return String.format("%s[%s %d %s - %d bytes]",
117 HttpContentResponse.class.getSimpleName(),
118 getVersion(),
119 getStatus(),
120 getReason(),
121 getContent().length);
122 }
123 }