org/sonews/web/SonewsConfigServlet.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.ArrayList;
    23 import java.util.List;
    24 import java.util.Set;
    25 import javax.servlet.http.HttpServletRequest;
    26 import javax.servlet.http.HttpServletResponse;
    27 import org.sonews.util.StringTemplate;
    28 import org.sonews.util.io.Resource;
    29 
    30 /**
    31  * Servlet providing a configuration web interface.
    32  * @author Christian Lins
    33  * @since sonews/0.5.0
    34  */
    35 public class SonewsConfigServlet extends AbstractSonewsServlet
    36 {
    37   
    38   private static final long serialVersionUID = 2432543253L;
    39   
    40   @Override
    41   public void doGet(HttpServletRequest req, HttpServletResponse resp)
    42     throws IOException
    43   {
    44     synchronized(this)
    45     {
    46       connectToNewsserver();
    47       String which = req.getParameter("which");
    48 
    49       if(which != null && which.equals("config"))
    50       {
    51         whichConfig(req, resp);
    52       }
    53       else if(which != null && which.equals("groupadd"))
    54       {
    55         whichGroupAdd(req, resp);
    56       }
    57       else if(which != null && which.equals("groupdelete"))
    58       {
    59         whichGroupDelete(req, resp);
    60       }
    61       else
    62       {
    63         whichNone(req, resp);
    64       }
    65 
    66       disconnectFromNewsserver();
    67     }
    68   }
    69   
    70   private void whichConfig(HttpServletRequest req, HttpServletResponse resp)
    71     throws IOException
    72   {
    73     StringBuilder keys = new StringBuilder();
    74 
    75     Set pnames = req.getParameterMap().keySet();
    76     for(Object obj : pnames)
    77     {
    78       String pname = (String)obj;
    79       if(pname.startsWith("configkey:"))
    80       {
    81         String value = req.getParameter(pname);
    82         String key   = pname.split(":")[1];
    83         if(!value.equals("<not set>"))
    84         {
    85           printlnToNewsserver("XDAEMON SET " + key + " " + value);
    86           readlnFromNewsserver();
    87           
    88           keys.append(key); 
    89           keys.append("<br/>");
    90         }
    91       }
    92     }
    93     
    94     StringTemplate tmpl = getTemplate("ConfigUpdated.tmpl");
    95     
    96     tmpl.set("UPDATED_KEYS", keys.toString());
    97     
    98     resp.setStatus(HttpServletResponse.SC_OK);
    99     resp.getWriter().println(tmpl.toString());
   100     resp.getWriter().flush();
   101   }
   102   
   103   private void whichGroupAdd(HttpServletRequest req, HttpServletResponse resp)
   104     throws IOException
   105   {
   106     String[] groupnames = req.getParameter("groups").split("\n");
   107     
   108     for(String groupname : groupnames)
   109     {
   110       groupname = groupname.trim();
   111       if(groupname.equals(""))
   112       {
   113         continue;
   114       }
   115 
   116       printlnToNewsserver("XDAEMON GROUPADD " + groupname + " 0");
   117       String line = readlnFromNewsserver();
   118       if(!line.startsWith("200 "))
   119       {
   120         System.out.println("Warning " + groupname + " probably not created!");
   121       }
   122     }
   123     
   124     StringTemplate tmpl = getTemplate("GroupAdded.tmpl");
   125     
   126     tmpl.set("GROUP", req.getParameter("groups"));
   127     
   128     resp.setStatus(HttpServletResponse.SC_OK);
   129     resp.getWriter().println(tmpl.toString());
   130     resp.getWriter().flush();
   131   }
   132   
   133   private void whichGroupDelete(HttpServletRequest req, HttpServletResponse resp)
   134     throws IOException
   135   {
   136     String groupname = req.getParameter("group");
   137     printlnToNewsserver("XDAEMON GROUPDEL " + groupname);
   138     String line = readlnFromNewsserver();
   139     if(!line.startsWith("200 "))
   140       throw new IOException(line);
   141     
   142     StringTemplate tmpl = getTemplate("GroupDeleted.tmpl");
   143     
   144     tmpl.set("GROUP", groupname);
   145     
   146     resp.setStatus(HttpServletResponse.SC_OK);
   147     resp.getWriter().println(tmpl.toString());
   148     resp.getWriter().flush();
   149   }
   150   
   151   private void whichNone(HttpServletRequest req, HttpServletResponse resp)
   152     throws IOException
   153   {
   154     StringTemplate tmpl = getTemplate("SonewsConfigServlet.tmpl");
   155     
   156     // Retrieve config keys from server
   157     List<String> configKeys = new ArrayList<String>();
   158     printlnToNewsserver("XDAEMON LIST CONFIGKEYS");
   159     String line = readlnFromNewsserver();
   160     if(!line.startsWith("200 "))
   161       throw new IOException("XDAEMON command not supported!");
   162     for(;;)
   163     {
   164       line = readlnFromNewsserver();
   165       if(line.equals("."))
   166         break;
   167       else
   168         configKeys.add(line);
   169     }
   170     
   171     // Construct config table
   172     StringBuilder strb = new StringBuilder();
   173     for(String key : configKeys)
   174     {
   175       strb.append("<tr><td><code>");
   176       strb.append(key);
   177       strb.append("</code></td><td>");
   178       
   179       // Retrieve config value from server
   180       String value = "<not set>";
   181       printlnToNewsserver("XDAEMON GET " + key);
   182       line = readlnFromNewsserver();
   183       if(line.startsWith("200 "))
   184       {
   185         value = readlnFromNewsserver();
   186         readlnFromNewsserver(); // Read the "."
   187       }
   188       
   189       strb.append("<input type=text name=\"configkey:");
   190       strb.append(key);
   191       strb.append("\" value=\"");
   192       strb.append(value);
   193       strb.append("\"/></td></tr>");
   194     }
   195     tmpl.set("CONFIG", strb.toString());
   196     
   197     // Retrieve served newsgroup names from server
   198     List<String> groups = new ArrayList<String>();
   199     printlnToNewsserver("LIST");
   200     line = readlnFromNewsserver();
   201     if(line.startsWith("215 "))
   202     {
   203       for(;;)
   204       {
   205         line = readlnFromNewsserver();
   206         if(line.equals("."))
   207         {
   208           break;
   209         }
   210         else
   211         {
   212           groups.add(line.split(" ")[0]);
   213         }
   214       }
   215     }
   216     else
   217       throw new IOException("Error issuing LIST command!");
   218     
   219     // Construct groups list
   220     StringTemplate tmplGroupList = new StringTemplate(
   221       Resource.getAsString("org/sonews/web/tmpl/GroupList.tmpl", true));
   222     strb = new StringBuilder();
   223     for(String group : groups)
   224     {
   225       tmplGroupList.set("GROUPNAME", group);
   226       strb.append(tmplGroupList.toString());
   227     }
   228     tmpl.set("GROUP", strb.toString());
   229     
   230     // Set server name
   231     tmpl.set("SERVERNAME", hello.split(" ")[2]);
   232     tmpl.set("TITLE", "Configuration");
   233     
   234     resp.getWriter().println(tmpl.toString());
   235     resp.getWriter().flush();
   236     resp.setStatus(HttpServletResponse.SC_OK);
   237   }
   238 
   239 }