代码改变世界

axis2客户端实现

2014-05-12 09:50  卡尔丶  阅读(403)  评论(0编辑  收藏  举报

package com.huawei.sdprpt.sso.utils;

import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Date;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.TypeMapping;
import javax.xml.rpc.encoding.TypeMappingRegistry;
import javax.xml.soap.SOAPException;

import org.apache.axis.MessageContext;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.message.MessageElement;
import org.apache.axis.message.SOAPHeaderElement;


import com.huawei.bme.commons.util.debug.DebugLog;
import com.huawei.bme.commons.util.debug.LogFactory;
import com.huawei.sdp.ib.partnermanagementservices.request.GetPartnerInfoReq;
import com.huawei.sdp.ib.partnermanagementservices.response.GetPartnerInfoRsp;
import com.huawei.sdp.ib.schema.DialectInfo;
import com.huawei.sdp.ib.schema.NamedParameter;
import com.huawei.sdp.ib.schema.TestDevice;
import com.huawei.sdp.ib.schema.partner.PartnerProfile;
import com.huawei.sso.client.util.AppContext;

public class GetSpid {
    
    /**
     * 调测日志记录器。
     */
    private static final DebugLog DEBUGGER = LogFactory.getDebugLog(GetSpid.class);
    
    private final static String endpoint =AppContext.getProperty("sso.ssoib.soapServerName") + "/IB/services/PartnerManagementServices";

    private final static String getPartnerInfo = "getPartnerInfo";

    private static final String namespace = "http://soapheader.ib.sdp.huawei.com";
    
    private static final String localPart = "IBSoapHeader";
    
    private static final String type = "2";

    public String getSpid(String userName) {
        HttpPostUtil httpPostUtil = new HttpPostUtil();
        HttpXMLTools httpXMLTools = new HttpXMLTools();
        Service service = new Service();
        Date date = new Date();
        Call call = null;
        String result = null;
        try {
            call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(getPartnerInfo);
            SOAPHeaderElement header = new SOAPHeaderElement(namespace, localPart);
            header.setNamespaceURI(namespace);
            header.addChildElement("version").addTextNode("V1.0");
            String traceUniqueID = "0000001" + "0405" + HttpPostUtil.SDF_yyMMddHHmmss.format(date) + "0001001";
            
            header.addChildElement("traceUniqueID", "ns1").addTextNode(traceUniqueID);
            header.addChildElement("sourceDeviceType", "ns1").addTextNode(HttpPostUtil.sourceDeviceCode.substring(0, 2));
            header.addChildElement("sourceDeviceCode", "ns1").addTextNode(HttpPostUtil.sourceDeviceCode);
            header.addChildElement("destDeviceType", "ns1").addTextNode("0");
            header.addChildElement("destDeviceCode", "ns1").addTextNode("0000000001");
            header.addChildElement("authenticatorSource", "ns1").addTextNode(httpPostUtil.getAuthenticatorSource(HttpPostUtil.SDF_yyyyMMddHHmmss.format(date)));
            header.addChildElement("timeStamp", "ns1").addTextNode(HttpPostUtil.SDF_yyyyMMddHHmmss.format(date));
            call.addHeader(header);
            
            QName resetPartnerPasswordReq = new QName("getPartnerInfo");

            call.setOperationName(resetPartnerPasswordReq);

            call.addParameter("queryType", XMLType.XSD_STRING, ParameterMode.IN);// 参数的类型

            call.addParameter("account", XMLType.XSD_INT, ParameterMode.IN);

            call.setReturnType(new QName("GetPartnerInfoRsp"),GetPartnerInfoRsp.class);
            call.setReturnType(new QName("PartnerProfile"),PartnerProfile.class);
            call.setReturnType(new QName("NamedParameter"),    NamedParameter.class);
            call.setReturnType(new QName("DialectInfo"), DialectInfo.class);
            call.setReturnType(new QName("TestDevice"), TestDevice.class);
            call.setReturnType(new QName("MessageElement"), MessageElement.class);

            call.addHeader(header);
            TypeMappingRegistry registry = service.getTypeMappingRegistry();
            TypeMapping mapping = registry.createTypeMapping();
            registerBeanMapping(mapping, GetPartnerInfoReq.class,
                    new javax.xml.namespace.QName("GetPartnerInfoReq"));
            registry.register(namespace, mapping);

            call.invoke(new Object[] { type, userName });

            MessageContext response = call.getMessageContext();
            
            if(DEBUGGER.isDebugEnable()){
                DEBUGGER.debug("request:" + response.getRequestMessage().getSOAPPartAsString());
                DEBUGGER.debug("response:" + response.getResponseMessage().getSOAPPartAsString());
            }
            
            String rsp = response.getResponseMessage().getSOAPPartAsString();
            
            result = httpXMLTools.soapXMLTools(rsp);
            
        } catch (ServiceException e1) {
            e1.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (SOAPException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return result;
    }

    protected static void registerBeanMapping(TypeMapping mapping, Class type, QName qname) {
        mapping.register(type, qname, new BeanSerializerFactory(type, qname),
                new BeanDeserializerFactory(type, qname));

    }
    
    
    public static void main(String[] args) {
        GetSpid gs = new GetSpid();
        String partnerId = gs.getSpid("gkq_sp01");
        System.out.println("partnerId:" + partnerId);
    }
}