5

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 */
    }
}
Kansuler
  • 1,539
  • 2
  • 11
  • 17
  • possible duplicate of [How can you pass data from a Filter to the endpoint in Jersey](http://stackoverflow.com/questions/5551839/how-can-you-pass-data-from-a-filter-to-the-endpoint-in-jersey) – Smutje Jun 22 '14 at 09:57
  • I have also seen that thread, but as a novice in Java I don't know how to implement it, if someone could provide a piece of code from my example above, I would be extremely grateful. – Kansuler Jun 22 '14 at 11:53
  • As this question is more specific about Header-Manipulation than the linked question I added an answer. – lefloh Jun 22 '14 at 13:11

1 Answers1

7

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) {

}
lefloh
  • 10,653
  • 3
  • 28
  • 50
  • Ah, this worked exactly as I wanted. It seem to be a very simple solution, I didn't know it could be fixed like that. Thank you! – Kansuler Jun 22 '14 at 13:16