Package com.vaadin.flow.server
Class StaticFileServer
java.lang.Object
com.vaadin.flow.server.StaticFileServer
- All Implemented Interfaces:
StaticFileHandler
,Serializable
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:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
browserHasNewestVersion
(jakarta.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
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Serves a static resource for the requested path if a resource can be found.protected void
writeCacheHeaders
(String filenameWithPath, jakarta.servlet.http.HttpServletResponse response) Writes cache headers for the file into the response.protected long
writeModificationTimestamp
(URL resourceUrl, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Writes the modification timestamp info for the file into the response.
-
Field Details
-
APP_THEME_ASSETS_PATTERN
-
-
Constructor Details
-
StaticFileServer
Constructs a file server.- Parameters:
vaadinService
- vaadin service for the deployment, notnull
-
-
Method Details
-
serveStaticResource
public boolean serveStaticResource(jakarta.servlet.http.HttpServletRequest request, jakarta.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 interfaceStaticFileHandler
- Parameters:
request
- the request object to read fromresponse
- 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
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 viaServletContext
.- 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:
-
writeModificationTimestamp
protected long writeModificationTimestamp(URL resourceUrl, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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 object- Returns:
- the written timestamp or -1 if no timestamp was written
-
writeCacheHeaders
protected void writeCacheHeaders(String filenameWithPath, jakarta.servlet.http.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
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(jakarta.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
-