Let me start with Filters:
FILTERS:
Filters can modify inbound and outbound requests and responses including modification of headers, entity and other request/response parameters.
Further Filters are classified as Container/Server Filters and Client Filters.
Consider Server filters: There are further two obvious types:
- request filter
- response filter
There two main differences between these two types(depending on what they access):
- Response filters implement ContainerResponseFilter interface and has two arguments:
container request(ContainerRequestContext requestContext)
and
container response(ContainerResponseContext responseContext)
.
Now what are the things these two arguments access:
public interface ContainerRequestContext
Container request filter context. A mutable class that provides request-specific information for the filter, such as request URI, message headers, message entity or request-scoped properties. The exposed setters allow modification of the exposed request-specific information.
public interface ContainerResponseContext
Container response filter context. A mutable class that provides response-specific information for the filter, such as message headers, message entity or request-scoped properties. The exposed setters allow modification of the exposed response-specific information.
Notice that requestContext can access one additional thing: requestURI
- Request filters implements ContainerRequestFilter and has only one argument:
container request(ContainerRequestContext requestContext)
Coming to your question:
Which stream is referred in the method setEntityStream(arg0) and getEntityStream()?
This can be broken down as:
- getEntityStream():
Request entity stream on the server side is where an entity is read from the client request and response entity stream on the client side
is where an entity is read from the server response.
- setEntityStream(arg0): On the server means when writing out a response entity and on the client side writing request entity for a
request to be sent out to the server.**
EDIT:
- Filters can be used to verify certain criteria for any request. If the criteria is not met, filters can also abort the response.
Consider the following example from jersey documentation:
public class AuthorizationRequestFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext)
throws IOException {
final SecurityContext securityContext =
requestContext.getSecurityContext();
if (securityContext == null ||
!securityContext.isUserInRole("privileged")) {
requestContext.abortWith(Response
.status(Response.Status.UNAUTHORIZED)
.entity("User cannot access the resource.")
.build());
}
}
}
The AuthorizationRequestFilter in the example checks whether the authenticated user is in the privileged role. When the filter method is finished the response passed as a parameter to the abortWith method is used to respond to the request. Response filters, if any are registered, will be executed and will have possibility to process the aborted response.
Filters can influence which method will be matched. Pre-matching filters are request filters that are executed before the request matching is started.
@PreMatching
public class PreMatchingFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext)
throws IOException {
// change all PUT methods to POST
if (requestContext.getMethod().equals("PUT")) {
requestContext.setMethod("POST");
}
}
}
The PreMatchingFilter is a simple pre-matching filter which changes all PUT HTTP methods to POST. This might be useful when you want to always handle these PUT and POST HTTP methods with the same Java code.
INTERCEPTORS:
Write-Interceptors wraps the entity into GZIPOutput stream and Reader-Interceptor wraps the entity into GZIPInput stream which decompresses the data from the compressed entity received.
IN ADDITION, Interceptors can also be used to provide Digital Signatures. For Digital Signatures, a hash of the body needs to be calculated and added to the Signature request (client-side) or response (server-side) header.