org/sonews/daemon/command/XDaemonCommand.java
author cli
Mon Aug 24 13:00:05 2009 +0200 (2009-08-24)
changeset 20 6ae5e4f8329b
parent 3 2fdc9cc89502
child 21 4b2c8bedb094
permissions -rw-r--r--
Add stubs for org.sonews.acl and add method "impliedCapability" for org.sonews.command.Command interface.
     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.daemon.command;
    20 
    21 import java.io.IOException;
    22 import java.net.InetSocketAddress;
    23 import java.util.List;
    24 import org.sonews.config.Config;
    25 import org.sonews.daemon.NNTPConnection;
    26 import org.sonews.storage.StorageBackendException;
    27 import org.sonews.storage.StorageManager;
    28 import org.sonews.feed.FeedManager;
    29 import org.sonews.feed.Subscription;
    30 import org.sonews.storage.Group;
    31 import org.sonews.util.Stats;
    32 
    33 /**
    34  * The XDAEMON command allows a client to get/set properties of the
    35  * running server daemon. Only locally connected clients are allowed to
    36  * use this command.
    37  * The restriction to localhost connection can be suppressed by overriding
    38  * the sonews.xdaemon.host bootstrap config property.
    39  * @author Christian Lins
    40  * @since sonews/0.5.0
    41  */
    42 public class XDaemonCommand implements Command
    43 {
    44 
    45   @Override
    46   public String[] getSupportedCommandStrings()
    47   {
    48     return new String[]{"XDAEMON"};
    49   }
    50 
    51   @Override
    52   public boolean hasFinished()
    53   {
    54     return true;
    55   }
    56 
    57   @Override
    58   public String impliedCapability()
    59   {
    60     return null;
    61   }
    62 
    63   @Override
    64   public boolean isStateful()
    65   {
    66     return false;
    67   }
    68 
    69   // TODO: Refactor this method to reduce complexity!
    70   @Override
    71   public void processLine(NNTPConnection conn, String line, byte[] raw)
    72     throws IOException, StorageBackendException
    73   {
    74     InetSocketAddress addr = (InetSocketAddress)conn.getSocketChannel().socket()
    75       .getRemoteSocketAddress();
    76     if(addr.getHostName().equals(
    77       Config.inst().get(Config.XDAEMON_HOST, "localhost")))
    78     {
    79       String[] commands = line.split(" ", 4);
    80       if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST"))
    81       {
    82         if(commands[2].equalsIgnoreCase("CONFIGKEYS"))
    83         {
    84           conn.println("100 list of available config keys follows");
    85           for(String key : Config.AVAILABLE_KEYS)
    86           {
    87             conn.println(key);
    88           }
    89           conn.println(".");
    90         }
    91         else if(commands[2].equalsIgnoreCase("PEERINGRULES"))
    92         {
    93           List<Subscription> pull = 
    94             StorageManager.current().getSubscriptions(FeedManager.TYPE_PULL);
    95           List<Subscription> push =
    96             StorageManager.current().getSubscriptions(FeedManager.TYPE_PUSH);
    97           conn.println("100 list of peering rules follows");
    98           for(Subscription sub : pull)
    99           {
   100             conn.println("PULL " + sub.getHost() + ":" + sub.getPort()
   101               + " " + sub.getGroup());
   102           }
   103           for(Subscription sub : push)
   104           {
   105             conn.println("PUSH " + sub.getHost() + ":" + sub.getPort()
   106               + " " + sub.getGroup());
   107           }
   108           conn.println(".");
   109         }
   110         else
   111         {
   112           conn.println("401 unknown sub command");
   113         }
   114       }
   115       else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE"))
   116       {
   117         StorageManager.current().delete(commands[2]);
   118         conn.println("200 article " + commands[2] + " deleted");
   119       }
   120       else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD"))
   121       {
   122         StorageManager.current().addGroup(commands[2], Integer.parseInt(commands[3]));
   123         conn.println("200 group " + commands[2] + " created");
   124       }
   125       else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL"))
   126       {
   127         Group group = StorageManager.current().getGroup(commands[2]);
   128         if(group == null)
   129         {
   130           conn.println("400 group not found");
   131         }
   132         else
   133         {
   134           group.setFlag(Group.DELETED);
   135           group.update();
   136           conn.println("200 group " + commands[2] + " marked as deleted");
   137         }
   138       }
   139       else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET"))
   140       {
   141         String key = commands[2];
   142         String val = commands[3];
   143         Config.inst().set(key, val);
   144         conn.println("200 new config value set");
   145       }
   146       else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET"))
   147       {
   148         String key = commands[2];
   149         String val = Config.inst().get(key, null);
   150         if(val != null)
   151         {
   152           conn.println("100 config value for " + key + " follows");
   153           conn.println(val);
   154           conn.println(".");
   155         }
   156         else
   157         {
   158           conn.println("400 config value not set");
   159         }
   160       }
   161       else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG"))
   162       {
   163         Group group = null;
   164         if(commands.length > 3)
   165         {
   166           group = Group.getByName(commands[3]);
   167         }
   168 
   169         if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS"))
   170         {
   171           conn.println("100 number of connections follow");
   172           conn.println(Integer.toString(Stats.getInstance().connectedClients()));
   173           conn.println(".");
   174         }
   175         else if(commands[2].equalsIgnoreCase("POSTED_NEWS"))
   176         {
   177           conn.println("100 hourly numbers of posted news yesterday");
   178           for(int n = 0; n < 24; n++)
   179           {
   180             conn.println(n + " " + Stats.getInstance()
   181               .getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
   182           }
   183           conn.println(".");
   184         }
   185         else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS"))
   186         {
   187           conn.println("100 hourly numbers of gatewayed news yesterday");
   188           for(int n = 0; n < 24; n++)
   189           {
   190             conn.println(n + " " + Stats.getInstance()
   191               .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
   192           }
   193           conn.println(".");
   194         }
   195         else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS"))
   196         {
   197           conn.println("100 hourly numbers of news transmitted to peers yesterday");
   198           for(int n = 0; n < 24; n++)
   199           {
   200             conn.println(n + " " + Stats.getInstance()
   201               .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
   202           }
   203           conn.println(".");
   204         }
   205         else if(commands[2].equalsIgnoreCase("HOSTED_NEWS"))
   206         {
   207           conn.println("100 number of overall hosted news");
   208           conn.println(Integer.toString(Stats.getInstance().getNumberOfNews()));
   209           conn.println(".");
   210         }
   211         else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS"))
   212         {
   213           conn.println("100 number of hosted groups");
   214           conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
   215           conn.println(".");
   216         }
   217         else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR"))
   218         {
   219           conn.println("100 posted news per hour");
   220           conn.println(Double.toString(Stats.getInstance().postedPerHour(-1)));
   221           conn.println(".");
   222         }
   223         else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR"))
   224         {
   225           conn.println("100 feeded news per hour");
   226           conn.println(Double.toString(Stats.getInstance().feededPerHour(-1)));
   227           conn.println(".");
   228         }
   229         else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR"))
   230         {
   231           conn.println("100 gatewayed news per hour");
   232           conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
   233           conn.println(".");
   234         }
   235         else
   236         {
   237           conn.println("401 unknown sub command");
   238         }
   239       }
   240       else
   241       {
   242         conn.println("400 invalid command usage");
   243       }
   244     }
   245     else
   246     {
   247       conn.println("501 not allowed");
   248     }
   249   }
   250   
   251 }