⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soapproxy.java

📁 陕西电信sp客户端
💻 JAVA
字号:
// ----------------------------------------------------------------------------
// $Source: /cvs/vas2006/webpro2/webpro_java/src/com/onewaveinc/portalman/webpro/soap/SOAPProxy.java,v $
// ----------------------------------------------------------------------------
// Copyright (c) 2002 by Onewave Inc.
// ----------------------------------------------------------------------------
// $Id: SOAPProxy.java,v 1.1.1.1 2006/08/01 05:49:34 zhengx Exp $
// ----------------------------------------------------------------------------
// $Log: SOAPProxy.java,v $
// Revision 1.1.1.1  2006/08/01 05:49:34  zhengx
// no message
//
// Revision 1.1  2006/06/02 03:33:17  wuyan
// *** empty log message ***
//
// Revision 1.1  2005/12/08 10:37:59  like
// no message
//
// Revision 1.1  2003/07/28 06:30:15  zengc
// no message
//
// Revision 1.4  2002/09/11 11:47:22  zengc
// WebPro JavaClient V1.3
//
// Revision 1.3  2002/09/02 09:37:55  zengc
// WebPro java Client  V1.2
//
// Revision 1.2  2002/08/15 17:20:57  zengc
// 增加了对参数是String[]数组类型的支持
//
// Revision 1.1  2002/08/09 08:36:12  zengc
// SOAP Tools
//
// ----------------------------------------------------------------------------

package com.onewaveinc.portalman.webpro.soap;

/**
 * <p>Title: PortalMAN SDK API Documentation</p>
 * <p>Description: OneWave Technologies., Inc. PortalMAN Value-add Management Platform 3rd Software Development Kit</p>
 * <p>Copyright: Copyright (c) 2002 </p>
 * <p>Company: OneWave Technologies., Inc.</p>
 * @author 3rd AAA & ICP Integration Developement Team
 * @version 1.5
 */

/*********************************************************

The following parameter types are supported:

1. The following Java primitives and their objectified forms
		boolean, Boolean,
		double, Double,
		float, Float,
		long, Long,
		int, Integer,
		short, Short,
		byte, Byte

   Note: The server side always receives the primitive

2. JavaBeans

   Note:
	a. The JavaBean must not contain other JavaBeans
	b. The JavaBean must not contain vectors or arrays that contain any parameter
	   other than those in 1 and Strings.

3. The following classes:
		String, Vector

   Notes:
	a. A vector may contain all parameters in 1, 2, and Strings. (Exception in 2b)
	b. The server side receives a Vector as an array of Objects

4. Arrays of all parameters in 1, 2, and Strings. (Exception in 2b)

***********************************************************/

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.URL;
import org.apache.soap.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
import java.util.*;
import com.onewaveinc.portalman.webpro.*;

public class SOAPProxy implements InvocationHandler{

	private String urn = null;
	private static URL serverURL = null;
	final private static String _paramName = "Parameter";
	private SOAPMappingRegistry smr = null;
	private BeanSerializer beanSer = null;

	final public static Object newInstance(String url, String urn, Class[] interfaces) throws Exception
	{
		SOAPProxy proxy = new SOAPProxy(url,urn);
		return(proxy.initialize(interfaces));
	}

