PAX CDI + Interceptor

CDI Provides many features like
  • Beans
  • Interceptor
  • Decorator
  • Portable Exception
  • Bean Proxy
Interceptor

    Interceptors allow you to add functionality to the business methods of a bean without modifying the bean’s method directly. The interceptor is executed before any of the business methods of the bean. Interceptors are defined as part of the Enterprise JavaBeans specification, which can be found at JSR 318

Define the Binding


package com.rest.interceptor;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;


import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.interceptor.InterceptorBinding;

@InterceptorBinding
@Target({ TYPE, METHOD })
@Retention(RUNTIME)
public @interface Log {
}

Mark the interceptor implementation

package com.rest.interceptor;

import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;


import com.osgi.api.HelloService;
import com.security.config.SecurityConfig;
import com.security.token.util.PropertiesUtil;

@Interceptor
@Log 
public class LoggingInterceptor {

@AroundInvoke
public Object logMethodEntry(InvocationContext ctx) throws Exception {

System.out.println("Entering method: " + ctx.getMethod().getName());
Response result = (Response) ctx.proceed();

return result;

}
}

Use the interceptor in your business code


package com.rest.service;


import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.core.Response;

import org.ops4j.pax.cdi.api.OsgiService;

import com.osgi.api.HelloService;
import com.rest.api.PersonService;
import com.rest.interceptor.Log;

@ApplicationScoped
@Named
public class PersonServiceImpl implements PersonService {

@Inject
@OsgiService
HelloService helloService;
@Override
@Log
public Response getAll() {

Response response = Response.ok("<recipe2>"+helloService.sayHello("Athish")+"</recipe2>").status(200).build();
return response;
}
}

Enable the interceptor in your deployment, by adding it to META-INF/beans.xml orWEB-INF/beans.xml

<?xml version="1.0"?>

<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_1.xsd" version="1.1">
<scan><exclude name="org.apache.cxf.**" /></scan>
<interceptors>
<class>com.rest.interceptor.LoggingInterceptor</class>
</interceptors>
</beans>

Thus the Interceptor can be used to intercept the method and can be used to do some important function like
  • Security
  • Logging
  • Wrapping Method Request and Responses

The Binding will Help us to only look up for those methods which is annotated around the methods

Comments

Popular posts from this blog

Apache Karaf + Pax CDI OSGI Service

Apache CXF CDI in Karaf (Using CXF and CDI 1.1 (JSR-346))

OSGI + PAX CDI + Drools + Karaf