Using SOAP

Here is a complete working example of how to build and install a soap service. The service name is called Candidates. It has two methods

String getPresident(String party, String date)

String getVicePresident(String party, String date)
<\pre>

Note: the set of data is pretty limited and the error handling is poor. Step 1: Write the server application

Make a subdirectory called candidates You will need this because everything is in a package called candidates. Put these two files in it.

Candidates.java

package candidates;

public interface Candidates {
    public String getPresident(String party, String date);
    public String getVicePresident(String party, String date);
}

CandidatesImpl.java

package candidates;

public class CandidatesImpl implements Candidates {
    public String getPresident(String party, String date) {
          if (party.equals("Republican")) {
             if (date.equals("2004") || date.equals("2000"))
                      return "George Bush";
             else if (date.equals("1996")) 
                     return "Bob Dole";
             else if (date.equals("1992"))
                     return "George Bush";
          }
          else if (party.equals("Democrat")) {
             if (date.equals("2004"))
                      return "John Kerry";
             else if (date.equals("2000")) 
                     return "Al Gore";
             else if (date.equals("1996"))
                     return "Bill Clinton";
          }
          return null;
    }
    public String getVicePresident(String party, String date) {
          if (party.equals("Republican")) {
             if (date.equals("2004") || date.equals("2000"))
                      return "Dick Cheney";
             else if (date.equals("1996")) 
                     return "Jack Kemp";
             else if (date.equals("1992"))
                     return "Dan Quayle";
          }
          else if (party.equals("Democrat")) {
             if (date.equals("2004"))
                      return "John Edwards";
             else if (date.equals("2000")) 
                     return "Joel Lieberman";
             else if (date.equals("1996"))
                     return "Al Gore";
          }
          return null;
	  }
}

Step 2:

Run Java2WSDL to create a wsdl file for this application.

java org.apache.axis.wsdl.Java2WSDL 
     -o candidates.wsdl
     -l http:\\localhost:8080/axis/services/candidates
     -n urn:candidates
     -p"candidates" urn:candidates
     candidates.Candidates

Where:
-o option is the name of hte output file
-l is the url of the web service
-n is the target namespace for the wsdl
-p maps the java package to the name space

This generates the wsdl file candidates.wsdl

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="urn:candidates" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="urn:candidates" xmlns:intf="urn:candidates" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.3
Built on Oct 05, 2005 (05:23:37 EDT)-->
   <wsdl:message name="getVicePresidentResponse">
      <wsdl:part name="getVicePresidentReturn" type="soapenc:string"/>
   </wsdl:message>
   <wsdl:message name="getPresidentRequest">
      <wsdl:part name="in0" type="soapenc:string"/>
      <wsdl:part name="in1" type="soapenc:string"/>
   </wsdl:message>
   <wsdl:message name="getVicePresidentRequest">
      <wsdl:part name="in0" type="soapenc:string"/>
      <wsdl:part name="in1" type="soapenc:string"/>
   </wsdl:message>
   <wsdl:message name="getPresidentResponse">
      <wsdl:part name="getPresidentReturn" type="soapenc:string"/>
   </wsdl:message>
   <wsdl:portType name="Candidates">
      <wsdl:operation name="getPresident" parameterOrder="in0 in1">
         <wsdl:input message="impl:getPresidentRequest" name="getPresidentRequest"/>
         <wsdl:output message="impl:getPresidentResponse" name="getPresidentResponse"/>
      </wsdl:operation>
      <wsdl:operation name="getVicePresident" parameterOrder="in0 in1">
         <wsdl:input message="impl:getVicePresidentRequest" name="getVicePresidentRequest"/>
         <wsdl:output message="impl:getVicePresidentResponse" name="getVicePresidentResponse"/>
      </wsdl:operation>
   </wsdl:portType>
   <wsdl:binding name="candidatesSoapBinding" type="impl:Candidates">
      <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
      <wsdl:operation name="getPresident">
         <wsdlsoap:operation soapAction=""/>
         <wsdl:input name="getPresidentRequest">
            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:candidates" use="encoded"/>
         </wsdl:input>
         <wsdl:output name="getPresidentResponse">
            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:candidates" use="encoded"/>
         </wsdl:output>
      </wsdl:operation>
      <wsdl:operation name="getVicePresident">
         <wsdlsoap:operation soapAction=""/>
         <wsdl:input name="getVicePresidentRequest">
            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:candidates" use="encoded"/>
         </wsdl:input>
         <wsdl:output name="getVicePresidentResponse">
            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:candidates" use="encoded"/>
         </wsdl:output>
      </wsdl:operation>
   </wsdl:binding>
   <wsdl:service name="CandidatesService">
      <wsdl:port binding="impl:candidatesSoapBinding" name="candidates">
         <wsdlsoap:address location="http://localhost:8080/axis/services/candidates"/>
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>
    <hr>
    <address><a href="mailto:"></a></address>
