Class InternalServer
- java.lang.Object
-
- org.astrogrid.samp.xmlrpc.internal.InternalServer
-
- All Implemented Interfaces:
SampXmlRpcServer
- Direct Known Subclasses:
RpcLoggingInternalServer
,XmlLoggingInternalServer
public class InternalServer extends java.lang.Object implements SampXmlRpcServer
SampXmlRpcServer implementation without external dependencies. ThereqInfo
argument passed to thehandleCall
method of registeredSampXmlRpcHandler
s is the associatedHttpServer.Request
.- Since:
- 27 Aug 2008
- Author:
- Mark Taylor
-
-
Field Summary
Fields Modifier and Type Field Description private java.net.URL
endpoint_
private static HttpServer.Response
GET_RESPONSE
private java.util.List
handlerList_
private static HttpServer.Response
HEAD_RESPONSE
private static java.util.logging.Logger
logger_
private HttpServer
server_
private HttpServer.Handler
serverHandler_
-
Constructor Summary
Constructors Constructor Description InternalServer()
Constructs a server running with default characteristics.InternalServer(HttpServer httpServer, java.lang.String path)
Constructor based on a given HTTP server.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addHandler(SampXmlRpcHandler handler)
Adds a handler which can service certain XML-RPC methods.private static HttpServer.Response
createInfoResponse(boolean withData)
Returns a simple response suitable for GET/HEAD at the XML-RPC server's endpoint.java.net.URL
getEndpoint()
Returns the server's endpoint.private byte[]
getFaultBytes(java.lang.Throwable error)
Turns an exception into an array of bytes giving an XML-RPC methodResponse (fault) document.HttpServer
getHttpServer()
Returns the HTTP server hosting this XML-RPC server.private byte[]
getResultBytes(java.lang.Object result)
Turns a SAMP-friendly (string, list, map only) object into an array of bytes giving an XML-RPC methodResponse document.protected HttpServer.Response
getXmlRpcResponse(HttpServer.Request request)
Returns the HTTP response object given an incoming XML-RPC POST request.private java.lang.Object
getXmlRpcResult(HttpServer.Request request)
Returns the SAMP-friendly (string, list and map only) object representing the reply to an XML-RPC request given by a request.protected java.lang.Object
handleCall(SampXmlRpcHandler handler, java.lang.String methodName, java.util.List paramList, HttpServer.Request request)
Actually passes the XML-RPC method name and parameter list to one of the registered servers for processing.void
removeHandler(SampXmlRpcHandler handler)
Removes a previously-added handler.
-
-
-
Field Detail
-
server_
private final HttpServer server_
-
endpoint_
private final java.net.URL endpoint_
-
handlerList_
private final java.util.List handlerList_
-
serverHandler_
private final HttpServer.Handler serverHandler_
-
GET_RESPONSE
private static final HttpServer.Response GET_RESPONSE
-
HEAD_RESPONSE
private static final HttpServer.Response HEAD_RESPONSE
-
logger_
private static final java.util.logging.Logger logger_
-
-
Constructor Detail
-
InternalServer
public InternalServer(HttpServer httpServer, java.lang.String path) throws java.io.IOException
Constructor based on a given HTTP server. It is the caller's responsibility to configure and start the HttpServer.- Parameters:
httpServer
- server for processing HTTP requestspath
- path part of server endpoint (starts with "/");- Throws:
java.io.IOException
-
InternalServer
public InternalServer() throws java.io.IOException
Constructs a server running with default characteristics. Currently, the default serverUtilServer.getInstance()
is used.- Throws:
java.io.IOException
-
-
Method Detail
-
getEndpoint
public java.net.URL getEndpoint()
Description copied from interface:SampXmlRpcServer
Returns the server's endpoint.- Specified by:
getEndpoint
in interfaceSampXmlRpcServer
- Returns:
- URL to which XML-RPC requests are POSTed
-
getHttpServer
public HttpServer getHttpServer()
Returns the HTTP server hosting this XML-RPC server.- Returns:
- http server
-
addHandler
public void addHandler(SampXmlRpcHandler handler)
Description copied from interface:SampXmlRpcServer
Adds a handler which can service certain XML-RPC methods.- Specified by:
addHandler
in interfaceSampXmlRpcServer
- Parameters:
handler
- handler to add
-
removeHandler
public void removeHandler(SampXmlRpcHandler handler)
Description copied from interface:SampXmlRpcServer
Removes a previously-added handler.- Specified by:
removeHandler
in interfaceSampXmlRpcServer
- Parameters:
handler
- handler to remove
-
getXmlRpcResponse
protected HttpServer.Response getXmlRpcResponse(HttpServer.Request request)
Returns the HTTP response object given an incoming XML-RPC POST request. Any error should be handled by returning a fault-type methodResponse element rather than by throwing an exception.- Parameters:
request
- POSTed HTTP request- Returns:
- XML-RPC response (possibly fault)
-
getXmlRpcResult
private java.lang.Object getXmlRpcResult(HttpServer.Request request) throws java.lang.Exception
Returns the SAMP-friendly (string, list and map only) object representing the reply to an XML-RPC request given by a request.- Parameters:
request
- POSTed HTTP request- Returns:
- SAMP-friendly object
- Throws:
java.lang.Exception
- in case of error (will become XML-RPC fault)
-
handleCall
protected java.lang.Object handleCall(SampXmlRpcHandler handler, java.lang.String methodName, java.util.List paramList, HttpServer.Request request) throws java.lang.Exception
Actually passes the XML-RPC method name and parameter list to one of the registered servers for processing.- Parameters:
handler
- handler which has declared it can handle the named methodmethodName
- XML-RPC method nameparamList
- list of parameters to XML-RPC callrequest
- HTTP request from which this call originated- Throws:
java.lang.Exception
-
getResultBytes
private byte[] getResultBytes(java.lang.Object result) throws java.io.IOException
Turns a SAMP-friendly (string, list, map only) object into an array of bytes giving an XML-RPC methodResponse document.- Parameters:
result
- SAMP-friendly object- Returns:
- XML methodResponse document as byte array
- Throws:
java.io.IOException
-
getFaultBytes
private byte[] getFaultBytes(java.lang.Throwable error) throws java.io.IOException
Turns an exception into an array of bytes giving an XML-RPC methodResponse (fault) document.- Parameters:
error
- throwable- Returns:
- XML methodResponse document as byte array
- Throws:
java.io.IOException
-
createInfoResponse
private static HttpServer.Response createInfoResponse(boolean withData)
Returns a simple response suitable for GET/HEAD at the XML-RPC server's endpoint.- Parameters:
withData
- true for text (GET), false for no text (HEAD)- Returns:
- HTTP response
-
-