0

I am using using jersey as my rest client. I want to write a custom annotation which will intercept the request so that i can validate the user and throw error accordingly. So i can annotate only those functions which i want to validate. there two problems around it.

  1. as far as i have seen the annotations they all inject property before the actual call. how can i control while this function has been called. I need to access the @Context HttpServletRequest request part to get the userId and make a DB call to validate..

  2. Classes which are going to be annotated my annotation parser need to know before hand. how can i make this independent of that. like spring annotations.

    @MYCustomFIlter
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{programId}/processCount")
    public Response getProgramInitializedCount(@PathParam("programId") String programId,
         @Context HttpServletRequest request)  {
     //this line will be moved to annotation filter.
     String userId = WfManagerUtil.getCurrentUser(request);
    

Any ideas around this how can this be achieved.

FIRST EDIT : with some research i found out how to do it. But this is not happening. Can somebody help me in finding out why.

Annotation :

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidateUser {

}

filter :

 public class ValidateUserFilter implements ContainerRequestFilter {


     @Override
     public ContainerRequest filter(ContainerRequest request) throws WebApplicationException{

         String user = request.getHeaderValue(USER_HEADER);
         //rest of code.

     }
 }

Binding :

@Provider
public class ResourceFilterBindingFeature implements DynamicFeature {

  @Override
  public void configure(ResourceInfo resourceInfo, FeatureContext context) {
    if (resourceInfo.getResourceMethod().isAnnotationPresent(ValidateUser.class)) {
      context.register(CommonResourceFilter.class);
    }

  }
}

and using it as :

@Path("/v1.0/programs")
public class ProgramsResource {


     @ValidateUser
     @GET
     @Produces(MediaType.APPLICATION_JSON)
     @Path("/{programId}/initialization/progress/")
     public Response getProgramInitializedCount(@PathParam("programId") String programId,
             @Context HttpServletRequest request) throws WorkflowManagerException {
         //code
     }

}

POM :

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0.1</version>
</dependency>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-core</artifactId>
    <version>1.8</version>
    <!-- <scope>provided</scope> -->
</dependency>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.8</version>
    <!-- <scope>provided</scope> -->
</dependency> 

Where possibly i am doing wrong. the filter class is not being invoked.

user2696466
  • 650
  • 1
  • 14
  • 33
  • Which Jersey version are you using? – Paul Samsotha Apr 01 '15 at 12:44
  • You do realize that Jersey 1.x uses JAX-RS 1. `javax.ws.rs-api` is the JAX-RS 2 API jar, and is not supported by the Jersey 1.x runtime. If you are going to use Jersey 1, then get rid of that. Jersey 1 will use the jsr311 jar, which is JAX-RS 1 API. You are currently trying to use classes from JAX-RS 2 that are not supported by this version of Jersey. If you want to upgrade to Jersey 2.x, then you can do this. – Paul Samsotha Apr 01 '15 at 14:22
  • Are you stuck with using this version of Jersey? – Paul Samsotha Apr 01 '15 at 14:43
  • which jersey version should i change to. – user2696466 Apr 01 '15 at 15:18
  • in maven repo i can find upto 1.19 – user2696466 Apr 01 '15 at 15:22
  • If you're able to use Jersey 2.x, I would do so. [This dependency](http://stackoverflow.com/a/27894395/2587435) is the only one you need to get started. Use the latest 2.17. Use [this one (the first)](https://jersey.java.net/documentation/latest/media.html#json.jackson) for JSON support. – Paul Samsotha Apr 01 '15 at 15:26
  • in maven repo i could find only up to 1.19 @peeskillet – user2696466 Apr 01 '15 at 15:26
  • Remember to take out all the Jersey 1 stuff if you decide to use Jersey 2 – Paul Samsotha Apr 01 '15 at 15:27
  • Configuration is a little different from Jersey 1 to Jersey 2. I would read [this chapter](https://jersey.java.net/documentation/latest/deployment.html) to get a good understanding of the different deployment options/configurations. There are a few options. For a quick start, I'd go with [this option](https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet.3) – Paul Samsotha Apr 01 '15 at 15:30

0 Answers0