org/sonews/web/SonewsPeerServlet.java
changeset 2 1090e2141798
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/web/SonewsPeerServlet.java	Wed Jul 01 10:48:22 2009 +0200
     1.3 @@ -0,0 +1,95 @@
     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 java.util.HashSet;
    1.26 +import javax.servlet.http.HttpServletRequest;
    1.27 +import javax.servlet.http.HttpServletResponse;
    1.28 +import org.sonews.util.StringTemplate;
    1.29 +
    1.30 +/**
    1.31 + * Servlet that shows the Peers and the Peering Rules.
    1.32 + * @author Christian Lins
    1.33 + * @since sonews/0.5.0
    1.34 + */
    1.35 +public class SonewsPeerServlet extends AbstractSonewsServlet
    1.36 +{
    1.37 +
    1.38 +  private static final long serialVersionUID = 245345346356L;
    1.39 +  
    1.40 +  @Override
    1.41 +  public void doGet(HttpServletRequest req, HttpServletResponse resp)
    1.42 +    throws IOException
    1.43 +  {
    1.44 +    synchronized(this)
    1.45 +    {
    1.46 +      connectToNewsserver();
    1.47 +      StringTemplate tmpl = getTemplate("SonewsPeerServlet.tmpl");
    1.48 +
    1.49 +      // Read peering rules from newsserver
    1.50 +      printlnToNewsserver("XDAEMON LIST PEERINGRULES");
    1.51 +      String line = readlnFromNewsserver();
    1.52 +      if(!line.startsWith("200 "))
    1.53 +      {
    1.54 +        throw new IOException("Unexpected reply: " + line);
    1.55 +      }
    1.56 +
    1.57 +      // Create FEED_RULES String
    1.58 +      HashSet<String> peers        = new HashSet<String>();
    1.59 +      StringBuilder   feedRulesStr = new StringBuilder();
    1.60 +      for(;;)
    1.61 +      {
    1.62 +        line = readlnFromNewsserver();
    1.63 +        if(line.equals("."))
    1.64 +        {
    1.65 +          break;
    1.66 +        }
    1.67 +        else
    1.68 +        {
    1.69 +          feedRulesStr.append(line);
    1.70 +          feedRulesStr.append("<br/>");
    1.71 +
    1.72 +          String[] lineChunks = line.split(" ");
    1.73 +          peers.add(lineChunks[1]);
    1.74 +        }
    1.75 +      }
    1.76 +
    1.77 +      // Create PEERS string
    1.78 +      StringBuilder peersStr = new StringBuilder();
    1.79 +      for(String peer : peers)
    1.80 +      {
    1.81 +        peersStr.append(peer);
    1.82 +        peersStr.append("<br/>");
    1.83 +      }
    1.84 +
    1.85 +      // Set server name
    1.86 +      tmpl.set("PEERS", peersStr.toString());
    1.87 +      tmpl.set("PEERING_RULES", feedRulesStr.toString());
    1.88 +      tmpl.set("SERVERNAME", hello.split(" ")[2]);
    1.89 +      tmpl.set("TITLE", "Peers");
    1.90 +
    1.91 +      resp.getWriter().println(tmpl.toString());
    1.92 +      resp.getWriter().flush();
    1.93 +      resp.setStatus(HttpServletResponse.SC_OK);
    1.94 +      disconnectFromNewsserver();
    1.95 +    }
    1.96 +  }
    1.97 +  
    1.98 +}