axis2 调用外部WSDL的工具类

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;
	 
 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值