Class AbstractHttpFile

java.lang.Object
com.linecorp.armeria.server.file.AbstractHttpFile
All Implemented Interfaces:
HttpFile
Direct Known Subclasses:
StreamingHttpFile

public abstract class AbstractHttpFile extends Object implements HttpFile
A skeletal HttpFile implementation.
  • 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 - the MediaType of the file which will be used as the "content-type" header value. null to disable setting the "content-type" header.
      clock - the Clock which provides the current date and time
      dateEnabled - whether to set the "date" header automatically
      lastModifiedEnabled - whether to add the "last-modified" header automatically
      entityTagFunction - the BiFunction 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

      @Nullable protected final @Nullable MediaType contentType()
      Returns the MediaType of the file, which will be used for setting the "content-type" header.
      Returns:
      the MediaType of the file, or null if the "content-type" header will not be set automatically.
    • clock

      protected final Clock clock()
      Returns the Clock which provides the current date and time.
    • pathOrUri

      protected abstract String pathOrUri()
      Returns the String representation of the file path or URI, which is given to the entityTagFunction 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

      protected final HttpHeaders additionalHeaders()
      Returns the immutable additional HttpHeaders which will be set when building an HttpResponse.
    • generateEntityTag

      @Nullable protected final @Nullable String generateEntityTag(HttpFileAttributes attrs)
      Generates an entity tag of the file with the given attributes using the entityTagFunction which was specified with the constructor.
      Returns:
      the entity tag or null if entityTagFunction is null.
    • readHeaders

      public CompletableFuture<ResponseHeaders> readHeaders(Executor fileReadExecutor)
      Description copied from interface: HttpFile
      Reads the attributes of this file as ResponseHeaders, which could be useful for building a response for a HEAD request.
      Specified by:
      readHeaders in interface HttpFile
      Parameters:
      fileReadExecutor - the Executor which will perform the read operations against the file
      Returns:
      the CompletableFuture that will be completed with the headers. It will be completed with null if the file does not exist.
    • readHeaders

      Generates the ResponseHeaders from the specified HttpFileAttributes.
      Returns:
      the ResponseHeaders, or null if attrs is null.
    • read

      public final CompletableFuture<HttpResponse> read(Executor fileReadExecutor, ByteBufAllocator alloc)
      Description copied from interface: HttpFile
      Starts to stream this file into the returned HttpResponse.
      Specified by:
      read in interface HttpFile
      Parameters:
      fileReadExecutor - the Executor which will perform the read operations against the file
      alloc - the ByteBufAllocator 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 with null 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 new HttpResponse which streams the content of the file which follows the specified ResponseHeaders.
      Parameters:
      headers - the ResponseHeaders
      length - the content length. The returned HttpResponse must stream only as many bytes as this value.
      fileReadExecutor - the Executor which should be used for performing a blocking file I/O
      alloc - the ByteBufAllocator which should be used for allocating an input buffer
      Returns:
      the HttpResponse, or null 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 returned HttpResponse's error notification mechanism.
    • asService

      public HttpService asService()
      Description copied from interface: HttpFile
      Returns an HttpService which serves the file for HEAD and GET requests.
      Specified by:
      asService in interface HttpFile