Debian Codegraph @

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

raw
1 package org.astrogrid.samp.xmlrpc.internal;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8 import java.io.PrintStream;
9 import java.util.List;
10 import java.net.URL;
11
12 /**
13 * InternalClient subclass which additionally logs all XML-RPC calls/responses
14 * to an output stream.
15 *
16 * @author Mark Taylor
17 * @since 2 Dec 2008
18 */
19 public class XmlLoggingInternalClient extends InternalClient {
20
21 private final PrintStream out_;
22
23 /**
24 * Constructor.
25 *
26 * @param endpoint endpoint
27 * @param out output stream for logging
28 */
29 public XmlLoggingInternalClient( URL endpoint, PrintStream out ) {
30 super( endpoint );
31 out_ = out;
32 }
33
34 protected byte[] serializeCall( String method, List paramList )
35 throws IOException {
36 byte[] buf = super.serializeCall( method, paramList );
37 synchronized ( out_ ) {
38 out_.println( "CLIENT OUT:" );
39 out_.write( buf );
40 out_.println();
41 }
42 return buf;
43 }
44
45 protected Object deserializeResponse( InputStream in )
46 throws IOException {
47 ByteArrayOutputStream bout = new ByteArrayOutputStream();
48 byte[] buf = new byte[ 1024 ];
49 for ( int nb; ( nb = in.read( buf ) ) >= 0; ) {
50 bout.write( buf, 0, nb );
51 }
52 byte[] obuf = bout.toByteArray();
53 synchronized ( out_ ) {
54 out_.println( "CLIENT IN:" );
55 out_.write( obuf );
56 out_.println();
57 }
58 InputStream copyIn = new ByteArrayInputStream( bout.toByteArray() );
59 return super.deserializeResponse( copyIn );
60 }
61 }