Class MiddlewareChain<I,O>
java.lang.Object
com.google.genkit.core.middleware.MiddlewareChain<I,O>
- Type Parameters:
I- The input typeO- The output type
MiddlewareChain manages a list of middleware and provides execution of the
complete chain. It implements the chain of responsibility pattern where each
middleware can process or modify the request/response.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new MiddlewareChain.MiddlewareChain(List<Middleware<I, O>> middlewareList) Creates a new MiddlewareChain with the given middleware. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all middleware from the chain.copy()Creates a copy of this MiddlewareChain.execute(I request, ActionContext context, BiFunction<ActionContext, I, O> finalAction) Executes the middleware chain with the given request, context, and final action.List<Middleware<I, O>> Returns an unmodifiable view of the middleware list.booleanisEmpty()Checks if the chain is empty.static <I,O> MiddlewareChain <I, O> of(Middleware<I, O>... middleware) Creates a new MiddlewareChain with the specified middleware.intsize()Returns the number of middleware in the chain.use(Middleware<I, O> middleware) Adds a middleware to the chain.useAll(List<Middleware<I, O>> middlewareList) Adds multiple middleware to the chain.useFirst(Middleware<I, O> middleware) Inserts a middleware at the beginning of the chain.
-
Constructor Details
-
MiddlewareChain
public MiddlewareChain()Creates a new MiddlewareChain. -
MiddlewareChain
Creates a new MiddlewareChain with the given middleware.- Parameters:
middlewareList- the initial list of middleware
-
-
Method Details
-
copy
Creates a copy of this MiddlewareChain.- Returns:
- a new MiddlewareChain with the same middleware
-
use
Adds a middleware to the chain.- Parameters:
middleware- the middleware to add- Returns:
- this chain for fluent chaining
-
useAll
Adds multiple middleware to the chain.- Parameters:
middlewareList- the middleware to add- Returns:
- this chain for fluent chaining
-
useFirst
Inserts a middleware at the beginning of the chain.- Parameters:
middleware- the middleware to insert- Returns:
- this chain for fluent chaining
-
getMiddlewareList
Returns an unmodifiable view of the middleware list.- Returns:
- the middleware list
-
size
public int size()Returns the number of middleware in the chain.- Returns:
- the middleware count
-
isEmpty
public boolean isEmpty()Checks if the chain is empty.- Returns:
- true if no middleware is registered
-
clear
public void clear()Clears all middleware from the chain. -
execute
public O execute(I request, ActionContext context, BiFunction<ActionContext, I, throws GenkitExceptionO> finalAction) Executes the middleware chain with the given request, context, and final action.- Parameters:
request- the input requestcontext- the action contextfinalAction- the final action to execute after all middleware- Returns:
- the output response
- Throws:
GenkitException- if execution fails
-
of
Creates a new MiddlewareChain with the specified middleware.- Type Parameters:
I- input typeO- output type- Parameters:
middleware- the middleware to include- Returns:
- a new MiddlewareChain
-