• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.jsoup.integration.servlets;
2 
3 import org.jsoup.integration.TestServer;
4 
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 
10 /**
11  * Slowly, interminably writes output. For the purposes of testing timeouts and interrupts.
12  */
13 public class SlowRider extends BaseServlet {
14     public static final String Url;
15     public static final String TlsUrl;
16     static {
17         TestServer.ServletUrls urls = TestServer.map(SlowRider.class);
18         Url = urls.url;
19         TlsUrl = urls.tlsUrl;
20     }
21     private static final int SleepTime = 2000;
22     public static final String MaxTimeParam = "maxTime";
23 
24     @Override
doIt(HttpServletRequest req, HttpServletResponse res)25     protected void doIt(HttpServletRequest req, HttpServletResponse res) throws IOException {
26         pause(1000);
27         res.setContentType(TextHtml);
28         res.setStatus(HttpServletResponse.SC_OK);
29         PrintWriter w = res.getWriter();
30 
31         int maxTime = -1;
32         String maxTimeP = req.getParameter(MaxTimeParam);
33         if (maxTimeP != null) {
34             maxTime = Integer.parseInt(maxTimeP);
35         }
36 
37         long startTime = System.currentTimeMillis();
38         w.println("<title>Slow Rider</title>");
39         while (true) {
40             w.println("<p>Are you still there?");
41             boolean err = w.checkError(); // flush, and check still ok
42             if (err) {
43                 log("Remote connection lost");
44                 break;
45             }
46             if (pause(SleepTime)) break;
47 
48             if (maxTime > 0 && System.currentTimeMillis() > startTime + maxTime) {
49                 w.println("<h1>outatime</h1>");
50                 break;
51             }
52         }
53     }
54 
pause(int sleepTime)55     private static boolean pause(int sleepTime) {
56         try {
57             Thread.sleep(sleepTime);
58         } catch (InterruptedException e) {
59             return true;
60         }
61         return false;
62     }
63 
64     // allow the servlet to run as a main program, for local test
main(String[] args)65     public static void main(String[] args) {
66         TestServer.start();
67         System.out.println(Url);
68     }
69 }
70