I have a strange issue here. I have a filter which has two filter methods one with ContainerRequestContext as param and other with both ContainerRequestContext and ContainerResponseContext as mentioned below. When there is a cache hit I am aborting the ContainerRequestContext with 200 http response. But in Cache hit scenario its generating a http status as 201 which we want to avoid. I tried to use responseContext.setStatus(Response.Status.OK.getStatusCode()); in the response filter still I am getting 201 as final response.Any help will be appreciated.
@Override
public void filter(ContainerRequestContext requestContext) {
cacheKey = cacheservice.makekey(requestContext);
packetData = cacheservice.getValue(cacheKey);
if (packetData != null) {
requestContext.abortWith(
Response.status(Response.Status.OK)
.entity(packetData)
.build()
);
}
}
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
packetData = cacheservice.getValue(cacheKey);
if (packetData != null) {
setResponseContextAttributes(String.valueOf(100),responseContext,”Cache hit”);
responseContext.setStatus(Response.Status.OK.getStatusCode());
} else {
cacheKey = cacheservice.makekey(requestContext);
packetData = (Object) responseContext.getEntity();
cacheservice.setValue(cacheKey, packetData, 100);
setResponseContextAttributes(String.valueOf(100),responseContext,”Cache miss”);
}
}
1