Transcript
Page 1: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Lecture 10:Web Services

Page 2: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Outline

• Overview of Web Services

• Create a Web Service with Sun J2EE (JAX-RPC)

Page 3: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

What is a Web Service?• A web service is a network accessible interface to

application programs, built using standard Internet technologies.

• Clients of web services do NOT need to know how it is implemented.

Application

client

Application

programNetwork Web

Service

Page 4: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Web Service Architecture

Service provider

Service broker Service requestor

publish(WSDL)

find(UDDI)

bind(SOAP)

"server"

"client""naming service"

Page 5: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Web Service Technology Stack

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

Client Proxy = Stub, Server Proxy = Tie

publish WSDL URIs

Page 6: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 1. Write Web Service Method

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

Client Proxy = Stub, Server Proxy = Tie

publish WSDL URIs

Page 7: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 2. Describe Web Service using WSDL

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

WSDL can be automatically generated

publish WSDL URIs

Page 8: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 3. Deploy Service at Server

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

Tie will be created, service location stored in WSDL

publish WSDL URIs

Page 9: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 4. Publish Service

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

Client can locate the service querying UDDI

publish WSDL URIs

Page 10: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 5. Generate Client Proxy

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

publish WSDL URIs

Stubs can be generated from WSDL automatically

Page 11: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 6. Write Client to Invoke Proxy

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

publish

Stubs can be generated from WSDL automatically

Page 12: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 7. Execute Client

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDLSOAP pkg request

SOAP pkg responseProxyProxy

publish

Client invokes service (almost) like a local method

Page 13: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

JAX-RPC

• JAX-RPC = Java Web Services Architecture

• Sun's solution for writing Web Services and Web Service clients

• Example: "HelloWorld" Service

Page 14: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 1. Write Web Service Method

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

Client Proxy = Stub, Server Proxy = Tie

publish WSDL URIs

Page 15: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Service Interface

package iis;

import java.rmi.Remote;

import java.rmi.RemoteException;

public interface HelloIF extends Remote {

public String sayHello(String s) throws RemoteException;

}

Web Service Interface derived from class Remote

Methods required to throw RemoteException

Page 16: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Service Implementation

package iis;

public class HelloImpl implements HelloIF

{ public String message ="Hello"; public String sayHello(String s)

throws RemoteException { return message + s; }

}

Compile the classes: javac HelloIF.java HelloImpl.java

Page 17: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 2. Describe Web Service using WSDL

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

WSDL can be automatically generated

publish WSDL URIs

Page 18: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Configuration File

• All relevant information on Web Service

<?xml version="1.0" encoding="UTF-8"?>

<configuration

xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">

<service

name="HelloWorldService"

targetNamespace="http://lsirwww.epfl.ch/"

typeNamespace="http://lsirwww.epfl.ch/"

packageName="iis">

<interface name="cis.HelloIF"/>

</service>

</configuration>

Page 19: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Generate WSDL

• Automatically derived from Interface and Configuration Filewscompile -define -mapping build/mapping.xml -d build -nd build -classpath build config.xml

• Deploytool will need information from mapping.xml

Page 20: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Structure of WSDL<?xml version="1.0">

<definitions name="HelloWorldService" … Name Space Information …> <types>

<schema> definition of parameter data types in XML Schema (optional)

</schema></types> <message name="HelloIF_sayHello">

definition of a message (request, reply)</message> <portType name="HelloIF">

<operation name="sayHello">definition of an operation (request – reply pair)

</operation></portType> <binding name="HelloIfBinding" type="HelloIF">

definition of a protocol binding (typically SOAP)</binding><service name="HelloWorldService">

<port name="StockQuotePort">definition of a port (an Internet address)

</port></service></definitions>

abstract concrete

Page 21: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Messages

<message name="HelloIF_sayHello"> <part name="String_1" type="xsd:string"/></message><message name="HelloIF_sayHelloResponse"> <part name="result" type="xsd:string"/></message>

Provides message names and passing of parameters

Page 22: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Ports

<portType name="HelloIF"><operation name="sayHello" parameterOrder="String_1">

<input message="tns:HelloIF_sayHello"/> <output message="tns:HelloIF_sayHelloResponse"/>

</operation></portType>

Define message sequences corresponding to a service invocation

Page 23: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Protocol Binding

<binding name="HelloIFBinding" type="tns:HelloIF"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/> <operation name="sayHello"> <soap:operation soapAction=""/> <input> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"

