3 * see AUTHORS for the list of contributors
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.
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.
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/>.
19 package org.sonews.web;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
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;
31 * Servlet providing a configuration web interface.
32 * @author Christian Lins
35 public class SonewsConfigServlet extends AbstractSonewsServlet
38 private static final long serialVersionUID = 2432543253L;
41 public void doGet(HttpServletRequest req, HttpServletResponse resp)
46 connectToNewsserver();
47 String which = req.getParameter("which");
49 if(which != null && which.equals("config"))
51 whichConfig(req, resp);
53 else if(which != null && which.equals("groupadd"))
55 whichGroupAdd(req, resp);
57 else if(which != null && which.equals("groupdelete"))
59 whichGroupDelete(req, resp);
66 disconnectFromNewsserver();
70 private void whichConfig(HttpServletRequest req, HttpServletResponse resp)
73 StringBuilder keys = new StringBuilder();
75 Set pnames = req.getParameterMap().keySet();
76 for(Object obj : pnames)
78 String pname = (String)obj;
79 if(pname.startsWith("configkey:"))
81 String value = req.getParameter(pname);
82 String key = pname.split(":")[1];
83 if(!value.equals("<not set>"))
85 printlnToNewsserver("XDAEMON SET " + key + " " + value);
86 readlnFromNewsserver();
94 StringTemplate tmpl = getTemplate("ConfigUpdated.tmpl");
96 tmpl.set("UPDATED_KEYS", keys.toString());
98 resp.setStatus(HttpServletResponse.SC_OK);
99 resp.getWriter().println(tmpl.toString());
100 resp.getWriter().flush();
103 private void whichGroupAdd(HttpServletRequest req, HttpServletResponse resp)
106 String[] groupnames = req.getParameter("groups").split("\n");
108 for(String groupname : groupnames)
110 groupname = groupname.trim();
111 if(groupname.equals(""))
116 printlnToNewsserver("XDAEMON GROUPADD " + groupname + " 0");
117 String line = readlnFromNewsserver();
118 if(!line.startsWith("200 "))
120 System.out.println("Warning " + groupname + " probably not created!");
124 StringTemplate tmpl = getTemplate("GroupAdded.tmpl");
126 tmpl.set("GROUP", req.getParameter("groups"));
128 resp.setStatus(HttpServletResponse.SC_OK);
129 resp.getWriter().println(tmpl.toString());
130 resp.getWriter().flush();
133 private void whichGroupDelete(HttpServletRequest req, HttpServletResponse resp)
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);
142 StringTemplate tmpl = getTemplate("GroupDeleted.tmpl");
144 tmpl.set("GROUP", groupname);
146 resp.setStatus(HttpServletResponse.SC_OK);
147 resp.getWriter().println(tmpl.toString());
148 resp.getWriter().flush();
151 private void whichNone(HttpServletRequest req, HttpServletResponse resp)
154 StringTemplate tmpl = getTemplate("SonewsConfigServlet.tmpl");
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!");
164 line = readlnFromNewsserver();
168 configKeys.add(line);
171 // Construct config table
172 StringBuilder strb = new StringBuilder();
173 for(String key : configKeys)
175 strb.append("<tr><td><code>");
177 strb.append("</code></td><td>");
179 // Retrieve config value from server
180 String value = "<not set>";
181 printlnToNewsserver("XDAEMON GET " + key);
182 line = readlnFromNewsserver();
183 if(line.startsWith("200 "))
185 value = readlnFromNewsserver();
186 readlnFromNewsserver(); // Read the "."
189 strb.append("<input type=text name=\"configkey:");
191 strb.append("\" value=\"");
193 strb.append("\"/></td></tr>");
195 tmpl.set("CONFIG", strb.toString());
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 "))
205 line = readlnFromNewsserver();
212 groups.add(line.split(" ")[0]);
217 throw new IOException("Error issuing LIST command!");
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)
225 tmplGroupList.set("GROUPNAME", group);
226 strb.append(tmplGroupList.toString());
228 tmpl.set("GROUP", strb.toString());
231 tmpl.set("SERVERNAME", hello.split(" ")[2]);
232 tmpl.set("TITLE", "Configuration");
234 resp.getWriter().println(tmpl.toString());
235 resp.getWriter().flush();
236 resp.setStatus(HttpServletResponse.SC_OK);