org/sonews/web/SonewsConfigServlet.java
author chris <chris@marvin>
Fri, 26 Jun 2009 16:48:50 +0200
changeset 1 6fceb66e1ad7
permissions -rw-r--r--
Hooray... sonews/0.5.0 final

HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Remove all lines to abort the collapse operation.
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
chris@1
    19
package org.sonews.web;
chris@1
    20
chris@1
    21
import java.io.IOException;
chris@1
    22
import java.util.ArrayList;
chris@1
    23
import java.util.List;
chris@1
    24
import java.util.Set;
chris@1
    25
import javax.servlet.http.HttpServletRequest;
chris@1
    26
import javax.servlet.http.HttpServletResponse;
chris@1
    27
import org.sonews.util.StringTemplate;
chris@1
    28
import org.sonews.util.io.Resource;
chris@1
    29
chris@1
    30
/**
chris@1
    31
 * Servlet providing a configuration web interface.
chris@1
    32
 * @author Christian Lins
chris@1
    33
 * @since sonews/0.5.0
chris@1
    34
 */
chris@1
    35
public class SonewsConfigServlet extends AbstractSonewsServlet
chris@1
    36
{
chris@1
    37
  
chris@1
    38
  private static final long serialVersionUID = 2432543253L;
chris@1
    39
  
chris@1
    40
  @Override
chris@1
    41
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
chris@1
    42
    throws IOException
chris@1
    43
  {
chris@1
    44
    synchronized(this)
chris@1
    45
    {
chris@1
    46
      connectToNewsserver();
chris@1
    47
      String which = req.getParameter("which");
chris@1
    48
chris@1
    49
      if(which != null && which.equals("config"))
chris@1
    50
      {
chris@1
    51
        whichConfig(req, resp);
chris@1
    52
      }
chris@1
    53
      else if(which != null && which.equals("groupadd"))
chris@1
    54
      {
chris@1
    55
        whichGroupAdd(req, resp);
chris@1
    56
      }
chris@1
    57
      else if(which != null && which.equals("groupdelete"))
chris@1
    58
      {
chris@1
    59
        whichGroupDelete(req, resp);
chris@1
    60
      }
chris@1
    61
      else
chris@1
    62
      {
chris@1
    63
        whichNone(req, resp);
chris@1
    64
      }
chris@1
    65
chris@1
    66
      disconnectFromNewsserver();
chris@1
    67
    }
chris@1
    68
  }
chris@1
    69
  
chris@1
    70
  private void whichConfig(HttpServletRequest req, HttpServletResponse resp)
chris@1
    71
    throws IOException
chris@1
    72
  {
chris@1
    73
    StringBuilder keys = new StringBuilder();
chris@1
    74
chris@1
    75
    Set pnames = req.getParameterMap().keySet();
chris@1
    76
    for(Object obj : pnames)
chris@1
    77
    {
chris@1
    78
      String pname = (String)obj;
chris@1
    79
      if(pname.startsWith("configkey:"))
chris@1
    80
      {
chris@1
    81
        String value = req.getParameter(pname);
chris@1
    82
        String key   = pname.split(":")[1];
chris@1
    83
        if(!value.equals("<not set>"))
chris@1
    84
        {
chris@1
    85
          printlnToNewsserver("XDAEMON SET " + key + " " + value);
chris@1
    86
          readlnFromNewsserver();
chris@1
    87
          
chris@1
    88
          keys.append(key); 
chris@1
    89
          keys.append("<br/>");
chris@1
    90
        }
chris@1
    91
      }
chris@1
    92
    }
chris@1
    93
    
chris@1
    94
    StringTemplate tmpl = getTemplate("ConfigUpdated.tmpl");
chris@1
    95
    
chris@1
    96
    tmpl.set("UPDATED_KEYS", keys.toString());
chris@1
    97
    
chris@1
    98
    resp.setStatus(HttpServletResponse.SC_OK);
chris@1
    99
    resp.getWriter().println(tmpl.toString());
chris@1
   100
    resp.getWriter().flush();
chris@1
   101
  }
chris@1
   102
  
chris@1
   103
  private void whichGroupAdd(HttpServletRequest req, HttpServletResponse resp)
chris@1
   104
    throws IOException
chris@1
   105
  {
chris@1
   106
    String[] groupnames = req.getParameter("groups").split("\n");
chris@1
   107
    
chris@1
   108
    for(String groupname : groupnames)
chris@1
   109
    {
chris@1
   110
      groupname = groupname.trim();
chris@1
   111
      if(groupname.equals(""))
chris@1
   112
      {
chris@1
   113
        continue;
chris@1
   114
      }
chris@1
   115
chris@1
   116
      printlnToNewsserver("XDAEMON GROUPADD " + groupname + " 0");
chris@1
   117
      String line = readlnFromNewsserver();
chris@1
   118
      if(!line.startsWith("200 "))
chris@1
   119
      {
chris@1
   120
        System.out.println("Warning " + groupname + " probably not created!");
chris@1
   121
      }
chris@1
   122
    }
chris@1
   123
    
chris@1
   124
    StringTemplate tmpl = getTemplate("GroupAdded.tmpl");
chris@1
   125
    
chris@1
   126
    tmpl.set("GROUP", req.getParameter("groups"));
chris@1
   127
    
chris@1
   128
    resp.setStatus(HttpServletResponse.SC_OK);
chris@1
   129
    resp.getWriter().println(tmpl.toString());
chris@1
   130
    resp.getWriter().flush();
chris@1
   131
  }
chris@1
   132
  
chris@1
   133
  private void whichGroupDelete(HttpServletRequest req, HttpServletResponse resp)
chris@1
   134
    throws IOException
chris@1
   135
  {
chris@1
   136
    String groupname = req.getParameter("group");
chris@1
   137
    printlnToNewsserver("XDAEMON GROUPDEL " + groupname);
chris@1
   138
    String line = readlnFromNewsserver();
chris@1
   139
    if(!line.startsWith("200 "))
chris@1
   140
      throw new IOException(line);
chris@1
   141
    
chris@1
   142
    StringTemplate tmpl = getTemplate("GroupDeleted.tmpl");
chris@1
   143
    
chris@1
   144
    tmpl.set("GROUP", groupname);
chris@1
   145
    
chris@1
   146
    resp.setStatus(HttpServletResponse.SC_OK);
chris@1
   147
    resp.getWriter().println(tmpl.toString());
chris@1
   148
    resp.getWriter().flush();
chris@1
   149
  }
chris@1
   150
  
chris@1
   151
  private void whichNone(HttpServletRequest req, HttpServletResponse resp)
chris@1
   152
    throws IOException
chris@1
   153
  {
chris@1
   154
    StringTemplate tmpl = getTemplate("SonewsConfigServlet.tmpl");
chris@1
   155
    
chris@1
   156
    // Retrieve config keys from server
chris@1
   157
    List<String> configKeys = new ArrayList<String>();
chris@1
   158
    printlnToNewsserver("XDAEMON LIST CONFIGKEYS");
chris@1
   159
    String line = readlnFromNewsserver();
chris@1
   160
    if(!line.startsWith("200 "))
chris@1
   161
      throw new IOException("XDAEMON command not supported!");
chris@1
   162
    for(;;)
chris@1
   163
    {
chris@1
   164
      line = readlnFromNewsserver();
chris@1
   165
      if(line.equals("."))
chris@1
   166
        break;
chris@1
   167
      else
chris@1
   168
        configKeys.add(line);
chris@1
   169
    }
chris@1
   170
    
chris@1
   171
    // Construct config table
chris@1
   172
    StringBuilder strb = new StringBuilder();
chris@1
   173
    for(String key : configKeys)
chris@1
   174
    {
chris@1
   175
      strb.append("<tr><td><code>");
chris@1
   176
      strb.append(key);
chris@1
   177
      strb.append("</code></td><td>");
chris@1
   178
      
chris@1
   179
      // Retrieve config value from server
chris@1
   180
      String value = "<not set>";
chris@1
   181
      printlnToNewsserver("XDAEMON GET " + key);
chris@1
   182
      line = readlnFromNewsserver();
chris@1
   183
      if(line.startsWith("200 "))
chris@1
   184
      {
chris@1
   185
        value = readlnFromNewsserver();
chris@1
   186
        readlnFromNewsserver(); // Read the "."
chris@1
   187
      }
chris@1
   188
      
chris@1
   189
      strb.append("<input type=text name=\"configkey:");
chris@1
   190
      strb.append(key);
chris@1
   191
      strb.append("\" value=\"");
chris@1
   192
      strb.append(value);
chris@1
   193
      strb.append("\"/></td></tr>");
chris@1
   194
    }
chris@1
   195
    tmpl.set("CONFIG", strb.toString());
chris@1
   196
    
chris@1
   197
    // Retrieve served newsgroup names from server
chris@1
   198
    List<String> groups = new ArrayList<String>();
chris@1
   199
    printlnToNewsserver("LIST");
chris@1
   200
    line = readlnFromNewsserver();
chris@1
   201
    if(line.startsWith("215 "))
chris@1
   202
    {
chris@1
   203
      for(;;)
chris@1
   204
      {
chris@1
   205
        line = readlnFromNewsserver();
chris@1
   206
        if(line.equals("."))
chris@1
   207
        {
chris@1
   208
          break;
chris@1
   209
        }
chris@1
   210
        else
chris@1
   211
        {
chris@1
   212
          groups.add(line.split(" ")[0]);
chris@1
   213
        }
chris@1
   214
      }
chris@1
   215
    }
chris@1
   216
    else
chris@1
   217
      throw new IOException("Error issuing LIST command!");
chris@1
   218
    
chris@1
   219
    // Construct groups list
chris@1
   220
    StringTemplate tmplGroupList = new StringTemplate(
chris@1
   221
      Resource.getAsString("org/sonews/web/tmpl/GroupList.tmpl", true));
chris@1
   222
    strb = new StringBuilder();
chris@1
   223
    for(String group : groups)
chris@1
   224
    {
chris@1
   225
      tmplGroupList.set("GROUPNAME", group);
chris@1
   226
      strb.append(tmplGroupList.toString());
chris@1
   227
    }
chris@1
   228
    tmpl.set("GROUP", strb.toString());
chris@1
   229
    
chris@1
   230
    // Set server name
chris@1
   231
    tmpl.set("SERVERNAME", hello.split(" ")[2]);
chris@1
   232
    tmpl.set("TITLE", "Configuration");
chris@1
   233
    
chris@1
   234
    resp.getWriter().println(tmpl.toString());
chris@1
   235
    resp.getWriter().flush();
chris@1
   236
    resp.setStatus(HttpServletResponse.SC_OK);
chris@1
   237
  }
chris@1
   238
chris@1
   239
}