SOA using Java Web Services and Ajax

Preview:

Citation preview

SOA using Java Web Services and Ajax

Mark D. HansenAuthor of “SOA Using Java Web Services”

http://soabook.com

Mark Hansen’s Background

• Consultant, Entrepreneur, MIT PhD

• Book and code examples at http://soabook.com

• SOA-J open source project at http://soa-j.org

Outline

• What is SOA?

• What are Web Services?

• What are Java Web Services (JWS)?

• What is AJAX?

• How can you implement SOA with JWS and AJAX?

• Demo (eBay, Amazon, Yahoo!)

What is SOA?

• Loose coupling• Service contract• Autonomy• Abstraction• Reusability• Composability• Statelessness• Discoverability

“Service-Oriented Architecture”, Thomas Erl, Chapter 3 pg 34 - 37

What is a Web Service?

• A provider that enables access to the service via either:– SOAP– XML / HTTP

• A description or contract that specifies the request/response message structure.– WSDL– XML Schema (insufficient)– WADL (new)

WSDL

XML Schema

SOAP

XML /HTTP

* Maybe WADL?

REST

SOAP provider

provider

What are Java Web Services (JWS)?

• The provider is written in Java and deployed using a Java container.

• The consumer is written in Java.

• Java Web Service APIs are used:– JAX-WS– JAXB– WS-Metadata (JSR-181)– WSEE (JSR-109)

JAX-RPC is deprecated starting in Java EE 5

Web Services Container

SOAP Endpoint

WSDL Interface

Java Virtual Machine

Web Service Proxy

Java InterfaceSOAP

Message

service

Client Side JWS

Proxy Instance

WSDL to JavaMapping Tool

(e.g., wsimport)

ServiceEndpointInterface

Parameters

(JAXB Generated Class Instances)

Web Service

WSDL

Endpoint URL

SOAP Request

SOAP Response

Return Value

(JAXB Generated Class Instance)

InvocationHandler

ServiceEndpointInterface

javax.xml.ws.Service

getPort(...)

1

2

3

4

5

Server Side JWS

Web Service

Meta-Data

(WSDL, Handler

File, Depl Descrpt.)

@WebService or@WebServiceProvider

SOAP Request

SOAP Response

1

Dis

patc

her

JAX

-WS

and

JA

XB

Java

/XM

L B

ind

ing

End

po

int

Lis

tene

r

JAX-WS Runtime Services

SEI

Other Impl. Classes

(via WSDL or user defined)

Publish WSDL

SOAP Protocol Binding

mu

stU

nder

sta

nd

Pro

cess

ing

WSEE & WS-Metadata

Services

Handler Chain

Handler

(SOAPHandler)

SOAP Fault Processing

Web Service Client

(Java, .NET, PHP, etc.)

Get WSDL

2

3

4

5

67

8

9

JAX-WS Maps WSDL to a Java API

package samples;

import javax.jws.WebService;

@WebServicepublic class Hello { public String sayHello(String s) { return "Hello: " + s; } }

<definitions ... targetNamespace="http://samples/" name="HelloService">

<types> ... </types> <message name="sayHello"> ... </message> <portType name="Hello"> <operation name="sayHello"> <input message="tns:sayHello"/> <output message="tns:sayHelloResponse"/> </operation> </portType> <binding name="HelloPortBinding"

type="tns:Hello"> <soap:binding ... /> <operation name="sayHello"> ... </operation> </binding> <service name="HelloService"> <port name="HelloPort“ ... </port> </service></definitions>

JAXB Maps XML Schema to Java Classes

<schema ... > <element name="simpleOrder"> <complexType> <sequence> <element name="billTo"> <complexType> <sequence> <element name="name" type="string"/> <element name="street" type="string"/> <element name="city" type="string"/> <element name="state" type="string"/> <element name="zip" type="string"/> <element name="phone" type="string"/> </sequence> </complexType> </element> <element name="items"> <complexType> <sequence> <element name="item" type="oms:ItemType" maxOccurs="unbounded"/> </sequence> </complexType> </element> </sequence> </complexType> </element> <complexType name="ItemType"> ... </complexType></schema>

