implementing webservices with camel and cxf in servicemix

43
right © 2010 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 1 A Progress Software Company A Progress Software Company Creating and consuming web services with camel-cxf Adrian Trenaman, March 3 rd 2011 twitter : adrian_trenaman | LinkedIn: adrian.trenaman http://trenaman.blogspot.com

Upload: adrian-trenaman

Post on 27-Jan-2015

16.971 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2010 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 1 A Progress Software Company

A Progress Software Company

Creating and consuming web services with camel-cxf

Adrian Trenaman,March 3rd 2011

twitter : adrian_trenaman | LinkedIn: adrian.trenamanhttp://trenaman.blogspot.com

Page 2: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 2 A Progress Software Company

When you joined today’s session …

Audio is broadcast from your computer

Submit your questions via the Chat Window

Contact today’s Host

via the Chat Window

Submit your questions via the Chat Window

Contact today’s Host

via the Chat Window

Page 3: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 3 A Progress Software Company

Our Speaker – Adrian Trenaman

15 years IT consulting experience• IONA Technologies, Progress Software Corp,

FuseSource• Committer, Apache Karaf• SOA, ESB, Open Source, BPM, Web Services, CORBA, …

Solution focused: architecting, mentoring, speaking, engineering, doing…

PhD Artificial Intelligence• Dip. Business Development• BA Mod Computer Science

http://trenaman.blogspot.comhttp://slideshare.net/trenaman twitter: adrian_trenamanLinkedIn: adrian.trenaman

http://trenaman.blogspot.comhttp://slideshare.net/trenaman twitter: adrian_trenamanLinkedIn: adrian.trenaman

Page 4: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 4 A Progress Software Company

SOAP 101: it’s all about body and headers

<soapenv:Envelope

xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:cus="http://demo.fusesource.com/wsdl/CustomerService/">

<soapenv:Header/>

<soapenv:Body>

<cus:lookupCustomer>

<customerId>0123</customerId>

</cus:lookupCustomer>

</soapenv:Body>

</soapenv:Envelope>

Message can contain zero or more headers!

Message can contain zero or more headers!

Body can contain a list of elements (RPC-literal) or a single element (Doc-literal / Wrapped Doc-literal)

Body can contain a list of elements (RPC-literal) or a single element (Doc-literal / Wrapped Doc-literal)

Key idea: the camel-cxf component lets you easily manipulate, route and transform the content of your SOAP messages.

Key idea: the camel-cxf component lets you easily manipulate, route and transform the content of your SOAP messages.

Page 5: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 5 A Progress Software Company

The goal – deploy / consume web services in with Camel

Show how to use the camel-cxf component to produce and consume SOAP messages

Some motivating use cases: • Trigger an integration flow from an incoming SOAP message, and provide

a SOAP response when it’s complete.– i.e. Implement a web service interface using a camel route.– Remember: there’s nothing wrong implementing a web service using plain-

old-java-code! See the first webinar for more!• Route XML from incoming SOAP/HTTP calls to XML/JMS.• Invoke on a web service from within an integration flow• Apply elegant Camel EIPs such as retry, error handling and throttling to a

web service endpoint.

Page 6: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 6 A Progress Software Company

Agenda: implementing web services with CXF and ServiceMix

A look at the options available to you…• POJO-based approach: you work with JAX-B objects.• Payload-based approach: you work with raw XML payload

– No code generation required!• Provider-based approach: SAXSource, DOMSource or StreamSource

Refresh on key techniques from first CXF webinar: • Code generation: WSDL, XSD -> JAX-WS, JAX-B• OSGi bundle-based packaging & deployment

New techniques using Camel: • Define camel-cxf endpoints for POJO, Payload and Provider

approaches.• How to work with the different payload types.

Useful references for your future work.

Page 7: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 7 A Progress Software Company

You will be spoilt for choice.

Apache ServiceMix thrives on innovation and experimentation.• As a result, there’s many different techniques you can use to implement

flows with camel-cxf.

My recommendations:• If you want to transform and route SOAP traffic with little marshalling

overhead, then prefer Payload mode.– If large files, consider using camel-cxf with a SAXSource Provider!

• If you want to route SOAP traffic but perform intensive Java manipulation of message content, then use POJO mode.

Page 8: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 8 A Progress Software Company

Sit back, relax, enjoy and ask questions.

Sample code will be shared at the end of the webinar!

We’ll cover a lot – ask questions as we go.

This session is being recorded• You can re-view later if you miss something.

Page 9: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 9 A Progress Software Company

Using camel-cxf in POJO mode

Page 10: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 10 A Progress Software Company

What does POJO format really mean?

POJO format is the default format for camel-cxf.

The SOAP body is marshaled into a list of Java objects• One Java object for each part of the SOAP body, or each parameter of a

wrapped doc-literal request.• Appropriate Java classes are code-generated in advance using

JAX-WS/JAX-B.• The body of the Camel message contains a MessageContentsList object

Any SOAP headers are transmitted as a header in the exchange’s in message.

Page 11: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 11 A Progress Software Company

