Methods & Tools Software Development Magazine

Software Development Magazine - Project Management, Programming, Software Testing

Scrum Expert - Articles, tools, videos, news and other resources on Agile, Scrum and Kanban

Apache CXF - Open Source Web Services for Java

Axel Irriger, iteratec GmbH, http://www.iteratec.de

Apache CXF is an open source framework for web service development for the Java programming language, which features a XML-free configuration and has a strong focus on embedding into existing applications. These services can use a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI. This article provides an overview of the Apache CXF framework and its basic use cases.

Web Site: https://cxf.apache.org
Version discussed: Apache CXF 2.2 and 2.3
License & Pricing: Open Source with commercial support and packaging by FuseSource
Support: User mailing list, developer mailing list, Internet Relay Chat, FuseSource forums.

Introduction

Before detailing the CXF framework, it is worth looking at basic concepts first. In the context of method invocation, two things are important: first, how you can invoke something interoperable in general and second, which steps you must follow, in particular.

General interoperability concerns

In order to access software functions or methods on a different system environment, some steps are fundamental. For these, it is not important, whether you are dealing with a remote machine or with an application written in a different programming language. The basic steps are:

  • Identify the function and the parameters to invoke
  • Map this to a portable format
  • Transport this to the target environment
  • Perform the function call and obtain any return values
  • Transport the result back, also using a portable format

Any framework, regardless of its architecture, must perform these steps. In this context, "interoperable" can mean two different things: first, it can mean a specific format for a chosen programming language. A typical example is Java remote method invocation (RMI). Second, it can mean that many different languages can understand the format. This last definition is commonly accepted. There exist various implementations for such interoperable communication. A very popular one is the Common Object Request Broker Architecture (CORBA).

The CXF framework implements a portable invocation of remote functions in the Java programming language. For this implementation, it follows the basic steps listed above. By default, it supports SOAP based web services and XML-based REST-services. Although it implements these two standards, it is not limited to these. You can extend it to support your own protocol, as well.

Web service frameworks for Java

As a Java developer, you have quite a lot of choices, when it comes to web services. Among the more popular frameworks, the typical short list consists of:

  • Apache Axis
  • Metro
  • Sun reference implementation

Each of these has various strengths and weaknesses. This article covers only attributes specific to Apache CXF. A detailed comparison is far beyond the scope of this article.

Integration with the Spring framework

Apache CXF uses the Spring framework internally. You can categorize the Spring framework as the Java developer's Swiss-army knife. It provides a feature-rich dependency injection container and comes along with an extended set of support libraries, greatly simplifying everyday implementations. CXF uses the Spring framework as one of its cornerstones to support implementation and extension by a customized instance of ApplicationContext for operation and configuration.

Integration with existing applications

To embed a software component into an existing application, the component must be self-reliant. This means, all necessary libraries and dependencies are included with that component. Nonetheless, there may be version conflicts with CXF libraries and the ones in the host application.

The ApplicationContext of Apache CXF encapsulates all operations. With that, there is a clearly defined bean for you to integrate and configure. You need not refactor your existing code base, in order to use Apache CXF. Even though it does rely on third party libraries, for issues like data marshalling, it does not need any interaction with other components, like a servlet container. In addition, it supports various deployment models, with each being configurable using its API.

If you want to deploy CXF standalone (or the application it is coupled with does not provide a servlet context), its embedded web server (Jetty) can be used. This is already part of the distribution.

Extensibility

To extend any given framework, well-defined extension points ("hooks") are mandatory. In Apache CXF, a central "bus" infrastructure connects all core components. Components can be added to this bus on-demand, in order to replace existing components or to add new components to the framework. Using this approach, adding support for new protocols or marshalling implementations is easy.

Besides components, CXF implements its functionality in a layer-based architecture with different phases, where each phase performs a clearly defined processing step. Various objects, implementing the interceptor pattern, apply these. Each phase iterates over a list of configured interceptors for the phase at hand.

For example, there is a marshalling phase, before any actual request is sent over the network. Within this phase, interceptors handle validation, mapping and various other steps necessary to convert the message to a portable representation.

You can easily extend each phase using custom interceptors to implement special functionality or override existing ones.

Generic "Service" approach

If you take a look at the web service implementation stack so far, you might describe it as "over-engineered". However, at its core, Apache CXF wants to able to cover technology and protocols. For the moment, it implements technologies, which are popular today, but it must also cover ones of the future. To be able to deliver this value, a more general approach is mandatory. Thus, the developers describe the framework itself as a "services" framework. With this definition, you have all liberty you need.

