1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.client.util;
20
21 import java.io.UnsupportedEncodingException;
22 import java.nio.ByteBuffer;
23 import java.nio.charset.Charset;
24 import java.nio.charset.StandardCharsets;
25 import java.nio.charset.UnsupportedCharsetException;
26 import java.util.Locale;
27
28 import org.eclipse.jetty.client.api.Response;
29 import org.eclipse.jetty.client.api.Result;
30 import org.eclipse.jetty.http.HttpFields;
31 import org.eclipse.jetty.http.HttpHeader;
32
33
34
35
36
37
38
39 public abstract class BufferingResponseListener extends Response.Listener.Empty
40 {
41 private final int maxLength;
42 private volatile byte[] buffer = new byte[0];
43 private volatile String encoding;
44
45
46
47
48 public BufferingResponseListener()
49 {
50 this(2 * 1024 * 1024);
51 }
52
53
54
55
56
57
58 public BufferingResponseListener(int maxLength)
59 {
60 this.maxLength = maxLength;
61 }
62
63 @Override
64 public void onHeaders(Response response)
65 {
66 HttpFields headers = response.getHeaders();
67 long length = headers.getLongField(HttpHeader.CONTENT_LENGTH.asString());
68 if (length > maxLength)
69 {
70 response.abort(new IllegalArgumentException("Buffering capacity exceeded"));
71 }
72 else
73 {
74 String contentType = headers.get(HttpHeader.CONTENT_TYPE);
75 if (contentType != null)
76 {
77 String charset = "charset=";
78 int index = contentType.toLowerCase(Locale.ENGLISH).indexOf(charset);
79 if (index > 0)
80 {
81 String encoding = contentType.substring(index + charset.length());
82
83 index = encoding.indexOf(';');
84 if (index > 0)
85 encoding = encoding.substring(0, index);
86 this.encoding = encoding;
87 }
88 }
89 }
90 }
91
92 @Override
93 public void onContent(Response response, ByteBuffer content)
94 {
95 long newLength = buffer.length + content.remaining();
96 if (newLength > maxLength)
97 {
98 response.abort(new IllegalArgumentException("Buffering capacity exceeded"));
99 }
100 else
101 {
102 byte[] newBuffer = new byte[(int)newLength];
103 System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
104 content.get(newBuffer, buffer.length, content.remaining());
105 buffer = newBuffer;
106 }
107 }
108
109 @Override
110 public abstract void onComplete(Result result);
111
112 public String getEncoding()
113 {
114 return encoding;
115 }
116
117
118
119
120
121 public byte[] getContent()
122 {
123 return buffer;
124 }
125
126
127
128
129
130
131 public String getContentAsString()
132 {
133 String encoding = this.encoding;
134 if (encoding == null)
135 return getContentAsString(StandardCharsets.UTF_8);
136 return getContentAsString(encoding);
137 }
138
139
140
141
142
143
144 public String getContentAsString(String encoding)
145 {
146 try
147 {
148 return new String(getContent(), encoding);
149 }
150 catch (UnsupportedEncodingException x)
151 {
152 throw new UnsupportedCharsetException(encoding);
153 }
154 }
155
156
157
158
159
160
161 public String getContentAsString(Charset encoding)
162 {
163 return new String(getContent(), encoding);
164 }
165 }