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.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 encoding;
37
38 public HttpContentResponse(Response response, byte[] content, String encoding)
39 {
40 this.response = response;
41 this.content = content;
42 this.encoding = encoding;
43 }
44
45 @Override
46 public Request getRequest()
47 {
48 return response.getRequest();
49 }
50
51 @Override
52 @Deprecated
53 public long getConversationID()
54 {
55 return getRequest().getConversationID();
56 }
57
58 @Override
59 public <T extends ResponseListener> List<T> getListeners(Class<T> listenerClass)
60 {
61 return response.getListeners(listenerClass);
62 }
63
64 @Override
65 public HttpVersion getVersion()
66 {
67 return response.getVersion();
68 }
69
70 @Override
71 public int getStatus()
72 {
73 return response.getStatus();
74 }
75
76 @Override
77 public String getReason()
78 {
79 return response.getReason();
80 }
81
82 @Override
83 public HttpFields getHeaders()
84 {
85 return response.getHeaders();
86 }
87
88 @Override
89 public boolean abort(Throwable cause)
90 {
91 return response.abort(cause);
92 }
93
94 @Override
95 public byte[] getContent()
96 {
97 return content;
98 }
99
100 @Override
101 public String getContentAsString()
102 {
103 String encoding = this.encoding;
104 if (encoding == null)
105 {
106 return new String(getContent(), StandardCharsets.UTF_8);
107 }
108 else
109 {
110 try
111 {
112 return new String(getContent(), encoding);
113 }
114 catch (UnsupportedEncodingException e)
115 {
116 throw new UnsupportedCharsetException(encoding);
117 }
118 }
119 }
120
121 @Override
122 public String toString()
123 {
124 return String.format("%s[%s %d %s - %d bytes]",
125 HttpContentResponse.class.getSimpleName(),
126 getVersion(),
127 getStatus(),
128 getReason(),
129 getContent().length);
130 }
131 }