org.apache.xmlrpc
Class WebServer

java.lang.Object
  extended byorg.apache.xmlrpc.WebServer
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
SecureWebServer

public class WebServer
extends java.lang.Object
implements java.lang.Runnable

A minimal web server that exclusively handles XML-RPC requests.

Author:
Hannes Wallnoefer, Jason van Zyl, Daniel Rall

Nested Class Summary
(package private)  class WebServer.AddressMatcher
           
(package private)  class WebServer.Connection
           
(package private)  class WebServer.Runner
          Responsible for handling client connections.
 
Field Summary
protected  java.util.Vector accept
           
private  java.net.InetAddress address
           
protected static byte[] clength
           
protected static byte[] conclose
           
protected static byte[] conkeep
           
protected static byte[] ctype
           
protected  java.util.Vector deny
           
protected static byte[] doubleNewline
           
private static java.lang.String HTTP_11
           
protected  java.lang.Thread listener
           
protected static byte[] newline
           
protected static byte[] ok
           
private  boolean paranoid
           
private  int port
           
protected  java.lang.ThreadGroup runners
           
protected static byte[] server
           
protected  java.net.ServerSocket serverSocket
           
private static java.lang.String STAR
           
protected  java.util.Stack threadpool
           
protected static byte[] wwwAuthenticate
           
protected  XmlRpcServer xmlrpc
           
 
Constructor Summary
WebServer(int port)
          Creates a web server at the specified port number.
WebServer(int port, java.net.InetAddress addr)
          Creates a web server at the specified port number and IP address.
WebServer(int port, java.net.InetAddress addr, XmlRpcServer xmlrpc)
          Creates a web server at the specified port number and IP address.
 
Method Summary
 void acceptClient(java.lang.String address)
          Add an IP address to the list of accepted clients.
protected  void addDefaultHandlers()
          Adds the bundled handlers to the server.
 void addHandler(java.lang.String name, java.lang.Object target)
          Register a handler object with this name.
protected  boolean allowConnection(java.net.Socket s)
          Checks incoming connections to see if they should be allowed.
protected  boolean checkSocket(java.net.Socket s)
          Deprecated. Use allowConnection(Socket) instead.
protected  java.net.ServerSocket createServerSocket(int port, int backlog, java.net.InetAddress addr)
          Factory method to manufacture the server socket.
 void denyClient(java.lang.String address)
          Add an IP address to the list of denied clients.
protected static int determinePort(java.lang.String[] argv, int defaultPort)
          Examines command line arguments from argv.
protected  WebServer.Runner getRunner()
           
static void main(java.lang.String[] argv)
          This can be called from command line, but you'll have to edit and recompile to change the server port or handler objects.
(package private)  void releaseRunner(WebServer.Runner runner)
           
 void removeHandler(java.lang.String name)
          Remove a handler object that was previously registered with this server.
 void run()
          Listens for client requests until stopped.
 void setParanoid(boolean p)
          Switch client filtering on/off.
private  void setupServerSocket(int backlog)
          Initializes this server's listener socket with the specified attributes, assuring that a socket timeout has been set.
 void shutdown()
          Stop listening on the server port.
 void start()
          Spawns a new thread which binds this server to the port it's configured to accept connections on.
protected static byte[] toHTTPBytes(java.lang.String text)
          Returns the US-ASCII encoded byte representation of text for HTTP use (as per section 2.2 of RFC 2068).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

xmlrpc

protected XmlRpcServer xmlrpc

serverSocket

protected java.net.ServerSocket serverSocket

listener

protected java.lang.Thread listener

accept

protected java.util.Vector accept

deny

protected java.util.Vector deny

threadpool

protected java.util.Stack threadpool

runners

protected java.lang.ThreadGroup runners

address

private java.net.InetAddress address

port

private int port

paranoid

private boolean paranoid

ctype

protected static final byte[] ctype

clength

protected static final byte[] clength

newline

protected static final byte[] newline

doubleNewline

protected static final byte[] doubleNewline

conkeep

protected static final byte[] conkeep

conclose

protected static final byte[] conclose

ok

protected static final byte[] ok

server

protected static final byte[] server

wwwAuthenticate

protected static final byte[] wwwAuthenticate

HTTP_11

private static final java.lang.String HTTP_11
See Also:
Constant Field Values

STAR

private static final java.lang.String STAR
See Also:
Constant Field Values
Constructor Detail

WebServer

public WebServer(int port)
Creates a web server at the specified port number.


WebServer

public WebServer(int port,
                 java.net.InetAddress addr)
