Class Filter
public abstract class Filter extends Object
HttpContext
instances. Each Filter
in the chain, invokes the next filter within its own doFilter(HttpExchange, Chain)
implementation. The final Filter
in the chain invokes the applications exchange handler.
- Since:
- 1.6
Nested Class Summary
Modifier and Type | Class | Description |
---|---|---|
static class |
Filter.Chain |
A chain of filters associated with a HttpServer . |
Constructor Summary
Modifier | Constructor | Description |
---|---|---|
protected |
Constructor for subclasses to call. |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
static Filter |
afterHandler |
Returns a post-processing Filter with the given description and operation. |
static Filter |
beforeHandler |
Returns a pre-processing Filter with the given description and operation. |
abstract String |
description() |
Returns a short description of this Filter . |
abstract void |
doFilter |
Asks this filter to pre/post-process the given exchange. |
Constructor Details
Filter
protected Filter()
Method Details
doFilter
public abstract void doFilter(HttpExchange exchange, Filter.Chain chain) throws IOException
- Examine or modify the request headers.
- Filter the request body or the response body, by creating suitable filter streams and calling
HttpExchange.setStreams(InputStream, OutputStream)
. - Set attribute objects in the exchange, which other filters or the exchange handler can access.
- Decide to either:
- Invoke the next filter in the chain, by calling
Filter.Chain.doFilter(HttpExchange)
. - Terminate the chain of invocation, by not calling
Filter.Chain.doFilter(HttpExchange)
.
- Invoke the next filter in the chain, by calling
- If option 1. above is taken, then when doFilter() returns all subsequent filters in the Chain have been called, and the response headers can be examined or modified.
- If option 2. above is taken, then this Filter must use the HttpExchange to send back an appropriate response.
- Parameters:
-
exchange
- theHttpExchange
to be filtered -
chain
- theChain
which allows the next filter to be invoked - Throws:
-
IOException
- may be thrown by any filter module, and if caught, must be rethrown again -
NullPointerException
- if either exchange or chain arenull
description
public abstract String description()
Filter
.- Returns:
- a
String
describing theFilter
beforeHandler
public static Filter beforeHandler(String description, Consumer<HttpExchange> operation)
Filter
with the given description and operation. The operation
is the effective implementation of the filter. It is executed for each HttpExchange
before invoking either the next filter in the chain or the exchange handler (if this is the final filter in the chain). Exceptions thrown by the operation
are not handled by the filter.
- API Note:
- A beforeHandler filter is typically used to examine or modify the exchange state before it is handled. The filter
operation
is executed beforeFilter.Chain.doFilter(HttpExchange)
is invoked, so before any subsequent filters in the chain and the exchange handler are executed. The filteroperation
is not expected to handle the request or send response headers, since this is commonly done by the exchange handler.Example of adding the
"Foo"
response header to all responses:var filter = Filter.beforeHandler("Add response header Foo", e -> e.getResponseHeaders().set("Foo", "Bar")); httpContext.getFilters().add(filter);
- Parameters:
-
description
- the string to be returned fromdescription()
-
operation
- the operation of the returned filter - Returns:
- a filter whose operation is invoked before the exchange is handled
- Throws:
-
NullPointerException
- if any argument is null - Since:
- 17
afterHandler
public static Filter afterHandler(String description, Consumer<HttpExchange> operation)
Filter
with the given description and operation. The operation
is the effective implementation of the filter. It is executed for each HttpExchange
after invoking either the next filter in the chain or the exchange handler (if this filter is the final filter in the chain). Exceptions thrown by the operation
are not handled by the filter.
- API Note:
- An afterHandler filter is typically used to examine the exchange state rather than modifying it. The filter
operation
is executed afterFilter.Chain.doFilter(HttpExchange)
is invoked, this means any subsequent filters in the chain and the exchange handler have been executed. The filteroperation
is not expected to handle the exchange or send the response headers. Doing so is likely to fail, since the exchange has commonly been handled before theoperation
is invoked. More specifically, the response may be sent before the filteroperation
is executed.Example of adding a filter that logs the response code of all exchanges:
var filter = Filter.afterHandler("Log response code", e -> log(e.getResponseCode()); httpContext.getFilters().add(filter);
Example of adding a sequence of afterHandler filters to a context:
The order in which the filter operations are invoked is reverse to the order in which the filters are added to the context's filter-list.var a1Set = Filter.afterHandler("Set a1", e -> e.setAttribute("a1", "some value")); var a1Get = Filter.afterHandler("Get a1", e -> doSomething(e.getAttribute("a1"))); httpContext.getFilters().addAll(List.of(a1Get, a1Set));
The operation of
a1Get
will be invoked after the operation ofa1Set
becausea1Get
was added beforea1Set
. - Parameters:
-
description
- the string to be returned fromdescription()
-
operation
- the operation of the returned filter - Returns:
- a filter whose operation is invoked after the exchange is handled
- Throws:
-
NullPointerException
- if any argument is null - Since:
- 17
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/jdk.httpserver/com/sun/net/httpserver/Filter.html