| 1 |
package org.astrogrid.samp.xmlrpc.internal; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.List; |
| 6 |
import org.astrogrid.samp.SampUtils; |
| 7 |
import org.astrogrid.samp.httpd.HttpServer; |
| 8 |
import org.astrogrid.samp.xmlrpc.SampXmlRpcHandler; |
| 9 |
|
| 10 |
/** |
| 11 |
* InternalServer subclass which additionally logs all XML-RPC calls/responses |
| 12 |
* to an output stream. |
| 13 |
* |
| 14 |
* @author Mark Taylor |
| 15 |
* @since 2 Dec 2008 |
| 16 |
*/ |
| 17 |
public class RpcLoggingInternalServer extends InternalServer { |
| 18 |
|
| 19 |
private final PrintStream out_; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor based on a given HTTP server. |
| 23 |
* It is the caller's responsibility to configure and start the HttpServer. |
| 24 |
* |
| 25 |
* @param server server for processing HTTP requests |
| 26 |
* @param path path part of server endpoint (starts with "/"); |
| 27 |
* @param out output stream for logging |
| 28 |
*/ |
| 29 |
public RpcLoggingInternalServer( HttpServer server, String path, |
| 30 |
PrintStream out ) |
| 31 |
throws IOException { |
| 32 |
super( server, path ); |
| 33 |
out_ = out; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Constructs a server running with default characteristics |
| 38 |
* on any free port. The server is started as a daemon thread. |
| 39 |
* |
| 40 |
* @param out output stream for logging |
| 41 |
*/ |
| 42 |
public RpcLoggingInternalServer( PrintStream out ) throws IOException { |
| 43 |
super(); |
| 44 |
out_ = out; |
| 45 |
} |
| 46 |
|
| 47 |
protected Object handleCall( SampXmlRpcHandler handler, String methodName, |
| 48 |
List paramList, HttpServer.Request request ) |
| 49 |
throws Exception { |
| 50 |
String paramString = SampUtils.formatObject( paramList, 2 ); |
| 51 |
synchronized ( out_ ) { |
| 52 |
out_.println( "SERVER IN:" ); |
| 53 |
out_.println( methodName ); |
| 54 |
out_.println( paramString ); |
| 55 |
out_.println(); |
| 56 |
} |
| 57 |
final Object result; |
| 58 |
try { |
| 59 |
result = super.handleCall( handler, methodName, paramList, |
| 60 |
request ); |
| 61 |
} |
| 62 |
catch ( Throwable e ) { |
| 63 |
synchronized ( out_ ) { |
| 64 |
out_.println( "SERVER ERROR:" ); |
| 65 |
out_.println( methodName ); |
| 66 |
e.printStackTrace( out_ ); |
| 67 |
out_.println(); |
| 68 |
} |
| 69 |
if ( e instanceof Error ) { |
| 70 |
throw (Error) e; |
| 71 |
} |
| 72 |
else { |
| 73 |
throw (Exception) e; |
| 74 |
} |
| 75 |
} |
| 76 |
String resultString = SampUtils.formatObject( result, 2 ); |
| 77 |
synchronized ( out_ ) { |
| 78 |
out_.println( "SERVER OUT:" ); |
| 79 |
out_.println( methodName ); |
| 80 |
out_.println( resultString ); |
| 81 |
out_.println(); |
| 82 |
} |
| 83 |
return result; |
| 84 |
} |
| 85 |
} |