Class Filter

public abstract class Filter
extends Object

A filter used to pre- and post-process incoming requests. Pre-processing occurs before the application's exchange handler is invoked, and post-processing occurs after the exchange handler returns. Filters are organised in chains, and are associated with HttpContext instances.

Each Filter in the chain, invokes the next filter within its own doFilter() implementation. The final Filter in the chain invokes the applications exchange handler.

Since:
1.6

Nested Classes

Modifier and Type Class Description
static class  Filter.Chain

a chain of filters associated with a HttpServer.

Constructors

Modifier Constructor Description
protected Filter()

Methods

Modifier and Type Method Description
abstract String description()

returns a short description of this Filter

abstract void doFilter​(HttpExchange exchange, Filter.Chain chain)

Asks this filter to pre/post-process the given exchange.

Methods declared in class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

Filter

protected Filter()

Methods

doFilter

public abstract void doFilter(HttpExchange exchange,
                              Filter.Chain chain)
                       throws IOException

Asks this filter to pre/post-process the given exchange. The filter can:

  • 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
    1. invoke the next filter in the chain, by calling Filter.Chain.doFilter(HttpExchange)
    2. terminate the chain of invocation, by not calling Filter.Chain.doFilter(HttpExchange)
  • if option 1. above 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 taken, then this Filter must use the HttpExchange to send back an appropriate response

Parameters:
exchange - the HttpExchange to be filtered.
chain - the Chain 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 are null

description

public abstract String description()

returns a short description of this Filter

Returns:
a string describing the Filter

© 1993, 2020, 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/11/docs/api/jdk.httpserver/com/sun/net/httpserver/Filter.html