Class WebAppContext

All Implemented Interfaces:
WebAppClassLoader.Context, AliasCheck, Deployable, Handler, Handler.Container, Handler.Singleton, Request.Handler, Attributes, ClassVisibilityChecker, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle, Invocable

@ManagedObject("Web Application ContextHandler") public class WebAppContext extends ServletContextHandler implements WebAppClassLoader.Context, Deployable
Web Application Context Handler.

The WebAppContext handler is an extension of ContextHandler that coordinates the construction and configuration of nested handlers: ConstraintSecurityHandler, SessionHandler and ServletHandler. The handlers are configured by pluggable configuration classes, with the default being WebXmlConfiguration and JettyWebXmlConfiguration.

  • Field Details

  • Constructor Details

    • WebAppContext

      public WebAppContext()
    • WebAppContext

      public WebAppContext(String webApp, String contextPath)
      Parameters:
      webApp - The URL or filename of the webapp directory or war file.
      contextPath - The context path
    • WebAppContext

      public WebAppContext(Resource webApp, String contextPath)
      Parameters:
      webApp - The URL or filename of the webapp directory or war file.
      contextPath - The context path
    • WebAppContext

      public WebAppContext(SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)
      Parameters:
      sessionHandler - SessionHandler for this web app
      securityHandler - SecurityHandler for this web app
      servletHandler - ServletHandler for this web app
      errorHandler - ErrorHandler for this web app
    • WebAppContext

      public WebAppContext(String contextPath, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler, int options)
      Parameters:
      contextPath - the context path
      sessionHandler - SessionHandler for this web app
      securityHandler - SecurityHandler for this web app
      servletHandler - ServletHandler for this web app
      errorHandler - ErrorHandler for this web app
      options - the options (ServletContextHandler.SESSIONS and/or ServletContextHandler.SECURITY)
  • Method Details

    • getCurrentWebAppContext

      public static WebAppContext getCurrentWebAppContext()
    • initializeDefaults

      public void initializeDefaults(Map<String,String> properties)
      Specified by:
      initializeDefaults in interface Deployable
    • isContextPathDefault

      public boolean isContextPathDefault()
    • setContextPath

      public void setContextPath(String contextPath)
      Overrides:
      setContextPath in class ServletContextHandler
      Parameters:
      contextPath - The _contextPath to set.
    • setDefaultContextPath

      public void setDefaultContextPath(String contextPath)
    • setDisplayName

      public void setDisplayName(String servletContextName)
      Overrides:
      setDisplayName in class ContextHandler
      Parameters:
      servletContextName - The servletContextName to set.
    • getUnavailableException

      public Throwable getUnavailableException()
      Get an exception that caused the webapp to be unavailable
      Returns:
      A throwable if the webapp is unavailable or null
    • setResourceAlias

      public void setResourceAlias(String alias, String uri)
      Set Resource Alias. Resource aliases map resource uri's within a context. They may optionally be used by a handler when looking for a resource.
      Parameters:
      alias - the alias for a resource
      uri - the uri for the resource
    • getResourceAliases

      public Map<String,String> getResourceAliases()
    • setResourceAliases

      public void setResourceAliases(Map<String,String> map)
    • getResourceAlias

      public String getResourceAlias(String path)
    • removeResourceAlias

      public String removeResourceAlias(String alias)
    • setClassLoader

      public void setClassLoader(ClassLoader classLoader)
      Overrides:
      setClassLoader in class ContextHandler
    • getResourceFactory

      public ResourceFactory getResourceFactory()
    • getResource

      public Resource getResource(String pathInContext) throws MalformedURLException
      Description copied from class: ServletContextHandler
      Attempt to get a Resource from the Context.
      Overrides:
      getResource in class ServletContextHandler
      Parameters:
      pathInContext - the path within the base resource to attempt to get
      Returns:
      the resource, or null if not available.
      Throws:
      MalformedURLException - if unable to form a Resource from the provided path
    • isConfigurationDiscovered

      public boolean isConfigurationDiscovered()
      Is the context Automatically configured.
      Returns:
      true if configuration discovery.
    • setConfigurationDiscovered

      public void setConfigurationDiscovered(boolean discovered)
      Set the configuration discovery mode. If configuration discovery is set to true, then the JSR315 servlet 3.0 discovered configuration features are enabled. These are:
      • Web Fragments
      • META-INF/resource directories
      Parameters:
      discovered - true if configuration discovery is enabled for automatic configuration from the context
    • preConfigure

      public void preConfigure() throws Exception
      Pre configure the web application.

      The method is normally called from AbstractLifeCycle.start(). It performs the discovery of the configurations to be applied to this context, specifically:

      Throws:
      Exception - if unable to pre configure
    • configureClassLoader

      protected ClassLoader configureClassLoader(ClassLoader loader)
      Configure the context ClassLoader, potentially wrapping it.
      Parameters:
      loader - The loader initially set on this context by setClassLoader(ClassLoader)
      Returns:
      Either the configured loader, or a new ClassLoader that uses the loader.
    • createTempDirectory

      protected void createTempDirectory()
      Description copied from class: ContextHandler

      Create the temporary directory. If the directory exists, but is not persistent, then it is first deleted and then recreated. Once created, this method is a noop if called again before stopping the context.

      Overrides:
      createTempDirectory in class ContextHandler
    • configure

      public boolean configure() throws Exception
      Throws:
      Exception
    • postConfigure

      public void postConfigure() throws Exception
      Throws:
      Exception
    • doStart

      protected void doStart() throws Exception
      Description copied from class: ContainerLifeCycle
      Starts the managed lifecycle beans in the order they were added.
      Overrides:
      doStart in class ServletContextHandler
      Throws:
      Exception - If there was a problem starting. Will cause a transition to FAILED state
    • destroy

      public void destroy()
      Description copied from class: ContainerLifeCycle
      Destroys the managed Destroyable beans in the reverse order they were added.
      Specified by:
      destroy in interface Destroyable
      Overrides:
      destroy in class ContextHandler
    • getConfigurationClasses

      @ManagedAttribute(value="configuration classes used to configure webapp", readonly=true) public String[] getConfigurationClasses()
      Returns:
      Returns the configurations.
    • getConfigurations

      public Configurations getConfigurations()
      Returns:
      Returns the configurations.
    • getDefaultsDescriptor

      @ManagedAttribute(value="default web.xml deascriptor applied before standard web.xml", readonly=true) public String getDefaultsDescriptor()
      The default descriptor is a web.xml format file that is applied to the context before the standard WEB-INF/web.xml
      Returns:
      Returns the defaultsDescriptor.
    • getOverrideDescriptor

      public String getOverrideDescriptor()
      The override descriptor is a web.xml format file that is applied to the context after the standard WEB-INF/web.xml
      Returns:
      Returns the Override Descriptor.
    • getOverrideDescriptors

      @ManagedAttribute(value="web.xml deascriptors applied after standard web.xml", readonly=true) public List<String> getOverrideDescriptors()
      An override descriptor is a web.xml format file that is applied to the context after the standard WEB-INF/web.xml
      Returns:
      Returns the Override Descriptor list
    • getPermissions

      public PermissionCollection getPermissions()
      Specified by:
      getPermissions in interface WebAppClassLoader.Context
      Returns:
      Returns the permissions.
    • setServerClassMatcher

      public void setServerClassMatcher(ClassMatcher serverClasses)
      Set the server classes patterns.

      Server classes/packages are classes used to implement the server and are hidden from the context. If the context needs to load these classes, it must have its own copy of them in WEB-INF/lib or WEB-INF/classes.

      Parameters:
      serverClasses - the server classes pattern
    • setSystemClassMatcher

      public void setSystemClassMatcher(ClassMatcher systemClasses)
      Set the system classes patterns.

      System classes/packages are classes provided by the JVM and that cannot be replaced by classes of the same name from WEB-INF, regardless of the value of setParentLoaderPriority(boolean).

      Parameters:
      systemClasses - the system classes pattern
    • addServerClassMatcher

      public void addServerClassMatcher(ClassMatcher serverClasses)
      Add a ClassMatcher for server classes by combining with any existing matcher.
      Parameters:
      serverClasses - The class matcher of patterns to add to the server ClassMatcher
    • addSystemClassMatcher

      public void addSystemClassMatcher(ClassMatcher systemClasses)
      Add a ClassMatcher for system classes by combining with any existing matcher.
      Parameters:
      systemClasses - The class matcher of patterns to add to the system ClassMatcher
    • getSystemClassMatcher

      public ClassMatcher getSystemClassMatcher()
      Returns:
      The ClassMatcher used to match System (protected) classes
    • getServerClassMatcher

      public ClassMatcher getServerClassMatcher()
      Returns:
      The ClassMatcher used to match Server (hidden) classes
    • getSystemClasses

      @ManagedAttribute(value="classes and packages protected by context classloader", readonly=true) public String[] getSystemClasses()
    • getServerClasses

      @ManagedAttribute(value="classes and packages hidden by the context classloader", readonly=true) public String[] getServerClasses()
    • isServerClass

      public boolean isServerClass(Class<?> clazz)
      Description copied from interface: ClassVisibilityChecker
      Is the class a Server Class. A Server class is a class that is part of the implementation of the server and is NIT visible to a webapplication. The web application may provide it's own implementation of the class, to be loaded from WEB-INF/lib or WEB-INF/classes
      Specified by:
      isServerClass in interface ClassVisibilityChecker
      Parameters:
      clazz - The fully qualified name of the class.
      Returns:
      True if the class is a server class.
    • isSystemClass

      public boolean isSystemClass(Class<?> clazz)
      Description copied from interface: ClassVisibilityChecker
      Is the class a System Class. A System class is a class that is visible to a webapplication, but that cannot be overridden by the contents of WEB-INF/lib or WEB-INF/classes
      Specified by:
      isSystemClass in interface ClassVisibilityChecker
      Parameters:
      clazz - The fully qualified name of the class.
      Returns:
      True if the class is a system class.
    • isServerResource

      public boolean isServerResource(String name, URL url)
      Specified by:
      isServerResource in interface WebAppClassLoader.Context
    • isSystemResource

      public boolean isSystemResource(String name, URL url)
      Specified by:
      isSystemResource in interface WebAppClassLoader.Context
    • setServer

      public void setServer(Server server)
      Description copied from interface: Handler
      Set the Server to associate to this Handler.
      Specified by:
      setServer in interface Handler
      Overrides:
      setServer in class ContextHandler
      Parameters:
      server - the Server to associate to this Handler
    • getWar

      @ManagedAttribute(value="war file location", readonly=true) public String getWar()
      Returns:
      Returns the war as a file or URL string (Resource). The war may be different to the @link ContextHandler.getBaseResource() if the war has been expanded and/or copied.
    • getWebInf

      public Resource getWebInf() throws IOException
      Throws:
      IOException
    • isDistributable

      @ManagedAttribute("web application distributable") public boolean isDistributable()
      Returns:
      Returns the distributable.
    • isExtractWAR

      @ManagedAttribute(value="extract war", readonly=true) public boolean isExtractWAR()
      Returns:
      Returns the extractWAR.
    • isCopyWebDir

      @ManagedAttribute(value="webdir copied on deploy (allows hot replacement on windows)", readonly=true) public boolean isCopyWebDir()
      Returns:
      True if the webdir is copied (to allow hot replacement of jars on windows)
    • isCopyWebInf

      public boolean isCopyWebInf()
      Returns:
      True if the web-inf lib and classes directories are copied (to allow hot replacement of jars on windows)
    • isParentLoaderPriority

      @ManagedAttribute(value="parent classloader given priority", readonly=true) public boolean isParentLoaderPriority()
      Specified by:
      isParentLoaderPriority in interface WebAppClassLoader.Context
      Returns:
      True if the classloader should delegate first to the parent classloader (standard java behaviour) or false if the classloader should first try to load from WEB-INF/lib or WEB-INF/classes (servlet spec recommendation). Default is false or can be set by the system property org.eclipse.jetty.server.webapp.parentLoaderPriority
    • loadConfigurations

      protected void loadConfigurations()
    • newConfigurations

      protected Configurations newConfigurations()
    • newServletContextApi

      public ServletContextHandler.ServletContextApi newServletContextApi()
      Overrides:
      newServletContextApi in class ServletContextHandler
    • toString

      public String toString()
      Overrides:
      toString in class ContextHandler
    • dump

      public void dump(Appendable out, String indent) throws IOException
      Description copied from interface: Dumpable
      Dump this object (and children) into an Appendable using the provided indent after any new lines. The indent should not be applied to the first object dumped.
      Specified by:
      dump in interface Dumpable
      Overrides:
      dump in class ServletContextHandler
      Parameters:
      out - The appendable to dump to
      indent - The indent to apply after any new lines.
      Throws:
      IOException - if unable to write to Appendable
    • setConfigurationClasses

      public void setConfigurationClasses(String[] configurations)
      Parameters:
      configurations - The configuration class names. If setConfigurations is not called these classes are used to create a configurations array.
    • setConfigurationClasses

      public void setConfigurationClasses(List<String> configurations)
    • setConfigurations

      public void setConfigurations(Configurations configurations)
      Parameters:
      configurations - The configurations to set.
    • setConfigurations

      public void setConfigurations(Configuration[] configurations)
      Parameters:
      configurations - The configurations to set.
    • addConfiguration

      public void addConfiguration(Configuration... configuration)
    • getConfiguration

      public <T> T getConfiguration(Class<? extends T> configClass)
    • removeConfiguration

      public void removeConfiguration(Configuration... configurations)
    • removeConfiguration

      public void removeConfiguration(Class<? extends Configuration>... configurations)
    • setDefaultsDescriptor

      public void setDefaultsDescriptor(String defaultsDescriptor)
      The default descriptor is a web.xml format file that is applied to the context before the standard WEB-INF/web.xml
      Parameters:
      defaultsDescriptor - The defaultsDescriptor to set.
    • setOverrideDescriptor

      public void setOverrideDescriptor(String overrideDescriptor)
      The override descriptor is a web.xml format file that is applied to the context after the standard WEB-INF/web.xml
      Parameters:
      overrideDescriptor - The overrideDescritpor to set.
    • setOverrideDescriptors

      public void setOverrideDescriptors(List<String> overrideDescriptors)
      The override descriptor is a web.xml format file that is applied to the context after the standard WEB-INF/web.xml
      Parameters:
      overrideDescriptors - The overrideDescriptors (file or URL) to set.
    • addOverrideDescriptor

      public void addOverrideDescriptor(String overrideDescriptor)
      The override descriptor is a web.xml format file that is applied to the context after the standard WEB-INF/web.xml
      Parameters:
      overrideDescriptor - The overrideDescriptor (file or URL) to add.
    • getDescriptor

      @ManagedAttribute(value="standard web.xml descriptor", readonly=true) public String getDescriptor()
      Returns:
      the web.xml descriptor to use. If set to null, WEB-INF/web.xml is used if it exists.
    • setDescriptor

      public void setDescriptor(String descriptor)
      Set the web.xml descriptor to use. If set to null, WEB-INF/web.xml is used if it exists..
      Parameters:
      descriptor - the web.xml descriptor to use. If set to null, WEB-INF/web.xml is used if it exists.
    • setDistributable

      public void setDistributable(boolean distributable)
      Parameters:
      distributable - The distributable to set.
    • removeEventListener

      public boolean removeEventListener(EventListener listener)
      Description copied from interface: Container
      Remove an event listener.
      Specified by:
      removeEventListener in interface Container
      Specified by:
      removeEventListener in interface LifeCycle
      Overrides:
      removeEventListener in class ServletContextHandler
      Parameters:
      listener - the listener to remove
      Returns:
      true if the listener was removed
      See Also:
    • setExtractWAR

      public void setExtractWAR(boolean extractWAR)
      Parameters:
      extractWAR - True if war files are extracted
    • setCopyWebDir

      public void setCopyWebDir(boolean copy)
      Parameters:
      copy - True if the webdir is copied (to allow hot replacement of jars)
    • setCopyWebInf

      public void setCopyWebInf(boolean copyWebInf)
      Parameters:
      copyWebInf - True if the web-inf lib and classes directories are copied (to allow hot replacement of jars on windows)
    • setParentLoaderPriority

      public void setParentLoaderPriority(boolean java2compliant)
      Parameters:
      java2compliant - True if the classloader should delegate first to the parent classloader (standard java behaviour) or false if the classloader should first try to load from WEB-INF/lib or WEB-INF/classes (servlet spec recommendation). Default is false or can be set by the system property org.eclipse.jetty.server.webapp.parentLoaderPriority
    • setPermissions

      public void setPermissions(PermissionCollection permissions)
      Parameters:
      permissions - The permissions to set.
    • setContextWhiteList

      public void setContextWhiteList(String... contextWhiteList)
      Set the context white list In certain circumstances you want may want to deny access of one webapp from another when you may not fully trust the webapp. Setting this white list will enable a check when a servlet called ServletContextHandler.Context#getContext(String), validating that the uriInPath for the given webapp has been declaratively allows access to the context.
      Parameters:
      contextWhiteList - the whitelist of contexts
    • setWar

      public void setWar(String war)
      Set the war of the webapp. From this value a ContextHandler.setBaseResource(Resource) value is computed by WebInfConfiguration, which may be changed from the war URI by unpacking and/or copying.
      Parameters:
      war - The war to set as a file name or URL.
    • setWarResource

      public void setWarResource(Resource war)
      Set the war of the webapp as a Resource.
      Parameters:
      war - The war to set as a Resource.
      See Also:
    • getExtraClasspath

      @ManagedAttribute(value="extra classpath for context classloader", readonly=true) public List<Resource> getExtraClasspath()
      Specified by:
      getExtraClasspath in interface WebAppClassLoader.Context
      Returns:
      Comma or semicolon separated path of filenames or URLs pointing to directories or jar files. Directories should end with '/'.
    • setExtraClasspath

      public void setExtraClasspath(String extraClasspath)
      Set the Extra ClassPath via delimited String.

      This is a convenience method for setExtraClasspath(List)

      Parameters:
      extraClasspath - Comma or semicolon separated path of filenames or URLs pointing to directories or jar files. Directories should end with '/'.
      See Also:
    • setExtraClasspath

      public void setExtraClasspath(List<Resource> extraClasspath)
    • isLogUrlOnStart

      public boolean isLogUrlOnStart()
    • setLogUrlOnStart

      public void setLogUrlOnStart(boolean logOnStart)
      Sets whether or not the web app name and URL is logged on startup
      Parameters:
      logOnStart - whether or not the log message is created
    • isAllowDuplicateFragmentNames

      public boolean isAllowDuplicateFragmentNames()
    • setAllowDuplicateFragmentNames

      public void setAllowDuplicateFragmentNames(boolean allowDuplicateFragmentNames)
    • setThrowUnavailableOnStartupException

      public void setThrowUnavailableOnStartupException(boolean throwIfStartupException)
    • isThrowUnavailableOnStartupException

      public boolean isThrowUnavailableOnStartupException()
    • startContext

      protected void startContext() throws Exception
      Description copied from class: ServletContextHandler
      Finish constructing handlers and link them together.
      Overrides:
      startContext in class ServletContextHandler
      Throws:
      Exception
    • stopContext

      protected void stopContext() throws Exception
      Overrides:
      stopContext in class ServletContextHandler
      Throws:
      Exception
    • startWebapp

      protected void startWebapp() throws Exception
      Continue the startContext() before calling super.startContext().
      Throws:
      Exception - If there was a problem starting
    • stopWebapp

      protected void stopWebapp() throws Exception
      Continue the stopContext() before calling super.stopContext().
      Throws:
      Exception - If there was a problem stopping
    • cleanupAfterStop

      protected void cleanupAfterStop() throws Exception
      Prevent the temp directory from being deleted during the normal stop sequence, and require that ContextHandler.cleanupAfterStop() is explicitly called after the webapp classloader is closed
      Overrides:
      cleanupAfterStop in class ContextHandler
      Throws:
      Exception
    • setServletSecurity

      public Set<String> setServletSecurity(jakarta.servlet.ServletRegistration.Dynamic registration, jakarta.servlet.ServletSecurityElement servletSecurityElement)
      Description copied from class: ServletContextHandler
      Delegate for ServletRegistration.Dynamic.setServletSecurity method
      Overrides:
      setServletSecurity in class ServletContextHandler
      Parameters:
      registration - ServletRegistration.Dynamic instance that setServletSecurity was called on
      servletSecurityElement - new security info
      Returns:
      the set of exact URL mappings currently associated with the registration that are also present in the web.xml security constraints and thus will be unaffected by this call.
    • getMetaData

      public MetaData getMetaData()
    • addServerClasses

      public static void addServerClasses(Server server, String... pattern)
    • addSystemClasses

      public static void addSystemClasses(Server server, String... pattern)