1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/org/sonews/web/AbstractSonewsServlet.java Wed Jul 01 10:48:22 2009 +0200
1.3 @@ -0,0 +1,113 @@
1.4 +/*
1.5 + * SONEWS News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package org.sonews.web;
1.23 +
1.24 +import java.io.BufferedReader;
1.25 +import java.io.IOException;
1.26 +import java.io.InputStreamReader;
1.27 +import java.io.PrintWriter;
1.28 +import java.net.Socket;
1.29 +import javax.servlet.http.HttpServlet;
1.30 +import org.sonews.util.StringTemplate;
1.31 +import org.sonews.util.io.Resource;
1.32 +
1.33 +/**
1.34 + * Base class for all sonews servlets.
1.35 + * @author Christian Lins
1.36 + * @since sonews/0.5.0
1.37 + */
1.38 +public class AbstractSonewsServlet extends HttpServlet
1.39 +{
1.40 +
1.41 + public static final String TemplateRoot = "org/sonews/web/tmpl/";
1.42 +
1.43 + protected String hello = null;
1.44 +
1.45 + private BufferedReader in = null;
1.46 + private PrintWriter out = null;
1.47 + private Socket socket = null;
1.48 +
1.49 + protected void connectToNewsserver()
1.50 + throws IOException
1.51 + {
1.52 + // Get sonews port from properties
1.53 + String port = System.getProperty("sonews.port", "9119");
1.54 + String host = System.getProperty("sonews.host", "localhost");
1.55 +
1.56 + try
1.57 + {
1.58 + this.socket = new Socket(host, Integer.parseInt(port));
1.59 +
1.60 + this.in = new BufferedReader(
1.61 + new InputStreamReader(socket.getInputStream()));
1.62 + this.out = new PrintWriter(socket.getOutputStream());
1.63 +
1.64 + hello = in.readLine(); // Read hello message
1.65 + }
1.66 + catch(IOException ex)
1.67 + {
1.68 + System.out.println("sonews.host=" + host);
1.69 + System.out.println("sonews.port=" + port);
1.70 + System.out.flush();
1.71 + throw ex;
1.72 + }
1.73 + }
1.74 +
1.75 + protected void disconnectFromNewsserver()
1.76 + {
1.77 + try
1.78 + {
1.79 + printlnToNewsserver("QUIT");
1.80 + out.close();
1.81 + readlnFromNewsserver(); // Wait for bye message
1.82 + in.close();
1.83 + socket.close();
1.84 + }
1.85 + catch(IOException ex)
1.86 + {
1.87 + ex.printStackTrace();
1.88 + }
1.89 + }
1.90 +
1.91 + protected StringTemplate getTemplate(String res)
1.92 + {
1.93 + StringTemplate tmpl = new StringTemplate(
1.94 + Resource.getAsString(TemplateRoot + "AbstractSonewsServlet.tmpl", true));
1.95 + String content = Resource.getAsString(TemplateRoot + res, true);
1.96 + String stylesheet = System.getProperty("sonews.web.stylesheet", "style.css");
1.97 +
1.98 + tmpl.set("CONTENT", content);
1.99 + tmpl.set("STYLESHEET", stylesheet);
1.100 +
1.101 + return new StringTemplate(tmpl.toString());
1.102 + }
1.103 +
1.104 + protected void printlnToNewsserver(final String line)
1.105 + {
1.106 + this.out.println(line);
1.107 + this.out.flush();
1.108 + }
1.109 +
1.110 + protected String readlnFromNewsserver()
1.111 + throws IOException
1.112 + {
1.113 + return this.in.readLine();
1.114 + }
1.115 +
1.116 +}