Created
February 3, 2011 21:10
-
-
Save cstamas/810210 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SlowAndBigHandler extends AbstractHandler { | |
public void handle(String pathInContext, Request request, | |
HttpServletRequest httpRequest, HttpServletResponse httpResponse) | |
throws IOException, ServletException { | |
// 512MB large download | |
// 512 * 1024 * 1024 = 536870912 | |
httpResponse.setStatus(200); | |
httpResponse.setContentLength(HALF_GIG); | |
httpResponse.setContentType("application/octet-stream"); | |
httpResponse.flushBuffer(); | |
boolean wantFailure = httpRequest.getHeader("X-FAIL-TRANSFER") != null; | |
boolean wantSlow = httpRequest.getHeader("X-SLOW") != null; | |
OutputStream os = httpResponse.getOutputStream(); | |
for (int i = 0; i < HALF_GIG; i++) { | |
os.write(i % 255); | |
if (wantSlow) { | |
try { | |
Thread.sleep(300); | |
} catch (InterruptedException ex) { | |
// nuku | |
} | |
} | |
if (i > HALF_GIG / 2) { | |
if (wantFailure) { | |
httpResponse.sendError(500); | |
} | |
} | |
} | |
httpResponse.getOutputStream().flush(); | |
httpResponse.getOutputStream().close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment