---
title: "Middleware"
url: https://docs.zeroc.com/ice/3.8/runtime/dispatch/middleware
language: java
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Middleware

Programming language: Java. Other languages: [C++](https://docs.zeroc.com/ice/3.8/runtime/dispatch/middleware/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/runtime/dispatch/middleware/csharp.md), [JavaScript](https://docs.zeroc.com/ice/3.8/runtime/dispatch/middleware/js.md), [Swift](https://docs.zeroc.com/ice/3.8/runtime/dispatch/middleware/swift.md).

A *middleware* is a [dispatcher](https://docs.zeroc.com/ice/3.8/runtime/dispatch/dispatcher-api/java.md) that intercepts an incoming request before it reaches the servant, and the outgoing response before the object adapter sends it back. A middleware holds the next dispatcher of the [dispatch pipeline](https://docs.zeroc.com/ice/3.8/runtime/dispatch/dispatch-pipeline.md): its dispatch method can run code before and after calling this next dispatcher, or return a response itself, such as an error, without calling it.

## Installing a Middleware

[`use`](https://code.zeroc.com/ice/3.8/api/java/com.zeroc.ice/com/zeroc/Ice/ObjectAdapter.html#use(java.util.function.Function)) installs a middleware on an object adapter. It takes a factory that creates the middleware from the next dispatcher, and the object adapter calls this factory when it dispatches its first request. Install all the middleware of an object adapter before this first dispatch. They intercept requests in the order you install them.

For example, the following middleware takes the next dispatcher and a token, and the call to `use` installs it:

```java
class AuthorizationMiddleware implements com.zeroc.Ice.Object
{
    public CompletionStage<OutgoingResponse> dispatch(
        IncomingRequest request) throws UserException {
        ...
    }

    AuthorizationMiddleware(com.zeroc.Ice.Object next, String validToken) {
        ...
    }
}

adapter.use(next -> new AuthorizationMiddleware(next, "iced tea"));
```

## Built-in Middleware

Ice installs a logger middleware at the beginning of the dispatch pipeline, unless both `Ice.Warn.Dispatch` and `Ice.Trace.Dispatch` are set to `0`.

Ice also installs an observer middleware, after the logger middleware, when the [Metrics admin facet](https://docs.zeroc.com/ice/3.8/administration/administrative-facility/metrics-facet/java.md) is enabled.
