org/sonews/web/AbstractSonewsServlet.java
author chris <chris@marvin>
Fri, 26 Jun 2009 16:48:50 +0200
changeset 1 6fceb66e1ad7
permissions -rw-r--r--
Hooray... sonews/0.5.0 final

HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Remove all lines to abort the collapse operation.
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
chris@1
    19
package org.sonews.web;
chris@1
    20
chris@1
    21
import java.io.BufferedReader;
chris@1
    22
import java.io.IOException;
chris@1
    23
import java.io.InputStreamReader;
chris@1
    24
import java.io.PrintWriter;
chris@1
    25
import java.net.Socket;
chris@1
    26
import javax.servlet.http.HttpServlet;
chris@1
    27
import org.sonews.util.StringTemplate;
chris@1
    28
import org.sonews.util.io.Resource;
chris@1
    29
chris@1
    30
/**
chris@1
    31
 * Base class for all sonews servlets.
chris@1
    32
 * @author Christian Lins
chris@1
    33
 * @since sonews/0.5.0
chris@1
    34
 */
chris@1
    35
public class AbstractSonewsServlet extends HttpServlet
chris@1
    36
{
chris@1
    37
chris@1
    38
  public static final String TemplateRoot = "org/sonews/web/tmpl/";
chris@1
    39
  
chris@1
    40
  protected String        hello = null;
chris@1
    41
  
chris@1
    42
  private BufferedReader  in     = null;
chris@1
    43
  private PrintWriter     out    = null;
chris@1
    44
  private Socket          socket = null;
chris@1
    45
  
chris@1
    46
  protected void connectToNewsserver()
chris@1
    47
    throws IOException
chris@1
    48
  {
chris@1
    49
    // Get sonews port from properties
chris@1
    50
    String port = System.getProperty("sonews.port", "9119");
chris@1
    51
    String host = System.getProperty("sonews.host", "localhost");
chris@1
    52
    
chris@1
    53
    try
chris@1
    54
    {
chris@1
    55
      this.socket = new Socket(host, Integer.parseInt(port));
chris@1
    56
chris@1
    57
      this.in     = new BufferedReader(
chris@1
    58
        new InputStreamReader(socket.getInputStream()));
chris@1
    59
      this.out = new PrintWriter(socket.getOutputStream());
chris@1
    60
chris@1
    61
      hello = in.readLine(); // Read hello message
chris@1
    62
    }
chris@1
    63
    catch(IOException ex)
chris@1
    64
    {
chris@1
    65
      System.out.println("sonews.host=" + host);
chris@1
    66
      System.out.println("sonews.port=" + port);
chris@1
    67
      System.out.flush();
chris@1
    68
      throw ex;
chris@1
    69
    }
chris@1
    70
  }
chris@1
    71
  
chris@1
    72
  protected void disconnectFromNewsserver()
chris@1
    73
  {
chris@1
    74
    try
chris@1
    75
    {
chris@1
    76
      printlnToNewsserver("QUIT");
chris@1
    77
      out.close();
chris@1
    78
      readlnFromNewsserver(); // Wait for bye message
chris@1
    79
      in.close();
chris@1
    80
      socket.close();
chris@1
    81
    }
chris@1
    82
    catch(IOException ex)
chris@1
    83
    {
chris@1
    84
      ex.printStackTrace();
chris@1
    85
    }
chris@1
    86
  }
chris@1
    87
  
chris@1
    88
  protected StringTemplate getTemplate(String res)
chris@1
    89
  {
chris@1
    90
    StringTemplate tmpl = new StringTemplate(
chris@1
    91
      Resource.getAsString(TemplateRoot + "AbstractSonewsServlet.tmpl", true));
chris@1
    92
    String content    = Resource.getAsString(TemplateRoot + res, true);
chris@1
    93
    String stylesheet = System.getProperty("sonews.web.stylesheet", "style.css");
chris@1
    94
    
chris@1
    95
    tmpl.set("CONTENT", content);
chris@1
    96
    tmpl.set("STYLESHEET", stylesheet);
chris@1
    97
    
chris@1
    98
    return new StringTemplate(tmpl.toString());
chris@1
    99
  }
chris@1
   100
  
chris@1
   101
  protected void printlnToNewsserver(final String line)
chris@1
   102
  {
chris@1
   103
    this.out.println(line);
chris@1
   104
    this.out.flush();
chris@1
   105
  }
chris@1
   106
  
chris@1
   107
  protected String readlnFromNewsserver()
chris@1
   108
    throws IOException
chris@1
   109
  {
chris@1
   110
    return this.in.readLine();
chris@1
   111
  }
chris@1
   112
  
chris@1
   113
}