Class StaticHandler<A extends ExchangeContext>

java.lang.Object
io.inverno.mod.web.server.StaticHandler<A>
Type Parameters:
A - the exchange context type
All Implemented Interfaces:
ExchangeHandler<A,WebExchange<A>>, ReactiveExchangeHandler<A,WebExchange<A>>

public class StaticHandler<A extends ExchangeContext> extends Object implements ExchangeHandler<A,WebExchange<A>>

A static handler used to serve static resources resolved from a base resource.

This handler is typically used as a handler in a web route to serve static content. It uses a configurable path parameter to determine the path of the resource to serve relative to the base path.


 WebRouter router = ...
 
 router
     .route()
     .path("/static/{path:.*}")
     .handler(new StaticHandler<>(new FileResource("/path/to/resources/"));
 
 
Since:
1.0
Author:
Jeremy Kuhn
See Also:
  • Field Details

    • DEFAULT_PATH_PARAMETER_NAME

      public static final String DEFAULT_PATH_PARAMETER_NAME
      The default name of the path parameter defining the path to the resource.
      See Also:
    • DEFAULT_WELCOME_PAGE

      public static final String DEFAULT_WELCOME_PAGE
      The default welcome page file name: index.html.
      See Also:
  • Constructor Details

    • StaticHandler

      public StaticHandler(Resource baseResource)

      Creates a static handler resolving resources from the specified base resource using the default path parameter name and welcome page.

      Parameters:
      baseResource - the base resource
      See Also:
    • StaticHandler

      public StaticHandler(Resource baseResource, String pathParameterName)

      Creates a static handler resolving resources from the specified base resource using the specified path parameter name.

      Parameters:
      baseResource - the base resource
      pathParameterName - the path parameter name
      See Also:
  • Method Details

    • setPathParameterName

      public void setPathParameterName(String pathParameterName)

      Sets the name of the path parameter that specifies the path of a resource relative to the base path.

      Parameters:
      pathParameterName - a path parameter name
    • setWelcomePage

      public void setWelcomePage(String welcomePage)

      Sets the name of the welcome page file to look for when a directory resource is requested.

      Parameters:
      welcomePage - a file name
    • handle

      public void handle(WebExchange<A> exchange) throws HttpException
      Description copied from interface: ExchangeHandler

      Processes the specified server exchange.

      This method is more convenient than ExchangeHandler.defer(io.inverno.mod.http.server.Exchange) when the handling logic does not need to be reactive.

      Specified by:
      handle in interface ExchangeHandler<A extends ExchangeContext,WebExchange<A extends ExchangeContext>>
      Parameters:
      exchange - the exchange to process
      Throws:
      HttpException - if an error occurs during the processing of the exchange