Creates a web server at the specified port number and IP address.


WebServer

public WebServer(int port,
                 java.net.InetAddress addr,
                 XmlRpcServer xmlrpc)
Creates a web server at the specified port number and IP address.

Method Detail

main

public static void main(java.lang.String[] argv)
This can be called from command line, but you'll have to edit and recompile to change the server port or handler objects. By default, it sets up the following responders:

See Also:
addDefaultHandlers()

determinePort

protected static int determinePort(java.lang.String[] argv,
                                   int defaultPort)
Examines command line arguments from argv. If a port may have been provided, parses that port (exiting with error status if the port cannot be parsed). If no port is specified, defaults to defaultPort.

Parameters:
defaultPort - The port to use if none was specified.

toHTTPBytes

protected static final byte[] toHTTPBytes(java.lang.String text)
Returns the US-ASCII encoded byte representation of text for HTTP use (as per section 2.2 of RFC 2068).


createServerSocket

protected java.net.ServerSocket createServerSocket(int port,
                                                   int backlog,
                                                   java.net.InetAddress addr)
                                            throws java.lang.Exception
Factory method to manufacture the server socket. Useful as a hook method for subclasses to override when they desire different flavor of socket (i.e. a SSLServerSocket).

Parameters:
port -
backlog -
addr - If null, binds to INADDR_ANY, meaning that all network interfaces on a multi-homed host will be listening.
Throws:
java.lang.Exception - Error creating listener socket.

setupServerSocket

private void setupServerSocket(int backlog)
                        throws java.lang.Exception
Initializes this server's listener socket with the specified attributes, assuring that a socket timeout has been set. The createServerSocket(int, int, InetAddress) method can be overridden to change the flavor of socket used.

Throws:
java.lang.Exception
See Also:
createServerSocket(int, int, InetAddress)

start

public void start()
Spawns a new thread which binds this server to the port it's configured to accept connections on.

See Also:
run()

addHandler

public void addHandler(java.lang.String name,
                       java.lang.Object target)
Register a handler object with this name. Methods of this objects will be callable over XML-RPC as "name.method".


addDefaultHandlers

protected void addDefaultHandlers()
                           throws java.lang.Exception
Adds the bundled handlers to the server. Called by main(String[]).

Throws:
java.lang.Exception

removeHandler

public void removeHandler(java.lang.String name)
Remove a handler object that was previously registered with this server.


setParanoid

public void setParanoid(boolean p)
Switch client filtering on/off.

See Also:
acceptClient(java.lang.String), denyClient(java.lang.String)

acceptClient

public void acceptClient(java.lang.String address)
                  throws java.lang.IllegalArgumentException
Add an IP address to the list of accepted clients. The parameter can contain '*' as wildcard character, e.g. "192.168.*.*". You must call setParanoid(true) in order for this to have any effect.

Throws:
java.lang.IllegalArgumentException
See Also:
denyClient(java.lang.String), setParanoid(boolean)

denyClient

public void denyClient(java.lang.String address)
                throws java.lang.IllegalArgumentException
Add an IP address to the list of denied clients. The parameter can contain '*' as wildcard character, e.g. "192.168.*.*". You must call setParanoid(true) in order for this to have any effect.

Throws:
java.lang.IllegalArgumentException
See Also:
acceptClient(java.lang.String), setParanoid(boolean)

allowConnection

protected boolean allowConnection(java.net.Socket s)
Checks incoming connections to see if they should be allowed. If not in paranoid mode, always returns true.

Parameters:
s - The socket to inspect.
Returns:
Whether the connection should be allowed.

checkSocket

protected boolean checkSocket(java.net.Socket s)
Deprecated. Use allowConnection(Socket) instead.

DEPRECATED: Do not use this method, it will be removed soon. Use allowConnection(Socket) instead.

See Also:
allowConnection(Socket)

run

public void run()
Listens for client requests until stopped. Call start() to invoke this method, and shutdown() to break out of it.

Specified by:
run in interface java.lang.Runnable
Throws:
java.lang.RuntimeException - Generally caused by either an UnknownHostException or BindException with the vanilla web server.
See Also:
start(), shutdown()

shutdown

public void shutdown()
Stop listening on the server port. Shutting down our listener effectively breaks it out of its run() loop.

See Also:
run()

getRunner

protected WebServer.Runner getRunner()
Returns:

releaseRunner

void releaseRunner(WebServer.Runner runner)
Parameters:
runner -


Copyright © ${year} Apache Software Foundation. All Rights Reserved.