FileDocCategorySizeDatePackage
PluggableHTTPServer.javaAPI DocApache log4j 1.2.158170Sat Aug 25 00:09:34 BST 2007com.psibt.framework.net

PluggableHTTPServer

public class PluggableHTTPServer extends Object implements Runnable
This class implements a HTTP-server frame. All HTTP-requests are handled by HTTPRequestHandler classes which implement the HTTPRequestHandler interface. Every RequestHandler has to be registered in the PluggableHTTPServer with the addRequestHandler method. A new thread is created for each connection to handle the request. If all reply data are sent to the client the connection is closed and the thread ends. An example how to use the PluggableHTTPServer class can be found in the main method at the end of the source file.
author
Volker Mentzner

Fields Summary
public static final int
DEFAULT_PORT
static Category
cat
private int
port
private Vector
handler
private ServerSocket
server
Constructors Summary
public PluggableHTTPServer(int port)
Creates a new server object on the given TCP port. If the port is occupied by another process a IOException (java.net.BindException) is thrown.

param
port - TCP port number to listen on for requests


                                       
       
    this.port = port;
    this.handler = new Vector();
    cat.setPriority(Priority.ERROR);
    server = new ServerSocket(this.port);
  
public PluggableHTTPServer()
Creates a new server object on the default TCP port 80 If the port is occupied by another process a IOException (java.net.BindException) is thrown.

    this(DEFAULT_PORT);
  
Methods Summary
public voidaddRequestHandler(HTTPRequestHandler h)
Registers the given HTTPRequestHandler

param
h - the HTTPRequestHandler to register

    handler.add(h);
  
public voidautoCreateRootPage(int index)
Creates the ReplyHTML data for the root page

param
index - index of the RootRequestHandler

    if (handler.get(index) instanceof RootRequestHandler) {
      RootRequestHandler r = (RootRequestHandler)handler.get(index);
      String html = "<HTML><HEAD><TITLE>"+r.getTitle()+"</TITLE></HEAD>\r\n";
      html = html + "<BODY><H1>"+r.getDescription()+"</H1>\r\n";
      for (int i = 0; i < handler.size(); i++) {
        html = html + "<a href=\"" + ((HTTPRequestHandler)handler.get(i)).getHandledPath();
        html = html + "\">" + ((HTTPRequestHandler)handler.get(i)).getDescription() + "</a><br>";
      }
      html = html + "</BODY></HTML>\r\n";
      r.setReplyHTML(html);
    }
  
public static voidmain(java.lang.String[] args)
Demo how to use the PluggableHTTPServer.


    int thePort;

    // create some logging stuff
    BasicConfigurator.configure();
    Category cat1 = Category.getInstance("cat1");
    cat1.addAppender(new org.apache.log4j.ConsoleAppender(new PatternLayout("%m%n")));
    Category cat2 = Category.getInstance("cat2");
    cat2.setPriority(Priority.INFO);
    cat2.addAppender(new org.apache.log4j.ConsoleAppender(new PatternLayout("%c - %m%n")));

    // set TCP port number
    try {
      thePort = Integer.parseInt(args[1]);
    }
    catch (Exception e) {
      thePort = PluggableHTTPServer.DEFAULT_PORT;
    }

    PluggableHTTPServer server = null;
    while (server == null) {
      try {
        server = new PluggableHTTPServer(thePort);
        server.addRequestHandler(new RootRequestHandler());
        server.addRequestHandler(new Log4jRequestHandler());
        server.addRequestHandler(new UserDialogRequestHandler());
        server.autoCreateRootPage(0);
        Thread t = new Thread(server);
        t.start();
      } catch (IOException e) {
        server = null;
        thePort++;
      }
    }

  
public voidremoveRequestHandler(HTTPRequestHandler h)
Unregisters the given HTTPRequestHandler

param
h - the HTTPRequestHandler to unregister

    handler.remove(h);
  
public static voidreplyMethodNotAllowed(java.io.Writer out)
Sends the HTTP message 405 - Method Not Allowed see RFC2616 for details

param
out - Out stream for sending data to client browser

    try {
      out.write("HTTP/1.1 405 Method Not Allowed\r\n");
      out.write("Allow: GET, PUT\r\n");
      out.write("<HTML><HEAD><TITLE>Method Not Allowed</TITLE></HEAD>\r\n");
      out.write("<BODY><H1>Method Not Allowed</H1>\r\n");
      out.write("</BODY></HTML>\r\n");
      out.flush();
    }  // end try
    catch (IOException e) {
    }
  
public static voidreplyNotFound(java.io.Writer out)
Sends the HTTP message 404 - File Not Found see RFC2616 for details

param
out - Out stream for sending data to client browser

    try {
      out.write("HTTP/1.0 404 Not Found\r\n");
      out.write("<HTML><HEAD><TITLE>Not Found</TITLE></HEAD>\r\n");
      out.write("<BODY><H1>Not Found</H1>\r\n");
      out.write("</BODY></HTML>\r\n");
      out.flush();
    }  // end try
    catch (IOException e) {
    }
  
public voidrun()
Main loop of the PluggableHTTPServer

    while (true) {
      try {
        Socket s = server.accept();
        Thread t = new ServerThread(s);
        t.start();
      }
      catch (IOException e) {
      }
    }