|
31 | 31 | importjava.util.concurrent.CountDownLatch; |
32 | 32 | importjava.util.concurrent.ExecutionException; |
33 | 33 |
|
| 34 | +importjavax.servlet.AsyncContext; |
| 35 | +importjavax.servlet.ReadListener; |
34 | 36 | importjavax.servlet.ServletException; |
| 37 | +importjavax.servlet.ServletInputStream; |
35 | 38 | importjavax.servlet.http.Cookie; |
36 | 39 | importjavax.servlet.http.HttpServlet; |
37 | 40 | importjavax.servlet.http.HttpServletRequest; |
38 | 41 | importjavax.servlet.http.HttpServletResponse; |
39 | 42 |
|
40 | 43 | importorg.apache.catalina.Context; |
| 44 | +importorg.apache.catalina.Wrapper; |
41 | 45 | importorg.apache.catalina.startup.Tomcat; |
42 | | -importorg.apache.commons.io.IOUtils; |
43 | 46 | importorg.asynchttpclient.AsyncHttpClient; |
44 | 47 | importorg.asynchttpclient.BoundRequestBuilder; |
45 | 48 | importorg.asynchttpclient.HttpResponseBodyPart; |
@@ -84,7 +87,7 @@ public void setUpGlobal() throws Exception { |
84 | 87 | tomcat.setBaseDir(path); |
85 | 88 | Contextctx =tomcat.addContext("",path); |
86 | 89 |
|
87 | | -Tomcat.addServlet(ctx,"webdav",newHttpServlet() { |
| 90 | +Wrapperwrapper =Tomcat.addServlet(ctx,"webdav",newHttpServlet() { |
88 | 91 |
|
89 | 92 | @Override |
90 | 93 | publicvoidservice(HttpServletRequesthttpRequest,HttpServletResponsehttpResponse)throwsServletException,IOException { |
@@ -156,39 +159,45 @@ public void service(HttpServletRequest httpRequest, HttpServletResponse httpResp |
156 | 159 | } |
157 | 160 | } |
158 | 161 |
|
159 | | -StringrequestBodyLength =httpRequest.getHeader("X-" +CONTENT_LENGTH); |
| 162 | +finalAsyncContextcontext =httpRequest.startAsync(); |
| 163 | +finalServletInputStreaminput =httpRequest.getInputStream(); |
| 164 | +finalByteArrayOutputStreambaos =newByteArrayOutputStream(); |
160 | 165 |
|
161 | | -if (requestBodyLength !=null) { |
162 | | -byte[]requestBodyBytes =IOUtils.toByteArray(httpRequest.getInputStream()); |
163 | | -inttotal =requestBodyBytes.length; |
| 166 | +input.setReadListener(newReadListener() { |
164 | 167 |
|
165 | | -httpResponse.addIntHeader("X-" +CONTENT_LENGTH,total); |
166 | | -Stringmd5 =TestUtils.md5(requestBodyBytes,0,total); |
167 | | -httpResponse.addHeader(CONTENT_MD5.toString(),md5); |
| 168 | +byte[]buffer =newbyte[5 *1024]; |
168 | 169 |
|
169 | | -httpResponse.getOutputStream().write(requestBodyBytes,0,total); |
170 | | -}else { |
171 | | -intsize =16384; |
172 | | -if (httpRequest.getContentLength() >0) { |
173 | | -size =httpRequest.getContentLength(); |
| 170 | +@Override |
| 171 | +publicvoidonError(Throwablet) { |
| 172 | +t.printStackTrace(); |
| 173 | +httpResponse.setStatus(io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR.code()); |
| 174 | +context.complete(); |
174 | 175 | } |
175 | | -if (size >0) { |
176 | | -intread =0; |
177 | | -while (read > -1) { |
178 | | -byte[]bytes =newbyte[size]; |
179 | | -read =httpRequest.getInputStream().read(bytes); |
180 | | -if (read >0) { |
181 | | -httpResponse.getOutputStream().write(bytes,0,read); |
182 | | - } |
| 176 | + |
| 177 | +@Override |
| 178 | +publicvoidonDataAvailable()throwsIOException { |
| 179 | +intlen = -1; |
| 180 | +while (input.isReady() && (len =input.read(buffer)) != -1) { |
| 181 | +baos.write(buffer,0,len); |
183 | 182 | } |
184 | 183 | } |
185 | | - } |
186 | 184 |
|
187 | | -httpResponse.getOutputStream().flush(); |
188 | | -// FIXME don't always close, depends on the test, cf ReactiveStreamsTest |
189 | | -// httpResponse.getOutputStream().close(); |
| 185 | +@Override |
| 186 | +publicvoidonAllDataRead()throwsIOException { |
| 187 | +byte[]requestBodyBytes =baos.toByteArray(); |
| 188 | +inttotal =requestBodyBytes.length; |
| 189 | + |
| 190 | +httpResponse.addIntHeader("X-" +CONTENT_LENGTH,total); |
| 191 | +Stringmd5 =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 | + }); |
190 | 198 | } |
191 | 199 | }); |
| 200 | +wrapper.setAsyncSupported(true); |
192 | 201 | ctx.addServletMappingDecoded("/*","webdav"); |
193 | 202 | tomcat.start(); |
194 | 203 | port1 =tomcat.getConnector().getLocalPort(); |
|