// 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.io.IOException;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class JAXWS {
    public static void test(String url, String actionprefix, String paramns, boolean useaction, boolean usenames) throws SOAPException, IOException {
        Service service = Service.create(new QName("http://soap.ws", "TestService"));
        service.addPort(new QName("http://soap.ws", "TestPort"), SOAPBinding.SOAP11HTTP_BINDING, url);
        Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName("http://soap.ws", "TestPort"), SOAPMessage.class, Service.Mode.MESSAGE);
        dispatch.getRequestContext().put(BindingProvider.SOAPACTION_USE_PROPERTY, useaction);
        MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
        SOAPMessage request;
        SOAPElement operation;
        SOAPMessage response;
        // getOne
        if(useaction) dispatch.getRequestContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, "http://soap.ws/" + actionprefix + "getOne");   
        request = mf.createMessage();
        operation = request.getSOAPBody().addBodyElement(new QName("http://soap.ws", "getOne"));
        SOAPElement f1getone = operation.addChildElement(new QName(paramns, usenames ? "f1" : "arg0"));
        f1getone.addTextNode(Integer.toString(123));
        request.saveChanges();
        response = dispatch.invoke(request);
        Node xo = response.getSOAPBody().getFirstChild().getFirstChild();
        T1 o = new T1(Integer.parseInt(xo.getFirstChild().getTextContent()), xo.getLastChild().getTextContent());
        System.out.println(o.getF1() + " " + o.getF2());
        // getAll
        if(useaction) dispatch.getRequestContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, "http://soap.ws/" + actionprefix + "getAll");   
        request = mf.createMessage();
        operation = request.getSOAPBody().addBodyElement(new QName("http://soap.ws", "getAll"));
        request.saveChanges();
        response = dispatch.invoke(request);
        NodeList xall = response.getSOAPBody().getFirstChild().getFirstChild().getChildNodes();
        for(int i = 0; i < xall.getLength(); i++) {
            Node xone = xall.item(i);
            T1 one = new T1(Integer.parseInt(xone.getFirstChild().getTextContent()), xone.getLastChild().getTextContent());
            System.out.println(one.getF1() + " " + one.getF2());
        }
        // save
        if(useaction) dispatch.getRequestContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, "http://soap.ws/" + actionprefix + "save"); 
        request = mf.createMessage();
        operation = request.getSOAPBody().addBodyElement(new QName("http://soap.ws", "save"));
        SOAPElement f1save = operation.addChildElement(new QName(paramns, usenames ? "f1" : "arg0"));
        f1save.addTextNode(Integer.toString(123));
        SOAPElement f2save = operation.addChildElement(new QName(paramns, usenames ? "f2" : "arg1"));
        f2save.addTextNode("ABC");
        request.saveChanges();
        response = dispatch.invoke(request);
        Node xressave = response.getSOAPBody().getFirstChild().getFirstChild();
        boolean ressave = Boolean.parseBoolean(xressave.getTextContent());
        System.out.println(ressave);
        // saveObject
        if(useaction) dispatch.getRequestContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, "http://soap.ws/" + actionprefix + "saveObject");   
        request = mf.createMessage();
        operation = request.getSOAPBody().addBodyElement(new QName("http://soap.ws", "saveObject"));
        SOAPElement osaveobj = operation.addChildElement(new QName(paramns, usenames ? "o" : "arg0"));
        SOAPElement f1saveobj = osaveobj.addChildElement(new QName("http://soap.ws", "f1", usenames ? "t1ns" : "")); // has to specify prefix
        f1saveobj.addTextNode(Integer.toString(o.getF1()));
        SOAPElement f2saveobj = osaveobj.addChildElement(new QName("http://soap.ws", "f2", usenames ? "t1ns" : "")); // has to specify prefix
        f2saveobj.addTextNode(o.getF2());
        request.saveChanges();
        response = dispatch.invoke(request);
        Node xressaveobj = response.getSOAPBody().getFirstChild().getFirstChild();
        boolean ressaveobj = Boolean.parseBoolean(xressaveobj.getTextContent());
        System.out.println(ressaveobj);
        // delete
        if(useaction) dispatch.getRequestContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, "http://soap.ws/" + actionprefix + "delete");   
        request = mf.createMessage();
        operation = request.getSOAPBody().addBodyElement(new QName("http://soap.ws", "delete"));
        SOAPElement f1delete = operation.addChildElement(new QName(paramns, usenames ? "f1" : "arg0"));
        f1delete.addTextNode(Integer.toString(123));
        request.saveChanges();
        response = dispatch.invoke(request);
        Node xresdelete = response.getSOAPBody().getFirstChild().getFirstChild();
        boolean resdelete = Boolean.parseBoolean(xresdelete.getTextContent());
        System.out.println(resdelete);
    }
    public static void testWithHandler(String url, String actionprefix, String paramns, boolean useaction, boolean usenames) {
        System.out.println(url);
        try {
            test(url, actionprefix, paramns, useaction, usenames);
        } catch(Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    public static void main(String[] args) {
        testWithHandler("http://localhost:8080/axis/services/TestService", "", "http://soap.ws", false, true);
        testWithHandler("http://localhost:8080/wssoap/Test", "", "", false, false);
        testWithHandler("http://localhost/TestService.asmx", "", "http://soap.ws", true, true);
        testWithHandler("http://localhost/Test.svc", "Test/", "http://soap.ws", true, true);
        testWithHandler("http://localhost/TestAlt.svc", "Test/", "", true, true);
    }
}
