chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: chris@1: package org.sonews.web; chris@1: chris@1: import java.io.IOException; chris@1: import java.util.ArrayList; chris@1: import java.util.List; chris@1: import java.util.Set; chris@1: import javax.servlet.http.HttpServletRequest; chris@1: import javax.servlet.http.HttpServletResponse; chris@1: import org.sonews.util.StringTemplate; chris@1: import org.sonews.util.io.Resource; chris@1: chris@1: /** chris@1: * Servlet providing a configuration web interface. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ chris@1: public class SonewsConfigServlet extends AbstractSonewsServlet chris@1: { chris@1: chris@1: private static final long serialVersionUID = 2432543253L; chris@1: chris@1: @Override chris@1: public void doGet(HttpServletRequest req, HttpServletResponse resp) chris@1: throws IOException chris@1: { chris@1: synchronized(this) chris@1: { chris@1: connectToNewsserver(); chris@1: String which = req.getParameter("which"); chris@1: chris@1: if(which != null && which.equals("config")) chris@1: { chris@1: whichConfig(req, resp); chris@1: } chris@1: else if(which != null && which.equals("groupadd")) chris@1: { chris@1: whichGroupAdd(req, resp); chris@1: } chris@1: else if(which != null && which.equals("groupdelete")) chris@1: { chris@1: whichGroupDelete(req, resp); chris@1: } chris@1: else chris@1: { chris@1: whichNone(req, resp); chris@1: } chris@1: chris@1: disconnectFromNewsserver(); chris@1: } chris@1: } chris@1: chris@1: private void whichConfig(HttpServletRequest req, HttpServletResponse resp) chris@1: throws IOException chris@1: { chris@1: StringBuilder keys = new StringBuilder(); chris@1: chris@1: Set pnames = req.getParameterMap().keySet(); chris@1: for(Object obj : pnames) chris@1: { chris@1: String pname = (String)obj; chris@1: if(pname.startsWith("configkey:")) chris@1: { chris@1: String value = req.getParameter(pname); chris@1: String key = pname.split(":")[1]; chris@1: if(!value.equals("")) chris@1: { chris@1: printlnToNewsserver("XDAEMON SET " + key + " " + value); chris@1: readlnFromNewsserver(); chris@1: chris@1: keys.append(key); chris@1: keys.append("
"); chris@1: } chris@1: } chris@1: } chris@1: chris@1: StringTemplate tmpl = getTemplate("ConfigUpdated.tmpl"); chris@1: chris@1: tmpl.set("UPDATED_KEYS", keys.toString()); chris@1: chris@1: resp.setStatus(HttpServletResponse.SC_OK); chris@1: resp.getWriter().println(tmpl.toString()); chris@1: resp.getWriter().flush(); chris@1: } chris@1: chris@1: private void whichGroupAdd(HttpServletRequest req, HttpServletResponse resp) chris@1: throws IOException chris@1: { chris@1: String[] groupnames = req.getParameter("groups").split("\n"); chris@1: chris@1: for(String groupname : groupnames) chris@1: { chris@1: groupname = groupname.trim(); chris@1: if(groupname.equals("")) chris@1: { chris@1: continue; chris@1: } chris@1: chris@1: printlnToNewsserver("XDAEMON GROUPADD " + groupname + " 0"); chris@1: String line = readlnFromNewsserver(); chris@1: if(!line.startsWith("200 ")) chris@1: { chris@1: System.out.println("Warning " + groupname + " probably not created!"); chris@1: } chris@1: } chris@1: chris@1: StringTemplate tmpl = getTemplate("GroupAdded.tmpl"); chris@1: chris@1: tmpl.set("GROUP", req.getParameter("groups")); chris@1: chris@1: resp.setStatus(HttpServletResponse.SC_OK); chris@1: resp.getWriter().println(tmpl.toString()); chris@1: resp.getWriter().flush(); chris@1: } chris@1: chris@1: private void whichGroupDelete(HttpServletRequest req, HttpServletResponse resp) chris@1: throws IOException chris@1: { chris@1: String groupname = req.getParameter("group"); chris@1: printlnToNewsserver("XDAEMON GROUPDEL " + groupname); chris@1: String line = readlnFromNewsserver(); chris@1: if(!line.startsWith("200 ")) chris@1: throw new IOException(line); chris@1: chris@1: StringTemplate tmpl = getTemplate("GroupDeleted.tmpl"); chris@1: chris@1: tmpl.set("GROUP", groupname); chris@1: chris@1: resp.setStatus(HttpServletResponse.SC_OK); chris@1: resp.getWriter().println(tmpl.toString()); chris@1: resp.getWriter().flush(); chris@1: } chris@1: chris@1: private void whichNone(HttpServletRequest req, HttpServletResponse resp) chris@1: throws IOException chris@1: { chris@1: StringTemplate tmpl = getTemplate("SonewsConfigServlet.tmpl"); chris@1: chris@1: // Retrieve config keys from server chris@1: List configKeys = new ArrayList(); chris@1: printlnToNewsserver("XDAEMON LIST CONFIGKEYS"); chris@1: String line = readlnFromNewsserver(); chris@1: if(!line.startsWith("200 ")) chris@1: throw new IOException("XDAEMON command not supported!"); chris@1: for(;;) chris@1: { chris@1: line = readlnFromNewsserver(); chris@1: if(line.equals(".")) chris@1: break; chris@1: else chris@1: configKeys.add(line); chris@1: } chris@1: chris@1: // Construct config table chris@1: StringBuilder strb = new StringBuilder(); chris@1: for(String key : configKeys) chris@1: { chris@1: strb.append(""); chris@1: strb.append(key); chris@1: strb.append(""); chris@1: chris@1: // Retrieve config value from server chris@1: String value = ""; chris@1: printlnToNewsserver("XDAEMON GET " + key); chris@1: line = readlnFromNewsserver(); chris@1: if(line.startsWith("200 ")) chris@1: { chris@1: value = readlnFromNewsserver(); chris@1: readlnFromNewsserver(); // Read the "." chris@1: } chris@1: chris@1: strb.append(""); chris@1: } chris@1: tmpl.set("CONFIG", strb.toString()); chris@1: chris@1: // Retrieve served newsgroup names from server chris@1: List groups = new ArrayList(); chris@1: printlnToNewsserver("LIST"); chris@1: line = readlnFromNewsserver(); chris@1: if(line.startsWith("215 ")) chris@1: { chris@1: for(;;) chris@1: { chris@1: line = readlnFromNewsserver(); chris@1: if(line.equals(".")) chris@1: { chris@1: break; chris@1: } chris@1: else chris@1: { chris@1: groups.add(line.split(" ")[0]); chris@1: } chris@1: } chris@1: } chris@1: else chris@1: throw new IOException("Error issuing LIST command!"); chris@1: chris@1: // Construct groups list chris@1: StringTemplate tmplGroupList = new StringTemplate( chris@1: Resource.getAsString("org/sonews/web/tmpl/GroupList.tmpl", true)); chris@1: strb = new StringBuilder(); chris@1: for(String group : groups) chris@1: { chris@1: tmplGroupList.set("GROUPNAME", group); chris@1: strb.append(tmplGroupList.toString()); chris@1: } chris@1: tmpl.set("GROUP", strb.toString()); chris@1: chris@1: // Set server name chris@1: tmpl.set("SERVERNAME", hello.split(" ")[2]); chris@1: tmpl.set("TITLE", "Configuration"); chris@1: chris@1: resp.getWriter().println(tmpl.toString()); chris@1: resp.getWriter().flush(); chris@1: resp.setStatus(HttpServletResponse.SC_OK); chris@1: } chris@1: chris@1: }