Skip to content

Instantly share code, notes, and snippets.

@cstamas
Created February 3, 2011 21:10
Show Gist options
  • Save cstamas/810210 to your computer and use it in GitHub Desktop.
Save cstamas/810210 to your computer and use it in GitHub Desktop.
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