Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey: Pass value from ContainerRequestFilter to endpoint

I am using Jersey 2.9 and I have created a filter which will take an encrypted header value, and decipher it and then pass it along to the endpoint which was called on. I have no idea of how to do this, and I have been searching on the internet but not really found a concrete example of what I want to do. The filter is called, I just have issues passing a value from it to the endpoint.

Could you guys help me!

Here is some sample code:

public class MyFilter implements ContainerRequestFilter
{

    @Override
    public void filter(ContainerRequestContext requestContext) throws WebApplicationException {

        String EncryptedString = requestContext.getHeaderString("Authentication");

        /* Doing some methods to remove encryption */

        /* Get a string which I want to pass to the endpoint which was called on, in this example: localhost:4883/rest/test */
    }
}

@Path("rest")
public class restTest
{

    @Path("test")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String Testing(){

        /* Process the value from the MyFilter */
    }
}
like image 246
Kansuler Avatar asked Jun 22 '14 09:06

Kansuler


1 Answers

You can easily modify the header or add another one:

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    requestContext.getHeaders().add("X-Authentication-decrypted", decryptedValue);
}

This value can be injected in your resource-method:

@GET
@Produces(MediaType.APPLICATION_JSON)
public String Testing(@HeaderParam("X-Authentication-decrypted") String auth) {

}
like image 183
lefloh Avatar answered Nov 14 '22 23:11

lefloh