SOAP消息发送(拼装消息体)

1、ISoap.java

package com.linkage.lcsmp.interfaces.soap.epg.soapmanager;

import java.rmi.RemoteException;
import java.util.Map;

import org.apache.axiom.om.OMElement;

public interface ISoapSender {
	Map<String,Object> send() throws RemoteException;
	
	OMElement buildMethodBody();
	
	Map<String,Object> buildResult(OMElement ome);
}

2、部分实现父类SoapTemplate.java

package com.linkage.lcsmp.interfaces.soap.epg.soapmanager;

import java.rmi.RemoteException;
import java.util.Map;

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.axis2.AxisFault;
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.commons.logging.Log;

import com.zte.iptv.volcano.core.io.logging.LogFactory;
import com.zte.iptv.volcano.core.io.logging.LogModel;

public abstract class SoapSenderTemplate implements ISoapSender 
{
	private static Log log = LogFactory.getLog(SoapSenderTemplate.class, LogModel.Soap);
	
	//请求soap接口地址,示例:http://10.47.215.153:8088/iptvtest/soap/shanghai/service
	private String address;
	//请求soap接口命名空间:http://webservice.iptv3a.shtel.com/,一般是类所在包名的倒序
	private String nameSapce;
	
	private OMFactory fac;
	private OMNamespace omNameSapce;
	private ServiceClient sender;
	
	
	public SoapSenderTemplate(){}
	
	public SoapSenderTemplate(String address, String nameSapce) 
	{
		this.address = address;
		this.nameSapce = nameSapce;
		//初始化SOAP消息发送器
		init();
	}

	private void init() {
		Options options = new Options();
		// 指定调用WebService的URL
		EndpointReference targetEPR = new EndpointReference(this.address);
		options.setTo(targetEPR);
		options.setProperty(HTTPConstants.CHUNKED, false);
		try {
			sender = new ServiceClient();
			sender.setOptions(options);
		} catch (AxisFault e) {
			log.error("init soap service client error!", e);
		}
		
		fac = OMAbstractFactory.getOMFactory();
		omNameSapce = fac.createOMNamespace(this.nameSapce,"");
	}

	@Override
	public Map<String,Object> send() throws RemoteException {
		//构建SOAP消息体
		OMElement method = buildMethodBody();
		method.build();
		//发送SOAP消息
		OMElement result = sender.sendReceive(method);
		return buildResult(result);
	}

	public OMFactory getFac() {
		return fac;
	}

	public void setFac(OMFactory fac) {
		this.fac = fac;
	}

	public OMNamespace getOmNameSapce() {
		return omNameSapce;
	}

	public void setOmNameSapce(OMNamespace omNameSapce) {
		this.omNameSapce = omNameSapce;
	}
	
	
}

3、具体实现类 AuthSoapSender.java

package com.shtel.iptv3a.webservice;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.axiom.om.OMElement;
import org.apache.commons.logging.Log;

import com.linkage.lcsmp.interfaces.soap.epg.soapmanager.ISoapSender;
import com.linkage.lcsmp.interfaces.soap.epg.soapmanager.SoapSenderTemplate;
import com.zte.iptv.epg.util.EpgUtility;
import com.zte.iptv.volcano.core.io.logging.LogFactory;
import com.zte.iptv.volcano.core.io.logging.LogModel;
import com.zte.iptv.volcano.epg.util.EpgUtils;

/**
 * 对接上海BMS业务鉴权接口
 * @author 10154908
 *
 */
public class ShanghaiAuthSaopSender extends SoapSenderTemplate 
{
	private static Log log = LogFactory.getLog(ShanghaiAuthSaopSender.class, LogModel.Soap);
	
	private Map<String,Object> params;
	
	public ShanghaiAuthSaopSender() {
		super();
	}
	
	public ShanghaiAuthSaopSender(String address, String nameSapce,Map<String, Object> params) {
		super(address, nameSapce);
		this.params = params;
	}

