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.
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.
|
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.
|
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 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 © 2020. All rights reserved.