Interceptors can be attached to endpoints programmatically using either one of two approaches:
the InterceptorProvider
API
Java annotations
Using the InterceptorProvider
API allows the developer to attach
interceptors to any of the FUSE Services Framework runtime components that have interceptor chains, but it requires working with the underlying
FUSE Services Framework classes. The Java annotations can only be added to service interfaces or service implementations, but they allow
developers to stay within the JAX-WS API or the JAX-RS API.
All of the FUSE Services Framework runtime components that can have interceptor chains implement the
InterceptorProvider
interface shown in Example 5.3, “The InterceptorProvider Interface”.
Example 5.3. The InterceptorProvider
Interface
package org.apache.cxf.interceptor; import java.util.List; public interface InterceptorProvider { List<Interceptor> getInInterceptors(); List<Interceptor> getOutInterceptors(); List<Interceptor> getInFaultInterceptors(); List>Interceptor> getOutFaultInterceptors(); }
The four methods in the interface allow you to retrieve each of an endpoint's interceptor chains as a Java
List
object. Using the methods offered by the Java List
object, developers can
add and remove interceptors to any of the chains.
To use the InterceptorProvider
API to attach an interceptor to a runtime component's
interceptor chain do the following:
Get access to the runtime component with the chain to which the interceptor is being attached.
Developers will need to use FUSE Services Framework specific APIs to access the runtime components from standard Java application code The runtime components are usually accessible by casting the JAX-WS or JAX-RS artifacts into the underlying FUSE Services Framework objects.
Create an instance of the interceptor.
Use the proper get method to retrieve the desired interceptor chain.
Use the List
object's add()
method to attach the interceptor to the
interceptor chain.
This step is usually combined with retrieving the interceptor chain.
Example 5.4, “Attaching an Interceptor to a Consumer Programmatically” shows code for attaching an interceptor to the inbound interceptor chain of a JAX-WS consumer.
Example 5.4. Attaching an Interceptor to a Consumer Programmatically
package com.fusesource.demo; import java.io.File; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import org.apache.cxf.endpoint.ClientProxy; import org.apache.cxf.endpoint.ClientProxy; public class Client { public static void main(String args[]) { QName serviceName = new QName("http://demo.eric.org", "stockQuoteReporter"); Service s = Service.create(serviceName);QName portName = new QName("http://demo.eric.org", "stockQuoteReporterPort"); s.addPort(portName, "http://schemas.xmlsoap.org/soap/", "http://localhost:9000/EricStockQuote");
quoteReporter proxy = s.getPort(portName, quoteReporter.class);
Client cxfClient = ClientProxy.getClient(proxy);
ValidateInterceptor validInterceptor = new ValidateInterceptor();
cxfClient.getInInterceptor().add(validInterceptor);
... } }
The code in Example 5.4, “Attaching an Interceptor to a Consumer Programmatically” does the following:
Creates a JAX-WS | |
Adds a port to the | |
Creates the proxy used to invoke methods on the service provider. | |
Gets the FUSE Services Framework | |
Creates an instance of the interceptor. | |
Attaches the interceptor to the inbound interceptor chain. |
Example 5.5, “Attaching an Interceptor to a Service Provider Programmatically” shows code for attaching an interceptor to a service provider's outbound interceptor chain.
Example 5.5. Attaching an Interceptor to a Service Provider Programmatically
package com.fusesource.demo; import java.util.*; import org.apache.cxf.endpoint.Server; import org.apache.cxf.frontend.ServerFactoryBean; import org.apache.cxf.frontend.EndpointImpl; public class stockQuoteReporter implements quoteReporter { ... public stockQuoteReporter() { ServerFactoryBean sfb = new ServerFactoryBean();Server server = sfb.create();
EndpointImpl endpt = server.getEndpoint();
AuthTokenInterceptor authInterceptor = new AuthTokenInterceptor();
endpt.getOutInterceptor().add(authInterceptor);
} }
The code in Example 5.5, “Attaching an Interceptor to a Service Provider Programmatically” does the following:
Creates a | |
Gets the | |
Gets the FUSE Services Framework | |
Creates an instance of the interceptor. | |
Attaches the interceptor to the endpoint;s outbound interceptor chain. |
Example 5.6, “Attaching an Interceptor to a Bus” shows code for attaching an interceptor to a bus' inbound interceptor chain.
The code in Example 5.6, “Attaching an Interceptor to a Bus” does the following:
Gets the default bus for the runtime instance. | |
Creates an instance of the interceptor. | |
Attaches the interceptor to the inbound interceptor chain. |
The WatchInterceptor
will be attached to the inbound interceptor chain of all endpoints created by
the runtime instance.
FUSE Services Framework provides four Java annotations that allow a developer to specify the interceptor chains used by an endpoint. Unlike the other means of attaching interceptors to endpoints, the annotations are attached to application-level artifacts. Which artifact determines the scope of the annotation's effect.
The annotations can be placed on the following artifacts:
the service endpoint interface(SEI) defining the endpoint
If the annotations are placed on an SEI all of the service providers that implement the interface and all of the consumers that use the SEI to create proxies will be effected.
a service implementation class
If the annotations are placed on an implementation class all of the service providers using the implementation class will be effected.
The annotations are all in the org.apache.cxf.interceptor package and are described in Table 5.2, “Interceptor Chain Annotations”.
Table 5.2. Interceptor Chain Annotations
The list of interceptors is specified as a list of fully qualified class names using the syntax shown in Example 5.7, “Syntax for Listing Interceptors in a Chain Annotation”.
Example 5.7. Syntax for Listing Interceptors in a Chain Annotation
interceptors={"interceptor1
", "interceptor2
", ..., "interceptorN
"}
Example 5.8, “Attaching Interceptors to a Service Implementation” shows annotations that attach two interceptors to the inbound
interceptor chain of endpoints that use the logic provided by SayHiImpl
.
Example 5.8. Attaching Interceptors to a Service Implementation
import org.apache.cxf.interceptor.InInterceptors;
@InInterceptors(interceptors={"com.sayhi.interceptors.FirstLast", "com.sayhi.interceptors.LogName"})
public class SayHiImpl implements SayHi
{
...
}