@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { @SuppressWarnings("unchecked") ResponseObjectAwareMessageBodyWriter<Object> writer = (ResponseObjectAwareMessageBodyWriter<Object>) getMessageWriter(responseContext.getEntityClass()); if (writer != null) { Object entity = responseContext.getEntity(); Class<?> entityClass = responseContext.getEntityClass(); int status = writer.getStatusCode(entity, entityClass); responseContext.setStatus(status); if (status == Status.NO_CONTENT.getStatusCode()) { writer.writeTo(entity, entityClass, null, null, responseContext.getMediaType(), responseContext.getHeaders(), responseContext.getEntityStream()); } } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { if(responseContext.getEntityClass() == Template.class) { SuspendableContainerResponseContext ctx = (SuspendableContainerResponseContext) responseContext; ctx.suspend(); Template template = (Template) responseContext.getEntity(); try { template.render(requestContext.getRequest()) .subscribe(resp -> { // make sure we avoid setting a null media type because that causes // an NPE further down if(resp.getMediaType() != null) ctx.setEntity(resp.getEntity(), null, resp.getMediaType()); else ctx.setEntity(resp.getEntity()); ctx.setStatus(resp.getStatus()); ctx.resume(); }, err -> { ctx.resume(err); }); }catch(Throwable t) { ctx.resume(t); } } } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { if(responseContext.getEntityClass() == Template.class) { SuspendableContainerResponseContext ctx = (SuspendableContainerResponseContext) responseContext; ctx.suspend(); Template template = (Template) responseContext.getEntity(); try { template.render(requestContext.getRequest()) .subscribe(resp -> { // make sure we avoid setting a null media type because that causes // an NPE further down if(resp.getMediaType() != null) ctx.setEntity(resp.getEntity(), null, resp.getMediaType()); else ctx.setEntity(resp.getEntity()); ctx.setStatus(resp.getStatus()); ctx.resume(); }, err -> { ctx.resume(err); }); }catch(Throwable t) { ctx.resume(t); } } } }
types = workersProvider.get() .getMessageBodyWriterMediaTypes( responseContext.getEntityClass(), responseContext.getEntityType(), responseContext.getEntityAnnotations()); types = workersProvider.get() .getMessageBodyReaderMediaTypes( responseContext.getEntityClass(), responseContext.getEntityType(), responseContext.getEntityAnnotations());
public class MyContainerResponseFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException { // You can get the body of the response from the ContainerResponseContext Object entity = containerResponseContext.getEntity(); // You'll need to know what kind of Object the entity is in order to do something useful though // You can get some data using these functions Class<?> entityClass = containerResponseContext.getEntityClass(); Type entityType = containerResponseContext.getEntityType(); // And/or by looking at the ContainerRequestContext and knowing what the response entity will be String method = containerRequestContext.getMethod(); UriInfo uriInfo = containerRequestContext.getUriInfo(); // Then you can modify your Authorization header in some way String authorizationHeaderValue = containerResponseContext.getHeaderString(HttpHeaders.AUTHORIZATION); authorizationHeaderValue = authorizationHeaderValue + " a signature you calculated"; containerResponseContext.getHeaders().putSingle(HttpHeaders.AUTHORIZATION, authorizationHeaderValue); } }