Web Services (Lecture Slides) - e
Download
Report
Transcript Web Services (Lecture Slides) - e
Net-centric Computing
Web Services
Lecture Outline
What is Web Service
Web Service Architecture
Creating and using Java Web Services
Apache Axis
Simple Examples
What is a Web Service
A web service is a network accessible
interface to application functionality, built using
standard Internet technologies.
Clients of web services do NOT need to know
how it is implemented.
Application
client
Network
Web
Service
Application
code
A more detailed definition
"A Web service is a software system designed
to support interoperable machine-to-machine
interaction over a network. It has an interface
described in a machine-readable format
(specifically WSDL). Other systems interact
with the Web service in a manner prescribed
by its description using SOAP messages,
typically conveyed using HTTP with an XML
serialization in conjunction with other Webrelated standards."
http://www.w3.org/TR/ws-arch/
4
University of Pennsylvania
Servlets/CGI vs Web Services
Browser
Browser
GUI
Client
Web
Server
WSDL
SOAP
WSDL
SOAP
Web
Server
WSDL
JDBC
Web
Server
WSDL
HTTP GET/POST
JDBC
DB
DB
Web Services Architecture
There are two ways we can view Web Services
architecture
1.
Web Service Roles
2.
Web Service Protocol Stack
Web Service Roles
There are three major roles
Logically Centralized directory
of services
Service
Registry (UDDI)
2) Discover services (WSDL)
Service
Requestor
Consumer of the Web Service
3) Invoke service (SOAP)
1) Register service
(WSDL)
Service
Provider
Provider of the Web Service
Web Service Protocol Stack
Discovery
Description
XML Messaging
Transport
UDDI
Responsible for centralizing services
WSDL
Responsible for describing the public
interface to a specific web service
Responsible for encoding messages
XML-RPC,SOAP,XMLL
in common XML format
HTTP,SMTP,FTP
Responsible for transporting
messages
Web Services Standard Prorocols
WSDL – Web Service Description Language
SOAP – Simple Object Access Protocol
Describes how the service will be used
Guidelines for constructing SOAP messages
XML message format between client and
service
UDDI – Universal Description, Discovery and
Integration protocol
A protocol for publishing web service
descriptions
SOAP Message
SOAP Message
Envelope
Header
Body
Envelope is like a
wrapper for content
Header is a optional
element that could
contain control
information
Body element includes
requests and responses
Body element will
include a Fault element
in the event of an error
Sample SOAP Request
<SOAP-ENV:Envelope
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<ns1:sayHello
xmlns:ns1="http://agram.com/">
<name xsi:type="xsd:string">Java</name>
</ns1:sayHello>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Sample SOAP Response
<SOAP-ENV:Envelope
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<ns1:sayHelloReponse
xmlns:ns1="http://agram.com/">
<result xsi:type="xsd:string">Hello Java</result>
</ns1:sayHelloResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
WSDL major elements
Has 6 major elements
1.
definitions – defines the name of the web
service
2.
types – describes all the data types that will be
transmitted
3.
message – defines the name of the message
that will be transmitted
portType – defines the operations
4.
5.
binding – defines how the message will be
transmitted
6.
service – defines where the service is located
Development plan for Service Requestor
1) Find web service via UDDI
2) Retrieve service description file
3) Create XML-RPC or SOAP client
4) Invoke remote service
Development plan for Service Provider
1) Create the core functionality
2) Create XML-RPC or SOAP service wrapper
3) Create service description file
4) Deploy service
5) Register new service via UDDI
Tools for developing Services
Apache Axis
Sun Web Services for Java
SOAP Lite for Pearl
.NET tools from MS
gSOAP for C++
…
Apache Tomcat vs Axis
HTTP Server (e.g. Apache Tomcat)
Servlet engine
Any class
Any class
processing
Any class
processing
Any class
the incoming
processing
the incoming
processing
requests
the incoming
requests
the incoming
(“business
logic”
requests
(“business
logic”
requests
(“business logic”
(“business logic”
SOAP-aware
Servlet
(e.g. Apache Axis)
Sending
requests,
getting
results
Get the Code
Axis Web site:
http://ws.apache.org/axis/
Select downloads.
Get release 1.4
From the mirror site,
download the .zip.
Right click to save to
desktop.
.tar.gz is for the Unix/Linux
tar utility.
Right click the axis-1_4 zip
folder icon to extract the files.
Installing Apache Axis
2) Deploy Axis Server in the Tomcat 4.X
server
a) Copy Axis Web application (webapps/axis
directory) to $TOMCAT_HOME/webapps
b) Add the following libraries (available under
…/axis/WEB-INF/lib folder) into your
CLASSPATH environment variable: axis.jar,
commons-discovery.jar, commonslogging.jar, jaxrpc.jar, log4j-1.2.8.jar, saar.jar,
wsdl4j-1.5.1.jar
CLASSPATH example
set AXIS_HOME=C:\jakarta-tomcat-4.0.6\webapps\axis
set AXIS_LIB=%AXIS_HOME%\WEB-INF\lib
set AXIS_CP=.;%AXIS_CP%;%AXIS_LIB%\axis.jar;
%AXIS_LIB%\commons-discovery.jar;
%AXIS_LIB%\commons-logging.jar;%AXIS_LIB%\jaxrpc.jar;
%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar;
%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\wsdl4j.jar
set classpath=%AXIS_CP%
Validating the Installation
1)
2)
Start the Tomcat Web Server
Goto http://localhost:8080/axis/
- you should be able to see Apache-Axis start
page
- if you did not , then the axis is not correctly
installed or the web server is not running
3)
Goto http://localhost:8080/axis/happyaxis.jsp
- this test page verifies whether all the needed
and optional libraries are present.
- Axis will not perform properly until all the
needed libraries are present.
Creating a Web Service in Java
Typical steps:
1.
Create the application
2.
Generate WSDL document using some Web
Service tool
3.
Deploy Web Service to a Web Server
4.
Generate client stubs from WSDL
5.
Create client application
Then publish, discover and use web service
Develop a service
Step 1 – Write a Java Class
public class AdderImpl implements Adder
{
public int add(int x, int y) throws RemoteException
{
return x + y;
}
}
Adder.java
public interface Adder {
int add (int x, int y);
}
AdderImpl.java
public class AdderImpl implements Adder
{
public int add(int x, int y) throws RemoteException
{
return x + y;
}
}
Develop a service
Step 2 - Deploy to the SOAP engine
Create the deployment descriptor (*.wsdd)
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name=“adderService" provider="java:RPC">
<parameter name="className" value=“AdderImpl"/>
<parameter name="allowedMethods" value=“*"/>
</service>
</deployment>
Develop a service
Step 2 - Deploy to the SOAP engine
Copy the Java Class to the Web
Server
Start the Web Server
Deploy
java org.apache.axis.client.AdminClient *.wsdd
Develop a service
Step 3 - Check
Check (List all services)
http://localhost:8080/axis/servlet/AxisServlet
Consume a service
Step 1 – Get the WSDL file of the service
Java2WSDL (usage example)
java org.apache.axis.wsdl.Java2WSDL
-o adder.wsdl
location
output
-l http://localhost:8080/axis/services/adderService
-n http://cop4991/adder
adderImpl
namespace
class name
We use this tool because the service is built by
ourselves (we have the Java Class)
Consume a service
Step 2 – Generate the Client Stub
WSDL2Java (usage example)
java org.apache.axis.wsdl.WSDL2Java
myecho.wsdl
4 files will be generated
interface
AdderImpl.java
Service
AdderImplService.java
AdderImplServiceLocator.java
AdderServiceSoapBindingStub.java
Service factory
Binding stub
Consume a service
Step 3 – Write the Client Program
…
// Make a service
AdderImplService adderService = new AdderImplServiceLocator();
// Now use the service to get a stub
AdderImpl adder = adderService.getadderService();
// Make the actual call
int sum = adder.add(10,9);
…
AdderClient.java
public class AdderClient{
public static void main(String[] args){
try{
//Make a service instance
AdderImplService adderService = new AdderImplServiceLocator();
//Now use the service to get a stub
AdderImpl adder = adderService.getadderService();
//Make the actual call
int sum = adder.add(10,9);
System.out.println("the sum is: "+sum);
}catch(Exception e){e.printStackTrace();}
}
}
Static Stub Client
Our client is static
Tightly bound to generated stub
Can only use one service
Dynamic Clients
Use WSIF (Web Service Invocation Framework)
instead