24
SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services” http://soabook.com

SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

  • View
    218

  • Download
    1

Embed Size (px)

Citation preview

Page 1: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

SOA using Java Web Services and Ajax

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

http://soabook.com

Page 2: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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

Page 3: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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!)

Page 4: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

What is SOA?

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

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

Page 5: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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

Page 6: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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

Page 7: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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

Page 8: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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

Page 9: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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>

Page 10: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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

Page 11: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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

Page 12: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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

Page 13: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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

Page 14: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

Demo

SOAShopper – Integrating Yahoo!, Amazon, and eBay

Page 15: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

eBay WSDL

Page 16: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

Ant Task to Compile eBay WSDL

Page 17: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

Using the Generated eBay API

Page 18: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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);

}

Page 19: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

Implementing a Binding Service

public abstract class BindingService {

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

}

}

Page 20: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

Flash Demo

Page 21: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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>

Page 22: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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");

... }

}

Page 23: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

Loading the Dojo Table

function populateTableFromLiveSOAShopperData() {

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

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

}

Page 24: SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services”

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