Skip to content

Commit 8c6a633

Browse files
committed
Use Servlet 3.1
1 parent 38345ef commit 8c6a633

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

client/src/test/java/org/asynchttpclient/reactivestreams/ReactiveStreamsTest.java

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,18 @@
3131
import java.util.concurrent.CountDownLatch;
3232
import java.util.concurrent.ExecutionException;
3333

34+
import javax.servlet.AsyncContext;
35+
import javax.servlet.ReadListener;
3436
import javax.servlet.ServletException;
37+
import javax.servlet.ServletInputStream;
3538
import javax.servlet.http.Cookie;
3639
import javax.servlet.http.HttpServlet;
3740
import javax.servlet.http.HttpServletRequest;
3841
import javax.servlet.http.HttpServletResponse;
3942

4043
import org.apache.catalina.Context;
44+
import org.apache.catalina.Wrapper;
4145
import org.apache.catalina.startup.Tomcat;
42-
import org.apache.commons.io.IOUtils;
4346
import org.asynchttpclient.AsyncHttpClient;
4447
import org.asynchttpclient.BoundRequestBuilder;
4548
import org.asynchttpclient.HttpResponseBodyPart;
@@ -84,7 +87,7 @@ public void setUpGlobal() throws Exception {
8487
tomcat.setBaseDir(path);
8588
Context ctx = tomcat.addContext("", path);
8689

87-
Tomcat.addServlet(ctx, "webdav", new HttpServlet() {
90+
Wrapper wrapper = Tomcat.addServlet(ctx, "webdav", new HttpServlet() {
8891

8992
@Override
9093
public void service(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException, IOException {
@@ -156,39 +159,45 @@ public void service(HttpServletRequest httpRequest, HttpServletResponse httpResp
156159
}
157160
}
158161

159-
String requestBodyLength = httpRequest.getHeader("X-" + CONTENT_LENGTH);
162+
final AsyncContext context = httpRequest.startAsync();
163+
final ServletInputStream input = httpRequest.getInputStream();
164+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
160165

161-
if (requestBodyLength != null) {
162-
byte[] requestBodyBytes = IOUtils.toByteArray(httpRequest.getInputStream());
163-
int total = requestBodyBytes.length;
166+
input.setReadListener(new ReadListener() {
164167

165-
httpResponse.addIntHeader("X-" + CONTENT_LENGTH, total);
166-
String md5 = TestUtils.md5(requestBodyBytes, 0, total);
167-
httpResponse.addHeader(CONTENT_MD5.toString(), md5);
168+
byte[] buffer = new byte[5 * 1024];
168169

169-
httpResponse.getOutputStream().write(requestBodyBytes, 0, total);
170-
} else {
171-
int size = 16384;
172-
if (httpRequest.getContentLength() > 0) {
173-
size = httpRequest.getContentLength();
170+
@Override
171+
public void onError(Throwable t) {
172+
t.printStackTrace();
173+
httpResponse.setStatus(io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
174+
context.complete();
174175
}
175-
if (size > 0) {
176-
int read = 0;
177-
while (read > -1) {
178-
byte[] bytes = new byte[size];
179-
read = httpRequest.getInputStream().read(bytes);
180-
if (read > 0) {
181-
httpResponse.getOutputStream().write(bytes, 0, read);
182-
}
176+
177+
@Override
178+
public void onDataAvailable() throws IOException {
179+
int len = -1;
180+
while (input.isReady() && (len = input.read(buffer)) != -1) {
181+
baos.write(buffer, 0, len);
183182
}
184183
}
185-
}
186184

187-
httpResponse.getOutputStream().flush();
188-
// FIXME don't always close, depends on the test, cf ReactiveStreamsTest
189-
// httpResponse.getOutputStream().close();
185+
@Override
186+
public void onAllDataRead() throws IOException {
187+
byte[] requestBodyBytes = baos.toByteArray();
188+
int total = requestBodyBytes.length;
189+
190+
httpResponse.addIntHeader("X-" + CONTENT_LENGTH, total);
191+
String md5 = TestUtils.md5(requestBodyBytes, 0, total);
192+
httpResponse.addHeader(CONTENT_MD5.toString(), md5);
193+
194+
httpResponse.getOutputStream().write(requestBodyBytes, 0, total);
195+
context.complete();
196+
}
197+
});
190198
}
191199
});
200+
wrapper.setAsyncSupported(true);
192201
ctx.addServletMappingDecoded("/*", "webdav");
193202
tomcat.start();
194203
port1 = tomcat.getConnector().getLocalPort();

0 commit comments

Comments
 (0)