// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package ws.soap;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class JAXRPC {
    public static void test(String url, String opstyle, String encstyle, String actionprefix) throws ServiceException, RemoteException {
        ServiceFactory factory = ServiceFactory.newInstance();
        Service service = factory.createService(new QName("http://soap.ws"));
        Call call = service.createCall();
        call.setTargetEndpointAddress(url);
        call.setProperty(Call.SOAPACTION_USE_PROPERTY, true);
        call.setProperty(Call.OPERATION_STYLE_PROPERTY, opstyle);
        call.setProperty(Call.ENCODINGSTYLE_URI_PROPERTY , encstyle);
        BeanSerializerFactory bsf = new BeanSerializerFactory(T1.class, new QName ("http://soap.ws", "T1"));
        BeanDeserializerFactory bdf = new BeanDeserializerFactory(T1.class, new QName ("http://soap.ws", "T1"));
        service.getTypeMappingRegistry().getDefaultTypeMapping().register(T1.class, new QName ("http://soap.ws", "T1"), bsf, bdf);
        // getOne
        call.setOperationName(new QName("http://soap.ws", "getOne"));
        call.setProperty(Call.SOAPACTION_URI_PROPERTY, "http://soap.ws/" + actionprefix + "getOne");
        call.removeAllParameters();
        call.addParameter("f1", XMLType.XSD_INT, Integer.class, ParameterMode.IN);
        call.setReturnType(new QName("http://soap.ws", "T1"), T1.class);
        T1 o = (T1)call.invoke(new Object[] { 123 });
        System.out.println(o.getF1() + " " + o.getF2());
        // getAll
        call.setOperationName(new QName("http://soap.ws", "getAll"));
        call.setProperty(Call.SOAPACTION_URI_PROPERTY, "http://soap.ws/" + actionprefix + "getAll");
        call.removeAllParameters();
        call.setReturnType(new QName("http://soap.ws", "ArrayOfT1"), T1[].class);
        T1[] all = (T1[])call.invoke(new Object[0]);
        for(T1 one: all) {
            System.out.println(one.getF1() + " " + one.getF2());
        }
        // save
        call.setOperationName(new QName("http://soap.ws", "save"));
        call.setProperty(Call.SOAPACTION_URI_PROPERTY, "http://soap.ws/" + actionprefix + "save");
        call.removeAllParameters();
        call.addParameter("f1", XMLType.XSD_INT, Integer.class, ParameterMode.IN);
        call.addParameter("f2", XMLType.XSD_STRING, String.class, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_BOOLEAN);
        boolean saveres = (Boolean)call.invoke(new Object[] { 123, "ABC" });
        System.out.println(saveres);
        // saveObject
        call.setOperationName(new QName("http://soap.ws", "saveObject"));
        call.setProperty(Call.SOAPACTION_URI_PROPERTY, "http://soap.ws/" + actionprefix + "saveObject");
        call.removeAllParameters();
        call.addParameter("o", new QName("http://soap.ws", "T1"), T1.class, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_BOOLEAN);
        boolean saveobjres = (Boolean)call.invoke(new Object[] { o });
        System.out.println(saveobjres);
        // delete
        call.setOperationName(new QName("http://soap.ws", "delete"));
        call.setProperty(Call.SOAPACTION_URI_PROPERTY, "http://soap.ws/" + actionprefix + "delete");
        call.removeAllParameters();
        call.addParameter("f1", XMLType.XSD_INT, Integer.class, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_BOOLEAN);
        boolean deleteres = (Boolean)call.invoke(new Object[] { 123 });
        System.out.println(deleteres);
    }
    public static void testWithHandler(String url, String opstyle, String encstyle, String actionprefix) {
        System.out.println(url);
        try {
            test(url, opstyle, encstyle, actionprefix);
        } catch(Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    public static void main(String[] args) {
        Logger.getRootLogger().setLevel(Level.FATAL);
        testWithHandler("http://localhost:8080/axis/services/TestService", "rpc", "encoded", "");
        //testWithHandler("http://localhost:8080/wssoap/Test", "rpc", "literal", "");
        //testWithHandler("http://localhost/TestService.asmx", "document", "literal", "");
        //testWithHandler("http://localhost/Test.svc", "document", "literal", "Test/");
        //testWithHandler("http://localhost/TestAlt.svc", "rpc", "literal", "Test/");
    }
}
