Class StaticFileServer

  • All Implemented Interfaces:
    StaticFileHandler, Serializable

    public class StaticFileServer
    extends Object
    implements StaticFileHandler
    Handles sending of resources from the WAR root (web content) or META-INF/resources in the case that VaadinServlet is mapped using "/*".

    This class is primarily meant to be used during developing time. For a production mode site you should consider serving static resources directly from the servlet (using a default servlet if such exists) or through a stand alone static file server.

    Since:
    1.0
    Author:
    Vaadin Ltd
    See Also:
    Serialized Form
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected boolean browserHasNewestVersion​(javax.servlet.http.HttpServletRequest request, long resourceLastModifiedTimestamp)
      Checks if the browser has an up to date cached version of requested resource using the "If-Modified-Since" header.
      protected int getCacheTime​(String filenameWithPath)
      Calculates the cache lifetime for the given filename in seconds.
      protected URL getStaticResource​(String path)
      Returns a URL to the static Web resource at the given URI or null if no file found.
      boolean serveStaticResource​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
      Serves a static resource for the requested path if a resource can be found.
      protected void writeCacheHeaders​(String filenameWithPath, javax.servlet.http.HttpServletResponse response)
      Writes cache headers for the file into the response.
      protected long writeModificationTimestamp​(URL resourceUrl, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
      Writes the modification timestamp info for the file into the response.
    • Field Detail

      • APP_THEME_PATTERN

        public static final Pattern APP_THEME_PATTERN
    • Constructor Detail

      • StaticFileServer

        public StaticFileServer​(VaadinService vaadinService)
        Constructs a file server.
        Parameters:
        vaadinService - vaadin service for the deployment, not null
    • Method Detail

      • serveStaticResource

        public boolean serveStaticResource​(javax.servlet.http.HttpServletRequest request,
                                           javax.servlet.http.HttpServletResponse response)
                                    throws IOException
        Description copied from interface: StaticFileHandler
        Serves a static resource for the requested path if a resource can be found.
        Specified by:
        serveStaticResource in interface StaticFileHandler
        Parameters:
        request - the request object to read from
        response - the response object to write to
        Returns:
        true if a file was served and the request has been handled, false otherwise.
        Throws:
        IOException - if the underlying servlet container reports an exception
      • getStaticResource

        protected URL getStaticResource​(String path)
        Returns a URL to the static Web resource at the given URI or null if no file found.

        The resource will be exposed via HTTP (available as a static web resource). The null return value means that the resource won't be exposed as a Web resource even if it's a resource available via ServletContext.

        Parameters:
        path - the path for the resource
        Returns:
        the resource located at the named path to expose it via Web, or null if there is no resource at that path or it should not be exposed
        See Also:
        VaadinService.getStaticResource(String)
      • writeModificationTimestamp

        protected long writeModificationTimestamp​(URL resourceUrl,
                                                  javax.servlet.http.HttpServletRequest request,
                                                  javax.servlet.http.HttpServletResponse response)
        Writes the modification timestamp info for the file into the response.
        Parameters:
        resourceUrl - the internal URL of the file
        request - the request object
        response - the response object
        Returns:
        the written timestamp or -1 if no timestamp was written
      • writeCacheHeaders

        protected void writeCacheHeaders​(String filenameWithPath,
                                         javax.servlet.http.HttpServletResponse response)
        Writes cache headers for the file into the response.
        Parameters:
        filenameWithPath - the name and path of the file being sent
        response - the response object
      • getCacheTime

        protected int getCacheTime​(String filenameWithPath)
        Calculates the cache lifetime for the given filename in seconds.

        By default filenames containing ".nocache." return 0, filenames containing ".cache." return one year and all other files return 1 hour.

        Parameters:
        filenameWithPath - the name of the file being sent
        Returns:
        cache lifetime for the given filename in seconds
      • browserHasNewestVersion

        protected boolean browserHasNewestVersion​(javax.servlet.http.HttpServletRequest request,
                                                  long resourceLastModifiedTimestamp)
        Checks if the browser has an up to date cached version of requested resource using the "If-Modified-Since" header.
        Parameters:
        request - The HttpServletRequest from the browser.
        resourceLastModifiedTimestamp - The timestamp when the resource was last modified. -1 if the last modification time is unknown.
        Returns:
        true if the If-Modified-Since header tells the cached version in the browser is up to date, false otherwise