org/sonews/web/SonewsConfigServlet.java
changeset 1 6fceb66e1ad7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/web/SonewsConfigServlet.java	Fri Jun 26 16:48:50 2009 +0200
     1.3 @@ -0,0 +1,239 @@
     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.ArrayList;
    1.26 +import java.util.List;
    1.27 +import java.util.Set;
    1.28 +import javax.servlet.http.HttpServletRequest;
    1.29 +import javax.servlet.http.HttpServletResponse;
    1.30 +import org.sonews.util.StringTemplate;
    1.31 +import org.sonews.util.io.Resource;
    1.32 +
    1.33 +/**
    1.34 + * Servlet providing a configuration web interface.
    1.35 + * @author Christian Lins
    1.36 + * @since sonews/0.5.0
    1.37 + */
    1.38 +public class SonewsConfigServlet extends AbstractSonewsServlet
    1.39 +{
    1.40 +  
    1.41 +  private static final long serialVersionUID = 2432543253L;
    1.42 +  
    1.43 +  @Override
    1.44 +  public void doGet(HttpServletRequest req, HttpServletResponse resp)
    1.45 +    throws IOException
    1.46 +  {
    1.47 +    synchronized(this)
    1.48 +    {
    1.49 +      connectToNewsserver();
    1.50 +      String which = req.getParameter("which");
    1.51 +
    1.52 +      if(which != null && which.equals("config"))
    1.53 +      {
    1.54 +        whichConfig(req, resp);
    1.55 +      }
    1.56 +      else if(which != null && which.equals("groupadd"))
    1.57 +      {
    1.58 +        whichGroupAdd(req, resp);
    1.59 +      }
    1.60 +      else if(which != null && which.equals("groupdelete"))
    1.61 +      {
    1.62 +        whichGroupDelete(req, resp);
    1.63 +      }
    1.64 +      else
    1.65 +      {
    1.66 +        whichNone(req, resp);
    1.67 +      }
    1.68 +
    1.69 +      disconnectFromNewsserver();
    1.70 +    }
    1.71 +  }
    1.72 +  
    1.73 +  private void whichConfig(HttpServletRequest req, HttpServletResponse resp)
    1.74 +    throws IOException
    1.75 +  {
    1.76 +    StringBuilder keys = new StringBuilder();
    1.77 +
    1.78 +    Set pnames = req.getParameterMap().keySet();
    1.79 +    for(Object obj : pnames)
    1.80 +    {
    1.81 +      String pname = (String)obj;
    1.82 +      if(pname.startsWith("configkey:"))
    1.83 +      {
    1.84 +        String value = req.getParameter(pname);
    1.85 +        String key   = pname.split(":")[1];
    1.86 +        if(!value.equals("<not set>"))
    1.87 +        {
    1.88 +          printlnToNewsserver("XDAEMON SET " + key + " " + value);
    1.89 +          readlnFromNewsserver();
    1.90 +          
    1.91 +          keys.append(key); 
    1.92 +          keys.append("<br/>");
    1.93 +        }
    1.94 +      }
    1.95 +    }
    1.96 +    
    1.97 +    StringTemplate tmpl = getTemplate("ConfigUpdated.tmpl");
    1.98 +    
    1.99 +    tmpl.set("UPDATED_KEYS", keys.toString());
   1.100 +    
   1.101 +    resp.setStatus(HttpServletResponse.SC_OK);
   1.102 +    resp.getWriter().println(tmpl.toString());
   1.103 +    resp.getWriter().flush();
   1.104 +  }
   1.105 +  
   1.106 +  private void whichGroupAdd(HttpServletRequest req, HttpServletResponse resp)
   1.107 +    throws IOException
   1.108 +  {
   1.109 +    String[] groupnames = req.getParameter("groups").split("\n");
   1.110 +    
   1.111 +    for(String groupname : groupnames)
   1.112 +    {
   1.113 +      groupname = groupname.trim();
   1.114 +      if(groupname.equals(""))
   1.115 +      {
   1.116 +        continue;
   1.117 +      }
   1.118 +
   1.119 +      printlnToNewsserver("XDAEMON GROUPADD " + groupname + " 0");
   1.120 +      String line = readlnFromNewsserver();
   1.121 +      if(!line.startsWith("200 "))
   1.122 +      {
   1.123 +        System.out.println("Warning " + groupname + " probably not created!");
   1.124 +      }
   1.125 +    }
   1.126 +    
   1.127 +    StringTemplate tmpl = getTemplate("GroupAdded.tmpl");
   1.128 +    
   1.129 +    tmpl.set("GROUP", req.getParameter("groups"));
   1.130 +    
   1.131 +    resp.setStatus(HttpServletResponse.SC_OK);
   1.132 +    resp.getWriter().println(tmpl.toString());
   1.133 +    resp.getWriter().flush();
   1.134 +  }
   1.135 +  
   1.136 +  private void whichGroupDelete(HttpServletRequest req, HttpServletResponse resp)
   1.137 +    throws IOException
   1.138 +  {
   1.139 +    String groupname = req.getParameter("group");
   1.140 +    printlnToNewsserver("XDAEMON GROUPDEL " + groupname);
   1.141 +    String line = readlnFromNewsserver();
   1.142 +    if(!line.startsWith("200 "))
   1.143 +      throw new IOException(line);
   1.144 +    
   1.145 +    StringTemplate tmpl = getTemplate("GroupDeleted.tmpl");
   1.146 +    
   1.147 +    tmpl.set("GROUP", groupname);
   1.148 +    
   1.149 +    resp.setStatus(HttpServletResponse.SC_OK);
   1.150 +    resp.getWriter().println(tmpl.toString());
   1.151 +    resp.getWriter().flush();
   1.152 +  }
   1.153 +  
   1.154 +  private void whichNone(HttpServletRequest req, HttpServletResponse resp)
   1.155 +    throws IOException
   1.156 +  {
   1.157 +    StringTemplate tmpl = getTemplate("SonewsConfigServlet.tmpl");
   1.158 +    
   1.159 +    // Retrieve config keys from server
   1.160 +    List<String> configKeys = new ArrayList<String>();
   1.161 +    printlnToNewsserver("XDAEMON LIST CONFIGKEYS");
   1.162 +    String line = readlnFromNewsserver();
   1.163 +    if(!line.startsWith("200 "))
   1.164 +      throw new IOException("XDAEMON command not supported!");
   1.165 +    for(;;)
   1.166 +    {
   1.167 +      line = readlnFromNewsserver();
   1.168 +      if(line.equals("."))
   1.169 +        break;
   1.170 +      else
   1.171 +        configKeys.add(line);
   1.172 +    }
   1.173 +    
   1.174 +    // Construct config table
   1.175 +    StringBuilder strb = new StringBuilder();
   1.176 +    for(String key : configKeys)
   1.177 +    {
   1.178 +      strb.append("<tr><td><code>");
   1.179 +      strb.append(key);
   1.180 +      strb.append("</code></td><td>");
   1.181 +      
   1.182 +      // Retrieve config value from server
   1.183 +      String value = "<not set>";
   1.184 +      printlnToNewsserver("XDAEMON GET " + key);
   1.185 +      line = readlnFromNewsserver();
   1.186 +      if(line.startsWith("200 "))
   1.187 +      {
   1.188 +        value = readlnFromNewsserver();
   1.189 +        readlnFromNewsserver(); // Read the "."
   1.190 +      }
   1.191 +      
   1.192 +      strb.append("<input type=text name=\"configkey:");
   1.193 +      strb.append(key);
   1.194 +      strb.append("\" value=\"");
   1.195 +      strb.append(value);
   1.196 +      strb.append("\"/></td></tr>");
   1.197 +    }
   1.198 +    tmpl.set("CONFIG", strb.toString());
   1.199 +    
   1.200 +    // Retrieve served newsgroup names from server
   1.201 +    List<String> groups = new ArrayList<String>();
   1.202 +    printlnToNewsserver("LIST");
   1.203 +    line = readlnFromNewsserver();
   1.204 +    if(line.startsWith("215 "))
   1.205 +    {
   1.206 +      for(;;)
   1.207 +      {
   1.208 +        line = readlnFromNewsserver();
   1.209 +        if(line.equals("."))
   1.210 +        {
   1.211 +          break;
   1.212 +        }
   1.213 +        else
   1.214 +        {
   1.215 +          groups.add(line.split(" ")[0]);
   1.216 +        }
   1.217 +      }
   1.218 +    }
   1.219 +    else
   1.220 +      throw new IOException("Error issuing LIST command!");
   1.221 +    
   1.222 +    // Construct groups list
   1.223 +    StringTemplate tmplGroupList = new StringTemplate(
   1.224 +      Resource.getAsString("org/sonews/web/tmpl/GroupList.tmpl", true));
   1.225 +    strb = new StringBuilder();
   1.226 +    for(String group : groups)
   1.227 +    {
   1.228 +      tmplGroupList.set("GROUPNAME", group);
   1.229 +      strb.append(tmplGroupList.toString());
   1.230 +    }
   1.231 +    tmpl.set("GROUP", strb.toString());
   1.232 +    
   1.233 +    // Set server name
   1.234 +    tmpl.set("SERVERNAME", hello.split(" ")[2]);
   1.235 +    tmpl.set("TITLE", "Configuration");
   1.236 +    
   1.237 +    resp.getWriter().println(tmpl.toString());
   1.238 +    resp.getWriter().flush();
   1.239 +    resp.setStatus(HttpServletResponse.SC_OK);
   1.240 +  }
   1.241 +
   1.242 +}