CXF for the enterprise and web - PowerPoint PPT Presentation

1 / 61
About This Presentation
Title:

CXF for the enterprise and web

Description:

CXF is designed to have simple, extensible APIs. JAX-WS Frontend ... var customers = eval(res); customers[0].customer.name. 48. Introducing Jettison ... – PowerPoint PPT presentation

Number of Views:340
Avg rating:3.0/5.0
Slides: 62
Provided by: dandie
Category:
Tags: cxf | enterprise | var | web

less

Transcript and Presenter's Notes

Title: CXF for the enterprise and web


1
CXF for the enterprise and web
  • Dan Diephouse

2
What is CXF all about?
  • (You mean you cant tell by the name?)

3
Support for Web Services Standards
  • SOAP 1.1, 1.2
  • WSDL 1.1
  • WS-I BasicProfile 1.1
  • WS-Addressing 2004/08, 1.0
  • WS-Policy
  • WS-ReliableMessaging 1.0
  • WS-Security 1.0, 1.1

4
Developer ergonomics
  • CXF is designed to have simple, extensible APIs
  • JAX-WS Frontend
  • Annotations for building web services
  • WSDL2Java support
  • APIs for building dynamic services and clients
  • Simple Frontend
  • Build services with no annotations easily
  • Spring integration

5
Embeddability
  • CXF was designed as a component for
  • Applications
  • Application Servers
  • ESBs

6
Performance
  • Streaming XML model
  • Old Java SOAP frameworks (like Axis 1.x) CXF
    built a DOM representation
  • Slow
  • Memory intensive
  • CXF binds XML stream directly to POJOs using high
    performance databindings
  • JAXB
  • Aegis

7
History
  • Merger of Celtix and XFire communities
  • Apache Incubator project
  • Started July 2006
  • Improvements over XFire
  • Cleaner architecture
  • Focus on supporting more of WS-
  • Improved JMS support
  • Spring 2.0
  • REST
  • JSON
  • Asynchronous
  • Much more!

8
Building JAX-WS Services
9
The Customer Service
  • Customer Service
  • We need to
  • Add
  • Remove
  • Get
  • Delete

10
The Customer Service
  • public interface CustomerService
  • Customers getCustomers()
  • Customer getCustomer(long id)
  • void updateCustomer(Customer c)
  • long addCustomer(Customer c)
  • void deleteCustomer(long id)

11
JAX-WS
  • Annotations and APIs for building and consuming
    web services
  • Server
  • _at_WebService, _at_WebMethod, _at_WebParam, _at_WebResult..
  • Provider
  • wsdl2java generated services
  • Client
  • Dispatch
  • wsdl2java generated clients

12
JAX-WS
  • _at_WebService( endpointInterface
  • CustomerService )
  • public class CustomerServiceImpl
  • Customer getCustomer(long id)

13
JAX-WS
  • _at_WebService
  • public interface CustomerService
  • _at_WebResult(nameCustomer)
  • Customer getCustomer(
  • _at_WebParam(nameid) long id)

14
JAXB
  • Standard Java data-binding
  • Uses annotations to map classes to XML
  • Fast and easy to use

15
JAXB
  • _at_XmlRootElement
  • public class Customer
  • private long id
  • public long getId()
  • public void setId(long id)

16
Building a SOAP service with CXF
  • JaxWsServerFactoryBean sf
  • new JaxWsServerFactoryBean()
  • sf.setServiceClass(CustomerServiceImpl.class)
  • sf.setAddress("http//localhost8080/soap")
  • sf.setServiceBean(new CustomerServiceImpl()))
  • sf.create()

17
Spring 2 too
  • ltbeans ..gt
  • ltjaxwsendpoint
  • implementorcom.foo.CustomerServiceImpl
  • addresshttp//host/CustomerServicegt
  • lt/beansgt

18
Using your Web Service
  • Create a client proxy with JAX-WS APIs
  • Generate a client with WSDL2Java
  • Use .NET, Ruby, Python, etc