<!-- Created: Fri Mar 31 11:29:58 Eastern Standard Time 2006 -->
<!-- hhmts start -->
<!-- hhmts end -->

Step 3:

Run WSDL2Java to generate the various stub files and wrapper code

java org.apache.axis.wsdl.WSDL2Java
   -o .
   -d Session
   -s
   -p candidates.gen
   candidates.wsdl
The input is the wsdl file (the last argument), the -d argument is the scope of deployment, the -s option tells the program to generate server side code as well as client code, and the p flag is the name of the package to place the code.

This will create a new subdirctory candidates\gen and write five java source files, a deployer and an undeployer to it.

Candidates.java

/**
 * Candidates.java
 *
 * This file was auto-generated from WSDL
 * by the Apache Axis 1.3 Oct 05, 2005 (05:23:37 EDT) WSDL2Java emitter.
 */

package candidates.gen;

public interface Candidates extends java.rmi.Remote {
    public java.lang.String getPresident(java.lang.String in0, 
            java.lang.String in1) throws java.rmi.RemoteException;
    public java.lang.String getVicePresident(java.lang.String in0, 
            java.lang.String in1) throws java.rmi.RemoteException;
}

CandidatesServiceLocator.java

/**
 * CandidatesServiceLocator.java
 *
 * This file was auto-generated from WSDL
 * by the Apache Axis 1.3 Oct 05, 2005 (05:23:37 EDT) WSDL2Java emitter.
 */

package candidates.gen;

