(HttpServletRequest request, HttpServletResponse response)
| 213 | * Not thread-safe. OK for this test. NOt OK for use in the real world. |
| 214 | */ |
| 215 | @Override |
| 216 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 217 | |
| 218 | final AsyncContext asyncContext = request.startAsync(); |
| 219 | |
| 220 | response.setStatus(HttpServletResponse.SC_OK); |
| 221 | response.setContentType("application/binary"); |
| 222 | |
| 223 | final ServletOutputStream output = response.getOutputStream(); |
| 224 | output.setWriteListener(new WriteListener() { |
| 225 | |
| 226 | // Intermittent CI errors were observed where the response body |
| 227 | // was exactly one block too small. Use an AtomicInteger to be |
| 228 | // sure blockCount is thread-safe. |
| 229 | final AtomicInteger blockCount = new AtomicInteger(0); |
| 230 | byte[] bytes = new byte[BLOCK_SIZE]; |
| 231 | |
| 232 | |
| 233 | @Override |
| 234 | public void onWritePossible() throws IOException { |
| 235 | if (useNonContainerThreadForWrite) { |
| 236 | future = scheduler.schedule(new Runnable() { |
| 237 | |
| 238 | @Override |
| 239 | public void run() { |
| 240 | try { |
| 241 | write(); |
| 242 | } catch (IOException ioe) { |
| 243 | throw new IllegalStateException(ioe); |
| 244 | } |
| 245 | } |
| 246 | }, 200, TimeUnit.MILLISECONDS); |
| 247 | } else { |
| 248 | write(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 | private void write() throws IOException { |
| 254 | while (output.isReady()) { |
| 255 | blockCount.incrementAndGet(); |
| 256 | output.write(bytes); |
| 257 | if (blockCount.get() == blockLimit) { |
| 258 | asyncContext.complete(); |
| 259 | scheduler.shutdown(); |
| 260 | return; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void onError(Throwable t) { |
| 267 | if (future != null) { |
| 268 | future.cancel(false); |
| 269 | } |
| 270 | t.printStackTrace(); |
| 271 | } |
| 272 | }); |
nothing calls this directly
no test coverage detected