Ticklist – creating a route using camel-cxf pojo

Generate JAX-WS code from WSDL• … using the Maven cxf-codegen-plugin

Configure camel-cxf endpoints and integration framework using Spring Framework.• … using appropriate conventions so ServiceMix can locate your

Spring contexts.

Package your service• Here, use the maven-bundle-plugin for OSGi bundles and

deploy into Fuse ESB

Make sure you have appropriate TypeConverters available• Test your route!

Page 12: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 12 A Progress Software Company

Using the cxf-codegen-plugin

Maven-friendly location for generated code!

Maven-friendly location for generated code!

Location of WSDL fileLocation of WSDL file

Page 13: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 13 A Progress Software Company

Configuring a service implementation using Spring Framework

Here’s where we define the camel-cxf endpoint, and tie it to your SEI.

Here’s where we define the camel-cxf endpoint, and tie it to your SEI.

Useful technique: header-based routing by operationName to custom Processors

Useful technique: header-based routing by operationName to custom Processors

Page 14: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 14 A Progress Software Company

Accessing the message parts as POJOs

Use Camel Processors to get access to the CXF messages.

The body of a Camel CXF POJO message contains a MessageContentList.• For a request, this contains the In parameters, and InOut/Out parameter

holders in the order they are defined in the WSDL message.• For a response, this contains the return value (if any), followed by the

InOut/Out parameter holders in the order they are defined in the WSDL message.

Note: you need to know what to expect in the MCL, and in what order!

Page 15: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 15 A Progress Software Company

Accessing the message parts as POJOs (cont’)

For example, consider the getCustomerStatus operation:• 1 x In parameter: customerId• 2 x Out parameters: status, statusMessage.

Page 16: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 16 A Progress Software Company

Accessing the message parts as POJOs (cont’)

… the generated JAX-WS method looks like this:

Page 17: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 17 A Progress Software Company

Accessing the message parts as POJOs (cont’)

… the corresponding payload will look like this: • Request MCL =

{String customerId, Holder<String> status, Holder<String> statusMsg}

• ResponseMCL = {Holder<String> status, Holder<String> statusMsg}

You can access the payload as a MessageContentsList object or as an Object[] see next slide for example)

Page 18: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 18 A Progress Software Company

Accessing the message parts as POJOs (cont’)

Example of accessing the data (see GetCustomerProcessor):

Page 19: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 19 A Progress Software Company

Packaging – OSGi bundles

The Spring configuration should be placed in the META-INF/spring directory in src/main/resources.

For OSGi bundling: • Usually keep implementation

packages private; however, default configuration will work fine.

• See Over!

Page 20: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 20 A Progress Software Company

Packaging – OSGi bundles (cont’) – maven-bundle-plugin

Configuration of maven-bundle-plugin is minimal!• Don’t forget to set <packaging>bundle</packaging> to

activate this.

Page 21: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 21 A Progress Software Company

Demo – deploy the POJO route!

Page 22: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 22 A Progress Software Company

Using the PAYLOAD format with camel-cxf

Page 23: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 23 A Progress Software Company

What is the ‘PAYLOAD’ format?

In PAYLOAD format the body of the Camel message contains a CxfPayload object.• getHeaders() returns a list of SoapHeader.• getBody() returns a list of org.w3c.dom.Element.

Allows you get to access body as a DOM Element• No need for generated code• Benefit: can save on the overhead of marshaling to and from JAX-B

objects.• Drawback: uses a DOM model; may not be good for large files.

Suits situations where you wish to manipulate raw XML payload; examples:• Route from SOAP/HTTP to JMS• Generate response using templates like Velocity

Page 24: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 24 A Progress Software Company

Ticklist – creating a route using camel-cxf payload

Configure camel-cxf endpoints and integration framework using Spring Framework.• … using appropriate conventions so ServiceMix can locate your

Spring contexts.

Package your service• Here, use the maven-bundle-plugin for OSGi bundles and

deploy into Fuse ESB

Make sure you have appropriate TypeConverters available• Test your route!

Page 25: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 25 A Progress Software Company

Spring configuration for PAYLOAD format

You must set the dataFormat to PAYLOAD in the URI.

You must set the dataFormat to PAYLOAD in the URI.

No need for generated code! Just specify the WSDL location.

No need for generated code! Just specify the WSDL location.

Page 26: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 26 A Progress Software Company

SOAP/HTTP to JMS

Very popular use-case!• Place incoming payload onto a reliable JMS queue for offline processing,

and return an acknowledgment response.• Note usage of inOut and jmsMessageType when sending to the

queue!• Note creation of response from inline XML – neat!

Page 27: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 27 A Progress Software Company

Using XPath templates for request-response traffic

Nice idea: inject values into a pre-packaged XML template.• Use XPath to extract useful data from the request and store as a header.• Inject response values into XML!

Nice use of the velocity component to generate response!

Nice use of the velocity component to generate response!

Processor does the work of getting the customer status, maybe from a DB or backend system.

Processor does the work of getting the customer status, maybe from a DB or backend system.

