public class StaticFileConf extends Object
A StaticFileConf
contains file metadata like fileSize
,
and config methods like exclude(boolean)
.
It is created with default config values, then
passed to StaticHandler's
confMod to be modified.
Example usage:
new StaticHandler("/uri", "/dir", conf-> { if(conf.fileSize<5000) conf.cache(true); // cache the file in memory if it's small enough conf.header("Cache-Control", "public"); });
Modifier and Type | Field and Description |
---|---|
Path |
filePath
The path of the file.
|
long |
fileSize
The size of the file.
|
Instant |
fileCreationTime
When the file was created.
|
Instant |
fileLastModified
When the file was last modified.
|
HeaderMap |
headers
Additional response headers for this file.
|
Constructor and Description |
---|
StaticFileConf(Path filePath)
Create a StaticFileConf with default values.
|
Instance Methods | |
---|---|
StaticFileConf |
exclude(boolean exclude)
Whether to exclude this file.
|
StaticFileConf |
gzip(boolean gzip)
Whether this file should be compressed by gzip.
|
StaticFileConf |
cache(boolean cache)
Whether to cache the file content in memory.
|
StaticFileConf |
isIndexFile(boolean isIndexFile)
Whether this is the index file for the parent directory.
|
StaticFileConf |
expires(Instant expiresAbsolute,
Duration expiresRelative)
The expiration time of this file.
|
StaticFileConf |
contentType(ContentType contentType)
The content type of this file.
|
StaticFileConf |
etag(String etag)
The ETag of this file.
|
StaticFileConf |
header(String name,
String value)
Add a
response header . |
boolean |
get_exclude() |
boolean |
get_gzip() |
boolean |
get_cache() |
boolean |
get_isIndexFile() |
Duration |
get_expiresRelative() |
Instant |
get_expiresAbsolute() |
ContentType |
get_contentType() |
String |
get_etag() |
public final Path filePath
public final long fileSize
public final Instant fileCreationTime
public final Instant fileLastModified
public final HeaderMap headers
This map is initially empty; confMod
can populate it,
e.g. with Cache-Control, Content-Language
.
header(String, String)
public StaticFileConf exclude(boolean exclude)
default: false
If excluded, this file will not be served by the StaticHandler; if a request URI maps to this file, a 404 response will be generated.
public StaticFileConf gzip(boolean gzip)
default: true iff fileSize>1024 and the default contentType is text/* or application/javascript
Do not pre-gzip the file; just store the original file under the directory, StaticHandler will take care of caching the compressed file on disk or in memory.
public StaticFileConf cache(boolean cache)
default: false
public StaticFileConf isIndexFile(boolean isIndexFile)
default: true iff filePath.endsWith("index.html")
If a request URI maps to a directory, the index file (if there is one) will be served.
If a directory contains more than one index files, one of them is used; but it is uncertain which one is picked.
public StaticFileConf expires(Instant expiresAbsolute, Duration expiresRelative) throws IllegalArgumentException
default: expiresAbsolute=null, expiresRelative=Duration.ofSeconds(60)
expiresAbsolute
can be a time in the past;
expiresRelative
can be a negative duration.
The Expires
response header
can be specified either absolutely or relatively.
If relatively, the header value will be (responseTime+expiresRelative)
.
If both expiresAbsolute
and expiresRelative
are null,
there will be no Expires
header.
If both are non-null, this method throws IllegalArgumentException
.
Note: however, if the request URI is tagged,
the Expires
response header could be set to a distant future,
ignoring the settings here.
IllegalArgumentException
public StaticFileConf contentType(ContentType contentType)
default: FileSuffixToContentType.getGlobalInstance().find(filePath.toString())
null
is allowed, if the content type is unknown. The default value could be null.
FileSuffixToContentType
public StaticFileConf etag(String etag) throws IllegalArgumentException
default: a string representing fileLastModified in nano-second precision,
e.g. "t-53319ee8-623a7c0"
See HttpEntity.etag()
for requirements for this ETag.
If a response for this file uses "Content-Encoding: gzip"
,
its ETag will be this ETag appended with ".gzip"
.
IllegalArgumentException
public StaticFileConf header(String name, String value) throws IllegalArgumentException
response header
.
For example: conf.header("Cache-Control", "public");
IllegalArgumentException
public boolean get_exclude()
public boolean get_gzip()
public boolean get_cache()
public boolean get_isIndexFile()
public Duration get_expiresRelative()
public Instant get_expiresAbsolute()
public ContentType get_contentType()
public String get_etag()