	// Implementation of the java.lang.reflect.InvocationHandler interface
	final public Object invoke(Object SOAPProxy, Method m, Object[] args) throws Exception
	{
		Call call = new Call();
		call.setTargetObjectURI(urn);
		call.setMethodName(m.getName());
		call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

		//add mapping for Method Return Object ,add by zengcheng 2002-8-7
		Class returnClass = m.getReturnType();
		Object returnObject = null;

		if(returnClass.getName().equals((new String[0]).getClass().getName())){
			returnObject = new String[1];
		}else{
			returnObject = returnClass.newInstance();
		}
		if( isSimple(returnObject) || isSimpleArray(returnObject) )
		{

		}
		else if( isVector(returnObject) )
		{
			addMapping((java.util.Vector) returnObject);
		}
		// if this is an non-simple array then
		// Assume that this is an array of beans
		else if( isArray(returnObject) )
		{
			if( smr == null ){
			smr = new SOAPMappingRegistry();
			}
			if( beanSer == null ){
			beanSer = new BeanSerializer();
			}
			//System.out.println("Adding a default mapping");
			ArraySerializer arraySer = new ArraySerializer();
			smr.mapTypes(Constants.NS_URI_SOAP_ENC, null, null, beanSer, beanSer);
			smr.mapTypes(Constants.NS_URI_SOAP_ENC,null,returnClass, arraySer, arraySer);

		}		// Assume that this is a bean
		else
		{
			if( smr == null ){
			smr = new SOAPMappingRegistry();
			}
			if( beanSer == null ){
			beanSer = new BeanSerializer();
			}
			String qnamePart = returnClass.getName();
			//System.out.println("qnamePart = " + qnamePart);
			smr.mapTypes(Constants.NS_URI_SOAP_ENC,
				 new QName(urn, qnamePart),returnClass, beanSer, beanSer);

		}



		//for arg

		java.util.Vector params = new java.util.Vector();
		for( int i=0; i<args.length; i++ )
		{
			if( isSimple(args[i]) || isSimpleArray(args[i]) )
			{
				params.add(new Parameter(_paramName+(i+1),args[i].getClass(),args[i],null));
			}
			else if( isVector(args[i]) )
			{
				addMapping((java.util.Vector)args[i]);
				params.add(new Parameter(_paramName+(i+1),args[i].getClass(),args[i],null));
			}
			// if this is an non-simple array then
			// Assume that this is an array of beans
			else if( isArray(args[i]) )
			{
				if( smr == null )
					smr = new SOAPMappingRegistry();
				if( beanSer == null )
					beanSer = new BeanSerializer();
				//System.out.println("Adding a default mapping");
				ArraySerializer arraySer = new ArraySerializer();
				smr.mapTypes(Constants.NS_URI_SOAP_ENC, null, null, beanSer, beanSer);
				smr.mapTypes(Constants.NS_URI_SOAP_ENC,null,args[i].getClass(), arraySer, arraySer);
				params.add(new Parameter(_paramName+(i+1),args[i].getClass(),args[i],null));
			}
			// Assume that this is a bean
			else
			{
				if( smr == null )
					smr = new SOAPMappingRegistry();
				if( beanSer == null )
					beanSer = new BeanSerializer();
				String qnamePart = args[i].getClass().getName();
				//System.out.println("qnamePart = " + qnamePart);
				smr.mapTypes(Constants.NS_URI_SOAP_ENC,
							 new QName(urn, qnamePart),args[i].getClass(), beanSer, beanSer);
				params.add(new Parameter(_paramName+(i+1),args[i].getClass(),args[i],null));
			}
		}
		if( params.size() != 0 )
			call.setParams(params);

		if( smr != null )
			call.setSOAPMappingRegistry(smr);

		// Invoke the call.
		Response resp = call.invoke(serverURL, "");
		if( !resp.generatedFault() )
		{
			Parameter ret = resp.getReturnValue();
			return(ret.getValue());
		}
		else
		{
			Fault fault = resp.getFault();

			//add for exception trace,by zengcheng
			Vector entries = fault.getDetailEntries();
			if(entries != null){
				WebProManager.debug("SOAP ServerError encountered: " + fault.getFaultString());
				for (Iterator i = entries.iterator(); i.hasNext(); ) {
				org.w3c.dom.Element entry = (org.w3c.dom.Element)i.next();
				WebProManager.debug(entry.getFirstChild().getNodeValue());
				}
			}

			throw new SOAPException(fault.getFaultCode(),fault.getFaultString());
		}
	}

	// Private methods...

	// Not allowed to "construct" a new instance.
	private SOAPProxy(String url, String urn) throws Exception
	{
		this.urn = urn;
		if(urn == null){
			throw new Exception("SOAP server urn can't be null !");
		}
		if(url == null){
			throw new Exception("SOAP server route URL can't be null !");
		}

		try{
			this.serverURL = new URL(url);
		}catch( Exception e ){
			throw new Exception("SOAP server route URL is invalide !");
		}
	}

	private Object initialize(Class[] interfaces)
	{
		return(Proxy.newProxyInstance(getClass().getClassLoader(),interfaces,this));
	}

	private boolean isSimple(Object o)
	{
		if( o instanceof Integer || o instanceof Double || o instanceof Boolean ||
			o instanceof Byte || o instanceof Character || o instanceof Float ||
			o instanceof String || o instanceof Short || o instanceof Long )
			return(true);

		return(false);
	}

	private boolean isSimpleArray(Object o)
	{
		if( o instanceof int[] || o instanceof boolean[] || o instanceof long[] || o instanceof float[] ||
			o instanceof short[] || o instanceof byte[] || o instanceof java.lang.Integer[] || o instanceof java.lang.Double[] || o instanceof java.lang.Boolean[] ||
			o instanceof java.lang.Byte[] || o instanceof java.lang.Float[] ||
			o instanceof java.lang.String[] || o instanceof java.lang.Short[] || o instanceof java.lang.Long[] )
			return(true);

		return(false);
	}

	private boolean isVector(Object o)
	{
		if( o instanceof java.util.Vector )
			return(true);
		return(false);
	}

	private boolean isArray(Object o)
	{
		if( o instanceof Object[] )
			return(true);
		return(false);
	}

	private void addMapping(java.util.Vector v)
	{
		Object o = v.get(0);
		if( o == null )
			return;

		if( isSimple(o) || isSimpleArray(o) )
		{
			//System.out.println("Vector contains simple elements only.");
		}
		else if( isVector(o) )
		{
			//System.out.println("Recursive Vector...");
			addMapping((java.util.Vector)o);
		}
		else
		{
			if( smr == null )
				smr = new SOAPMappingRegistry();
			if( beanSer == null )
				beanSer = new BeanSerializer();
			//System.out.println("Adding a default mapping");
			smr.mapTypes(Constants.NS_URI_SOAP_ENC, null, null, beanSer, beanSer);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -