Axis

Getting started with Axis. A presentation of the

1 Installation

2 Building a Client

package samples.userguide.example1;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

import javax.xml.namespace.QName;

public class TestClient
{
   public static void main(String [] args) {
       try {
           String endpoint = 
                    "http://nagoya.apache.org:5049/axis/services/echo";
     
           Service  service = new Service();
           Call     call    = (Call) service.createCall();

           call.setTargetEndpointAddress( new java.net.URL(endpoint) );
           call.setOperationName(new QName("http://soapinterop.org/", "echoString") );

           // Call to addParameter/setReturnType as described in user-guide.html
           call.addParameter("testParam",
                             org.apache.axis.Constants.XSD_STRING,
                             javax.xml.rpc.ParameterMode.IN);
           call.setReturnType(org.apache.axis.Constants.XSD_STRING);

           String ret = (String) call.invoke( new Object[] { "Hello!" } );

           System.out.println("Sent 'Hello!', got '" + ret + "'");
       } catch (Exception e) {
           System.err.println(e.toString());
       }
   }
}
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <env:Body>
 <ns1:echoString xmlns:ns1="http://soapinterop.org/">
   <testParam xsi:type="xsd:string">Hello!</testParam>
 </ns1:echoString>
  </env:Body>
</env:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
 <env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <env:Body>
    <ns1:echoStringResponse xmlns:ns1="http://soapinterop.org/"> 
      <result xsi:type="xsd:string">Hello!</result>
    </ns1:echoStringResponse> 
  </env:Body>
 </env:Envelope>

3 Building a Server

public class Calculator {
  public int add(int i1, int i2)
  {
    return i1 + i2; 
  }


  public int subtract(int i1, int i2)
  {
    return i1 - i2;
  }
}

3.1 Custom Deployment

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
            xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
  <service name="MyService" provider="java:RPC">
    <parameter name="className" value="samples.userguide.example3.MyService"/>
      <parameter name="allowedMethods" value="*"/>
  </service>
</deployment>

3.2 Scoped Services

  1. Request: new object created for every request.
  2. Application: singleton object.
  3. Session: new object for each session-enabled client.
Note:

Since the web service is defined by a class, we need to have at least one instance of that class in order to invoke its methods. This raises the question of how many instances of the class should we have around, one? one for each call? a couple? In Java RMI there was only one instance of each remote service which is the equivalent of the Application-level scoping provided by SOAP. SOAP, however, allows one to have a new instance created each time a client invokes one of the methods (Request) or to have one new instance created each time a client logs-in (Session), presumably the client is using some form of HTTP authentication.

3.3 Handlers and Chains

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
   xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

  <!-- define the logging handler configuration -->
 <handler name="track" type="java:samples.userguide.example4.LogHandler">
  <parameter name="filename" value="MyService.log"/>
 </handler>
 
 <!-- define the service, using the log handler we just defined -->
 <service name="LogTestService" provider="java:RPC">
  <requestFlow>
   <handler type="track"/>
  </requestFlow>
 
  <parameter name="className" value="samples.userguide.example4.Service"/>
  <parameter name="allowedMethods" value="*"/>
 </service>
</deployment>
package samples.userguide.example4;

import org.apache.axis.AxisFault;
import org.apache.axis.Handler;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;

import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Date;

public class LogHandler extends BasicHandler {
    public void invoke(MessageContext msgContext) throws AxisFault
    {
        /** Log an access each time we get invoked.
         */
        try {
            Handler serviceHandler = msgContext.getService();
            String filename = (String)getOption("filename");
            if ((filename == null) || (filename.equals("")))
                throw new AxisFault("Server.NoLogFile",
                                 "No log file configured for the LogHandler!",
                                    null, null);
            FileOutputStream fos = new FileOutputStream(filename, true);
            
            PrintWriter writer = new PrintWriter(fos);
            
            Integer numAccesses =
                             (Integer)serviceHandler.getOption("accesses");
            if (numAccesses == null)
                numAccesses = new Integer(0);
            
            numAccesses = new Integer(numAccesses.intValue() + 1);
            
            Date date = new Date();
            String result = date + ": service " +
                            msgContext.getTargetService() +
                            " accessed " + numAccesses + " time(s).";
            serviceHandler.setOption("accesses", numAccesses);
            
            writer.println(result);
            
            writer.close();
        } catch (Exception e) {
            throw AxisFault.makeFault(e);
        }
    }
}

3.4 RPC Services

3.5 Document Services

4 Mappings

xsd:base64Binarybyte[]
xsd:booleanboolean
xsd:bytebyte
xsd:dateTimejava.util.Calendar
xsd:decimaljava.math.BigDecimal
xsd:doubledouble
xsd:floatfloat
xsd:hexBinarybyte[]
xsd:intint
xsd:integerjava.math.BigInteger
xsd:longlong
xsd:QNamejavax.xml.namespace.QName
xsd:shortshort
xsd:stringjava.lang.String

4.1 Exceptions and Collections

5 WSDL with Axis

5.1 PortType

5.2 Bindings

5.3 Services

5.4 Java2WSDL

URLs

  1. Axis User's Guide, http://ws.apache.org/axis/java/user-guide.html
  2. Axis installation instructions, http://ws.apache.org/axis/java/install.html

This talk available at http://jmvidal.cse.sc.edu/talks/axis/
Copyright © 2009 José M. Vidal . All rights reserved.

25 March 2004, 04:08PM