org/sonews/web/AbstractSonewsServlet.java
author chris <chris@marvin>
Wed Jul 01 10:48:22 2009 +0200 (2009-07-01)
changeset 2 1090e2141798
permissions -rw-r--r--
sonews/0.5.1 fixes merged
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 
    19 package org.sonews.web;
    20 
    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;
    29 
    30 /**
    31  * Base class for all sonews servlets.
    32  * @author Christian Lins
    33  * @since sonews/0.5.0
    34  */
    35 public class AbstractSonewsServlet extends HttpServlet
    36 {
    37 
    38   public static final String TemplateRoot = "org/sonews/web/tmpl/";
    39   
    40   protected String        hello = null;
    41   
    42   private BufferedReader  in     = null;
    43   private PrintWriter     out    = null;
    44   private Socket          socket = null;
    45   
    46   protected void connectToNewsserver()
    47     throws IOException
    48   {
    49     // Get sonews port from properties
    50     String port = System.getProperty("sonews.port", "9119");
    51     String host = System.getProperty("sonews.host", "localhost");
    52     
    53     try
    54     {
    55       this.socket = new Socket(host, Integer.parseInt(port));
    56 
    57       this.in     = new BufferedReader(
    58         new InputStreamReader(socket.getInputStream()));
    59       this.out = new PrintWriter(socket.getOutputStream());
    60 
    61       hello = in.readLine(); // Read hello message
    62     }
    63     catch(IOException ex)
    64     {
    65       System.out.println("sonews.host=" + host);
    66       System.out.println("sonews.port=" + port);
    67       System.out.flush();
    68       throw ex;
    69     }
    70   }
    71   
    72   protected void disconnectFromNewsserver()
    73   {
    74     try
    75     {
    76       printlnToNewsserver("QUIT");
    77       out.close();
    78       readlnFromNewsserver(); // Wait for bye message
    79       in.close();
    80       socket.close();
    81     }
    82     catch(IOException ex)
    83     {
    84       ex.printStackTrace();
    85     }
    86   }
    87   
    88   protected StringTemplate getTemplate(String res)
    89   {
    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");
    94     
    95     tmpl.set("CONTENT", content);
    96     tmpl.set("STYLESHEET", stylesheet);
    97     
    98     return new StringTemplate(tmpl.toString());
    99   }
   100   
   101   protected void printlnToNewsserver(final String line)
   102   {
   103     this.out.println(line);
   104     this.out.flush();
   105   }
   106   
   107   protected String readlnFromNewsserver()
   108     throws IOException
   109   {
   110     return this.in.readLine();
   111   }
   112   
   113 }