Class MiddlewareChain<I,O>

java.lang.Object
com.google.genkit.core.middleware.MiddlewareChain<I,O>
Type Parameters:
I - The input type
O - The output type

public class MiddlewareChain<I,O> extends Object
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 Details

    • MiddlewareChain

      public MiddlewareChain()
      Creates a new MiddlewareChain.
    • MiddlewareChain

      public MiddlewareChain(List<Middleware<I,O>> middlewareList)
      Creates a new MiddlewareChain with the given middleware.
      Parameters:
      middlewareList - the initial list of middleware
  • Method Details

    • copy

      public MiddlewareChain<I,O> copy()
      Creates a copy of this MiddlewareChain.
      Returns:
      a new MiddlewareChain with the same middleware
    • use

      public MiddlewareChain<I,O> use(Middleware<I,O> middleware)
      Adds a middleware to the chain.
      Parameters:
      middleware - the middleware to add
      Returns:
      this chain for fluent chaining
    • useAll

      public MiddlewareChain<I,O> useAll(List<Middleware<I,O>> middlewareList)
      Adds multiple middleware to the chain.
      Parameters:
      middlewareList - the middleware to add
      Returns:
      this chain for fluent chaining
    • useFirst

      public MiddlewareChain<I,O> useFirst(Middleware<I,O> middleware)
      Inserts a middleware at the beginning of the chain.
      Parameters:
      middleware - the middleware to insert
      Returns:
      this chain for fluent chaining
    • getMiddlewareList

      public List<Middleware<I,O>> 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,O> finalAction) throws GenkitException
      Executes the middleware chain with the given request, context, and final action.
      Parameters:
      request - the input request
      context - the action context
      finalAction - the final action to execute after all middleware
      Returns:
      the output response
      Throws:
      GenkitException - if execution fails
    • of

      @SafeVarargs public static <I, O> MiddlewareChain<I,O> of(Middleware<I,O>... middleware)
      Creates a new MiddlewareChain with the specified middleware.
      Type Parameters:
      I - input type
      O - output type
      Parameters:
      middleware - the middleware to include
      Returns:
      a new MiddlewareChain