Apache-XMLRPC client debug Test

Source: Internet
Author: User

The XMLRPC test is not very convenient, so I wrote a test page. The specific usage is: Add a servlet to the Web. xml file of the application:

<servlet><servlet-name>XmlrpcUtilServlet</servlet-name><servlet-class>com.suntek.vms.app.vvmagent.xmlrpc.XmlrpcUtilServlet</servlet-class></servlet><servlet-mapping><servlet-name>XmlrpcUtilServlet</servlet-name><url-pattern>/rpcservices</url-pattern></servlet-mapping>

The corresponding xmlrpcutilservlet class is as follows:

/*** XML-RPC test class ** @ author administrator <br/> * @ version <br/> * @ Email: zcg@suntektech.com <br/> * @ datetime: 2012-8-24 <br/> */@ suppresswarnings ("rawtypes") public class xmlrpcutilservlet extends httpservlet implementsxmlrpcmessagelistener {/***/Private Static final long serialversionuid = 1l; private Static logproxy logger = loghandler. getlogger (); Private Static concurrenthashmap <string, strin G> messagestore = new concurrenthashmap <string, string> (); Private Static method [] Methods = NULL; Private Static int flag = 0; Private Static final string url = "http: // 127.0.0.1: "+ system. getproperty (bootconstants. app_rpc_port) + "/" + system. getproperty (bootconstants. app_type) + "/XMLRPC"; protected void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexce Ption {string type = request. getparameter ("type"); If (type = NULL) {index (request, response); return;} else {rpchandle (request, response);} response. setcontenttype ("text/html"); printwriter writer = response. getwriter (); string reqstring = messagestore. get ("request"); flag = 0; string respstring = messagestore. get ("response"); reqstring = format (reqstring); respstring = format (respstring); stringbuilder B Uilder = new stringbuilder (); builder. append ("<body style = 'font-family: Arial; '>"); builder. append ("<Div style = 'font-size: 16px; font-weight: bold; '> request </div>"); builder. append ("<Div style = 'width: 80% '> <textarea style = 'font-size: 14px; width: 800px; Height: 280px;'> "). append (reqstring ). append ("</textarea> </div> <br/>"); builder. append ("<Div style = 'font-size: 16px; font-weight: bold; '> response </div>"); builder. AP Pend ("<Div style = 'width: 80% '> <textarea style = 'font-size: 14px; width: 800px; Height: 280px;'> "). append (respstring ). append ("</textarea> </div>"); builder. append ("</body>"); writer. write (builder. tostring (); writer. flush ();} private void index (httpservletrequest request, httpservletresponse response) throws ioexception {try {stringbuilder builder = new stringbuilder (); builder. append ("<body style = 'font-family: Rial; '> "); builder. append ("<Div style = 'padding: 10px; '>"); For (method M: getmethods () {string name = m. getname (); If (name. charat (0) <= 90 & name. charat (0)> = 65) {class [] ptypes = m. getparametertypes (); string returntype = m. getreturntype (). getsimplename (); string [] typearr = new string [ptypes. length]; for (INT I = 0; I <ptypes. length; I ++) {typearr [I] = ptypes [I]. getsimplename ();} string action =" Http: // "+ request. getlocaladdr () +": "+ request. getlocalport () +"/"+ system. getproperty (bootconstants. app_type) +"/rpcservices? Type = "+ name; builder. append ("<form style = 'margin: 0; padding: 0; 'Action = '"). append (action ). append ("'method = 'post'>"); builder. append ("<Div style = 'border: 1px solid # CCC; margin: 5px; width: 350px; Height: 200px; float: Left; '>"); builder. append (returntype ). append (""). append (name); builder. append ("<input type = 'submit 'value = 'invoke'/> <br/>"); builder. append ("<Div style = 'margin-left: 35px; '>"); builder. append (" <Table> "); For (INT I = 0; I <ptypes. length; I ++) {string ptype = typearr [I]; builder. append ("<tr>"); builder. append ("<TD> "). append (ptype ). append ("</TD>"); builder. append ("<TD> "). append ("<input type = 'text' name = 'Param _"). append (I ). append ("'style = 'border: 1px solid # CCC; width: 200px; Height: 20px; '/> "). append ("</TD>"); builder. append ("</tr>");} builder. append ("</table>"); builder. append ("</div>"); builder. A Ppend ("</div>"); builder. append ("</form>") ;}} builder. append ("</div>"); builder. append ("</body>"); response. setcontenttype ("text/html"); printwriter writer = response. getwriter (); writer. write (builder. tostring (); writer. flush ();} catch (exception e) {e. printstacktrace () ;}} private void rpchandle (httpservletrequest request, httpservletresponse response) {try {string method = request. getparameter ("type"); en Umeration <string> enums = request. getparameternames (); List <string> vallist = new arraylist <string> (); While (enums. hasmoreelements () {string key = enums. nextelement (); If (! Key. equals ("type") {string value = request. getparameter (key); vallist. add (value) ;}} int length = vallist. size (); string [] objects = new string [length]; for (method M: getmethods () {string name = m. getname (); If (name. equals (method) {class [] classes = m. getparametertypes (); For (INT I = 0; I <length; I ++) {objects [I] = classes [I]. getsimplename () ;}}list <Object> Params = new arraylist <Object> (); for (I NT I = 0; I <length; I ++) {If (objects [I]. equals ("integer") | objects [I]. equals ("int") {Params. add (integer. parseint (vallist. get (I);} else if (objects [I]. equals ("long") | objects [I]. equals ("long") {Params. add (Long. parselong (vallist. get (I);} else parameter params.add(vallist.get( I ?###getclient(cmd.exe cute ("swvvmg. "+ method, Params);} catch (exception e) {e. printstacktrace () ;}} private xmlrpcclient get Client () throws exception {xmlrpcclientconfigimpl Config = new xmlrpcclientconfigimpl (); config. setserverurl (new URL (URL); config. setenabledforextensions (true); xmlrpcclient client = new xmlrpcclient (); client. setconfig (config); client. setlistener (this); Return client;} public void onmessage (string xmlcontent) {flag ++; If (flag = 1) {messagestore. put ("request", xmlcontent);} If (logger. istraceenabled ()) {Logger. trace ("[xmlrpcutilservlet. onmessage] request: "+ format (xmlcontent) ;}} private method [] getmethods () throws exception {If (Methods = NULL) {inputstream input = xmlrpcutilservlet. class. getresourceasstream ("/resources/xmlrpc-config.properties"); properties Properties = new properties (); properties. load (input); string c = properties. getproperty ("swvvmg"); Class linoleic = Class. forname (c); methods = C La. getmethods () ;}return methods;} public static concurrenthashmap <string, string> getmessagestore () {return messagestore;} public static string format (string Str) {try {saxreader reader = new saxreader (); // Note: Create a string of Character Input streams stringreader in = new stringreader (STR); document DOC = reader. read (in); // Note: Create output format outputformat formater = outputformat. createprettyprint (); // Note: Set the XML output encoding formater. setencoding ("UTF -8 "); // Note: Create output (target) stringwriter out = new stringwriter (); // Note: Create output stream xmlwriter writer = new xmlwriter (Out, formater ); // Note: output formatted strings to the target, after execution. The formatted string is saved in the out. Writer. write (DOC); writer. close (); // Note: return the formatted result return out. tostring ();} catch (exception e) {e. printstacktrace (); Return "";}}}

The page effect is as follows, the methods in the class published in The xmlrpc-config.properties configuration can be displayed on the page, so that you can easily perform a unit test for each XMLRPC method.

The test results are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.