org.apache.xmlrpc

Class WebServer

public class WebServer extends Object implements Runnable

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

Author: Hannes Wallnoefer Jason van Zyl Daniel L. Rall

Nested Class Summary
classWebServer.AddressMatcher
classWebServer.Connection
classWebServer.Runner
Responsible for handling client connections.
Field Summary
protected Vectoraccept
InetAddressaddress
protected static byte[]clength
protected static byte[]conclose
protected static byte[]conkeep
protected static byte[]ctype
protected Vectordeny
protected static byte[]doubleNewline
static StringHTTP_11
protected Threadlistener
protected static byte[]newline
protected static byte[]ok
booleanparanoid
intport
protected ThreadGrouprunners
protected static byte[]server
protected ServerSocketserverSocket
static StringSTAR
protected Stackthreadpool
protected static byte[]wwwAuthenticate
protected XmlRpcServerxmlrpc
Constructor Summary
WebServer(int port)
Creates a web server at the specified port number.
WebServer(int port, InetAddress addr)
Creates a web server at the specified port number and IP address.
WebServer(int port, InetAddress addr, XmlRpcServer xmlrpc)
Creates a web server at the specified port number and IP address.
Method Summary
voidacceptClient(String address)
Add an IP address to the list of accepted clients.
protected voidaddDefaultHandlers()
Adds the bundled handlers to the server.
voidaddHandler(String name, Object target)
Register a handler object with this name.
protected booleanallowConnection(Socket s)
Checks incoming connections to see if they should be allowed.
protected booleancheckSocket(Socket s)
DEPRECATED: Do not use this method, it will be removed soon.
protected ServerSocketcreateServerSocket(int port, int backlog, InetAddress addr)
Factory method to manufacture the server socket.
voiddenyClient(String address)
Add an IP address to the list of denied clients.
protected static intdeterminePort(String[] argv, int defaultPort)
Examines command line arguments from argv.
protected WebServer.RunnergetRunner()
static voidmain(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.
voidremoveHandler(String name)
Remove a handler object that was previously registered with this server.
voidrepoolRunner(WebServer.Runner runner)
Put runner back into threadpool.
voidrun()
Listens for client requests until stopped.
voidsetParanoid(boolean p)
Switch client filtering on/off.
voidsetupServerSocket(int backlog)
Initializes this server's listener socket with the specified attributes, assuring that a socket timeout has been set.
voidshutdown()
Stop listening on the server port.
voidstart()
Spawns a new thread which binds this server to the port it's configured to accept connections on.
protected static byte[]toHTTPBytes(String text)
Returns the US-ASCII encoded byte representation of text for HTTP use (as per section 2.2 of RFC 2068).

Field Detail

accept

protected Vector accept

address

private InetAddress address

clength

protected static final byte[] clength

conclose

protected static final byte[] conclose

conkeep

protected static final byte[] conkeep

ctype

protected static final byte[] ctype

deny

protected Vector deny

doubleNewline

protected static final byte[] doubleNewline

HTTP_11

private static final String HTTP_11

listener

protected Thread listener

newline

protected static final byte[] newline

ok

protected static final byte[] ok

paranoid

private boolean paranoid

port

private int port

runners

protected ThreadGroup runners

server

protected static final byte[] server

serverSocket

protected ServerSocket serverSocket

STAR

private static final String STAR

threadpool

protected Stack threadpool

wwwAuthenticate

protected static final byte[] wwwAuthenticate

xmlrpc

protected XmlRpcServer xmlrpc

Constructor Detail

WebServer

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

WebServer

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

WebServer

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

Method Detail

acceptClient

public void acceptClient(String address)
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.

See Also: WebServer WebServer

addDefaultHandlers

protected void addDefaultHandlers()
Adds the bundled handlers to the server. Called by (String[]).

addHandler

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

allowConnection

protected boolean allowConnection(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(Socket s)

Deprecated: Use allowConnection(Socket) instead.

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

See Also: allowConnection

createServerSocket

protected ServerSocket createServerSocket(int port, int backlog, InetAddress addr)
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: Exception Error creating listener socket.

denyClient

public void denyClient(String address)
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.

See Also: WebServer WebServer

determinePort

protected static int determinePort(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.

getRunner

protected WebServer.Runner getRunner()

Returns:

main

public static void main(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

removeHandler

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

repoolRunner

void repoolRunner(WebServer.Runner runner)
Put runner back into threadpool.

Parameters: runner The instance to reclaim.

run

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

Throws: RuntimeException Generally caused by either an UnknownHostException or BindException with the vanilla web server.

See Also: start shutdown

setParanoid

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

See Also: WebServer WebServer

setupServerSocket

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

See Also: WebServer

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

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

toHTTPBytes

protected static final byte[] toHTTPBytes(String text)
Returns the US-ASCII encoded byte representation of text for HTTP use (as per section 2.2 of RFC 2068).
Copyright B) 1999-2002 Apache Software Foundation. All Rights Reserved.