public class CandidatesServiceLocator extends org.apache.axis.client.Service 
         implements candidates.gen.CandidatesService {

    public CandidatesServiceLocator() {
    }


   public CandidatesServiceLocator(org.apache.axis.EngineConfiguration config)
   {
        super(config);
    }

    public CandidatesServiceLocator(java.lang.String wsdlLoc, 
      javax.xml.namespace.QName sName) throws javax.xml.rpc.ServiceException {
        super(wsdlLoc, sName);
    }

    // Use to get a proxy class for candidates
    private java.lang.String candidates_address = 
            "http://localhost:8080/axis/services/candidates";

    public java.lang.String getcandidatesAddress() {
        return candidates_address;
    }

    // The WSDD service name defaults to the port name.
    private java.lang.String candidatesWSDDServiceName = "candidates";

    public java.lang.String getcandidatesWSDDServiceName() {
        return candidatesWSDDServiceName;
    }

    public void setcandidatesWSDDServiceName(java.lang.String name) {
        candidatesWSDDServiceName = name;
    }

    public candidates.gen.Candidates getcandidates() throws 
            javax.xml.rpc.ServiceException {
       java.net.URL endpoint;
        try {
            endpoint = new java.net.URL(candidates_address);
        }
        catch (java.net.MalformedURLException e) {
            throw new javax.xml.rpc.ServiceException(e);
        }
        return getcandidates(endpoint);
    }

    public candidates.gen.Candidates getcandidates(java.net.URL portAddress) 
                throws javax.xml.rpc.ServiceException {
        try {
            candidates.gen.CandidatesSoapBindingStub _stub = 
          new candidates.gen.CandidatesSoapBindingStub(portAddress, this);
            _stub.setPortName(getcandidatesWSDDServiceName());
            return _stub;
        }
        catch (org.apache.axis.AxisFault e) {
            return null;
        }
    }

    public void setcandidatesEndpointAddress(java.lang.String address) {
        candidates_address = address;
    }

    /**
     * For the given interface, get the stub implementation.
     * If this service has no port for the given interface,
     * then ServiceException is thrown.
     */
    public java.rmi.Remote getPort(Class serviceEndpointInterface) 
            throws javax.xml.rpc.ServiceException {
        try {
            if (candidates.gen.Candidates.class.isAssignableFrom
                    (serviceEndpointInterface)) {
                candidates.gen.CandidatesSoapBindingStub _stub = 
                        new candidates.gen.CandidatesSoapBindingStub
                        (new java.net.URL(candidates_address), this);
                _stub.setPortName(getcandidatesWSDDServiceName());
                return _stub;
            }
        }
        catch (java.lang.Throwable t) {
            throw new javax.xml.rpc.ServiceException(t);
        }
        throw new javax.xml.rpc.ServiceException
             ("There is no stub implementation for the interface:  " + 
               (serviceEndpointInterface == null ? 
                    "null" : serviceEndpointInterface.getName()));
    }

    /**
     * For the given interface, get the stub implementation.
     * If this service has no port for the given interface,
     * then ServiceException is thrown.
     */
    public java.rmi.Remote getPort(javax.xml.namespace.QName portName, 
    Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException {
        if (portName == null) {
            return getPort(serviceEndpointInterface);
        }
        java.lang.String inputPortName = portName.getLocalPart();
        if ("candidates".equals(inputPortName)) {
            return getcandidates();
        }
        else  {
            java.rmi.Remote _stub = getPort(serviceEndpointInterface);
            ((org.apache.axis.client.Stub) _stub).setPortName(portName);
            return _stub;
        }
    }

    public javax.xml.namespace.QName getServiceName() {
        return new javax.xml.namespace.QName("urn:candidates", 
             "CandidatesService");
    }

    private java.util.HashSet ports = null;

    public java.util.Iterator getPorts() {
        if (ports == null) {
            ports = new java.util.HashSet();
            ports.add(new javax.xml.namespace.QName("urn:candidates", 
                  "candidates"));
        }
        return ports.iterator();
    }

    /**
    * Set the endpoint address for the specified port name.
    */
    public void setEndpointAddress(java.lang.String portName, 
     java.lang.String address) throws javax.xml.rpc.ServiceException {
        
if ("candidates".equals(portName)) {
            setcandidatesEndpointAddress(address);
        }
        else 
{ // Unknown Port Name
            throw new javax.xml.rpc.ServiceException(
            " Cannot set Endpoint Address for Unknown Port" + portName);
        }
    }

    /**
    * Set the endpoint address for the specified port name.
    */
    public void setEndpointAddress(javax.xml.namespace.QName portName, 
      java.lang.String address) throws javax.xml.rpc.ServiceException {
        setEndpointAddress(portName.getLocalPart(), address);
    }
}

CandidatesService.java

/**
 * CandidatesService.java
 *
 * This file was auto-generated from WSDL
 * by the Apache Axis 1.3 Oct 05, 2005 (05:23:37 EDT) WSDL2Java emitter.
 */

package candidates.gen;

public interface CandidatesService extends javax.xml.rpc.Service {
    public java.lang.String getcandidatesAddress();

    public candidates.gen.Candidates getcandidates() 
      throws javax.xml.rpc.ServiceException;

    public candidates.gen.Candidates getcandidates(java.net.URL portAddress) 
           throws javax.xml.rpc.ServiceException;
}

CandidatesSoapBindingImpl.java

Note: The lines ending in ///// were manually added.

/**
 * CandidatesSoapBindingImpl.java
 *
 * This file was auto-generated from WSDL
 * by the Apache Axis 1.3 Oct 05, 2005 (05:23:37 EDT) WSDL2Java emitter.
 */

/***
 lines ending in /////
 were added by me
***/
package candidates.gen;
import candidates.*; /////
public class CandidatesSoapBindingImpl implements candidates.gen.Candidates{
    CandidatesImpl c = new CandidatesImpl(); /////
    public java.lang.String getPresident(java.lang.String in0, java.lang.String in1) throws java.rmi.RemoteException {
        return c.getPresident(in0, in1); /////
    }

    public java.lang.String getVicePresident(java.lang.String in0, java.lang.String in1) throws java.rmi.RemoteException {
        return c.getVicePresident(in0, in1); ////
    }

}

CandidatesSoapBindingStub.java

/**
 * CandidatesSoapBindingStub.java
 *
 * This file was auto-generated from WSDL
 * by the Apache Axis 1.3 Oct 05, 2005 (05:23:37 EDT) WSDL2Java emitter.
 */

package candidates.gen;

public class CandidatesSoapBindingStub extends org.apache.axis.client.Stub 
       implements candidates.gen.Candidates {
    private java.util.Vector cachedSerClasses = new java.util.Vector();
    private java.util.Vector cachedSerQNames = new java.util.Vector();
    private java.util.Vector cachedSerFactories = new java.util.Vector();
    private java.util.Vector cachedDeserFactories = new java.util.Vector();

    static org.apache.axis.description.OperationDesc [] _operations;

    static {
        _operations = new org.apache.axis.description.OperationDesc[2];
        _initOperationDesc1();
    }

    private static void _initOperationDesc1(){
        org.apache.axis.description.OperationDesc oper;
        org.apache.axis.description.ParameterDesc param;
        oper = new org.apache.axis.description.OperationDesc();
        oper.setName("getPresident");
        param = new org.apache.axis.description.ParameterDesc
                 (new javax.xml.namespace.QName("", "in0"), 
                  org.apache.axis.description.ParameterDesc.IN, 
                  new javax.xml.namespace.QName(
           "http://schemas.xmlsoap.org/soap/encoding/", 
           "string"), java.lang.String.class, false, false);
        oper.addParameter(param);
        param = new org.apache.axis.description.ParameterDesc
                  (new javax.xml.namespace.QName("", "in1"), 
                  org.apache.axis.description.ParameterDesc.IN, 
                      new javax.xml.namespace.QName(
                    "http://schemas.xmlsoap.org/soap/encoding/", "string"), 
                   java.lang.String.class, false, false);
        oper.addParameter(param);
        oper.setReturnType(new javax.xml.namespace.QName
                ("http://schemas.xmlsoap.org/soap/encoding/", "string"));
        oper.setReturnClass(java.lang.String.class);
        oper.setReturnQName(new javax.xml.namespace.QName("", 
                        "getPresidentReturn"));
        oper.setStyle(org.apache.axis.constants.Style.RPC);
        oper.setUse(org.apache.axis.constants.Use.ENCODED);
        _operations[0] = oper;

        oper = new org.apache.axis.description.OperationDesc();
        oper.setName("getVicePresident");
        param = new org.apache.axis.description.ParameterDesc
                      (new javax.xml.namespace.QName("", "in0"), 
                      org.apache.axis.description.ParameterDesc.IN, 
                      new javax.xml.namespace.QName
                       ("http://schemas.xmlsoap.org/soap/encoding/", "string"),
                     java.lang.String.class, false, false);
        oper.addParameter(param);
        param = new org.apache.axis.description.ParameterDesc
                (new javax.xml.namespace.QName("", "in1"), 
                 org.apache.axis.description.ParameterDesc.IN, 
                 new javax.xml.namespace.QName
               ("http://schemas.xmlsoap.org/soap/encoding/", "string"), 
                  java.lang.String.class, false, false);
        oper.addParameter(param);
        oper.setReturnType(new javax.xml.namespace.QName 
           ("http://schemas.xmlsoap.org/soap/encoding/", "string"));
        oper.setReturnClass(java.lang.String.class);
        oper.setReturnQName(new javax.xml.namespace.QName
                     ("", "getVicePresidentReturn"));
        oper.setStyle(org.apache.axis.constants.Style.RPC);
        oper.setUse(org.apache.axis.constants.Use.ENCODED);
        _operations[1] = oper;

    }

    public CandidatesSoapBindingStub() throws org.apache.axis.AxisFault {
         this(null);
    }

    public CandidatesSoapBindingStub(java.net.URL endpointURL, 
           javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
         this(service);
         super.cachedEndpoint = endpointURL;
    }

    public CandidatesSoapBindingStub(javax.xml.rpc.Service service) 
                 throws org.apache.axis.AxisFault {
        if (service == null) {
            super.service = new org.apache.axis.client.Service();
        } else {
            super.service = service;
        }
        ((org.apache.axis.client.Service)super.service).setTypeMappingVersion
           ("1.2");
    }

    protected org.apache.axis.client.Call createCall() 
                    throws java.rmi.RemoteException {
        try {
            org.apache.axis.client.Call _call = super._createCall();
            if (super.maintainSessionSet) {
                _call.setMaintainSession(super.maintainSession);
            }
            if (super.cachedUsername != null) {
                _call.setUsername(super.cachedUsername);
            }
            if (super.cachedPassword != null) {
                _call.setPassword(super.cachedPassword);
            }
            if (super.cachedEndpoint != null) {
                _call.setTargetEndpointAddress(super.cachedEndpoint);
            }
            if (super.cachedTimeout != null) {
                _call.setTimeout(super.cachedTimeout);
            }
            if (super.cachedPortName != null) {
                _call.setPortName(super.cachedPortName);
            }
            java.util.Enumeration keys = super.cachedProperties.keys();
            while (keys.hasMoreElements()) {
                java.lang.String key = (java.lang.String) keys.nextElement();
                _call.setProperty(key, super.cachedProperties.get(key));
            }
            return _call;
        }
        catch (java.lang.Throwable _t) {
            throw new org.apache.axis.AxisFault
                           ("Failure trying to get the Call object", _t);
        }
    }

    public java.lang.String getPresident(java.lang.String in0, 
                    java.lang.String in1) throws java.rmi.RemoteException {
        if (super.cachedEndpoint == null) {
            throw new org.apache.axis.NoEndPointException();
        }
        org.apache.axis.client.Call _call = createCall();
        _call.setOperation(_operations[0]);
        _call.setUseSOAPAction(true);
        _call.setSOAPActionURI("");
        _call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
        _call.setOperationName(new javax.xml.namespace.QName("urn:candidates", "getPresident"));

        setRequestHeaders(_call);
        setAttachments(_call);
 try {        java.lang.Object _resp = _call.invoke(new java.lang.Object[] 
             {in0, in1});

        if (_resp instanceof java.rmi.RemoteException) {
            throw (java.rmi.RemoteException)_resp;
        }
        else {
            extractAttachments(_call);
            try {
                return (java.lang.String) _resp;
            } catch (java.lang.Exception _exception) {
                return (java.lang.String) 
                         org.apache.axis.utils.JavaUtils.convert
                               (_resp, java.lang.String.class);
            }
        }
  } catch (org.apache.axis.AxisFault axisFaultException) {
  throw axisFaultException;
}
    }

    public java.lang.String getVicePresident(java.lang.String in0, java.lang.String in1) throws java.rmi.RemoteException {
        if (super.cachedEndpoint == null) {
            throw new org.apache.axis.NoEndPointException();
        }
        org.apache.axis.client.Call _call = createCall();
        _call.setOperation(_operations[1]);
        _call.setUseSOAPAction(true);
        _call.setSOAPActionURI("");
        _call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
        _call.setOperationName(new javax.xml.namespace.QName("urn:candidates", "getVicePresident"));

        setRequestHeaders(_call);
        setAttachments(_call);
 try {        java.lang.Object _resp = _call.invoke(new java.lang.Object[] {in0, in1});

        if (_resp instanceof java.rmi.RemoteException) {
            throw (java.rmi.RemoteException)_resp;
        }
        else {
            extractAttachments(_call);
            try {
                return (java.lang.String) _resp;
            } catch (java.lang.Exception _exception) {
                return (java.lang.String) org.apache.axis.utils.JavaUtils.convert(_resp, java.lang.String.class);
            }
        }
  } catch (org.apache.axis.AxisFault axisFaultException) {
  throw axisFaultException;
}
    }

}

deploy.wsdd undeploy.wsdd

Note that you have to add some code to the file CandidatesSoapBindingImpl.

Step 4 Deploy the Service

  1. Compile the service code (all files generated by WSDL2Java
  2. create a new subdirectory CATALINA_HOME/webapps/axis/WEB-INF/classes/ candidate/gen and copy all of the class files from candidate/gen directory here. Also copy the files Candidates.class and CandidatesImpl.class to the directory CATALINA_HOME/webapps/axis/WEB-INF/classes/candidates
  3. deploy the service with this command java org.apache.axis.client.AdminClient deploy.wsdd

Step 5: Write a Client

// package candidates;

import candidates.gen.*;

public class CandiateClient {

    public static void main(String[] args) throws Exception
    {
        CandidatesService s = new CandidatesServiceLocator();

        candidates.gen.Candidates c = s.getcandidates();

        System.out.println(c.getVicePresident("Democrat","2000"));
    }
}

When you compile and run the client, you should see the Democratic Vice Presidential candidate in the 2000 election, Joel Lieberman.