Professional Documents
Culture Documents
Handling Web Services With Apache AXIS Web-Service Review: by Examples ..
Handling Web Services With Apache AXIS Web-Service Review: by Examples ..
Web-Service Review
with Apache AXIS
By Examples ...
HTTP/1.0 200 OK
POST /soap HTTP/1.0
A request to Date: Sat, 07 May 2005 23:26:21 GMT
SOAPAction: ""
services.xmethods.net Content-Length: 492
Content-Length: 520
Content-Type: text/xml The Response
<?xml version="1.0" encoding="UTF-8"?>
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" <soap:Envelope
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
<soapenv:Body> xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
<ns1:getRate soap:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" <soap:Body>
xmlns:ns1="urn:xmethods-CurrencyExchange"> <n:getRateResponse
<country1 xsi:type="xsd:string">Euro</country1> xmlns:n='urn:xmethods-CurrencyExchange'>
<country2 xsi:type="xsd:string">Israel</country2> <Result xsi:type='xsd:float'>5.5825</Result>
</ns1:getRate> </n:getRateResponse>
</soapenv:Body> </soap:Body>
</soapenv:Envelope> </soap:Envelope>
1
<?xml version="1.0"?>
A WSDL Example <binding name="CurrencyExchangeBinding" type="tns:CurrencyExchangePortType">
<definitions name="CurrencyExchangeService" <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
targetNamespace="http://www.xmethods.net/sd/CurrencyExchangeService.wsdl" <operation name="getRate">
xmlns:tns="http://www.xmethods.net/sd/CurrencyExchangeService.wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" <soap:operation soapAction=""/>
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" <input name="getRate">
xmlns="http://schemas.xmlsoap.org/wsdl/">
<soap:body use="encoded" namespace="urn:xmethods-CurrencyExchange"
<message name="getRateRequest"> encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
<part name="country1" type="xsd:string"/> </input>
<part name="country2" type="xsd:string"/> <output name="getRateResponse">
</message> <soap:body use="encoded" namespace="urn:xmethods-CurrencyExchange"
<message name="getRateResponse"> encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
<part name="Result" type="xsd:float"/> </operation>
</message> </binding>
<portType name="CurrencyExchangePortType"> <service name="CurrencyExchangeService">
<documentation>Returns the exchange rate between the two currencies</documentation>
<operation name="getRate">
<port name="CurrencyExchangePort" binding="tns:CurrencyExchangeBinding">
<input message="tns:getRateRequest" name="getRate"/> <soap:address location="http://services.xmethods.net:80/soap"/>
<output message="tns:getRateResponse" </port>
name="getRateResponse"/> </service>
</operation></portType> </definitions>
WSDL Elements
2
Remote Method Invocation is not New What We Would Like to Create
• java.rmi has been in Java since Java’s early versions • Client applications: applications that can call a
• In Java RMI, objects can invoke methods of objects that remote Web service
reside on a remote computer
• Services: methods that can be called by remote
- (RMI = Remote Method Invocation)
applications
• So, what has been changed?
• Service descriptions: WSDL files that describe
- Using HTTP for communication
- Using agreed protocols, Java can invoke methods that were
our methods
not written in Java (e.g., .NET methods) and vice versa
- A complex registry procedure has been required in RMI
• Axis includes comfortable tools for managing • To invoke an operation of a Web service, do the
calls to Web services following:
• The programmer configures the request for the - Construct a Service instance
Web service using a friendly API - Using the Service instance, generate a Call instance
- with, or without considering the WSDL - Configure the Call instance
• According to the configuration, a method - Invoke the call
invocation is transformed into a SOAP request
that is sent to the remote server
3
Example: Currency Exchange Example: Currency Exchange
import org.apache.axis.client.*; (cont)
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode; call.setTargetEndpointAddress(endpointUrl);
import javax.xml.rpc.encoding.XMLType; call.setOperationName(new QName(nsuri,"getRate"));
call.addParameter("country1", XMLType.SOAP_STRING,
public class CurrencyExchange { ParameterMode.IN);
call.addParameter("country2", XMLType.SOAP_STRING,
ParameterMode.IN);
public static void main(String[] args) throws Exception {
System.setProperty("http.proxyHost","wwwproxy.huji.ac.il");
System.setProperty("http.proxyPort","8080"); call.setReturnType(XMLType.SOAP_FLOAT);
4
Generated Classes An Example
import com.brinkster.www50.vbfacileinpt.np.*;
public class PrimeNumbers2 {
System.out.println(ps.getPrimeNumbers("20"));
}
}
5
Creating a Web Service 1. Generate the Implementing Class
$CATALINA_BASE/webapps/axis/WEB-INF/
classes/myws/Power.class
• The AXIS plugin is simply a Web application • The wsdd file defines mappings between a Web
that resides in Tomcat (under webapps/) service elements to a Java class elements
• The Servlet AxisServlet of this application is - I.e., names, methods, etc.
responsible for invoking services • The class AdminClient sends a request to the
• All URLs of the form /services/* are mapped to application to register the service based on the
the AxisServlet wsdd content
- Where is that written? • When a SOAP request arrives, the AxisServlet
object parses the request and invokes the
corresponding method of the class associated
with the service URL
6
The Deployment Descriptor The Scope of the Object
<deployment xmlns="http://xml.apache.org/axis/wsdd/" • Request (the default): a new object is created for each
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> request, the service instance is available for the duration
of the request (regardless of forwarding)
<service name="mypower" provider="java:RPC">
<parameter name="className" value="myws.Power"/> • Session: a new object is created for each new session
<parameter name="scope" value="application"/> and the service instance is available for the entire
<parameter name="allowedMethods" value="*"/> session
</service>
• Application: a singleton shared service instance is used
</deployment> services.wsdd to serve all invocations
java org.apache.axis.client.AdminClient
• Of course, all helper classes should be accessible
-hlocalhost -p8080 undeploy.wsdd to the Axis application too
Power-Service WSDL
The Service WSDL <?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://inferno-02/axis/services/mypower"
xmlns="http://schemas.xmlsoap.org/wsdl/"
• Axis automatically provides a WSDL for each xmlns:apachesoap="http://xml.apache.org/xml-soap"
deployed service xmlns:impl="http://localhost/axis/services/mypower"
xmlns:intf="http://localhost/axis/services/mypower"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
• To get the WSDL, use the service URL with the xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
empty argument wsdl xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
http://localhost:8080/axis/services/mypower?wsdl <wsdl:message name="powerResponse">
<wsdl:part name="powerReturn" type="xsd:int"/>
</wsdl:message>
<wsdl:message name="powerRequest">
<wsdl:part name="a" type="xsd:int"/>
<wsdl:part name="n" type="xsd:int"/>
</wsdl:message>
7
<wsdl:binding name="mypowerSoapBinding" type="impl:Power">
<wsdlsoap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="power">
<wsdl:portType name="Power"> <wsdlsoap:operation soapAction=""/>
<wsdl:operation name="power" parameterOrder="a n"> <wsdl:input name="powerRequest">
<wsdl:input message="impl:powerRequest" <wsdlsoap:body
name="powerRequest"/> encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://myws" use="encoded"/>
<wsdl:output message="impl:powerResponse"
</wsdl:input>
name="powerResponse"/>
<wsdl:output name="powerResponse">
</wsdl:operation>
<wsdlsoap:body
</wsdl:portType> encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://localhost/axis/services/mypower" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
jws Files
8
How does it Work? When not to Use jws Files
• On the first time the jws file is being called, it is • When you do not have the Java source code
compiled into a class • When you don’t want to expose your code
- WEB-INF/jwsClasses/SimpleCalculator.class • When you want to use custom type mappings
• Axis then considers the URL of the jws as one of • When you want to use other configuration
a regular web service options
- Default configurations are assumed
• Sounds like any other technology we know?
9
Output in The WSDL Complex Type Example - Client Side
An Example
Java Beans
package myws;
public class Person {
• Java Beans are simply objects of classes that private String firstName, lastName;
follow some (natural) coding conventions: private int personID;
property firstName
- An empty constructor public String getFirstName() {return firstName;}
- A readable property has a matching getter public void setFirstName(String firstName) {
this.firstName = firstName;}
- A writable property has a matching setter public String getLastName() {return lastName;}
• A property has name and a type public void setLastName(String lastName) {
this.lastName = lastName;
• The getter of property prop: type getProp() } Property lastName
public void setId(int id) {personID = id;}
• The setter of property prop: void setProp(type) public int getId(int id) {return personID;}
} Property id
10
Using Beans in Web Services A Service Example
11
Exception in Service Calls Service Exception
• Several problems can cause exception to be • When the implementing service throws an
thrown when a service is being invoked exception, a SOAP fault response is returned by
• For example the server
- the server cannot be connected • The client invocation method translates this
- the server does not find the requested URL response to a java.rmi.RemoteException after
parsing the response
- the request is inappropriate (no such operation,
invalid arguments, etc.)
- the implementing service method has thrown an
exception
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:Server.userException</faultcode>
<faultstring>java.lang.IllegalStateException: I only throw an
exception!</faultstring>
<detail>...</detail>
</soapenv:Fault>
</soapenv:Body>
12