com.vaadin.flow.server.
Class StaticFileServer
- java.lang.Object
-
- com.vaadin.flow.server.StaticFileServer
-
All Implemented Interfaces:
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:
-
-
Constructor Summary
Constructors Constructor and Description StaticFileServer(VaadinServletService servletService)
Constructs a file server.
-
Method Summary
All Methods Modifier and Type Method and Description protected boolean
browserHasNewestVersion(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.
boolean
isStaticResourceRequest(HttpServletRequest request)
Checks if a static resource can be found for the requested path.
boolean
serveStaticResource(HttpServletRequest request, HttpServletResponse response)
Serves a static resource for the requested path if a resource can be found.
protected void
writeCacheHeaders(String filenameWithPath, HttpServletResponse response)
Writes cache headers for the file into the response.
protected long
writeModificationTimestamp(URL resourceUrl, HttpServletRequest request, HttpServletResponse response)
Writes the modification timestamp info for the file into the response.
-
-
-
Constructor Detail
-
StaticFileServer
public StaticFileServer(VaadinServletService servletService)
Constructs a file server.
Parameters:
servletService
- servlet service for the deployment, notnull
-
-
Method Detail
-
isStaticResourceRequest
public boolean isStaticResourceRequest(HttpServletRequest request)
Description copied from interface:
StaticFileHandler
Checks if a static resource can be found for the requested path.
Specified by:
isStaticResourceRequest
in interfaceStaticFileHandler
Parameters:
request
- the request to checkReturns:
true
if a static resource exists and can be sent as a response to this request,false
otherwise
-
serveStaticResource
public boolean serveStaticResource(HttpServletRequest request, 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 interfaceStaticFileHandler
Parameters:
request
- the request object to read fromresponse
- the response object to write toReturns:
true
if a file was served and the request has been handled,false
otherwise.Throws:
IOException
- if the underlying servlet container reports an exception
-
writeModificationTimestamp
protected long writeModificationTimestamp(URL resourceUrl, HttpServletRequest request, HttpServletResponse response)
Writes the modification timestamp info for the file into the response.
Parameters:
resourceUrl
- the internal URL of the filerequest
- the request objectresponse
- the response objectReturns:
the written timestamp or -1 if no timestamp was written
-
writeCacheHeaders
protected void writeCacheHeaders(String filenameWithPath, HttpServletResponse response)
Writes cache headers for the file into the response.
Parameters:
filenameWithPath
- the name and path of the file being sentresponse
- 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 sentReturns:
cache lifetime for the given filename in seconds
-
browserHasNewestVersion
protected boolean browserHasNewestVersion(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
-
-