Apache XML-RPC is a Java language for XML-RPC protocol encapsulation. The XML-RPC protocol transmits XML fragments over HTTP to implement remote method calls.
In the use of apache-xmlrpc-3.1.3 as the XML-RPC client can not view the message interaction code stream in the log, only through the packet capture tool packet analysis, so modified the Apache-XMLRPC source code, to display interactive XML code streams through logs.
Add listener:
Package Org. apache. XMLRPC;/*** XMLRPC message listener ** @ author administrator <br/> * @ version <br/> * @ Email: zcg@suntektech.com <br/> * @ datetime: 2012-8-28 <br/> */public interface xmlrpcmessagelistener {/*** callback, XML string content that includes client requests and server responses ** @ Param xmlcontent */void onmessage (string xmlcontent );}
Client modification:
Public object sendrequest (xmlrpcrequest prequest) throws xmlrpcexception {xmlrpcstreamrequestconfig Config = (xmlrpcstreamrequestconfig) prequest. getconfig (); Boolean closed = false; try {reqwriter = newreqwriter (prequest); // added by zcg 2012-08-28, the client displays the XML log that sends the request if (reqwriter instanceof xmlrpchttptransport. bytearrayreqwriter & getlistener ()! = NULL) {xmlrpchttptransport. bytearrayreqwriter tmpwriter = (xmlrpchttptransport. bytearrayreqwriter) reqwriter; // system. out. println (tmpwriter. getcontentstring (); getlistener (). onmessage (tmpwriter. getcontentstring ();} writerequest (reqwriter); inputstream istream = getinputstream (); If (isresponsegzipcompressed (config) {istream = new gzipinputstream (istream );} object result = readresponse (config, istr EAM); closed = true; close (); return result;} catch (ioexception e) {Throw new xmlrpcexception ("failed to read server's response:" + E. getmessage (), e);} catch (saxexception e) {exception EX = E. getexception (); If (ex! = NULL & Ex instanceof xmlrpcexception) {Throw (xmlrpcexception) Ex;} Throw new xmlrpcexception ("failed to generate request:" + E. getmessage (), e);} finally {If (! Closed) {try {close () ;}catch (throwable ignore) {}}} protected object readresponse (xmlrpcstreamrequestconfig pconfig, inputstream pstream) throws xmlrpcexception {// added by zcg 2012-09-06 the client displays the XML log if (getlistener () that received the response ()! = NULL) {stringbuilder builder = new stringbuilder (); bufferedreader r = NULL; try {r = new bufferedreader (New inputstreamreader (pstream); string line = ""; while (line = R. readline ())! = NULL) {builder. append (line ). append ("\ n");} getlistener (). onmessage (builder. tostring ();} catch (exception e) {e. printstacktrace ();} finally {If (R! = NULL) {try {R. close () ;}catch (ioexception e) {}} string STR = builder. tostring (); pstream = new bytearrayinputstream (Str. getbytes ();} inputsource isource = new inputsource (pstream); xmlreader xr = newxmlreader (); xmlrpcresponseparser XP; try {XP = new xmlrpcresponseparser (pconfig, getclient (). gettypefactory (); XR. setcontenthandler (XP); XR. parse (isource);} catch (saxexception e) {Throw new xmlrpcclientexception ("failed to parse server's response:" + E. getmessage (), e);} catch (ioexception e) {Throw new xmlrpcclientexception ("failed to read server's response:" + E. getmessage (), e);} If (XP. issuccess () {return XP. getresult ();} throwable T = XP. geterrorcause (); If (t = NULL) {Throw new xmlrpcexception (XP. geterrorcode (), XP. geterrormessage ();} If (T instanceof xmlrpcexception) {Throw (xmlrpcexception) T;} If (T instanceof runtimeexception) {Throw (runtimeexception) T;} Throw new xmlrpcexception (XP. geterrorcode (), XP. geterrormessage (), T );}
Server modification:
/** * Returns, whether the /** Processes a "connection". The "connection" is an * opaque object, which is being handled by the subclasses. * * @param pConfig * The request configuration. * @param pConnection * The "connection" being processed. * @throws XmlRpcException * Processing the request failed. */public void execute(XmlRpcStreamRequestConfig pConfig, ServerStreamConnection pConnection) throws XmlRpcException {log.debug("execute: ->");try {Object result;Throwable error;InputStream istream = null;try {istream = getInputStream(pConfig, pConnection);{// added by zcg 2012-09-06if (getListener() != null) {StringBuilder builder = new StringBuilder();BufferedReader r = null;try {r = new BufferedReader(new InputStreamReader(istream));String line = "";while ((line = r.readLine()) != null) {builder.append(line).append("\n");}getListener().onMessage(builder.toString());} catch (Exception e) {e.printStackTrace();} finally {if (r != null) {r.close();}}String str = builder.toString();istream = new ByteArrayInputStream(str.getBytes());}}XmlRpcRequest request = getRequest(pConfig, istream);result = execute(request);istream.close();istream = null;error = null;log.debug("execute: Request performed successfully");} catch (Throwable t) {logError(t);result = null;error = t;} finally {if (istream != null) {try {istream.close();} catch (Throwable ignore) {}}}boolean contentLengthRequired = isContentLengthRequired(pConfig);ByteArrayOutputStream baos;OutputStream ostream;if (contentLengthRequired) {baos = new ByteArrayOutputStream();ostream = baos;} else {baos = null;ostream = pConnection.newOutputStream();}ostream = getOutputStream(pConnection, pConfig, ostream);try {if (error == null) {writeResponse(pConfig, ostream, result);} else {writeError(pConfig, ostream, error);}ostream.close();ostream = null;} finally {if (ostream != null) {try {ostream.close();} catch (Throwable ignore) {}}}if (baos != null) {// added by zcg 2012-08-28if (getListener() != null) {getListener().onMessage(new String(baos.toByteArray()));}OutputStream dest = getOutputStream(pConfig, pConnection, baos.size());try {baos.writeTo(dest);dest.close();dest = null;} finally {if (dest != null) {try {dest.close();} catch (Throwable ignore) {}}}}pConnection.close();pConnection = null;} catch (IOException e) {throw new XmlRpcException("I/O error while processing request: " + e.getMessage(), e);} finally {if (pConnection != null) {try {pConnection.close();} catch (Throwable ignore) {}}}log.debug("execute: <-");}
Client usage:
Xmlrpcclient instance = new xmlrpcclient (); // configuration information of the Apache XMLRPC client xmlrpcclientconfigimpl Config = new xmlrpcclientconfigimpl (); config. setserverurl (new URL (URL); config. setconnectiontimeout (2*1000); config. setreplytimeout (30*1000); // allows the Apache XMLRPC Extension function config. setenabledforextensions (true); // if it is set to true, the server throws an exception and the client can capture the config. setenabledforexceptions (true); // generate the XMLRPC client instance. settransportfactory (New xmlrpclite14httptransportfactory (Instance); instance. setconfig (config); instance. setlistener (New vvmgwxmlrpcclienthandler ());
Server usage:
public class XmlRpcServerServlet extends XmlRpcServlet implements XmlRpcMessageListener {private static LogProxy logger = LogHandler.getLogger();/** * Creates a new handler mapping. The default implementation loads a * property file from the resource <code>/XmlRpcServlet.properties</code> */protected XmlRpcHandlerMapping newXmlRpcHandlerMapping() throws XmlRpcException {URL url = XmlRpcServerServlet.class.getResource("/resources/xmlrpc-config.properties");if (url == null) {throw new XmlRpcException("Failed to locate resource xmlrpc-config.properties");}try {return newPropertyHandlerMapping(url);} catch (IOException e) {throw new XmlRpcException("Failed to load resource " + url + ": " + e.getMessage(), e);}}public void onMessage(String xmlContent) {if (logger.isTraceEnabled()) {logger.trace("Response: " + xmlContent);}}public XmlRpcMessageListener getListener() {return this;}}