source: apps/routerconsole/jsp/viewstat.jsp @ a845d4f

Last change on this file since a845d4f was a845d4f, checked in by zzz <zzz@…>, 3 years ago

Servlets: Add Accept-Ranges headers

  • Property mode set to 100644
File size: 4.2 KB
Line 
1<%
2/*
3 * USE CAUTION WHEN EDITING
4 * Trailing whitespace OR NEWLINE on the last line will cause
5 * IllegalStateExceptions !!!
6 *
7 * Do not tag this file for translation.
8 */
9
10boolean rendered = false;
11/****  unused
12String templateFile = request.getParameter("template");
13if (templateFile != null) {
14  java.io.OutputStream cout = response.getOutputStream();
15  response.setContentType("image/png");
16  rendered = net.i2p.router.web.StatSummarizer.instance().renderPng(cout, templateFile);
17}
18****/
19net.i2p.stat.Rate rate = null;
20String stat = request.getParameter("stat");
21String period = request.getParameter("period");
22boolean fakeBw = (stat != null && ("bw.combined".equals(stat)));
23net.i2p.stat.RateStat rs = null;
24if (stat != null)
25    rs = net.i2p.I2PAppContext.getGlobalContext().statManager().getRate(stat);
26if ( !rendered && ((rs != null) || fakeBw) ) {
27  long per = -1;
28  try {
29    if (fakeBw)
30      per = 60*1000;
31    else
32      per = Long.parseLong(period);
33    if (!fakeBw)
34      rate = rs.getRate(per);
35    if ( (rate != null) || (fakeBw) ) {
36      java.io.OutputStream cout = response.getOutputStream();
37      String format = request.getParameter("format");
38      response.setHeader("X-Content-Type-Options", "nosniff");
39      if ("xml".equals(format)) {
40        if (!fakeBw) {
41          response.setContentType("text/xml");
42          rendered = net.i2p.router.web.StatSummarizer.instance().getXML(rate, cout);
43        }
44      } else {
45        response.setContentType("image/png");
46        // very brief 45 sec expire
47        response.setDateHeader("Expires", net.i2p.I2PAppContext.getGlobalContext().clock().now() + (45*1000));
48        response.setHeader("Accept-Ranges", "none");
49        // http://jira.codehaus.org/browse/JETTY-1346
50        // This doesn't actually appear in the response, but it fixes the problem,
51        // so Jetty must look for this header and close the connection.
52        response.setHeader("Connection", "Close");
53        int width = -1;
54        int height = -1;
55        int periodCount = -1;
56        int end = 0;
57        String str = request.getParameter("width");
58        if (str != null) try { width = Integer.parseInt(str); } catch (NumberFormatException nfe) {}
59        str = request.getParameter("height");
60        if (str != null) try { height = Integer.parseInt(str); } catch (NumberFormatException nfe) {}
61        str = request.getParameter("periodCount");
62        if (str != null) try { periodCount = Integer.parseInt(str); } catch (NumberFormatException nfe) {}
63        str = request.getParameter("end");
64        if (str != null) try { end = Integer.parseInt(str); } catch (NumberFormatException nfe) {}
65        boolean hideLegend = Boolean.parseBoolean(request.getParameter("hideLegend"));
66        boolean hideGrid = Boolean.parseBoolean(request.getParameter("hideGrid"));
67        boolean hideTitle = Boolean.parseBoolean(request.getParameter("hideTitle"));
68        boolean showEvents = Boolean.parseBoolean(request.getParameter("showEvents"));
69        boolean showCredit = false;
70        if (request.getParameter("showCredit") != null)
71          showCredit = Boolean.parseBoolean(request.getParameter("showCredit"));
72        if (fakeBw)
73            rendered = net.i2p.router.web.StatSummarizer.instance().renderRatePng(cout, width, height, hideLegend, hideGrid, hideTitle, showEvents, periodCount, end, showCredit);
74        else
75            rendered = net.i2p.router.web.StatSummarizer.instance().renderPng(rate, cout, width, height, hideLegend, hideGrid, hideTitle, showEvents, periodCount, end, showCredit);
76      }
77      if (rendered)
78        cout.close();
79      //System.out.println("Rendered period " + per + " for the stat " + stat + "? " + rendered);
80    }
81  } catch (NumberFormatException nfe) {}
82}
83/*
84 *  Send a 403 instead of a 404, because the server sends error.jsp
85 *  for 404 errors, complete with the summary bar, which would be
86 *  a huge load for a page full of graphs if there's a problem
87 */
88if (!rendered) {
89    if (stat != null) {
90        stat = net.i2p.data.DataHelper.stripHTML(stat);
91        response.sendError(403, "The stat " + stat + " is not available, it must be enabled for graphing on the stats configuration page.");
92    } else {
93        response.sendError(403, "No stat specified");
94    }
95}
96%>
Note: See TracBrowser for help on using the repository browser.