Class BaseResourceServletContext

java.lang.Object
de.aristaflow.adept2.base.servletcontainer.dynamic.BaseResourceServletContext
All Implemented Interfaces:
jakarta.servlet.ServletContext

public class BaseResourceServletContext extends Object implements jakarta.servlet.ServletContext
A wrapping servlet context that loads resources relative to a provided class.
  • Field Details

    • FILE_PROTOCOL

      public static final String FILE_PROTOCOL
      The protocol for a file.
      See Also:
    • JAR_FILE_PROTOCOL

      public static final String JAR_FILE_PROTOCOL
      The protocol for a Jar-file.
      See Also:
    • context

      protected final jakarta.servlet.ServletContext context
      The servlet context to which to delegate to.
    • loader

      protected final Class<?> loader
      The class relative to which we try to load resources.
    • loaderBase

      protected final URL loaderBase
      The URL that is the base of our loader (class) .
    • logger

      protected final Logger logger
      The logger for logging exception messages and important proceed information.
  • Constructor Details

    • BaseResourceServletContext

      public BaseResourceServletContext(jakarta.servlet.ServletContext context, Class<?> loader, Logger logger) throws MalformedURLException
      Parameters:
      context - The servlet context to which to delegate to.
      loader - The class relative to which we try to load resources.
      logger - The logger for logging exception messages and important proceed information.
      Throws:
      MalformedURLException - If there are problems creating the base URL, that is the URL relative to which to load resources, a MalformedURLException will be thrown.
  • Method Details

    • getContextPath

      public String getContextPath()
      Specified by:
      getContextPath in interface jakarta.servlet.ServletContext
    • getContext

      public jakarta.servlet.ServletContext getContext(String uripath)
      Specified by:
      getContext in interface jakarta.servlet.ServletContext
    • getMajorVersion

      public int getMajorVersion()
      Specified by:
      getMajorVersion in interface jakarta.servlet.ServletContext
    • getMinorVersion

      public int getMinorVersion()
      Specified by:
      getMinorVersion in interface jakarta.servlet.ServletContext
    • getEffectiveMajorVersion

      public int getEffectiveMajorVersion()
      Specified by:
      getEffectiveMajorVersion in interface jakarta.servlet.ServletContext
    • getEffectiveMinorVersion

      public int getEffectiveMinorVersion()
      Specified by:
      getEffectiveMinorVersion in interface jakarta.servlet.ServletContext
    • getMimeType

      public String getMimeType(String file)
      Specified by:
      getMimeType in interface jakarta.servlet.ServletContext
    • getResourcePaths

      public Set<String> getResourcePaths(String path)
      Specified by:
      getResourcePaths in interface jakarta.servlet.ServletContext
    • getResource

      public URL getResource(String path) throws MalformedURLException
      Specified by:
      getResource in interface jakarta.servlet.ServletContext
      Throws:
      MalformedURLException
    • listContentOfUrl

      protected Set<String> listContentOfUrl(URL resource, URL base)
      Lists the content of the designated URL. The URL is retrieved from loading a resource relative to a class. It may be specific to the servlet container that embeds this servlet. This method does not need to delegate to the underlying context. This will be done if this method does not return valid content.

      Usually you do not override this method but listContentOfUrlExt(URL, URL, String). If a file or Jar-file protocol is encountered, this implementation will usually suit your needs. In case of other protocols, listContentOfUrlExt(URL, URL, String) will be called.

      Parameters:
      resource - The URL identifying a resource as retrieved from our loader (class).
      base - The URL identifying the top-level path of our loader.
      Returns:
      The content of the designated URL (usually the content of the directory specified by the designated URL relative to the loader URL. If the directory for the resource is empty, the empty list will be returned, if no content can be retrieved, e.g. the resource URL cannot be interpreted as directory, null will be returned. Directories need to have a trailing slash.
    • listContentOfUrlExt

      protected Set<String> listContentOfUrlExt(URL resource, URL base, String relResPath)
      Lists the content of the designated URL and thus extends listContentOfUrl(URL, URL). This method will be called if the URL protocol cannot be handled there. Subclasses should override this method to support further protocols.

      This default implementation just returns null indicating that the resource cannot be interpreted as directory.

      Parameters:
      resource - The URL identifying a resource as retrieved from our loader (class).
      base - The URL identifying the top-level path of our loader.
      relResPath - The path to the resource URL relative to the base URL. Obviously this will only be valid if the designated resource is part of the designated base.
      Returns:
      The content of the designated URL (usually the content of the directory specified by the designated URL relative to the loader URL. If the directory for the resource is empty, the empty list will be returned, if no content can be retrieved, e.g. the resource URL cannot be interpreted as directory, null will be returned. Directories need to have a trailing slash.
    • getChildren

      protected Set<String> getChildren(String entryPath, JarFile file)
      Gets the children for the designated path in the designated jar-file.
      Parameters:
      entryPath - The entry for which to get the direct children. The path is relative to the designated jar-file.
      file - The jar-file from which to retrieve the children.
      Returns:
      The paths of the children of the designated entry relative to the designated jar-file. Note that directories must have a trailing "/".
    • getResourceFromLoader

      protected URL getResourceFromLoader(String path)
      Tries to retrieve the designated path from our loader (class). The path is tried relatively and absolutely. If the loader does not know the designated path, null will be returned.
      Parameters:
      path - The path which to search in our loader (class).
      Returns:
      The URL retrieved from our loader (class) or null if the loader does not know the path.
    • getResourceAsStream

      public InputStream getResourceAsStream(String path)
      Specified by:
      getResourceAsStream in interface jakarta.servlet.ServletContext
    • getRequestDispatcher

      public jakarta.servlet.RequestDispatcher getRequestDispatcher(String path)
      Specified by:
      getRequestDispatcher in interface jakarta.servlet.ServletContext
    • getNamedDispatcher

      public jakarta.servlet.RequestDispatcher getNamedDispatcher(String name)
      Specified by:
      getNamedDispatcher in interface jakarta.servlet.ServletContext
    • log

      public void log(String msg)
      Specified by:
      log in interface jakarta.servlet.ServletContext
    • log

      public void log(String message, Throwable throwable)
      Specified by:
      log in interface jakarta.servlet.ServletContext
    • getRealPath

      public String getRealPath(String path)
      Specified by:
      getRealPath in interface jakarta.servlet.ServletContext
    • getServerInfo

      public String getServerInfo()
      Specified by:
      getServerInfo in interface jakarta.servlet.ServletContext
    • getInitParameter

      public String getInitParameter(String name)
      Specified by:
      getInitParameter in interface jakarta.servlet.ServletContext
    • getInitParameterNames

      public Enumeration<String> getInitParameterNames()
      Specified by:
      getInitParameterNames in interface jakarta.servlet.ServletContext
    • setInitParameter

      public boolean setInitParameter(String name, String value)
      Specified by:
      setInitParameter in interface jakarta.servlet.ServletContext
    • getAttribute

      public Object getAttribute(String name)
      Specified by:
      getAttribute in interface jakarta.servlet.ServletContext
    • getAttributeNames

      public Enumeration<String> getAttributeNames()
      Specified by:
      getAttributeNames in interface jakarta.servlet.ServletContext
    • setAttribute

      public void setAttribute(String name, Object object)
      Specified by:
      setAttribute in interface jakarta.servlet.ServletContext
    • removeAttribute

      public void removeAttribute(String name)
      Specified by:
      removeAttribute in interface jakarta.servlet.ServletContext
    • getServletContextName

      public String getServletContextName()
      Specified by:
      getServletContextName in interface jakarta.servlet.ServletContext
    • addServlet

      public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, String className)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
    • addServlet

      public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, jakarta.servlet.Servlet servlet)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
    • addServlet

      public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
    • addJspFile

      public jakarta.servlet.ServletRegistration.Dynamic addJspFile(String servletName, String jspFile)
      Specified by:
      addJspFile in interface jakarta.servlet.ServletContext
    • createServlet

      public <T extends jakarta.servlet.Servlet> T createServlet(Class<T> clazz) throws jakarta.servlet.ServletException
      Specified by:
      createServlet in interface jakarta.servlet.ServletContext
      Throws:
      jakarta.servlet.ServletException
    • getServletRegistration

      public jakarta.servlet.ServletRegistration getServletRegistration(String servletName)
      Specified by:
      getServletRegistration in interface jakarta.servlet.ServletContext
    • getServletRegistrations

      public Map<String,? extends jakarta.servlet.ServletRegistration> getServletRegistrations()
      Specified by:
      getServletRegistrations in interface jakarta.servlet.ServletContext
    • addFilter

      public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
    • addFilter

      public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, jakarta.servlet.Filter filter)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
    • addFilter

      public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends jakarta.servlet.Filter> filterClass)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
    • createFilter

      public <T extends jakarta.servlet.Filter> T createFilter(Class<T> clazz) throws jakarta.servlet.ServletException
      Specified by:
      createFilter in interface jakarta.servlet.ServletContext
      Throws:
      jakarta.servlet.ServletException
    • getFilterRegistration

      public jakarta.servlet.FilterRegistration getFilterRegistration(String filterName)
      Specified by:
      getFilterRegistration in interface jakarta.servlet.ServletContext
    • getFilterRegistrations

      public Map<String,? extends jakarta.servlet.FilterRegistration> getFilterRegistrations()
      Specified by:
      getFilterRegistrations in interface jakarta.servlet.ServletContext
    • getSessionCookieConfig

      public jakarta.servlet.SessionCookieConfig getSessionCookieConfig()
      Specified by:
      getSessionCookieConfig in interface jakarta.servlet.ServletContext
    • setSessionTrackingModes

      public void setSessionTrackingModes(Set<jakarta.servlet.SessionTrackingMode> sessionTrackingModes)
      Specified by:
      setSessionTrackingModes in interface jakarta.servlet.ServletContext
    • getDefaultSessionTrackingModes

      public Set<jakarta.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
      Specified by:
      getDefaultSessionTrackingModes in interface jakarta.servlet.ServletContext
    • getEffectiveSessionTrackingModes

      public Set<jakarta.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
      Specified by:
      getEffectiveSessionTrackingModes in interface jakarta.servlet.ServletContext
    • addListener

      public void addListener(String className)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
    • addListener

      public <T extends EventListener> void addListener(T t)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
    • addListener

      public void addListener(Class<? extends EventListener> listenerClass)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
    • createListener

      public <T extends EventListener> T createListener(Class<T> clazz) throws jakarta.servlet.ServletException
      Specified by:
      createListener in interface jakarta.servlet.ServletContext
      Throws:
      jakarta.servlet.ServletException
    • getJspConfigDescriptor

      public jakarta.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
      Specified by:
      getJspConfigDescriptor in interface jakarta.servlet.ServletContext
    • getClassLoader

      public ClassLoader getClassLoader()
      Specified by:
      getClassLoader in interface jakarta.servlet.ServletContext
    • declareRoles

      public void declareRoles(String... roleNames)
      Specified by:
      declareRoles in interface jakarta.servlet.ServletContext
    • getVirtualServerName

      public String getVirtualServerName()
      Specified by:
      getVirtualServerName in interface jakarta.servlet.ServletContext
    • getSessionTimeout

      public int getSessionTimeout()
      Specified by:
      getSessionTimeout in interface jakarta.servlet.ServletContext
    • setSessionTimeout

      public void setSessionTimeout(int sessionTimeout)
      Specified by:
      setSessionTimeout in interface jakarta.servlet.ServletContext
    • getRequestCharacterEncoding

      public String getRequestCharacterEncoding()
      Specified by:
      getRequestCharacterEncoding in interface jakarta.servlet.ServletContext
    • setRequestCharacterEncoding

      public void setRequestCharacterEncoding(String encoding)
      Specified by:
      setRequestCharacterEncoding in interface jakarta.servlet.ServletContext
    • getResponseCharacterEncoding

      public String getResponseCharacterEncoding()
      Specified by:
      getResponseCharacterEncoding in interface jakarta.servlet.ServletContext
    • setResponseCharacterEncoding

      public void setResponseCharacterEncoding(String encoding)
      Specified by:
      setResponseCharacterEncoding in interface jakarta.servlet.ServletContext