use="encoded" namespace="http://lsirwww.epfl.ch/"/></input>

<output> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"

use="encoded" namespace="http://lsirwww.epfl.ch/"/> </output> </operation></binding>

Implement abstract messages according to SOAP protocol

Page 24: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Sample Soap Message

<?xml version='1.0' ?> <env:Envelope xmlns:env="http://www.w3.org/2002/12/soap-envelope"> <env:Header> <m:HelloIF_sayHello xmlns:m="http://lsirwww.epfl.ch/" env:role="http://www.w3.org/2002/12/soap-envelope/role/next" env:mustUnderstand="true"> </m:HelloIF_sayHello> </env:Header> <env:Body> <p:String_1 xmlns:p="http://lsirwww.epfl.ch/">

Hello World!</p:String_1>

</env:Body> </env:Envelope>

SOAP client SOAP serverRequest message

Response message

Page 25: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Service Access

<service name="HelloWorldService"> <port name="HelloIFPort"

binding="HelloIFBinding"> <soap:address location="REPLACE_WITH_ACTUAL_URL"/>

</port></service>

Location not known before deployment

Page 26: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 3. Deploy Service at Server

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

Tie will be created, service location stored in WSDL

publish WSDL URIs

Page 27: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Deploy Service

• Web service tie is implemented as servlet

• Servlet is a Java application that can interact with Web Server (also JSPs are implemented as servlets!)

• Tie automatically generated at deployment

Page 28: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

WAR file and Context Root

WAR file is archive collecting all needed files (like JAR)

Contect Root is location on Web Server

Page 29: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Provide WSDL File

As generated before

Page 30: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Provide Service Implementation

Note: WSDL does not know about the implementation!

Page 31: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Service Access

Note: Multiple Ports may have been provided in WSDL!

Page 32: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

WSDL after Deployment

<service name="HelloWorldService"> <port name="HelloIFPort" binding="tns:HelloIFBinding"> <soap:address location=

"http://lsir-cis-pcx:8009/hello/helloService"/> </port></service>

This can be published via UDDI

Page 33: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 4. Publish Service

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

Client can locate the service querying UDDI

publish WSDL URIs

Page 34: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

UDDI

Page 35: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 5. Generate Client Proxy

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

publish WSDL URIs

Stubs can be generated from WSDL automatically

Page 36: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Generate Stubs

• Client Configuration File<configuration

xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">

<wsdl location="build/HelloWorld.wsdl" packageName="iis"/>

</configuration>

• Automatically created using WSDL and client configuration file

wscompile -gen:client -d build -classpath build

config-client.xml

Page 37: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 6. Write Client to Invoke Proxy

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDL

SOAP pkg request

SOAP pkg response ProxyProxy

publish

Stubs can be generated from WSDL automatically

Page 38: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Client Application

package iis;

import javax.xml.rpc.Stub;

public class HelloClient {

private String endpointAddress;

public static void main(String[] args) {

try {

Stub stub = createProxy();

stub._setProperty (javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, args[0]);

HelloIF hello = (HelloIF)stub;

System.out.println(hello.sayHello(args[1]));

} catch (Exception ex) {

ex.printStackTrace();

}

}

see next slide

WS address

parameter

Page 39: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Creating Proxy (Stub)

private static Stub createProxy() {

return (Stub) (new HelloWorldService_Impl().getHelloIFPort());}

attaching _Impl to the service name is an implementation-specific naming convention

Page 40: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Compiling and Packaging

compile javac –classpath system_jars:server_class_files:

stub_class_files HelloClient.java

package jar cvf hello-client.jar

all_client_class_files:all_server_class_files

Note: in the exercise the commands are automated by using the asant scripting utility provided by Sun J2EE

Page 41: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Step 7. Execute Client

Discovery

Description

Packaging

Transport

Network

shopping web service?

WSDL URIsWeb ServiceClient

Web Service

UDDI

ProxyProxy

WSDLWSDLSOAP pkg request

SOAP pkg responseProxyProxy

publish

Client invokes service (almost) like a local method

Page 42: Lecture 10: Web Services. Outline Overview of Web Services Create a Web Service with Sun J2EE (JAX-RPC)

Invoke Client

java –classpath hello-client.jar:jwsdp-jars hello.HelloClient

"Hello World!"

Generated at Server – Displayed at Client


Recommended