axis2 调用外部WSDL的工具类
来源:互联网 发布:淘宝店铺信誉度 编辑:程序博客网 时间:2024/05/21 09:32
package com.sinosoft.webservice;import java.io.IOException;import java.util.Date;import org.apache.axiom.om.OMAbstractFactory;import org.apache.axiom.om.OMElement;import org.apache.axiom.om.OMFactory;import org.apache.axiom.om.OMNamespace;import org.apache.axiom.soap.SOAP11Constants;import org.apache.axis2.AxisFault;import org.apache.axis2.Constants;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.client.ServiceClient;import org.apache.axis2.transport.http.HTTPConstants;import org.apache.axis2.transport.http.HttpTransportProperties.ProxyProperties;import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator;import java.util.List; import java.util.Map; import org.apache.axiom.om.OMNode; public class WSDLUtil { OMElement response; static List<String> gList = new ArrayList<String>();public OMElement getResponse() {return response;}public void setResponse(OMElement response) {this.response = response;}//定义WSDL private static EndpointReference targetEPR; private static OMFactory fac = OMAbstractFactory.getOMFactory(); //设置命名空间类似于网关 static OMNamespace omNs ;//构造函数用于获取结果 /**调用构造函数传参说明 * WSDLUntil(String targetWsdl,String namespace,String actionUri,String MethodName,String[] ParamKey,String[] ParamValue) *@param targetWsdl :WSDL地址 如http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl *@param namespace :soap命名空间 如:http://WebXml.com.cn/ *@param actionUri :命名空间+方法名如:http://WebXml.com.cn/getWeatherbyCityName *@param MethodName :方法名:如:getWeatherbyCityName *@param ParamKey :参数的键:如 new String[]{"theCityName","theUserID"} *@param ParamValue :参数的值:如 new String[]{"杭州","01"} */public WSDLUtil(String targetWsdl,String nameSpace,String actionUri,String methodName,String[] paramKey,String[] paramValue) throws AxisFault{targetEPR = new EndpointReference(targetWsdl);omNs = fac.createOMNamespace(nameSpace, "tns");ServiceClient sender = new ServiceClient(); sender.setOptions(buildOptions(actionUri)); OMElement result = sender.sendReceive(buildParam(methodName,paramKey,paramValue)); setResponse(result);} /** * @see 解析参数 * @return */ public static OMElement buildParam(String method,String[] arg,String[] val) { OMElement data = fac.createOMElement(method, omNs); for(int i=0;i<arg.length;i++){ OMElement inner = fac.createOMElement(arg[i], omNs); inner.setText(val[i]); data.addChild(inner); } return data; } /** * @see 设置连接属性 * @return */ public static Options buildOptions(String action){ Options options = new Options(); options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI); // options.setAction("http://WebXml.com.cn/getSupportCity"); options.setTo(targetEPR); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); options.setProperty(HTTPConstants.CHUNKED, "false");//设置不受限制. //options.setProperty(HTTPConstants.PROXY, buildProxy()); options.setProperty(Constants.Configuration.HTTP_METHOD,HTTPConstants.HTTP_METHOD_POST); options.setAction(action); return options; } /** * @see 设置代理属性 * @return */ public static ProxyProperties buildProxy(){ ProxyProperties proxyProperties=new ProxyProperties(); proxyProperties.setProxyName("172.19.18.22"); proxyProperties.setProxyPort(8080); return proxyProperties; } /** * 遍历全部节点,将节点放入Map返回 * @param element * @return */ @SuppressWarnings("unchecked")public static Map getResults(OMElement element) { if (element == null) { return null; } Iterator iter = element.getChildElements(); Map map = new HashMap(); while (iter.hasNext()) { OMNode omNode = (OMNode) iter.next(); if (omNode.getType() == OMNode.ELEMENT_NODE) { OMElement omElement = (OMElement) omNode; String key = omElement.getLocalName().trim(); // System.out.println("sta: " + key); String value = omElement.getText().trim(); map.put(key, value); } } return map; } /** * 遍历当前父节点下的所有子节点 * @param element OMElement 对象 * @param parentNode 父节点 * @return List */ @SuppressWarnings("unchecked")public static List getNodeList(OMElement element, String parentNode) throws java.io.IOException{ if (element == null || parentNode == null) { return null; } List list = new ArrayList(); Iterator<OMElement> iter = element.getChildElements(); while(iter.hasNext()){ OMElement node = iter.next(); if(node.getLocalName().equals(parentNode)){ Map map = new HashMap(); Iterator<OMElement> iter1 = node.getChildElements(); while(iter1.hasNext()){ OMElement node1 = iter1.next(); map.put(node1.getLocalName(), node1.getText()); //System.out.println(node1.getLocalName()+":"+node1.getText()); } list.add(map);//System.out.println(list); } } return list; } @SuppressWarnings("unchecked") public static List<String> getAllElementList(OMElement element){ getChildNodes(element); return gList; } @SuppressWarnings("unchecked")public static List getChildNodes(OMElement element){ //List list = new ArrayList(); Iterator<OMElement> iterator = element.getChildElements(); while(iterator.hasNext()){ OMElement node = iterator.next(); String eleName = node.getLocalName(); String eleValue = node.getText(); gList.add(eleName+":"+eleValue); //System.out.println(eleName+"++:"+eleValue); List tmpList2 = getChildNodes(node); } return gList; }}