org/sonews/web/SonewsPeerServlet.java
author chris <chris@marvin>
Wed, 01 Jul 2009 10:48:22 +0200
changeset 2 1090e2141798
parent 1 6fceb66e1ad7
permissions -rw-r--r--
sonews/0.5.1 fixes merged
     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 java.util.HashSet;
    23 import javax.servlet.http.HttpServletRequest;
    24 import javax.servlet.http.HttpServletResponse;
    25 import org.sonews.util.StringTemplate;
    26 
    27 /**
    28  * Servlet that shows the Peers and the Peering Rules.
    29  * @author Christian Lins
    30  * @since sonews/0.5.0
    31  */
    32 public class SonewsPeerServlet extends AbstractSonewsServlet
    33 {
    34 
    35   private static final long serialVersionUID = 245345346356L;
    36   
    37   @Override
    38   public void doGet(HttpServletRequest req, HttpServletResponse resp)
    39     throws IOException
    40   {
    41     synchronized(this)
    42     {
    43       connectToNewsserver();
    44       StringTemplate tmpl = getTemplate("SonewsPeerServlet.tmpl");
    45 
    46       // Read peering rules from newsserver
    47       printlnToNewsserver("XDAEMON LIST PEERINGRULES");
    48       String line = readlnFromNewsserver();
    49       if(!line.startsWith("200 "))
    50       {
    51         throw new IOException("Unexpected reply: " + line);
    52       }
    53 
    54       // Create FEED_RULES String
    55       HashSet<String> peers        = new HashSet<String>();
    56       StringBuilder   feedRulesStr = new StringBuilder();
    57       for(;;)
    58       {
    59         line = readlnFromNewsserver();
    60         if(line.equals("."))
    61         {
    62           break;
    63         }
    64         else
    65         {
    66           feedRulesStr.append(line);
    67           feedRulesStr.append("<br/>");
    68 
    69           String[] lineChunks = line.split(" ");
    70           peers.add(lineChunks[1]);
    71         }
    72       }
    73 
    74       // Create PEERS string
    75       StringBuilder peersStr = new StringBuilder();
    76       for(String peer : peers)
    77       {
    78         peersStr.append(peer);
    79         peersStr.append("<br/>");
    80       }
    81 
    82       // Set server name
    83       tmpl.set("PEERS", peersStr.toString());
    84       tmpl.set("PEERING_RULES", feedRulesStr.toString());
    85       tmpl.set("SERVERNAME", hello.split(" ")[2]);
    86       tmpl.set("TITLE", "Peers");
    87 
    88       resp.getWriter().println(tmpl.toString());
    89       resp.getWriter().flush();
    90       resp.setStatus(HttpServletResponse.SC_OK);
    91       disconnectFromNewsserver();
    92     }
    93   }
    94   
    95 }