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.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.PrintWriter;
25 import java.net.Socket;
26 import javax.servlet.http.HttpServlet;
27 import org.sonews.util.StringTemplate;
28 import org.sonews.util.io.Resource;
31 * Base class for all sonews servlets.
32 * @author Christian Lins
35 public class AbstractSonewsServlet extends HttpServlet
38 public static final String TemplateRoot = "org/sonews/web/tmpl/";
40 protected String hello = null;
42 private BufferedReader in = null;
43 private PrintWriter out = null;
44 private Socket socket = null;
46 protected void connectToNewsserver()
49 // Get sonews port from properties
50 String port = System.getProperty("sonews.port", "9119");
51 String host = System.getProperty("sonews.host", "localhost");
55 this.socket = new Socket(host, Integer.parseInt(port));
57 this.in = new BufferedReader(
58 new InputStreamReader(socket.getInputStream()));
59 this.out = new PrintWriter(socket.getOutputStream());
61 hello = in.readLine(); // Read hello message
65 System.out.println("sonews.host=" + host);
66 System.out.println("sonews.port=" + port);
72 protected void disconnectFromNewsserver()
76 printlnToNewsserver("QUIT");
78 readlnFromNewsserver(); // Wait for bye message
88 protected StringTemplate getTemplate(String res)
90 StringTemplate tmpl = new StringTemplate(
91 Resource.getAsString(TemplateRoot + "AbstractSonewsServlet.tmpl", true));
92 String content = Resource.getAsString(TemplateRoot + res, true);
93 String stylesheet = System.getProperty("sonews.web.stylesheet", "style.css");
95 tmpl.set("CONTENT", content);
96 tmpl.set("STYLESHEET", stylesheet);
98 return new StringTemplate(tmpl.toString());
101 protected void printlnToNewsserver(final String line)
103 this.out.println(line);
107 protected String readlnFromNewsserver()
110 return this.in.readLine();