Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhirockzz/984123afa25973cafcff to your computer and use it in GitHub Desktop.
Save abhirockzz/984123afa25973cafcff to your computer and use it in GitHub Desktop.
Sharing contextual data b/w Request and Response filters
@Priority(Priorities.AUTHENTICATION)
public class ReqFilter_1 implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext cReqCtx) throws IOException {
cReqCtx.setProperty("random-token", "token-007"); //generated and used internally
}
}
public class ResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext cReqCtx, ContainerResponseContext cRespCtx) throws IOException {
String responseToken = (String) cReqCtx.getProperty("random-token"); //get the property
if(responseToken!=null){
cRespCtx.getHeaders.put("random-token-header" , responseToken); //set it to HTTP response header
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment