Package com.linecorp.armeria.server.file
Interface HttpFile
- All Known Subinterfaces:
AggregatedHttpFile
- All Known Implementing Classes:
AbstractHttpFile
,StreamingHttpFile
public interface HttpFile
A file-like HTTP resource which yields an
HttpResponse
.
HttpFile faviconFile = HttpFile.of(new File("/var/www/favicon.ico"));
ServerBuilder builder = Server.builder();
builder.service("/favicon.ico", faviconFile.asService());
Server server = builder.build();
- See Also:
HttpFileBuilder
-
Method Summary
Modifier and Type Method Description CompletableFuture<AggregatedHttpFile>
aggregate(Executor fileReadExecutor)
Converts this file into anAggregatedHttpFile
.CompletableFuture<AggregatedHttpFile>
aggregateWithPooledObjects(Executor fileReadExecutor, ByteBufAllocator alloc)
Converts this file into anAggregatedHttpFile
.HttpService
asService()
static HttpFileBuilder
builder(HttpData data)
static HttpFileBuilder
builder(HttpData data, long lastModifiedMillis)
Returns a newHttpFileBuilder
that builds anAggregatedHttpFile
from the specifiedHttpData
andlastModifiedMillis
.static HttpFileBuilder
builder(File file)
static HttpFileBuilder
builder(ClassLoader classLoader, String path)
Returns a newHttpFileBuilder
that builds anHttpFile
from the classpath resource at the specifiedpath
using the specifiedClassLoader
.static HttpFileBuilder
builder(Path path)
static AggregatedHttpFile
nonExistent()
Returns anAggregatedHttpFile
which represents a non-existent file.static AggregatedHttpFile
of(HttpData data)
Creates a newAggregatedHttpFile
which streams the specifiedHttpData
.static AggregatedHttpFile
of(HttpData data, long lastModifiedMillis)
Creates a newAggregatedHttpFile
which streams the specifiedHttpData
with the specifiedlastModifiedMillis
.static HttpFile
of(File file)
static HttpFile
of(ClassLoader classLoader, String path)
Creates a newHttpFile
which streams the resource at the specifiedpath
, loaded by the specifiedClassLoader
.static HttpFile
of(Path path)
static HttpFile
ofCached(HttpFile file, int maxCachingLength)
HttpResponse
read(Executor fileReadExecutor, ByteBufAllocator alloc)
Starts to stream this file into the returnedHttpResponse
.HttpFileAttributes
readAttributes()
Retrieves the attributes of this file.ResponseHeaders
readHeaders()
Reads the attributes of this file asResponseHeaders
, which could be useful for building a response for aHEAD
request.
-
Method Details
-
of
-
of
-
of
Creates a newHttpFile
which streams the resource at the specifiedpath
, loaded by the specifiedClassLoader
.- Parameters:
classLoader
- theClassLoader
which will load the resource at thepath
path
- the path to the resource
-
of
Creates a newAggregatedHttpFile
which streams the specifiedHttpData
. This method is a shortcut forHttpFile.of(data, System.currentTimeMillis()
. -
of
Creates a newAggregatedHttpFile
which streams the specifiedHttpData
with the specifiedlastModifiedMillis
.- Parameters:
data
- the data that provides the content of an HTTP responselastModifiedMillis
- when thedata
has been last modified, represented as the number of millisecond since the epoch
-
ofCached
-
nonExistent
Returns anAggregatedHttpFile
which represents a non-existent file. -
builder
-
builder
-
builder
Returns a newHttpFileBuilder
that builds anAggregatedHttpFile
from the specifiedHttpData
. The last modified date of the file is set to 'now'. Note that theHttpFileBuilder.build()
method of the returned builder will always return anAggregatedHttpFile
, which guarantees a safe downcast:AggregatedHttpFile f = (AggregatedHttpFile) HttpFile.builder(HttpData.ofUtf8("foo")).build();
-
builder
Returns a newHttpFileBuilder
that builds anAggregatedHttpFile
from the specifiedHttpData
andlastModifiedMillis
. Note that theHttpFileBuilder.build()
method of the returned builder will always return anAggregatedHttpFile
, which guarantees a safe downcast:AggregatedHttpFile f = (AggregatedHttpFile) HttpFile.builder(HttpData.ofUtf8("foo"), 1546923055020) .build();
- Parameters:
data
- the content of the filelastModifiedMillis
- the last modified time represented as the number of milliseconds since the epoch
-
builder
Returns a newHttpFileBuilder
that builds anHttpFile
from the classpath resource at the specifiedpath
using the specifiedClassLoader
. -
readAttributes
Retrieves the attributes of this file.- Returns:
- the attributes of this file, or
null
if the file does not exist. - Throws:
IOException
- if failed to retrieve the attributes of this file.
-
readHeaders
Reads the attributes of this file asResponseHeaders
, which could be useful for building a response for aHEAD
request.- Returns:
- the headers, or
null
if the file does not exist. - Throws:
IOException
- if failed to retrieve the attributes of this file.
-
read
Starts to stream this file into the returnedHttpResponse
.- Parameters:
fileReadExecutor
- theExecutor
which will perform the read operations against the filealloc
- theByteBufAllocator
which will allocate the buffers that hold the content of the file- Returns:
- the response, or
null
if the file does not exist.
-
aggregate
Converts this file into anAggregatedHttpFile
.- Parameters:
fileReadExecutor
- theExecutor
which will perform the read operations against the file- Returns:
- a
CompletableFuture
which will complete when the aggregation process is finished, or aCompletableFuture
successfully completed withthis
, if this file is already anAggregatedHttpFile
.
-
aggregateWithPooledObjects
CompletableFuture<AggregatedHttpFile> aggregateWithPooledObjects(Executor fileReadExecutor, ByteBufAllocator alloc)Converts this file into anAggregatedHttpFile
.AggregatedHttpFile.content()
will return a pooled object, and the caller must ensure to release it. If you don't know what this means, useaggregate(Executor)
.- Parameters:
fileReadExecutor
- theExecutor
which will perform the read operations against the filealloc
- theByteBufAllocator
which will allocate the content buffer- Returns:
- a
CompletableFuture
which will complete when the aggregation process is finished, or aCompletableFuture
successfully completed withthis
, if this file is already anAggregatedHttpFile
.
-
asService
HttpService asService()
-