ppt - Computer Science and Engineering

advertisement
Web Services
Dr. Miguel A. Labrador
Department of Computer Science & Engineering
labrador@csee.usf.edu
http://www.csee.usf.edu/~labrador
1
Outline
•
•
•
•
Introduction
Web services components
The Web Services API
Creating a simple Web service
– Server side
– MIDlet
2
Copyright© Dr. Miguel A. Labrador
2
Web Services
• Created to allow any networked application client to find
applications located anywhere in the Internet and use them as if
they were locally installed applications
– Regardless of platform
• To realize this vision, four critical aspects are needed:
– Develop and deploy applications that can be accessed and used by
everyone
– Describe the services
• Web Services Definition Language (WSDL)
– Find the services
• Universal Description, Discovery, and Integration (UDDI) registry
– Not really needed if you know the URL where the service is offered
– Connect and use the services
• Standard protocols and languages
– HTTP, SOAP, RPC, XML
3
Copyright© Dr. Miguel A. Labrador
3
The Java ME Platform
Request
Service
Creates
Web
Service
Application
Service
Provider
Stub
Creates
Creates
WSDL
Document
Client
Registers
UDDI
Finds and
Obtains
Service
WSDL
4
Copyright© Dr. Miguel A. Labrador
4
The Web Services API
• Based on JSR 172, J2ME Web Services APIs (WSA)
– The purpose of this specification is to define an optional package
that provides standard access from Java ME devices to Web
services
• Two optional packages
– Java API for XML-based RPC (JAX-RPC)
• Allows mobile devices to access remote XML-based Web services
• Java API for interacting with SOAP-based Web services
– Implementation of RPC in Java
– Java API for XML Processing (JAXP)
• Adds XML parsing capabilities to the Java ME platform
• Can be used by all applications
5
Copyright© Dr. Miguel A. Labrador
5
The Web Services API
Client
JAXP
API
Application
JAX-RPC Stub
JAXRPC API
Service Provider
Interface (SPI)
JAX-RPC
Runtime
MIDP 2.0
Service
Request
CLDC 1.1
SOAP XML/HTTP
Operating System
Server
Service
Response
6
Copyright© Dr. Miguel A. Labrador
6
The JAX-RPC Package
• Contains the javax.microedition.xml.rpc, javax.xml.namespace,
javax.xml.rpc and javax.rmi packages
– All classes and interfaces that make up the stub, SPI, and runtime
• Stub makes requests to the runtime and receives service
responses through the service provider interface
– An instance of the stub class is generated by the stub generator. The
instance is used to:
•
•
•
•
•
•
Set the properties of the stub to invoke an RPC
Make all appropriate format conversions
Create objects describing the input and return parameters
Encode the input values
Invoke the RPC
Decode the return value
• The runtime contains all the functionality needed to transfer data to
and from the server on behalf of the application
7
Copyright© Dr. Miguel A. Labrador
7
The JAXP Package
• Subset of the J2SE JAXP API
• Includes the javax.xml.parsers, org.xmal.sax,
org.xmal.sax.helpers packages
• Parse XML data
8
Copyright© Dr. Miguel A. Labrador
8
Creating a Web Service
• Creating a Web service is very much automated in NetBeans
• Let us go through the process of creating a Web service that
functions like a calculator
– MIDlet asks user for operands and type of operation (sum, …)
– MIDlet sends request to Web server
– Web server calculates the result and sends it back to MIDlet
• Need to develop Web server part and MIDlet
9
Copyright© Dr. Miguel A. Labrador
9
Web Service Part
• The process consists of the following chronological steps
– Define operations to perform (sum, subtraction, multiplication,
division)
– Define input and output parameters
– Create a New Application Project
•
•
•
•
From the Categories list, select Web
From the Projects list, select Web Application
Write in the name of your project
Click Next and Finish
– Create a new package for the project
• Select project in Projects’ tab and right click on it
• Select New->Java Package
• Write in name and click on Finish
10
Copyright© Dr. Miguel A. Labrador
10
Web Service Part
• The process consists of the following chronological steps
– Create Web Service
•
•
•
•
Select project in Projects’ tab and right click on it
Select New->Web Service
Write in Name
Select package just created and Finish
• The configuration screen appears to define operations and parameters
– Design the service
11
Copyright© Dr. Miguel A. Labrador
11
Web Service Part
12
Copyright© Dr. Miguel A. Labrador
12
Web Service Part
• The process consists of the following chronological steps
– Click on “Source” button to see the Web Service code that
NetBeans automatically creates for you
• Only need to include the code that performs each function in the
respective part
13
Copyright© Dr. Miguel A. Labrador
13
Creating a Web Service
• The process consists of the following chronological steps
– Save changes
– Click on service and right click for Clean and Build
– Click on service and right click to Undeploy and Deploy your
project
– NetBeans also automatically creates the WSDL file
– http://IPadd:port (Glassfish 12796)/Project Name (Context Path
from Properties/Run menu)/Web service
name(CalculatorService)?wsdl
– http://131.247.3.235:12796/WebAppExample/NewWebServiceServi
ce?wsdl
– http://localhost:12796/LbsBook/ServerCalculatorService?wsdl
14
Copyright© Dr. Miguel A. Labrador
14
Example WSDL File
<?xml version="1.0" encoding="UTF-8" ?>
-<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is
JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT.
-->
-<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is
JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT.
-->
-<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://services.client.book.edu.usf.cse/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://services.client.book.edu.usf.cse/"
name="ServerCalculatorService">
- <types>
- <xsd:schema>
<xsd:import namespace="http://services.client.book.edu.usf.cse/"
schemaLocation="http://localhost:12796/LbsBook/ServerCalculatorService?xsd=1" />
</xsd:schema>
</types>
- <message name="calculateSum">
<part name="parameters" element="tns:calculateSum" />
</message>
- <message name="calculateSumResponse">
<part name="parameters" element="tns:calculateSumResponse" />
</message>
- <message name="calculateSub">
<part name="parameters" element="tns:calculateSub" />
</message>
. . .
15
Copyright© Dr. Miguel A. Labrador
15
Creating the MIDlet
• The process consists of the following chronological steps
–
–
–
–
–
–
–
Next step is to create the MIDlet
Create New Project Mobility, MIDP Application
Write in name and click Finish
Create package as before
Select Project, right click, New Visual MIDlet
Write in the MIDlet name, select package
Select New Java ME Web Service Client
• Provide URL of WSDL document
• Click on Retrieve WSDL
– NetBean creates application skeleton
– Maps WSDL definitions to Java representation and XML data
types to Java types
– Design Visual MIDlet
16
Copyright© Dr. Miguel A. Labrador
16
Creating a MIDlet
17
Copyright© Dr. Miguel A. Labrador
17
Creating the MIDlet
18
Copyright© Dr. Miguel A. Labrador
18
Creating the MIDlet
• The process consists of the following chronological steps
– Add the code
public void commandAction(Command command, Displayable displayable) {
// write pre-action user code here
if (displayable == form) {
if (command == okCommand) {
// write pre-action user code here
// write post-action user code here
Runnable theInvoker = new Runnable(){
public void run() {
int op1 = Integer.parseInt(textField.getString());
int op2 = Integer.parseInt(textField1.getString());
ServerCalculatorService_Stub remoteCalculator =
new ServerCalculatorService_Stub();
int op3;
try
{
op3 = remoteCalculator.calculateSum(op1, op2);
textField2.setString(""+op3);
}
catch (RemoteException ex) {
ex.printStackTrace();
textField2.setString(ex.getMessage());
}
}
};
19
Thread t = new Thread(theInvoker);
t.start();
Copyright© Dr. Miguel A. Labrador
19
Creating the MIDlet
• The process consists of the following chronological steps
– Run the MIDlet
20
Copyright© Dr. Miguel A. Labrador
20
Download