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