package com.example.oms;

public class SimpleOrder {

protected BillTo billTo; protected Items items;

public BillTo getBillTo() { return billTo; } public void setBillTo(BillTo value) { this.billTo = value; } ...

SimpleOrder

public static class BillTo {

protected String name; protected String street; ...

public String getStreet() { return street; } ...

SimpleOrder.BillTo

What is AJAX?

• Asynchronous JavaScript and XML

• An Interaction Model• A Set of Technologies for

Rich Client Development• ...• A Composite Application

Framework for Flexible Business Process Management ???

“Ajax In Action”, Dave Crane et al., Chapter 2 pg 33

SOA Using JWS and Ajax

Java EE 5 Container

REST Services

@WebServiceProvider

Provider<Source>

SOAShopper Standard XML Schema

eBay API(SOAP)

SOAShopper Internals Amazon API

(SOAP)

Yahoo API(REST)

XML/HTTP

eBay Web Services

Amazon Web Service

Yahoo Shopping Web Services

Internet

Web Browser

retrieveURL(url)(JavaScript Function)

XMLHttpRequest

setData()(Dojo FilteredTable Function)

1

3

6

4

5

7 9

showSearchingMsg()(JavaScript Function)

2

processStateChange()(JavaScript Function)

8

Inte

rne

t

SOAShopper Architecture

Java EE 5 Container

Web Browser(AJAX)

SOAShopper Standard XML Schema

eBay Client Binding(SOAP)

SO

AS

hopp

er A

PI

Amazon Client

Binding(SOAP)

Yahoo Client Binding(REST)

POX

eBay Web Services

Amazon Web Service

Yahoo Shopping

Web Services

Internet

REST based Consumer

WSDL/SOAP based Consumer

SOAP

Inte

rnet

SOAP Endpoint Binding

SOAP Services

WSDL

@WebService

REST Endpoint Binding

REST Services

@WebServiceProvider

Provider<Source>

SOAPEndpoint

RESTEndpoint

Demo

SOAShopper – Integrating Yahoo!, Amazon, and eBay

eBay WSDL

Ant Task to Compile eBay WSDL

Using the Generated eBay API

A Client Binding Example

eBay Client Binding

SOAShopper API

eBay Web Services

Internet

public List<Offer> offerSearch(String keywords, Category category,

Price lowprice, Price highprice) {

ShopperImp binding = BindingService.getBinding(

ShopperImp.class, EBayAPIInterface.class);

return binding.offerSearch(keywords,

category, lowprice, highprice);

}

Implementing a Binding Service

public abstract class BindingService {

public static <C> C getBinding( Class<C> client, Class<?> service) { ...

}

}

Flash Demo

Using the Dojo Table Widget

<table dojoType="filteringTable" id="fromSOAShopperData" multiple="true" alternateRows="true" cellpadding="0" cellspacing="0" border="0" style="margin-bottom:24px;"> <thead> <tr> <th field="source" dataType="String">Source</th> <th field="thumbnail" dataType="html" align="center">Image</th> <th field="price" dataType="String">Price</th> <th field="summary" dataType="String">Summary</th> <th field="url" dataType="html">Link</th> </tr> </thead></table>

Invoking the REST Endpoint function retrieveURL(url) { restURL = url; showSearchingMsg(restURL); if (window.XMLHttpRequest) { // Non-IE browsers req = new XMLHttpRequest(); req.onreadystatechange = processStateChange; try { req.open("GET", url, true); req.setRequestHeader('Content-type','text/xml'); } catch (e) { alert(e); } req.send(null); } else if (window.ActiveXObject) { // IE req = new ActiveXObject("Microsoft.XMLHTTP");

... }

}

Loading the Dojo Table

function populateTableFromLiveSOAShopperData() {

try { var w = dojo.widget.byId("fromSOAShopperData");

w.store.setData(theSOAShopperLiveData); } catch(e) { alert(e); }

}

Resources

• SOA Using Java Web Services http://soabook.com

• Thomas Erl’s SOA Book http://www.thomaserl.com/books.asp

• Ajax In Action http://www.manning.com/crane/

• SOA-J open source project http://soa-j.org