Interface WebContext
Its WebContext.Builder allows programmatic web component registration (as opposed to declarative e.g. via
web.xml, OSGi HTTP Whiteboard blueprint integration, CXF BP etc.)
This is preferable because:
- using code instead of hiding class names in XML enables tools such as e.g. BND (in the maven-bundle-plugin) to correctly figure dependencies e.g. for OSGi Import-Package headers;
- explicit passing of web components instances, instead of providing class names in XML files and letting a web
container create the new instances using the default constructor, solves a pesky dependency injection (DI)
related problem which typically leads to weird hoops in code through
staticetc. that can be avoided using this; - tests can more easily programmatically instantiate web components.
This, not surprisingly, looks somewhat like a Servlet (3.x+) ServletContext, which also allows programmatic
dynamic registration e.g. via ServletRegistration; however in practice direct use of that API has been found
to be problematic under OSGi, because it is intended for JSE and
does not easily appear to permit dynamic registration
at any time (only during Servlet container initialization time by ServletContainerInitializer), and is
generally less clear to use than this simple API which intentionally maps directly to what one would have declared in
a web.xml file. This API is also slightly more focused and drops a number of concepts that API has which we do not
want to support here (including e.g. security, roles, multipart etc.)
It also looks somewhat similar to the OSGi HttpService, but we want to avoid any org.osgi dependency (both API and
impl) here, and that API is also less clear (and uses an ancient (!) Dictionary in its method
signature), and -most importantly- simply does not support Filters and Listeners, only Servlets. The Pax Web API does
extend the base OSGi API and adds supports for Filters, Listeners and context parameters, but is still OSGi specific,
whereas this offers a much simpler standalone API without OSGi dependency. (The Pax Web API also has confusing
signatures in its registerFilter() methods, where one can easily confuse which String[] is the urlPatterns; which we
had initially done accidentally; and left AAA broken.)
This is immutable, with a Builder, because contrary to a declarative approach in a file such as web.xml, the
registration order very much matters (e.g. an context parameter added after a Servlet registration would not be seen
by that Servlet; or a Filter added to protect a Servlet might not yet be active for an instant if the registerServlet
is before the registerFilter). Therefore, this API enforces atomicity and lets clients first register everything on
the Builder, and only then use WebServer.registerWebContext(WebContext).
- Author:
- Michael Vorburger.ch
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull WebContext.Builderbuilder()Create builder forWebContext.Get map of context params.@NonNull StringGet path which will be used as URL prefix to all registered servlets and filters.@NonNull List<FilterDetails>filters()Get list of filters.@NonNull List<javax.servlet.ServletContextListener>Get list of servlet context listeners.@NonNull Stringname()Get the descriptive name of this context.@NonNull List<ResourceDetails>Get lis of resources (e.g.@NonNull List<ServletDetails>servlets()Get list of servlets.booleanGet flag value whether this context supports web sessions.
-
Method Details
-
name
@NonNull String name()Get the descriptive name of this context.- Returns:
- A descriptive name.
-
contextPath
@NonNull String contextPath()Get path which will be used as URL prefix to all registered servlets and filters. Guaranteed to be non-empty- Returns:
Stringpath- See Also:
-
- "Java Servlet Specification Version 3.1, Section 3.5 Request Path Elements"
-
supportsSessions
boolean supportsSessions()Get flag value whether this context supports web sessions.- Returns:
- boolean flag value
-
servlets
@NonNull List<ServletDetails> servlets()Get list of servlets.- Returns:
Listlist ofServletDetails
-
filters
@NonNull List<FilterDetails> filters()Get list of filters.- Returns:
Listlist ofFilterDetails
-
listeners
@NonNull List<javax.servlet.ServletContextListener> listeners()Get list of servlet context listeners.- Returns:
Listlist ofServletContextListener
-
resources
@NonNull List<ResourceDetails> resources()Get lis of resources (e.g. html files) that can be accessed via the URI namespace.- Returns:
Listlist ofResourceDetails
-
contextParams
Get map of context params.These are the
ServletContexts initial parameters; contrary to individualServletDetails.initParams()andFilterDetails.initParams(). While a ServletContext accepts any Object as a parameter, that is not accepted in all implementations. Most notably OSGi HTTP Whiteboard specification allows only String values, hence we are enforcing that.- Returns:
Mapcontext parameters map
-
builder
Create builder forWebContext.- Returns:
WebContext.Builderbuilder instance
-