Features

To decide, whether Apache CXF is well suited for your environment, the implemented standards and protocols are a key factor. The following sections outline its support to date.

Protocols and Libraries

The layer-based approach of its internal operation reflects in the layers of supported protocol and libraries. The following table lists the supported technologies in the various stacks:

Category

Technologies and libraries

Transport

HTTP, Servlet, JMS, in-VM, Camel

Protocol bindings

SOAP, REST/HTTP, XML

Data bindings

JAXB 2.x, Aegis, XMLBeans, Service Data Objects, JiBX (under development)

Formats

XML, textual representation, JSON, FastInfoset

In order to actually use CXF, you do not need in-depth knowledge of the libraries it uses internally. Only the "front-end" API is necessary. For those, you have two options.

REST-based services

Services, defined as "REST-based", are implemented with the Internet protocol standards in mind, such as HTTP and XML. These services implement basic CRUD (create, read, update, delete) operations using the available HTTP operations PUT, GET, POST. The application encodes the object to operate on and the method to invoke in the URL.

This design strategy declares that every object is uniquely identifiable using an URL. Using traditional GET, POST and PUT operations, you invoke status changes on these objects.

Apache CXF supports REST-based services with the Java API for XML-based REST services (JAX-RS) standard. Using Java annotations, you enable it purely on the service interface. These annotations define the mapping between the URL and the service and its parameters. If you need to change something, you only alter the service interface. The underlying implementation, typically a Java bean, remains untouched.

SOAP web services

The first mainstream implementation of web services was done using XML and the Simple Object Access Protocol (SOAP). This "traditional" web service stack consists of either HTTP or JMS as the transport protocol and exchanges XML data on top.

The SOAP protocol also defines the XML document layout. With clearly identifiable XML tags is declares the service, the method on that service and its parameters. In order to model and validate more complex data structures, the SOAP protocol relies on XML schema, which covers the data modeling part.

To develop a SOAP compatible web service with Apache CXF, you annotate on the service interface, too. The Java API for XML Web Services (JAX-WS) standard declares all valid annotations.

Web service standard support

For SOAP web service development, it supports the following standards, to date:

Category

Standards

Basic support

WS-I Basic Profile 1.1

Quality of Service

WS-Reliable Messaging

Metadata

WS-Policy, WSDL-1.1

Communication security

WS-Security, WS-SecurityPolicy, WS-SecureConversation, WS-Trust (partial support)

Messaging support

WS-Adressing, SOAP 1.1, SOAP 1.2, Message Transmission Optimization Mechanism (MTOM)

Simple deployment model

Although most service development project decide on the programming model early on, you may want to experiment at first. For such "quick and dirty" solutions, you just want to get some logic into your beans and get started. Apache CXF delivers this, too.

The simple deployment model focuses on Java reflection to construct a service from a pure Java interface, without the need for annotations. Although you should discourage this for production use (due to reasons such as parameter naming), it is compelling for rapid construction and prototyping.

In order to create a service, only these steps are necessary:

1. Create a Java interface, reflecting the service:

public interface HelloWorld {
String sayHi(String text);
}

2. Expose the interface using Apache CXF:

ServerFactoryBean svrFactory = new ServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress("http://localhost:9000/Hello");
svrFactory.setServiceBean(helloWorldImpl);
svrFactory.create();

With these basic steps, the "Hello World" service is accessible under the URL http://localhost:9000/Hello.

Summary

This article presented Apache CXF, a simple to use framework for (web) service development. Along with its main focus of easy embedding, some its architectural features have been presented.

The given examples and descriptions should help you to use CXF to develop services using the Java language and expose them either as SOAP-based web or REST-based services. In addition, the simple deployment method using Java reflection was mentioned to support easy prototyping.

If you are focusing on implementing business functionality and need a painless way of exposing this, CXF definitely is a good choice. Since it relies mostly on Java annotations and separates service definition from implementation in a clean way, development and integration is greatly simplified.

You should also consider CXF if it is not clear yet, whether you will need SOAP or REST services. As it supports both styles of operation, you can provide additional channels of access afterwards without affecting business logic.


Click here to view the complete list of tools reviews

This article was originally published in the Spring 2011 issue of Methods & Tools

Methods & Tools
is supported by


Testmatick.com

Software Testing
Magazine


The Scrum Expert