// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package ws.client;

import java.util.*;

import javax.xml.rpc.*;
import javax.xml.rpc.encoding.*;
import javax.xml.namespace.*;

import samples.security.*;

import ws.server.*;
import ws.stub.*;

public class TestTest {
    public static void main(String[] args) throws Exception {
        NameValuePair[] nvp = new NameValuePair[2];
        nvp[0] = new NameValuePair();
        nvp[0].setName("nam1");
        nvp[0].setValue("val1");
        nvp[1] = new NameValuePair();
        nvp[1].setName("nam2");
        nvp[1].setValue("val2");
        testgen(nvp);
        testcodeunsigned(nvp);
        testcodesigned(nvp);
    }
    private static void testgen(NameValuePair[] nvp) throws Exception {
        TestDoubleSigLogService service = new TestDoubleSigLogServiceLocator();
        TestDoubleSigLog tst = service.getTestDoubleSigLogService();
        NameValuePair[] res = tst.execute(nvp);
        for(int i = 0; i < res.length; i++) {
            System.out.println(res[i].getName() + " = " + res[i].getValue());
        }
    }
    private static void testcodeunsigned(NameValuePair[] nvp) throws Exception {
        String endpoint = "http://localhost:8080/axis/services/TestDoubleSigLogService";
        ServiceFactory servfact = ServiceFactory.newInstance();
        Service serv = servfact.createService(new QName("localhost"));
        TypeMappingRegistry tmr = serv.getTypeMappingRegistry();
        TypeMapping tm = tmr.getDefaultTypeMapping();
        tm.register(NameValuePair[].class,
                    new QName("http://server.ws", "ArrayoffNameValuePair"),
                    new org.apache.axis.encoding.ser.ArraySerializerFactory(),
                    new org.apache.axis.encoding.ser.ArrayDeserializerFactory());
        tm.register(NameValuePair.class,
                    new QName("http://server.ws", "NameValuePair"),
                    new org.apache.axis.encoding.ser.BeanSerializerFactory(NameValuePair.class,new QName("http://server.ws", "NameValuePair")),
                    new org.apache.axis.encoding.ser.BeanDeserializerFactory(NameValuePair.class,new QName("http://server.ws", "NameValuePair")));
        Object[] arg = new Object[1];
        arg[0] = nvp;
        org.apache.axis.message.RPCElement rpc = new org.apache.axis.message.RPCElement("http://server.ws", "execute", arg);
        org.apache.axis.message.SOAPEnvelope env = new org.apache.axis.message.SOAPEnvelope();
        env.addBodyElement(rpc);
        Call cal = serv.createCall();
        cal.setTargetEndpointAddress(endpoint);
        cal.setReturnType(new QName("http://server.ws", "ArrayoffNameValuePair"));
        org.apache.axis.message.SOAPEnvelope resp = ((org.apache.axis.client.Call)cal).invoke(env);
        org.apache.axis.message.RPCElement rpcelm = (org.apache.axis.message.RPCElement)resp.getBodyByName("http://server.ws", "executeResponse");
        org.apache.axis.message.RPCParam rpcparam =  rpcelm.getParam("executeReturn");
        NameValuePair[] res = (NameValuePair[])rpcparam.getValue();
        for(int i = 0; i < res.length; i++) {
            System.out.println(res[i].getName() + " = " + res[i].getValue());
        }
    }
    private static void testcodesigned(NameValuePair[] nvp) throws Exception {
        String endpoint = "http://localhost:8080/axis/services/TestDoubleSigLogService";
        ServiceFactory servfact = ServiceFactory.newInstance();
        Service serv = servfact.createService(new QName("localhost"));
        TypeMappingRegistry tmr = serv.getTypeMappingRegistry();
        TypeMapping tm = tmr.getDefaultTypeMapping();
        tm.register(NameValuePair[].class,
                    new QName("http://server.ws", "ArrayoffNameValuePair"),
                    new org.apache.axis.encoding.ser.ArraySerializerFactory(),
                    new org.apache.axis.encoding.ser.ArrayDeserializerFactory());
        tm.register(NameValuePair.class,
                    new QName("http://server.ws", "NameValuePair"),
                    new org.apache.axis.encoding.ser.BeanSerializerFactory(NameValuePair.class,new QName("http://server.ws", "NameValuePair")),
                    new org.apache.axis.encoding.ser.BeanDeserializerFactory(NameValuePair.class,new QName("http://server.ws", "NameValuePair")));
        Object[] arg = new Object[1];
        arg[0] = nvp;
        org.apache.axis.message.RPCElement rpc = new org.apache.axis.message.RPCElement("http://server.ws", "execute", arg);
        org.apache.axis.message.SOAPEnvelope env = new org.apache.axis.message.SOAPEnvelope();
        env.addBodyElement(rpc);
        org.apache.axis.MessageContext msgctx = new org.apache.axis.MessageContext(new org.apache.axis.client.AxisClient());
        msgctx.setTypeMappingRegistry((org.apache.axis.encoding.TypeMappingRegistry)tmr);
        env = new SignedSOAPEnvelope(msgctx, env, "http://xml-security");
        Call cal = serv.createCall();
        cal.setTargetEndpointAddress(endpoint);
        cal.setReturnType(new QName("http://server.ws", "ArrayoffNameValuePair"));
        org.apache.axis.message.SOAPEnvelope resp = ((org.apache.axis.client.Call)cal).invoke(env);
        org.apache.axis.message.RPCElement rpcelm = (org.apache.axis.message.RPCElement)resp.getBodyByName("http://server.ws", "executeResponse");
        org.apache.axis.message.RPCParam rpcparam =  rpcelm.getParam("executeReturn");
        NameValuePair[] res = (NameValuePair[])rpcparam.getValue();
        for(int i = 0; i < res.length; i++) {
            System.out.println(res[i].getName() + " = " + res[i].getValue());
        }
    }
}
