Provides utility classes for working with servlets. Below are some examples; for a full list, see the API.
Package com.github.robtimus.servlet.parameters contains several classes that make it easy to read init parameters. For instance, to read an int init parameter that cannot be negative:
requestLimit = IntParameter.of(config, "requestLimit")
.atLeast(0)
.valueWithDefault(Integer.MAX_VALUE);
Sometimes it's necessary to transform the input and/or output. Class ServletUtils contains methods to transform a ServletInputStream
, ServletOutputStream
, BufferedReader
or PrintWriter
, as retrieved from a ServletRequest
or ServletResponse
.
In addition, classes InputTransformingHttpServletRequestWrapper and OutputTransformingHttpServletResponseWrapper make it easy to wrap an HttpServletRequest
or HttpServletResponse
with transforming input or output. Simply override the necessary transform
method to provide the actual transformation.
Class CookieUtils contains methods to read cookies as optionals and streams.
Method AsyncUtils.doFilter can be used as a replacement for a try-finally block that also works with asynchronous request handling.
The following only works for non-asynchronous request handling; for asynchronous request handling the code in the finally
block is often executed too early.
try {
chain.doFilter(request, response);
} finally {
doSomething(request, response);
}
The following works both with asynchronous and non-asynchronous request handling.
AsyncUtils.doFilter(request, response, chain, (req, res) -> {
doSomething(req, res);
});
Class BodyCapturingFilter can be used as base class for a filter that captures the request and response bodies. It captures the request body while it body is read, and provides callback methods to perform the necessary logic when the request body is read or when the response body is written. See its documentation for more details.