org/sonews/web/SonewsServlet.java
author chris <chris@marvin>
Fri Jun 26 16:48:50 2009 +0200 (2009-06-26)
changeset 1 6fceb66e1ad7
permissions -rw-r--r--
Hooray... sonews/0.5.0 final

HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Remove all lines to abort the collapse operation.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.web;
    20 
    21 import java.io.IOException;
    22 import javax.servlet.http.HttpServletRequest;
    23 import javax.servlet.http.HttpServletResponse;
    24 import org.sonews.daemon.Main;
    25 import org.sonews.util.StringTemplate;
    26 
    27 /**
    28  * Main sonews webpage servlet.
    29  * @author Christian Lins
    30  * @since sonews/0.5.0
    31  */
    32 public class SonewsServlet extends AbstractSonewsServlet
    33 {
    34 
    35   private static final long serialVersionUID = 2392837459834L;
    36 
    37   @Override
    38   public void doGet(HttpServletRequest res, HttpServletResponse resp)
    39     throws IOException
    40   {
    41     synchronized(this)
    42     {
    43       connectToNewsserver();
    44 
    45       String line;
    46       int    connectedClients = 0;
    47       int    hostedGroups     = 0;
    48       int    hostedNews       = 0;
    49 
    50       printlnToNewsserver("XDAEMON LOG CONNECTED_CLIENTS");
    51 
    52       line = readlnFromNewsserver();
    53       if(!line.startsWith("200 "))
    54       {
    55         throw new IOException("XDAEMON command not allowed by server");
    56       }
    57       line = readlnFromNewsserver();
    58       connectedClients = Integer.parseInt(line);
    59       line = readlnFromNewsserver(); // Read the "."
    60 
    61       printlnToNewsserver("XDAEMON LOG HOSTED_NEWS");
    62       line = readlnFromNewsserver();
    63       if(!line.startsWith("200 "))
    64       {
    65         throw new IOException("XDAEMON command not allowed by server");
    66       }
    67       line = readlnFromNewsserver();
    68       hostedNews = Integer.parseInt(line);
    69       line = readlnFromNewsserver(); // read the "."
    70 
    71       printlnToNewsserver("XDAEMON LOG HOSTED_GROUPS");
    72       line = readlnFromNewsserver();
    73       if(!line.startsWith("200 "))
    74       {
    75         throw new IOException("XDAEMON command not allowed by server");
    76       }
    77       line = readlnFromNewsserver();
    78       hostedGroups = Integer.parseInt(line);
    79       line = readlnFromNewsserver(); // read the "."
    80 
    81       printlnToNewsserver("XDAEMON LOG POSTED_NEWS_PER_HOUR");
    82       line = readlnFromNewsserver();
    83       if(!line.startsWith("200 "))
    84       {
    85         throw new IOException("XDAEMON command not allowed by server");
    86       }
    87       String postedNewsPerHour = readlnFromNewsserver();
    88       readlnFromNewsserver();
    89 
    90       printlnToNewsserver("XDAEMON LOG GATEWAYED_NEWS_PER_HOUR");
    91       line = readlnFromNewsserver();
    92       if(!line.startsWith("200 "))
    93       {
    94         throw new IOException("XDAEMON command not allowed by server");
    95       }
    96       String gatewayedNewsPerHour = readlnFromNewsserver();
    97       line = readlnFromNewsserver();
    98 
    99       printlnToNewsserver("XDAEMON LOG FEEDED_NEWS_PER_HOUR");
   100       line = readlnFromNewsserver();
   101       if(!line.startsWith("200 "))
   102       {
   103         throw new IOException("XDAEMON command not allowed by server");
   104       }
   105       String feededNewsPerHour = readlnFromNewsserver();
   106       line = readlnFromNewsserver();
   107 
   108       StringTemplate tmpl = getTemplate("SonewsServlet.tmpl");
   109       tmpl.set("SERVERNAME", hello.split(" ")[2]);
   110       tmpl.set("STARTDATE", Main.STARTDATE);
   111       tmpl.set("ACTIVE_CONNECTIONS", connectedClients);
   112       tmpl.set("STORED_NEWS", hostedNews);
   113       tmpl.set("SERVED_NEWSGROUPS", hostedGroups);
   114       tmpl.set("POSTED_NEWS", postedNewsPerHour);
   115       tmpl.set("GATEWAYED_NEWS", gatewayedNewsPerHour);
   116       tmpl.set("FEEDED_NEWS", feededNewsPerHour);
   117       tmpl.set("TITLE", "Overview");
   118 
   119       resp.getWriter().println(tmpl.toString());
   120       resp.getWriter().flush();
   121       resp.setStatus(HttpServletResponse.SC_OK);
   122 
   123       disconnectFromNewsserver();
   124     }
   125   }
   126   
   127 }