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.
Modifier and Type | Class | Description |
---|---|---|
static class |
Filter.Chain |
A chain of filters associated with a HttpServer . |
Modifier | Constructor | Description |
---|---|---|
protected |
Constructor for subclasses to call. |
Modifier and Type | Method | Description |
---|---|---|
static Filter |
adaptRequest |
Returns a pre-processing Filter that inspects and possibly adapts the request state. |
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. |
protected Filter()
public abstract void doFilter(HttpExchange exchange, Filter.Chain chain) throws IOException
HttpExchange.setStreams(InputStream, OutputStream)
. Filter.Chain.doFilter(HttpExchange)
. Filter.Chain.doFilter(HttpExchange)
. exchange
- the HttpExchange
to be filteredchain
- the Chain
which allows the next filter to be invokedIOException
- may be thrown by any filter module, and if caught, must be rethrown againNullPointerException
- if either exchange or chain are null
public abstract String description()
Filter
.String
describing the Filter
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.
operation
is executed before Filter.Chain.doFilter(HttpExchange)
is invoked, so before any subsequent filters in the chain and the exchange handler are executed. The filter operation
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);
description
- the string to be returned from description()
operation
- the operation of the returned filterNullPointerException
- if any argument is nullpublic 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.
operation
is executed after Filter.Chain.doFilter(HttpExchange)
is invoked, this means any subsequent filters in the chain and the exchange handler have been executed. The filter operation
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 the operation
is invoked. More specifically, the response may be sent before the filter operation
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 of a1Set
because a1Get
was added before a1Set
.
description
- the string to be returned from description()
operation
- the operation of the returned filterNullPointerException
- if any argument is nullpublic static Filter adaptRequest(String description, UnaryOperator<Request> requestOperator)
Request
returned by the requestOperator
will be the effective request state of the exchange. 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 requestOperator
are not handled by the filter.requestOperator
with the given exchange, ex
, in order to retrieve the adapted request state. It then invokes the next filter in the chain or the exchange handler, passing an exchange equivalent to ex
with the adapted request state set as the effective request state. Example of adding the "Foo"
request header to all requests:
var filter = Filter.adaptRequest("Add Foo header", r -> r.with("Foo", List.of("Bar")));
httpContext.getFilters().add(filter);
description
- the string to be returned from description()
requestOperator
- the request operatorNullPointerException
- if any argument is null
© 1993, 2023, 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/21/docs/api/jdk.httpserver/com/sun/net/httpserver/Filter.html