I am trying to apply the Before Aspect on all the JAX-RS call made in my project. I will capture All the Rest Services. When I Apply pointcut on the package level then it works fine but idea is to capture everything @Path is being referred in project from the security point of view.
AOP pass for pointcut
@Before("execution(* com.dnegi.service..(..))")
I have set up Spring MVC + Apache CXF + Tomcat.
Service to Aspect:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.stereotype.Component;
import com.dnegi.exception.AnimalNotFoundException;
import com.dnegi.model.CookingInstruction;
/**
* JAX-RS service interface.
*
* Defines our service methods annotated with the JAX-RS mappings
*
* Endpoints all sit under the root Path of /cookingtime
*
*/
@Component
@Path("/cookingtime")
public interface CookingTimeService {
/**
* Get cooking instructions for the specified type of meat and weight passed
* in as URL parameters, e.g. cookingtime/turkey/1.24
*
* @param fleshType type of meat to cook
* @param fleshWeight weight of meat to cook
*
* @return instructions containing the oven temperature and cooking time
*
* @throws AnimalNotFoundException if the type of meat is one we don't know how to cook
*/
@GET
@Produces({MediaType.APPLICATION_JSON})
@Path("/{fleshType}/{fleshWeight}")
@CustomAnnotation
public CookingInstruction getCookingTime(@PathParam("fleshType") String fleshType,
@PathParam("fleshWeight") Float fleshWeight)
throws AnimalNotFoundException;
}
Aspect to Apply
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class ReadOnlyUserAspect {
@Before("@annotation(javax.ws.rs.Path)")
public void logBefore(JoinPoint joinPoint) {
System.out.println("logBefore() is running!");
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("******");
}
}
It is not being applied on the Interface but if i define the @Path in the class then annotations work fine.
Working One:
import javax.management.Notification;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.springframework.stereotype.Component;
@Component
@Path("/notifications")
public class NotificationsResource {
@GET
@Path("/ping")
public Response ping() {
return Response.ok().entity("Service online").build();
}
@POST
@Path("/post/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response postNotification(Notification notification) {
return Response.status(201).entity(notification).build();
}
}
It seems like annotation are not inherited and because of which pointcut on annotation is not working. I created custom annotation then it worked when i used Inheritable Annotation on it. But I am still facing problem of how to make @Path [JAX-RS] Annotation work for my project.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface CustomAnnotation {
}