Page 28: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 28 A Progress Software Company

Using headers to transmit request information…

A custom Processor can retrieve the customerId, and store response information as headers on the message.

Page 29: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 29 A Progress Software Company

Creating a response using Velocity

Headers from the Camel Exchange are injected easily into a Velocity template using ${headers.<headerName>} place-holders.• Example velocity template

(src/main/resources/getCustomerStatusResponse.vm):

Page 30: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 30 A Progress Software Company

Payload conversion issues.

Working with CxfPayload is sometimes wearisome.• How do you convert a String to a CxfPayload?

– Velocity component produces a String, but camel-cxf needs a CxfPayload object in the response

• How do you convert a CxfPayload to a String? – Want to place a JMS TextMessage on the queue; however, no conversion

available from a CxfPayload to a String • How do you convert a CxfPayload to an org.w3c.dom.Node?

– Want to run an X-Path expression on the payload, but X-Path expects a Node

Without the right conversions in place, you may see: • Class cast exceptions in the route.• Empty SOAP body on the return message.

Page 31: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 31 A Progress Software Company

Payload conversion is common in Camel routes!

Recall: Camel doesn’t enforce a canonical format.• It is the endpoint at the start of the route that determines what’s in the

message body.• An InputStream, a String, a Node, a CxfPayload, …

Camel will lazily convert from one format to another when needed.• Popular conversions are provided within Camel! • You can write your own converters easily (see over)

– Also see http://camel.apache.org/type-converter.html

Our example includes some custom converters.• … I’ll contribute these to the community. Honest!

Page 32: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 32 A Progress Software Company

Sample converter

Note: @Converter annotation used to allow automatic registration of converters with Camel runtime.

Note: @Converter annotation used to allow automatic registration of converters with Camel runtime.

Page 33: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 33 A Progress Software Company

Demo – deploy the payload route!

Page 34: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 34 A Progress Software Company

Hidden Gem: Camel CXF and the JAX-WS Provider interface.

Page 35: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 35 A Progress Software Company

CxfPayload is great, but…

CxfPayload always creates DOM elements – this might be inappropriate for large XML payload.• If you want to access your payload as a SAXSource or StreamSource, use

CXF’s support for JAX-WS Providers.

JAX-WS Providers offer an elegant alternative accessing SOAP payload without marshaling to Java • See http://cxf.apache.org/docs/provider-services.html

Page 36: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 36 A Progress Software Company

Ticklist – creating a route using camel-cxf JAX-WS Provider

Create a Provider<?> implementation class.• Replacing ‘?’ with SAXSource, DOMSource or StreamSource

Configure camel-cxf endpoints and integration framework using Spring Framework.• … using appropriate conventions so ServiceMix can locate your

Spring contexts.

Package your service• Here, use the maven-bundle-plugin for OSGi bundles and

deploy into Fuse ESB

Make sure you have appropriate TypeConverters available• Test your route!

Page 37: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 37 A Progress Software Company

The Provider class

Note: the invoke() method is a dummy placeholder – only it’s signature is used when registering your service endpoint!

Note: the invoke() method is a dummy placeholder – only it’s signature is used when registering your service endpoint!

We define a provider to receive the SOAP body (payload) as a SAXSource

We define a provider to receive the SOAP body (payload) as a SAXSource

Page 38: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 38 A Progress Software Company

Spring configuration

Now your routes can access the message body as a SAXSource

Configure the cxfEndpoint, using your provider as the serviceClass.Configure the cxfEndpoint, using your provider as the serviceClass.

Page 39: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 39 A Progress Software Company

Summing up!

Page 40: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 40 A Progress Software Company

Summing up

Camel gives you a number of options on how to implement and consume and provide SOAP web services with CXF• POJO: Great for when you want to access the payload as Java objects• PAYLOAD: Great for when you want access to the SOAP Body for

transformation and routing purposes.• PROVIDER: Great for when you want to to access SOAP payload as a

DOMSource, SAXSource or StreamSource.

Your choice of payload should be compatible with other EIPs & components in your route!

All make use of Spring or OSGi Blueprint declarative configuration, and deploy easily into Fuse ESB.

Page 41: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 41 A Progress Software Company

Next Webinars

See http://fusesource.com/resources/video-archived-webinars/

April 7th 2011: • How to Secure CXF Web Services with SSL/TLS and WS-Security

See you then!

Page 42: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 42 A Progress Software Company

Useful references

FuseSource – http://fusesource.com• http://fusesource.com/products/enterprise-cxf/#documentation • http://fusesource.com/products/enterprise-servicemix/#documentation

Maven – http://maven.apache.org• http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html • http://maven.apache.org/plugins/maven-war-plugin/

Bnd - http://www.aqute.biz/Code/Bnd

CXF – http://cxf.apache.org• http://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html • http://camel.apache.org/cxf.html

Page 43: Implementing WebServices with Camel and CXF in ServiceMix

Copyright © 2011 Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved. 43 A Progress Software Company

Learn More at http://fusesource.com