org/sonews/web/SonewsServlet.java
changeset 1 6fceb66e1ad7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/web/SonewsServlet.java	Fri Jun 26 16:48:50 2009 +0200
     1.3 @@ -0,0 +1,127 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.web;
    1.23 +
    1.24 +import java.io.IOException;
    1.25 +import javax.servlet.http.HttpServletRequest;
    1.26 +import javax.servlet.http.HttpServletResponse;
    1.27 +import org.sonews.daemon.Main;
    1.28 +import org.sonews.util.StringTemplate;
    1.29 +
    1.30 +/**
    1.31 + * Main sonews webpage servlet.
    1.32 + * @author Christian Lins
    1.33 + * @since sonews/0.5.0
    1.34 + */
    1.35 +public class SonewsServlet extends AbstractSonewsServlet
    1.36 +{
    1.37 +
    1.38 +  private static final long serialVersionUID = 2392837459834L;
    1.39 +
    1.40 +  @Override
    1.41 +  public void doGet(HttpServletRequest res, HttpServletResponse resp)
    1.42 +    throws IOException
    1.43 +  {
    1.44 +    synchronized(this)
    1.45 +    {
    1.46 +      connectToNewsserver();
    1.47 +
    1.48 +      String line;
    1.49 +      int    connectedClients = 0;
    1.50 +      int    hostedGroups     = 0;
    1.51 +      int    hostedNews       = 0;
    1.52 +
    1.53 +      printlnToNewsserver("XDAEMON LOG CONNECTED_CLIENTS");
    1.54 +
    1.55 +      line = readlnFromNewsserver();
    1.56 +      if(!line.startsWith("200 "))
    1.57 +      {
    1.58 +        throw new IOException("XDAEMON command not allowed by server");
    1.59 +      }
    1.60 +      line = readlnFromNewsserver();
    1.61 +      connectedClients = Integer.parseInt(line);
    1.62 +      line = readlnFromNewsserver(); // Read the "."
    1.63 +
    1.64 +      printlnToNewsserver("XDAEMON LOG HOSTED_NEWS");
    1.65 +      line = readlnFromNewsserver();
    1.66 +      if(!line.startsWith("200 "))
    1.67 +      {
    1.68 +        throw new IOException("XDAEMON command not allowed by server");
    1.69 +      }
    1.70 +      line = readlnFromNewsserver();
    1.71 +      hostedNews = Integer.parseInt(line);
    1.72 +      line = readlnFromNewsserver(); // read the "."
    1.73 +
    1.74 +      printlnToNewsserver("XDAEMON LOG HOSTED_GROUPS");
    1.75 +      line = readlnFromNewsserver();
    1.76 +      if(!line.startsWith("200 "))
    1.77 +      {
    1.78 +        throw new IOException("XDAEMON command not allowed by server");
    1.79 +      }
    1.80 +      line = readlnFromNewsserver();
    1.81 +      hostedGroups = Integer.parseInt(line);
    1.82 +      line = readlnFromNewsserver(); // read the "."
    1.83 +
    1.84 +      printlnToNewsserver("XDAEMON LOG POSTED_NEWS_PER_HOUR");
    1.85 +      line = readlnFromNewsserver();
    1.86 +      if(!line.startsWith("200 "))
    1.87 +      {
    1.88 +        throw new IOException("XDAEMON command not allowed by server");
    1.89 +      }
    1.90 +      String postedNewsPerHour = readlnFromNewsserver();
    1.91 +      readlnFromNewsserver();
    1.92 +
    1.93 +      printlnToNewsserver("XDAEMON LOG GATEWAYED_NEWS_PER_HOUR");
    1.94 +      line = readlnFromNewsserver();
    1.95 +      if(!line.startsWith("200 "))
    1.96 +      {
    1.97 +        throw new IOException("XDAEMON command not allowed by server");
    1.98 +      }
    1.99 +      String gatewayedNewsPerHour = readlnFromNewsserver();
   1.100 +      line = readlnFromNewsserver();
   1.101 +
   1.102 +      printlnToNewsserver("XDAEMON LOG FEEDED_NEWS_PER_HOUR");
   1.103 +      line = readlnFromNewsserver();
   1.104 +      if(!line.startsWith("200 "))
   1.105 +      {
   1.106 +        throw new IOException("XDAEMON command not allowed by server");
   1.107 +      }
   1.108 +      String feededNewsPerHour = readlnFromNewsserver();
   1.109 +      line = readlnFromNewsserver();
   1.110 +
   1.111 +      StringTemplate tmpl = getTemplate("SonewsServlet.tmpl");
   1.112 +      tmpl.set("SERVERNAME", hello.split(" ")[2]);
   1.113 +      tmpl.set("STARTDATE", Main.STARTDATE);
   1.114 +      tmpl.set("ACTIVE_CONNECTIONS", connectedClients);
   1.115 +      tmpl.set("STORED_NEWS", hostedNews);
   1.116 +      tmpl.set("SERVED_NEWSGROUPS", hostedGroups);
   1.117 +      tmpl.set("POSTED_NEWS", postedNewsPerHour);
   1.118 +      tmpl.set("GATEWAYED_NEWS", gatewayedNewsPerHour);
   1.119 +      tmpl.set("FEEDED_NEWS", feededNewsPerHour);
   1.120 +      tmpl.set("TITLE", "Overview");
   1.121 +
   1.122 +      resp.getWriter().println(tmpl.toString());
   1.123 +      resp.getWriter().flush();
   1.124 +      resp.setStatus(HttpServletResponse.SC_OK);
   1.125 +
   1.126 +      disconnectFromNewsserver();
   1.127 +    }
   1.128 +  }
   1.129 +  
   1.130 +}