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.BufferedReader; chris@1: import java.io.IOException; chris@1: import java.io.InputStreamReader; chris@1: import java.io.PrintWriter; chris@1: import java.net.Socket; chris@1: import javax.servlet.http.HttpServlet; chris@1: import org.sonews.util.StringTemplate; chris@1: import org.sonews.util.io.Resource; chris@1: chris@1: /** chris@1: * Base class for all sonews servlets. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ chris@1: public class AbstractSonewsServlet extends HttpServlet chris@1: { chris@1: chris@1: public static final String TemplateRoot = "org/sonews/web/tmpl/"; chris@1: chris@1: protected String hello = null; chris@1: chris@1: private BufferedReader in = null; chris@1: private PrintWriter out = null; chris@1: private Socket socket = null; chris@1: chris@1: protected void connectToNewsserver() chris@1: throws IOException chris@1: { chris@1: // Get sonews port from properties chris@1: String port = System.getProperty("sonews.port", "9119"); chris@1: String host = System.getProperty("sonews.host", "localhost"); chris@1: chris@1: try chris@1: { chris@1: this.socket = new Socket(host, Integer.parseInt(port)); chris@1: chris@1: this.in = new BufferedReader( chris@1: new InputStreamReader(socket.getInputStream())); chris@1: this.out = new PrintWriter(socket.getOutputStream()); chris@1: chris@1: hello = in.readLine(); // Read hello message chris@1: } chris@1: catch(IOException ex) chris@1: { chris@1: System.out.println("sonews.host=" + host); chris@1: System.out.println("sonews.port=" + port); chris@1: System.out.flush(); chris@1: throw ex; chris@1: } chris@1: } chris@1: chris@1: protected void disconnectFromNewsserver() chris@1: { chris@1: try chris@1: { chris@1: printlnToNewsserver("QUIT"); chris@1: out.close(); chris@1: readlnFromNewsserver(); // Wait for bye message chris@1: in.close(); chris@1: socket.close(); chris@1: } chris@1: catch(IOException ex) chris@1: { chris@1: ex.printStackTrace(); chris@1: } chris@1: } chris@1: chris@1: protected StringTemplate getTemplate(String res) chris@1: { chris@1: StringTemplate tmpl = new StringTemplate( chris@1: Resource.getAsString(TemplateRoot + "AbstractSonewsServlet.tmpl", true)); chris@1: String content = Resource.getAsString(TemplateRoot + res, true); chris@1: String stylesheet = System.getProperty("sonews.web.stylesheet", "style.css"); chris@1: chris@1: tmpl.set("CONTENT", content); chris@1: tmpl.set("STYLESHEET", stylesheet); chris@1: chris@1: return new StringTemplate(tmpl.toString()); chris@1: } chris@1: chris@1: protected void printlnToNewsserver(final String line) chris@1: { chris@1: this.out.println(line); chris@1: this.out.flush(); chris@1: } chris@1: chris@1: protected String readlnFromNewsserver() chris@1: throws IOException chris@1: { chris@1: return this.in.readLine(); chris@1: } chris@1: chris@1: }