org.apache.xmlrpc

Class WebServer

Implemented Interfaces:
Runnable
Known Direct Subclasses:
SecureWebServer

public class WebServer
extends java.lang.Object
implements Runnable

A minimal web server that exclusively handles XML-RPC requests.
Authors:
Hannes Wallnoefer
Jason van Zyl
Daniel L. 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

private static String
HTTP_11
private static String
STAR
protected Vector
accept
private InetAddress
address
protected static byte[]
clength
protected static byte[]
conclose
protected static byte[]
conkeep
protected static byte[]
ctype
protected Vector
deny
protected static byte[]
doubleNewline
protected Thread
listener
protected static byte[]
newline
protected static byte[]
ok
private boolean
paranoid
private int
port
protected ThreadGroup
runners
protected static byte[]
server
protected ServerSocket
serverSocket
protected 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, 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

void
acceptClient(String address)
Add an IP address to the list of accepted clients.
protected void
addDefaultHandlers()
Adds the bundled handlers to the server.
void
addHandler(String name, Object target)
Register a handler object with this name.
protected boolean
allowConnection(Socket s)
Checks incoming connections to see if they should be allowed.
protected boolean
checkSocket(Socket s)
Deprecated. Use allowConnection(Socket) instead.
protected ServerSocket
createServerSocket(int port, int backlog, InetAddress addr)
Factory method to manufacture the server socket.
void
denyClient(String address)
Add an IP address to the list of denied clients.
protected static int
determinePort(String[] argv, int defaultPort)
Examines command line arguments from argv.
protected WebServer.Runner
getRunner()
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.
void
removeHandler(String name)
Remove a handler object that was previously registered with this server.
(package private) void
repoolRunner(WebServer.Runner runner)
Put runner back into threadpool.
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(String text)
Returns the US-ASCII encoded byte representation of text for HTTP use (as per section 2.2 of RFC 2068).

Field Details

HTTP_11

private static final String HTTP_11

STAR

private static final String STAR

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

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

threadpool

protected Stack threadpool

wwwAuthenticate

protected static final byte[] wwwAuthenticate

xmlrpc

protected XmlRpcServer xmlrpc

Constructor Details

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 Details

acceptClient

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

addDefaultHandlers

protected void addDefaultHandlers()
            throws Exception

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.


createServerSocket

protected ServerSocket createServerSocket(int port,
                                          int backlog,
                                          InetAddress addr)
            throws 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.

denyClient

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

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:
  • A java.lang.String object
  • The java.lang.Math class (making its static methods callable via XML-RPC)
  • An Echo handler that returns the argument array

removeHandler

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

repoolRunner

(package private)  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.

setParanoid

public void setParanoid(boolean p)
Switch client filtering on/off.
See Also:
acceptClient(java.lang.String), denyClient(java.lang.String)

setupServerSocket

private void setupServerSocket(int backlog)
            throws Exception

shutdown

public void shutdown()
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.

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.