SOA using Java Web Services and Ajax by zwj23860

VIEWS: 49 PAGES: 24

									SOA using Java Web
 Services and Ajax
       Mark D. Hansen
Author 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


   – SOAP                         OA
                                     P            provider
                                 S
   – XML / HTTP                      WSDL

• A description or contract
  that specifies the                     XML /
                                         HTTP
  request/response
                                   ST
  message structure.             RE
                                                  provider
   – WSDL                             XML
                                     Schema


   – XML Schema (insufficient)            * Maybe WADL?




   – WADL (new)
 What are Java Web Services (JWS)?

• The provider is written in
  Java and deployed using             Java Virtual Machine

  a Java container.
• The consumer is written                     Web Service Proxy

  in Java.
• Java Web Service APIs                               Java Interface
                                                                              SOAP

  are used:                                                                  Message


   –   JAX-WS
   –   JAXB                            Web Services Container

   –   WS-Metadata (JSR-181)
   –   WSEE (JSR-109)                         SOAP Endpoint
                                                                       service

  JAX-RPC is deprecated starting in
  Java EE 5                                          WSDL Interface
                     Client Side JWS
                                                                           Web Service
                                1       WSDL to Java
                     Service            Mapping Tool
                                                                                WSDL
                    Endpoint
                    Interface           (e.g., wsimport)
                                                                             Endpoint URL



                                                                              SOAP
                                                                             Response
                                    javax.xml.ws.Service               4
                                                              SOAP
                                            getPort(...) 2   Request

  Parameters
                         3            Proxy Instance
(JAXB Generated
 Class Instances)
                                 Service
                                                Invocation
  Return Value                  Endpoint
                                                 Handler
                                Interface
(JAXB Generated
 Class Instance)         5
                                     Server Side JWS
    2
                                                          SOAP Protocol Binding                      6             Web Service
                                                                                                                                               7
                 Endpoint Listener
 SOAP                                3




                                                                                                JAX-WS and JAXB
                                                                                                                      @WebService or




                                                                                                Java/XML Binding
                                                          8 SOAP Fault Processing


                                         Dispatcher
Request
                                                                                                                     @WebServiceProvider
                                                                           Handler Chain




                                                           nd Processing
                                                           mustUndersta
                                                                                            5                          SEI
 SOAP
                                                      4                                                                           Meta-Data
Response                                                                        Handler                             Other Impl.
                                                                                                                     Classes       (WSDL,
9                                                                           (SOAPHandler)                                          Handler
                       Publish                                                                                      (via WSDL
                       WSDL                                                                                                       File, Depl
                                                                                                                      or user
                                                                                                                                  Descrpt.)
                                                                                                                     defined)
                 WSEE &
                WS-Metadata                                        JAX-WS Runtime Services
                 Services


           1   Get WSDL


                 Web Service Client
               (Java, .NET, PHP, etc.)
      JAX-WS Maps WSDL to a Java API

package samples;                         <definitions ...
                                             targetNamespace="http://samples/"
import javax.jws.WebService;                 name="HelloService">
                                           <types> ... </types>
@WebService                                <message name="sayHello"> ... </message>
public class Hello {                       <portType name="Hello">
                                             <operation name="sayHello">
    public String sayHello(String s) {         <input message="tns:sayHello"/>
      return "Hello: " + s;                    <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 ... >                                         SimpleOrder
     <element name="simpleOrder">
       <complexType>                                     package com.example.oms;
         <sequence>
          <element name="billTo">                        public class SimpleOrder {
           <complexType>
             <sequence>                                    protected BillTo billTo;
              <element name="name" type="string"/>         protected Items items;
              <element name="street" type="string"/>
              <element name="city" type="string"/>         public BillTo getBillTo() {
              <element name="state" type="string"/>          return billTo;
              <element name="zip" type="string"/>          }
              <element name="phone" type="string"/>
             </sequence>                                   public void setBillTo(BillTo value) {
           </complexType>                                    this.billTo = value;
          </element>                                       }
          <element name="items">
           <complexType>                                   ...
             <sequence>
              <element name="item" type="oms:ItemType"
               maxOccurs="unbounded"/>
             </sequence>
           </complexType>                                   SimpleOrder.BillTo
          </element>
         </sequence>                                     public static class BillTo {
       </complexType>
     </element>                                            protected String name;
     <complexType name="ItemType">                         protected String street;
     ...                                                   ...
     </complexType>
    </schema>                                              public String getStreet() {
                                                             return street;
                                                           }

                                                           ...
               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
 Web Browser
                       1
    retrieveURL(url)          showSearchingMsg()           setData()
    (JavaScript Function)     (JavaScript Function)        (Dojo FilteredTable
                                2                          Function)
      XMLHttpRequest
                              processStateChange()
               3       7                                     9
                              (JavaScript Function)
                                8
                   4

                       XML/
                       HTTP


                                        Internet




 Java EE 5 Container

                       5
    REST Services                                      eBay API                               eBay Web
                                       6
                                                        (SOAP)                                 Services




                                                                                 Internet
      @WebServiceProvider        SOAShopper
                                  Internals           Amazon API
                                                                                             Amazon Web
        Provider<Source>                               (SOAP)
                                                                                               Service
                                                      Yahoo API
    SOAShopper Standard                                (REST)                               Yahoo Shopping
       XML Schema                                                                            Web Services
SOAShopper Architecture
              Web Browser          REST based                 WSDL/SOAP
                (AJAX)              Consumer                based Consumer




                          POX                                           SOAP


                                                Internet


           Java EE 5 Container

              SOAP Endpoint
              Binding SOAP Services                                            eBay Client
                                                                                                         eBay Web
                                                                                Binding
                                                                                                          Services
                                 @WebService                                    (SOAP)
 SOAP
Endpoint
                                                       SOAShopper API
                                 WSDL

                                                                                Amazon




                                                                                             Internet
                                                                                 Client                 Amazon Web
                   SOAShopper Standard                                          Binding                   Service
                      XML Schema
                                                                                (SOAP)
              REST Endpoint
              Binding REST Services
                                                                           Yahoo Client                   Yahoo
                             Provider<Source>
                                                                             Binding                     Shopping
 REST
Endpoint                         @WebService                                 (REST)                     Web Services
                                  Provider
        Demo

SOAShopper – Integrating
Yahoo!, Amazon, and eBay
eBay WSDL
Ant Task to Compile eBay WSDL
Using the Generated eBay API
A Client Binding Example
                    public List<Offer> offerSearch(
SOAShopper API        String keywords, Category category,
                      Price lowprice, Price highprice) {

                        ShopperImp binding =
                           BindingService.getBinding(
   eBay Client               ShopperImp.class,
                             EBayAPIInterface.class);
    Binding
                        return
                           binding.offerSearch(keywords,
     Internet                 category, lowprice, highprice);

                    }


eBay Web Services
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

								
To top