public class StaticFileServer extends Object implements StaticFileHandler
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.
Modifier and Type | Field and Description |
---|---|
protected static Pattern |
APP_THEME_PATTERN |
Constructor and Description |
---|
StaticFileServer(VaadinServletService servletService)
Constructs a file server.
|
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.
|
protected URL |
getStaticResource(String path)
Returns a URL to the static Web resource at the given URI or null if no
file found.
|
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.
|
protected static final Pattern APP_THEME_PATTERN
public StaticFileServer(VaadinServletService servletService)
servletService
- servlet service for the deployment, not null
public boolean isStaticResourceRequest(HttpServletRequest request)
StaticFileHandler
isStaticResourceRequest
in interface StaticFileHandler
request
- the request to checktrue
if a static resource exists and can be sent as
a response to this request, false
otherwisepublic boolean serveStaticResource(HttpServletRequest request, HttpServletResponse response) throws IOException
StaticFileHandler
serveStaticResource
in interface StaticFileHandler
request
- the request object to read fromresponse
- the response object to write totrue
if a file was served and the request has been
handled, false
otherwise.IOException
- if the underlying servlet container reports an exceptionprotected URL getStaticResource(String path)
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
.
path
- the path for the resourcenull
if there is no resource at that path or it should
not be exposedVaadinService.getStaticResource(String)
protected long writeModificationTimestamp(URL resourceUrl, HttpServletRequest request, HttpServletResponse response)
resourceUrl
- the internal URL of the filerequest
- the request objectresponse
- the response objectprotected void writeCacheHeaders(String filenameWithPath, HttpServletResponse response)
filenameWithPath
- the name and path of the file being sentresponse
- the response objectprotected int getCacheTime(String filenameWithPath)
By default filenames containing ".nocache." return 0, filenames containing ".cache." return one year and all other files return 1 hour.
filenameWithPath
- the name of the file being sentprotected boolean browserHasNewestVersion(HttpServletRequest request, long resourceLastModifiedTimestamp)
request
- The HttpServletRequest from the browser.resourceLastModifiedTimestamp
- The timestamp when the resource was last modified. -1 if the
last modification time is unknown.Copyright © 2025. All rights reserved.