	@Override
	public OMElement buildMethodBody() {
		OMElement ServiceAuth = getFac().createOMElement("ServiceAuth",getOmNameSapce());
		OMElement ServiceAuthIn = getFac().createOMElement("ServiceAuthIn",getOmNameSapce());
		
		OMElement SPID =  getFac().createOMElement("SPID",getOmNameSapce());
		OMElement UserID =  getFac().createOMElement("UserID",getOmNameSapce());
		OMElement UserToken =  getFac().createOMElement("UserToken",getOmNameSapce());
		OMElement ProductID =  getFac().createOMElement("ProductID",getOmNameSapce());
		OMElement ServiceID =  getFac().createOMElement("ServiceID",getOmNameSapce());
		OMElement contentID =  getFac().createOMElement("contentID",getOmNameSapce());
		OMElement TimeStamp =  getFac().createOMElement("TimeStamp",getOmNameSapce());
		OMElement IP =  getFac().createOMElement("IP",getOmNameSapce());
		OMElement TransactionID =  getFac().createOMElement("TransactionID",getOmNameSapce());
		OMElement MAC =  getFac().createOMElement("MAC",getOmNameSapce());
		
		int timestampLength = EpgUtils.getParamAsInt(params.get("timestamplength"),19);
		SPID.addChild(getFac().createOMText(EpgUtils.getParamAsString(params.get("spid"))));
		UserID.addChild(getFac().createOMText(EpgUtils.getParamAsString(params.get("userid"))));
		UserToken.addChild(getFac().createOMText(EpgUtils.getParamAsString(params.get("usertoken"))));
		ProductID.addChild(getFac().createOMText(EpgUtils.getParamAsString(params.get("productid"))));
		ServiceID.addChild(getFac().createOMText(EpgUtils.getParamAsString(params.get("serviceid"))));
		contentID.addChild(getFac().createOMText(EpgUtils.getParamAsString(params.get("contentid"))));
		TimeStamp.addChild(getFac().createOMText(EpgUtils.getParamAsString(System.currentTimeMillis())));
		IP.addChild(getFac().createOMText(EpgUtils.getParamAsString(params.get("ip"))));
		TransactionID.addChild(getFac().createOMText(EpgUtils.getParamAsString(params.get("transactionid"))));
		MAC.addChild(getFac().createOMText(EpgUtils.getParamAsString(params.get("mac"))));
		
		ServiceAuthIn.addChild(SPID);
		ServiceAuthIn.addChild(UserID);
		ServiceAuthIn.addChild(UserToken);
		ServiceAuthIn.addChild(ProductID);
		ServiceAuthIn.addChild(ServiceID);
		ServiceAuthIn.addChild(contentID);
		ServiceAuthIn.addChild(TimeStamp);
		ServiceAuthIn.addChild(IP);
		ServiceAuthIn.addChild(TransactionID);
		ServiceAuthIn.addChild(MAC);
		
		ServiceAuth.addChild(ServiceAuthIn);
		if (log.isDebugEnabled()) 
		{
			log.debug("the shanghai bms auth request soap : " + ServiceAuth);
		}
		return ServiceAuth;
	}

	@Override
	public Map<String,Object> buildResult(OMElement result) {
		if (log.isDebugEnabled()) 
		{
			log.debug("the shanghai bms auth reponse soap : " + result);
		}
		if (result == null) {
			return Collections.emptyMap();
		}
		Iterator<OMElement> firstIter = result.getChildElements();
		if (!firstIter.hasNext()) 
		{
			return Collections.emptyMap();
		}
		Map<String,Object> returnMap = new HashMap<String,Object>();
		OMElement serviceAuthReturn =  firstIter.next();
		Iterator<OMElement> secondIter = serviceAuthReturn.getChildElements();
		while(secondIter.hasNext()) 
		{
			OMElement valueElement = secondIter.next();
			String name = valueElement.getLocalName();
			if ("ProductList".equals(name)) {
				returnMap.put(name, buildProductList(valueElement));
			}
			else
			{
				returnMap.put(name, valueElement.getText());
			}
		}
		return returnMap;
	}

	private List<Map<String,String>> buildProductList(OMElement productListElement) {
		if (productListElement == null) 
		{
			return Collections.emptyList();
		}
		List<Map<String,String>> returnList = new ArrayList<Map<String,String>>();
		Iterator<OMElement> productListElementIter = productListElement.getChildElements();
		while (productListElementIter.hasNext()) {
			OMElement productElement = productListElementIter.next();
			Iterator<OMElement> productElementIter = productElement.getChildElements();
			Map<String,String> product = new HashMap<String,String>();
			while(productElementIter.hasNext()) 
			{
				OMElement valueElement = productElementIter.next();
				product.put(valueElement.getLocalName(), valueElement.getText());
				returnList.add(product);
			}
		}
		return returnList;
	}

	public static void main(String[] args) throws RemoteException {
		String address = "http://IP:PORT/test/soap//service";
		String nameSapce = "http://soap.test.com";
		
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("spid", "123");
		params.put("productid", "dsfs34234");
		params.put("contentid", "11111");
		params.put("ip", "10.47.216.31");
		params.put("serviceid", "aaaaaa");
		params.put("transactionid", "bbbbbb");
		params.put("userid", "00130738");
		params.put("usertoken", "00130738");
		params.put("mac", "00-16-41-EE-C3-D5");
		params.put("timestamp", "201501301343");
		ISoapSender sender = new ShanghaiAuthSaopSender(address, nameSapce, params);
		Map<String,Object> result = sender.send();
		
		System.out.println(result);
		
	}
}


你可能感兴趣的:(SOAP,模板模式)