Debian Codegraph @

src/java/org/astrogrid/samp/xmlrpc/internal/XmlLoggingInternalServer.java

raw
1 package org.astrogrid.samp.xmlrpc.internal;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.OutputStream;
6 import java.io.PrintStream;
7 import org.astrogrid.samp.httpd.HttpServer;
8
9 /**
10 * Freestanding InternalServer implementation which logs all incoming
11 * and outgoing HTTP data.
12 *
13 * @author Mark Taylor
14 * @since 2 Dec 2008
15 */
16 public class XmlLoggingInternalServer extends InternalServer {
17
18 private final PrintStream out_;
19
20 /**
21 * Constructor based on a given HTTP server.
22 * It is the caller's responsibility to configure and start the HttpServer.
23 *
24 * @param server server for processing HTTP requests
25 * @param path path part of server endpoint (starts with "/");
26 * @param out output stream for loggging
27 */
28 public XmlLoggingInternalServer( HttpServer server, String path,
29 PrintStream out )
30 throws IOException {
31 super( server, path );
32 out_ = out;
33 }
34
35 /**
36 * Constructs a server running with default characteristics
37 * on any free port. The server is started as a daemon thread.
38 *
39 * @param out output stream for loggging
40 */
41 public XmlLoggingInternalServer( PrintStream out ) throws IOException {
42 super();
43 out_ = out;
44 }
45
46 protected HttpServer.Response
47 getXmlRpcResponse( HttpServer.Request request ) {
48 synchronized ( out_ ) {
49 out_.println( "SERVER IN:" );
50 try {
51 out_.write( request.getBody() );
52 }
53 catch ( IOException e ) {
54 }
55 out_.println();
56 }
57 return new LoggingResponse( super.getXmlRpcResponse( request ) );
58 }
59
60 private class LoggingResponse extends HttpServer.Response {
61 final HttpServer.Response base_;
62 LoggingResponse( HttpServer.Response base ) {
63 super( base.getStatusCode(), base.getStatusPhrase(),
64 base.getHeaderMap() );
65 base_ = base;
66 }
67 public void writeBody( OutputStream out ) throws IOException {
68 ByteArrayOutputStream bout = new ByteArrayOutputStream();
69 base_.writeBody( bout );
70 byte[] bbuf = bout.toByteArray();
71 synchronized ( out_ ) {
72 out_.println( "SERVER OUT:" );
73 out_.write( bbuf );
74 out_.println();
75 }
76 out.write( bbuf );
77 }
78 }
79 }