Middleware
2 min read
2 min read
2 min read
2 min read
2 min read
A middleware is a dispatcher 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: 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
useuseuseuseuseuse 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:
class AuthorizationMiddleware final : public Ice::Object{public: AuthorizationMiddleware(Ice::ObjectPtr next, std::string validToken); void dispatch( Ice::IncomingRequest& request, std::function<void(Ice::OutgoingResponse)> sendResponse) final;};
adapter->use( [](Ice::ObjectPtr next) { return make_shared<AuthorizationMiddleware>(std::move(next), "iced tea"); });internal class AuthorizationMiddleware : Ice.Object{ public ValueTask<Ice.OutgoingResponse> dispatchAsync( Ice.IncomingRequest request) { ... }
internal AuthorizationMiddleware(Ice.Object next, string validToken) { ... }}
adapter.use(next => new AuthorizationMiddleware(next, validToken: "iced tea"));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"));class AuthorizationMiddleware extends Ice.Object{ async dispatch(request) { // ... }
constructor(next, validToken) { // ... }}
adapter.use(next => new AuthorizationMiddleware(next, "iced tea"));class AuthorizationMiddleware extends Ice.Object{ override async dispatch( request: Ice.IncomingRequest): Promise<Ice.OutgoingResponse> { // ... }
constructor(next: Ice.Object, validToken: string) { // ... }}
adapter.use((next: Ice.Object) => new AuthorizationMiddleware(next, "iced tea"));final class AuthorizationMiddleware: Dispatcher { func dispatch( _ request: sending IncomingRequest) async throws -> OutgoingResponse { ... }
init(next: Dispatcher, validToken: String) { ... }}
adapter.use { next in AuthorizationMiddleware(next: next, validToken: "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 is enabled.