Class TenantFilter

java.lang.Object
com.codename1.backend.tenancy.TenantFilter
All Implemented Interfaces:
SecurityFilter

public final class TenantFilter extends Object implements SecurityFilter

Sets TenantContext for the length of each request and clears it after, whatever the handler does.

A request that names no tenant passes through with none set, so a tenant-partitioned entity touched while serving it fails with MissingTenantException; construct the filter with required true to answer 400 at the door instead. A malformed identifier is always a 400.

Trust: a header is whatever the client sent. Resolve the tenant from something the server vouches for -- the authenticated principal, a host name the proxy routes -- unless a gateway in front strips and sets the header.

  • Constructor Details

    • TenantFilter

      public TenantFilter(TenantResolver resolver, boolean required)
      Parameters:
      resolver - how the tenant is found
      required - whether a request without a tenant is refused
  • Method Details

    • header

      public static TenantResolver header(String name)
      A resolver reading a header, conventionally X-Tenant-ID.
      Parameters:
      name - the header name
      Returns:
      the resolver
    • subdomain

      public static TenantResolver subdomain(String domain)
      A resolver taking the first label of the Host header below domain: acme.example.com under example.com is tenant acme.
      Parameters:
      domain - the shared parent domain
      Returns:
      the resolver
    • authenticated

      public static TenantResolver authenticated(TenantFilter.PrincipalTenant rule)
      A resolver asking rule for the tenant of the authenticated principal. Install the filter after authentication, so the principal is set; a request that is not authenticated resolves no tenant.
      Parameters:
      rule - how a principal maps to its tenant, such as a claim lookup
      Returns:
      the resolver
    • doFilter

      public HttpServer.Response doFilter(HttpServer.Request request, FilterChain chain) throws Exception
      Description copied from interface: SecurityFilter
      The answer to request; null when nothing under the chain routes it, which the server answers 404.
      Specified by:
      doFilter in interface SecurityFilter
      Throws:
      Exception