Debian Codegraph @

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

raw
1 package org.astrogrid.samp.xmlrpc.internal;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.PrintStream;
6 import java.net.URL;
7 import java.util.List;
8 import java.util.Map;
9 import org.astrogrid.samp.SampUtils;
10
11 /**
12 * InternalClient subclass which additionally logs all XML-RPC calls/responses
13 * to an output stream.
14 *
15 * @author Mark Taylor
16 * @since 2 Dec 2008
17 */
18 public class RpcLoggingInternalClient extends InternalClient {
19
20 private final PrintStream out_;
21
22 /**
23 * Constructor.
24 *
25 * @param endpoint endpoint
26 * @param out logging output stream
27 */
28 public RpcLoggingInternalClient( URL endpoint, PrintStream out ) {
29 super( endpoint );
30 out_ = out;
31 }
32
33 protected byte[] serializeCall( String method, List paramList )
34 throws IOException {
35 String paramString = SampUtils.formatObject( paramList, 2 );
36 synchronized ( out_ ) {
37 out_.println( "CLIENT OUT:" );
38 out_.println( method );
39 out_.println( paramString );
40 out_.println();
41 }
42 return super.serializeCall( method, paramList );
43 }
44
45 protected Object deserializeResponse( InputStream in )
46 throws IOException {
47 Object response = super.deserializeResponse( in );
48 if ( response == null ||
49 response instanceof String && ((String) response).length() == 0 ||
50 response instanceof Map && ((Map) response).isEmpty() ||
51 response instanceof List && ((List) response).isEmpty() ) {
52 // treat as no response
53 }
54 else {
55 String responseString = SampUtils.formatObject( response, 2 );
56 synchronized ( out_ ) {
57 out_.println( "CLIENT IN:" );
58 out_.println( responseString );
59 out_.println();
60 }
61 }
62 return response;
63 }
64 }