Package com.linecorp.armeria.server.file
Class AbstractHttpFile
java.lang.Object
com.linecorp.armeria.server.file.AbstractHttpFile
- All Implemented Interfaces:
HttpFile
- Direct Known Subclasses:
StreamingHttpFile
A skeletal
HttpFile
implementation.-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractHttpFile
(@Nullable MediaType contentType, Clock clock, boolean dateEnabled, boolean lastModifiedEnabled, @Nullable BiFunction<String, HttpFileAttributes, String> entityTagFunction, HttpHeaders additionalHeaders) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected final HttpHeaders
Returns the immutable additionalHttpHeaders
which will be set when building anHttpResponse
.protected final Clock
clock()
Returns theClock
which provides the current date and time.Returns theMediaType
of the file, which will be used for setting the"content-type"
header.protected abstract @Nullable HttpResponse
doRead
(ResponseHeaders headers, long length, Executor fileReadExecutor, ByteBufAllocator alloc) Returns a newHttpResponse
which streams the content of the file which follows the specifiedResponseHeaders
.Generates an entity tag of the file with the given attributes using theentityTagFunction
which was specified with the constructor.protected final boolean
Returns whether to add the"date"
header automatically.protected final boolean
Returns whether to add the"last-modified"
header automatically.protected abstract String
Returns theString
representation of the file path or URI, which is given to theentityTagFunction
specified with the constructor.final CompletableFuture<HttpResponse>
read
(Executor fileReadExecutor, ByteBufAllocator alloc) Starts to stream this file into the returnedHttpResponse
.protected final @Nullable ResponseHeaders
Generates theResponseHeaders
from the specifiedHttpFileAttributes
.readHeaders
(Executor fileReadExecutor) Reads the attributes of this file asResponseHeaders
, which could be useful for building a response for aHEAD
request.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.linecorp.armeria.server.file.HttpFile
aggregate, aggregateWithPooledObjects, readAttributes
-
Constructor Details
-
AbstractHttpFile
protected AbstractHttpFile(@Nullable @Nullable MediaType contentType, Clock clock, boolean dateEnabled, boolean lastModifiedEnabled, @Nullable @Nullable BiFunction<String, HttpFileAttributes, String> entityTagFunction, HttpHeaders additionalHeaders) Creates a new instance.- Parameters:
contentType
- theMediaType
of the file which will be used as the"content-type"
header value.null
to disable setting the"content-type"
header.clock
- theClock
which provides the current date and timedateEnabled
- whether to set the"date"
header automaticallylastModifiedEnabled
- whether to add the"last-modified"
header automaticallyentityTagFunction
- theBiFunction
that generates an entity tag from the file's attributes.null
to disable setting the"etag"
header.additionalHeaders
- the additional headers to set
-
-
Method Details
-
contentType
Returns theMediaType
of the file, which will be used for setting the"content-type"
header.- Returns:
- the
MediaType
of the file, ornull
if the"content-type"
header will not be set automatically.
-
clock
Returns theClock
which provides the current date and time. -
pathOrUri
Returns theString
representation of the file path or URI, which is given to theentityTagFunction
specified with the constructor. -
isDateEnabled
protected final boolean isDateEnabled()Returns whether to add the"date"
header automatically. -
isLastModifiedEnabled
protected final boolean isLastModifiedEnabled()Returns whether to add the"last-modified"
header automatically. -
additionalHeaders
Returns the immutable additionalHttpHeaders
which will be set when building anHttpResponse
. -
generateEntityTag
Generates an entity tag of the file with the given attributes using theentityTagFunction
which was specified with the constructor.- Returns:
- the entity tag or
null
ifentityTagFunction
isnull
.
-
readHeaders
Description copied from interface:HttpFile
Reads the attributes of this file asResponseHeaders
, which could be useful for building a response for aHEAD
request.- Specified by:
readHeaders
in interfaceHttpFile
- Parameters:
fileReadExecutor
- theExecutor
which will perform the read operations against the file- Returns:
- the
CompletableFuture
that will be completed with the headers. It will be completed withnull
if the file does not exist.
-
readHeaders
@Nullable protected final @Nullable ResponseHeaders readHeaders(@Nullable @Nullable HttpFileAttributes attrs) Generates theResponseHeaders
from the specifiedHttpFileAttributes
.- Returns:
- the
ResponseHeaders
, ornull
ifattrs
isnull
.
-
read
public final CompletableFuture<HttpResponse> read(Executor fileReadExecutor, ByteBufAllocator alloc) Description copied from interface:HttpFile
Starts to stream this file into the returnedHttpResponse
.- Specified by:
read
in interfaceHttpFile
- 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
CompletableFuture
that will be completed with the response. It will be completed withnull
if the file does not exist.
-
doRead
@Nullable protected abstract @Nullable HttpResponse doRead(ResponseHeaders headers, long length, Executor fileReadExecutor, ByteBufAllocator alloc) throws IOException Returns a newHttpResponse
which streams the content of the file which follows the specifiedResponseHeaders
.- Parameters:
headers
- theResponseHeaders
length
- the content length. The returnedHttpResponse
must stream only as many bytes as this value.fileReadExecutor
- theExecutor
which should be used for performing a blocking file I/Oalloc
- theByteBufAllocator
which should be used for allocating an input buffer- Returns:
- the
HttpResponse
, ornull
if the file does not exist. - Throws:
IOException
- if failed to open the file. Note that an I/O error which occurred during content streaming will be notified via the returnedHttpResponse
's error notification mechanism.
-
asService
Description copied from interface:HttpFile
-