Class BaseResourceServletContext

  • All Implemented Interfaces:
    javax.servlet.ServletContext

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

      • context

        protected final javax.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 Detail

      • BaseResourceServletContext

        public BaseResourceServletContext​(javax.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 Detail

      • getContextPath

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

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

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

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

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

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

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

        public Set<String> getResourcePaths​(String path)
        Specified by:
        getResourcePaths in interface javax.servlet.ServletContext
      • 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 javax.servlet.ServletContext
      • getRequestDispatcher

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

        public javax.servlet.RequestDispatcher getNamedDispatcher​(String name)
        Specified by:
        getNamedDispatcher in interface javax.servlet.ServletContext
      • getServlet

        public javax.servlet.Servlet getServlet​(String name)
                                         throws javax.servlet.ServletException
        Specified by:
        getServlet in interface javax.servlet.ServletContext
        Throws:
        javax.servlet.ServletException
      • getServlets

        public Enumeration<javax.servlet.Servlet> getServlets()
        Specified by:
        getServlets in interface javax.servlet.ServletContext
      • getServletNames

        public Enumeration<String> getServletNames()
        Specified by:
        getServletNames in interface javax.servlet.ServletContext
      • log

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

        public void log​(Exception exception,
                        String msg)
        Specified by:
        log in interface javax.servlet.ServletContext
      • log

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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