Apache-XMLRpc客户端DEBUG测试

         在进行XMLRpc测试不是很方便,于是写了一个测试页面,具体使用方式为:在应用的web.xml中多加入一个Servlet:


		XmlrpcUtilServlet
		com.suntek.vms.app.vvmagent.xmlrpc.XmlrpcUtilServlet
	
	
		XmlrpcUtilServlet
		/rpcservices
	

           对应的XmlrpcUtilServlet类如下:

/**
 * XML-Rpc测试类
 * 
 * @author Administrator
* @version
* @email: [email protected]
* @datetime: 2012-8-24
*/ @SuppressWarnings("rawtypes") public class XmlrpcUtilServlet extends HttpServlet implements XmlRpcMessageListener { /** * */ private static final long serialVersionUID = 1L; private static LogProxy logger = LogHandler.getLogger(); private static ConcurrentHashMap messageStore = new ConcurrentHashMap(); 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, IOException { 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 builder = new StringBuilder(); builder.append(""); builder.append("
Request
"); builder.append( "

"); builder.append("
Response
"); builder.append( "
"); builder.append(""); writer.write(builder.toString()); writer.flush(); } private void index(HttpServletRequest request, HttpServletResponse response) throws IOException { try { StringBuilder builder = new StringBuilder(); builder.append(""); builder.append("
"); 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("
"); builder.append("
"); builder.append(returnType).append(" ").append(name); builder.append("
"); builder.append("
"); builder.append(""); for (int i = 0; i < pTypes.length; i++) { String pType = typeArr[i]; builder.append(""); builder.append(""); builder.append(""); builder.append(""); } builder.append("
").append(pType).append("") .append("") .append("
"); builder.append("
"); builder.append("
"); builder.append("
"); } } builder.append("
"); builder.append(""); 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"); Enumeration enums = request.getParameterNames(); List valList = new ArrayList(); 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 params = new ArrayList(); for (int 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 { params.add(valList.get(i)); } } getClient().execute("SwVvmg." + method, params); } catch (Exception e) { e.printStackTrace(); } } private XmlRpcClient getClient() 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 cla = Class.forName(c); methods = cla.getMethods(); } return methods; } public static ConcurrentHashMap getMessageStore() { return messageStore; } public static String format(String str) { try { SAXReader reader = new SAXReader(); // 注释:创建一个串的字符输入流 StringReader in = new StringReader(str); Document doc = reader.read(in); // 注释:创建输出格式 OutputFormat formater = OutputFormat.createPrettyPrint(); // 注释:设置xml的输出编码 formater.setEncoding("utf-8"); // 注释:创建输出(目标) StringWriter out = new StringWriter(); // 注释:创建输出流 XMLWriter writer = new XMLWriter(out, formater); // 注释:输出格式化的串到目标中,执行后。格式化后的串保存在out中。 writer.write(doc); writer.close(); // 注释:返回我们格式化后的结果 return out.toString(); } catch (Exception e) { e.printStackTrace(); return ""; } } }

           页面效果如下,在xmlrpc-config.properties配置中公布的类中的方法都可以在页面中展现出来,这样就可以很方便的对每个XMLRpc的方法进行单元测试了。

Apache-XMLRpc客户端DEBUG测试_第1张图片

            测试的效果如下:

Apache-XMLRpc客户端DEBUG测试_第2张图片





你可能感兴趣的:(Java,XML-Rpc)