19
JAX-WS Way
  • QName SERVICE_NAME
  • new QName("http//customer. com/",
    CustomerService")
  • QName PORT_NAME
  • new QName("http//server.hw.demo/", "
    CustomerServicePort")
  • Service service Service.create(new URL(wsdl),
    SERVICE_NAME)
  • service.addPort(PORT_NAME,
  • SOAPBinding.SOAP11HTTP_BINDING,
  • http//localhost/customer")
  • CustomerService client service.getPort(CustomerS
    ervice .class)
  • Customer c client.getCustomer()

20
CXF Way
  • JaxWsProxyFactoryBean pf new JaxWsProxyFactoryBe
    an()
  • pf.setServiceClass(CustomerService.class)
  • pf.setAddress(http//localhost/customer)
  • pf.setWsdlUrl(http//localhost/customer?wsdl)
  • CustomerService client (CustomerService)
    pf.create()

21
Spring 2 too
  • ltbeans ..gt
  • ltjaxwsclient idfooClient
  • implementorcom.foo.CustomerService
  • addresshttp//localhost/customers
  • wsdlUrlhttp//localhost/customers?wsdlgt
  • lt/beansgt

22
Faults
  • Similar to exceptions but for SOAP
  • You can declare your own custom faults
  • Need to
  • Create class FooFault
  • Annotate with _at_WebFault
  • Add a getFaultInfo()
  • Fault info is just any JAXB bean

23
Fault Example
  • _at_WebFault
  • public class CustomerNotFoundFault extends
    Exception
  • private CustomerNotFoundDetails details
  • public CustomerNotFoundFault(CustomerNotFoundD
    etails details)
  • super()
  • this.details details
  • public CustomerNotFoundDetails getFaultInfo()
  • return details

24
Update Service Class
  • Customer getCustomer(
  • _at_WebParam(name "id") String id)
  • throws CustomerNotFoundFault

25
What about WSDL first?
  • Gives clearer view into
  • what changes may or may not be backward/forward
    compatibile
  • what datatypes are being used (i.e. are you using
    dateTime when you mean date?)
  • Allows you to document your services more easily
  • Generates artifacts which look just like code
    first ones
  • Setup is exactly the same after generation!

26
Maven
  • ltplugingt
  • ltgroupIdgtorg.apache.cxflt/groupIdgt
  • ltartifactIdgtcxf-codegen-pluginlt/artifactIdgt
  • ltversiongt2.0.1-incubatorlt/versiongt
  • ltexecutionsgt
  • ltexecutiongt
  • ltidgtgenerate-sourceslt/idgt
  • ltphasegtgenerate-sourceslt/phasegt
  • ltconfigurationgt
  • ltsourceRootgtbasedir/target/generated/sr
    c/main/javalt/sourceRootgt
  • ltwsdlOptionsgt
  • ltwsdlOptiongt
  • ltwsdlgtbasedir/src/main/wsdl/myServi
    ce.wsdllt/wsdlgt
  • lt/wsdlOptiongt
  • lt/wsdlOptionsgt
  • lt/configurationgt
  • ltgoalsgt
  • ltgoalgtwsdl2javalt/goalgt
  • lt/goalsgt

27
Command line
  • wsdl2java -p com.acme.customer customer.wsdl

28
Ant
  • lttarget name"cxfWSDLToJava"gt
  • ltjava classname"org.apache.cxf.tools.wsdlto.WSD
    LToJava" fork"true"gt
  • ltarg value"-client" /gt
  • ltarg value"-d" /gt
  • ltarg value"src/main/java" /gt
  • ltarg valuecustomer.wsdl" /gt
  • ltclasspathgt
  • ltpath refid"cxf.classpath" /gt
  • lt/classpathgt
  • lt/javagt
  • lt/targetgt

29
Generated Client
  • CustomerServiceService service new
    CustomerServiceService()
  • CustomerService client service.getSoapPort()
  • // Possibly set an alternate request URL
  • ((BindingProvider) client).getRequestContext()
  • .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
  • "http//localhost63081/greeter")
  • Customer customer client.getCustomer()

30
More Client options
  • CustomerService client service.getSoapPort()
  • MapltString, Objectgt reqCtx ((BindingProvider)
    client).getRequestContext()
  • reqCtx.put(BindingProvider.ENDPOINT_ADDRESS_PROPER
    TY, "http//localhost63081/greeter")
  • reqCtx.put(BindingProvider.USERNAME, dan")
  • reqCtx.put(BindingProvider.PASSWORd, foobar")

31
Features
  • Includes support for enabling features with a
    single line of code
  • Can apply to Bus, Server, Client
  • Use bundled features or write your own!
  • Bundled features
  • WS Addressing
  • WS Policy
  • Logging
  • Failover
  • Response time
  • Colocation feature

32
Applying Features
  • ltbeans xmlns"http//www.springframework.org/schem
    a/beans"
  • xmlnsc"http//cxf.apache.org/core"
  • xmlnsxsi"http//www.w3.org/2001/XMLSchema-inst
    ance"
  • xsischemaLocation"
  • http//www.springframework.org/schema/beans
    http//www.springframework.org/schema/beans/spring
    -beans.xsd
  • http//cxf.apache.org/core http//cxf.apache.org/s
    chemas/core.xsd"gt
  • ltcbusgt
  • ltcfeaturesgt
  • ltclogging /gt
  • lt/cfeaturesgt
  • lt/cbusgt
  • lt/beansgt

33
Applying Features
  • ltjaxwsendpoint
  • implementorcom.foo.CustomerServiceImpl
  • addresshttp//host/CustomerServicegt
  • ltjaxwsfeaturesgt
  • ltcxf-corelogging/gt
  • ltwsaaddressing/gt
  • lt/jaxwsfeaturesgt
  • lt/jaxwsendpointgt

34
Write your own feature!
  • public class MyFeature extends AbstractFeature
  • public void initialize(Bus bus)
  • public void initialize(Client client, Bus
    bus)
  • public void initialize(Server server, Bus
    bus)

35
RESTful Services
36
RESTful Services
  • REpresentational State Transfer
  • Instead of messages, we have resources
  • Resources are accessed via verbs
  • URIs are accessed via GET, POST, PUT, DELETE
  • HTTP is the example.

37
Why Just use HTTP?
  • Linkability EVERYTHING is accessible and
    addressable via a simple URL
  • Simplicity
  • Uniform interface
  • Testable via a browser
  • No WSDL
  • Works well with caches
  • Integrates well with dynamic languages such as
    Javascript or Ruby

38
HTTP Methods
39
The CXF HTTP Binding
  • Allows you to map operations to different URI
    Verb combinations
  • Convention based mapping
  • Annotation based mapping
  • You can also supply your own class to do the
    mapping

40
Example using conventions
  • getCustomers() -gt GET /customers
  • getCustomer(id) -gt GET /customers/id
  • addCustomer(Customer) -gt POST /customers
  • updateCustomer(Customer) -gt PUT
    /customers/id
  • deleteCustomer(id) -gt DELETE /customers/id

41
Example using annotations
  • _at_Get
  • _at_HttpResource(/customers/id)
  • Customer getCustomer(String id)
  • _at_Post
  • _at_HttpResource(/customers)
  • String addCustomer(Customer c)

42
Schema -gt Parameter Matching
  • ltelement namegetCustomergt
  • ltcomplexTypegt
  • ltsequencegt
  • ltelement nameid typelong/gt
  • lt/sequencegt
  • lt/complexTypegt
  • lt/elementgt

Matches the id in the URI template
43
Schema -gt Parameter Matching
  • Client Request
  • Server Response
  • ltgetCustomergt
  • ltidgt123lt/idgt
  • lt/getCustomergt
  • ltgetCustomerResponsegt
  • ltcustomergt
  • .
  • lt/customergt
  • lt/getCustomerResponsegt

44
Unwrapped mode
  • _at_Get
  • _at_HttpResource(/customers/id)
  • Customer getCustomer(GetCustomers request)
  • Avoids returning
  • ltgetCustomersResponsegt
  • ltcustomersgt..lt/customersgt
  • lt/getCustomersResponsegt
  • Returns ltcustomersgtlt/customersgt

45
Creating the Service
  • JaxWsServerFactoryBean sf
  • new JaxWsServerFactoryBean()
  • sf.setServiceBean(new CustomerService())
  • sf.setBindingId(
  • HttpBindingFactory.HTTP_BINDING_ID)
  • sf.setAddress("http//localhost8080/xml")
  • sf.getServiceFactory().setWrapped(false)
  • sf.create()

46
Accessing the Service
  • To the web browser and command line!
  • wget http//localhost/xml/customers
  • wget --post-file customer.xml http//localhost/xml
    /customers

47
JSON
  • JSON Javascript Object Notation
  • The data format IS Javsacript
  • This enables easy integration with web pages
  • Example
  • customer
  • name Jim Bob,
  • id 12345

48
Javascript
  • var res
  • '(' xmlhttp.responseText ')'
  • var customers eval(res)
  • customers0.customer.name

49
Introducing Jettison
  • Jettison is a StAX implementation which reads and
    writes JSON instead of XML
  • http//jettison.codehaus.org
  • Two variations on how to map XML to JSON
  • Mapped convention
  • BadgerFish
  • Mapped Convention allows you to map an xml
    element to a JSON

50
Mapped Convention Example
  • ltcustomergt
  • ltnamegtJim Boblt/namegt
  • lt/customergt
  • Becomes
  • ltccustomer
  • xmlnscurnacmecustomergt
  • ltcnamegtJim Boblt/cnamegt
  • lt/ccustomergt
  • customer
  • name Jim Bob
  • acme.customer
  • acme.name Jim Bob

51
Enabling Jettison on your endpoint
  • Map nstojns new HashMap()
  • nstojns.put("http//customer.acme.com", "acme")
  • MappedXMLInputFactory xif new
    MappedXMLInputFactory(nstojns)
  • Map properties new HashMap()
  • properties.put(XMLInputFactory.class.getName(),
    xif)
  • serverFactory.setProperties(properties)

52
Using the Service
  • To the web browser and command line!
  • wget http//localhost/json/customers
  • wget --post-file customer.json http//localhost/js
    on/customers

53
Writing a Client
  • JaxWsProxyFactoryBean sf
  • new JaxWsProxyFactoryBean()
  • sf.setServiceClass(CustomerService.class)
  • sf.getServiceFactory().setWrapped(false)
  • sf.setBindingId(
  • HttpBindingFactory.HTTP_BINDING_ID)
  • sf.setAddress("http//localhost8080/xml/")
  • CustomerService cs (CustomerService)
    sf.create()

54
Advantages
  • Easy mapping to HTTP via conventions or
    annotations
  • Support for full set of HTTP verbs
  • Support for JSON via Jettison allows easy
    integration with Javascript

55
Security and 5 minute guide to ws-
56
Security
  • Authentication HTTP, WS-Security, Custom
    headers
  • Authorization Acegi, Custom code
  • Integrity WS-Security
  • Privacy HTTPS, JMS, WS-Security
  • Avoid WS-Security whenever possible. Its slow.
  • Possible exception UsernameToken

57
WS-Addressing
  • What
  • Headers to route messages and their responses as
    well as provide metadata
  • When
  • Long lived invocations use support for
    asynchronous decoupled interactions
  • Using WS-RM

58
WS-Security
  • What
  • Encryption, Signature, Timestamps, Tokens
  • When
  • SSL certificate information is lost by a gateway
  • Message passes through unencrypted transports and
    SSL isnt an option
  • You use authentication tokens like
    username/password, SAML, etc

59
WS-ReliableMessaging
  • What
  • Enables reliable delivery of messages through
    message acknowledgements and retries
  • When
  • Bridging systems over a non reliable transport
  • JMS isnt an option

60
WS-Policy
  • What
  • Allows you to specify criteria which must be met
    to interact with your service
  • When
  • Configuring WS-RM or WS-A requirements for a
    service
  • (Security policy hopefully coming in the next
    releas)

61
Questions?
  • Email dan.diephouse_at_mulesource.com
  • Company http//mulesource.com
  • Blog http//netzooid.com
Write a Comment
User Comments (0)
About PowerShow.com