Package com.linecorp.armeria.server.file
Class HttpFileBuilder
- java.lang.Object
-
- com.linecorp.armeria.server.file.AbstractHttpFileBuilder
-
- com.linecorp.armeria.server.file.HttpFileBuilder
-
public abstract class HttpFileBuilder extends AbstractHttpFileBuilder
Builds anHttpFile
from a file, a classpath resource or anHttpData
.// Build from a file. HttpFile f = HttpFileBuilder.of(new File("/var/www/index.html")) .lastModified(false) .setHeader(HttpHeaderNames.CONTENT_LANGUAGE, "en-US") .build(); // Build from a classpath resource. HttpFile f = HttpFileBuilder.ofResource(MyClass.class.getClassLoader(), "/foo.txt.gz") .setHeader(HttpHeaderNames.CONTENT_ENCODING, "gzip") .build(); // Build from an HttpData. Can be downcast into AggregatedHttpFile. AggregatedHttpFile f = (AggregatedHttpFile) HttpFileBuilder.of(HttpData.ofUtf8("content"), System.currentTimeMillis()) .entityTag((pathOrUri, attrs) -> "myCustomEntityTag") .build();
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description HttpFileBuilder
addHeader(CharSequence name, Object value)
Adds the specified HTTP header.HttpFileBuilder
addHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
Adds the specified HTTP headers.HttpFileBuilder
autoDetectedContentType(boolean contentTypeAutoDetectionEnabled)
Sets whether to set the"content-type"
header automatically based on the extension of the file.abstract HttpFile
build()
Returns a newly createdHttpFile
with the properties configured so far.HttpFileBuilder
cacheControl(CacheControl cacheControl)
Sets the"cache-control"
header.HttpFileBuilder
cacheControl(CharSequence cacheControl)
Sets the"cache-control"
header.HttpFileBuilder
clock(Clock clock)
Sets theClock
that provides the current date and time.HttpFileBuilder
contentType(MediaType contentType)
Sets the"content-type"
header.HttpFileBuilder
contentType(CharSequence contentType)
Sets the"content-type"
header.HttpFileBuilder
date(boolean dateEnabled)
Sets whether to set the"date"
header automatically.HttpFileBuilder
entityTag(boolean enabled)
Sets whether to set the"etag"
header automatically based on the path and attributes of the file.HttpFileBuilder
entityTag(BiFunction<String,HttpFileAttributes,String> entityTagFunction)
Sets the function which generates the entity tag that's used for setting the"etag"
header automatically.HttpFileBuilder
lastModified(boolean lastModifiedEnabled)
Sets whether to set the"last-modified"
header automatically.static HttpFileBuilder
of(HttpData data)
static HttpFileBuilder
of(HttpData data, long lastModifiedMillis)
Returns a newHttpFileBuilder
that builds anAggregatedHttpFile
from the specifiedHttpData
andlastModifiedMillis
.static HttpFileBuilder
of(File file)
static HttpFileBuilder
of(Path path)
static HttpFileBuilder
ofResource(ClassLoader classLoader, String path)
Returns a newHttpFileBuilder
that builds anHttpFile
from the classpath resource at the specifiedpath
using the specifiedClassLoader
.static HttpFileBuilder
ofResource(String path)
Returns a newHttpFileBuilder
that builds anHttpFile
from the classpath resource at the specifiedpath
.HttpFileBuilder
setHeader(CharSequence name, Object value)
Sets the specified HTTP header.HttpFileBuilder
setHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
Sets the specified HTTP headers.-
Methods inherited from class com.linecorp.armeria.server.file.AbstractHttpFileBuilder
buildHeaders, clock, entityTagFunction, isContentTypeAutoDetectionEnabled, isDateEnabled, isLastModifiedEnabled
-
-
-
-
Method Detail
-
of
public static HttpFileBuilder of(File file)
-
of
public static HttpFileBuilder of(Path path)
-
of
public static HttpFileBuilder of(HttpData data)
Returns a newHttpFileBuilder
that builds anAggregatedHttpFile
from the specifiedHttpData
. The last modified date of the file is set to 'now'. Note that thebuild()
method of the returned builder will always return anAggregatedHttpFile
, which guarantees a safe downcast:AggregatedHttpFile f = (AggregatedHttpFile) HttpFileBuilder.of(HttpData.ofUtf8("foo")).build();
-
of
public static HttpFileBuilder of(HttpData data, long lastModifiedMillis)
Returns a newHttpFileBuilder
that builds anAggregatedHttpFile
from the specifiedHttpData
andlastModifiedMillis
. Note that thebuild()
method of the returned builder will always return anAggregatedHttpFile
, which guarantees a safe downcast:AggregatedHttpFile f = (AggregatedHttpFile) HttpFileBuilder.of(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
-
ofResource
public static HttpFileBuilder ofResource(String path)
Returns a newHttpFileBuilder
that builds anHttpFile
from the classpath resource at the specifiedpath
. This method is a shortcut ofHttpFileBuilder.ofResource(HttpFile.class.getClassLoader(), path)
.
-
ofResource
public static HttpFileBuilder ofResource(ClassLoader classLoader, String path)
Returns a newHttpFileBuilder
that builds anHttpFile
from the classpath resource at the specifiedpath
using the specifiedClassLoader
.
-
build
public abstract HttpFile build()
Returns a newly createdHttpFile
with the properties configured so far. If this builder was created withof(HttpData)
orof(HttpData, long)
, the returned instance will always be anAggregatedHttpFile
.
-
clock
public HttpFileBuilder clock(Clock clock)
Description copied from class:AbstractHttpFileBuilder
Sets theClock
that provides the current date and time. By default,Clock.systemUTC()
is used.- Overrides:
clock
in classAbstractHttpFileBuilder
-
date
public HttpFileBuilder date(boolean dateEnabled)
Description copied from class:AbstractHttpFileBuilder
Sets whether to set the"date"
header automatically. By default, the"date"
header is set automatically.- Overrides:
date
in classAbstractHttpFileBuilder
-
lastModified
public HttpFileBuilder lastModified(boolean lastModifiedEnabled)
Description copied from class:AbstractHttpFileBuilder
Sets whether to set the"last-modified"
header automatically. By default, the"last-modified"
is set automatically.- Overrides:
lastModified
in classAbstractHttpFileBuilder
-
autoDetectedContentType
public HttpFileBuilder autoDetectedContentType(boolean contentTypeAutoDetectionEnabled)
Description copied from class:AbstractHttpFileBuilder
Sets whether to set the"content-type"
header automatically based on the extension of the file. By default, the"content-type"
header is set automatically.- Overrides:
autoDetectedContentType
in classAbstractHttpFileBuilder
-
entityTag
public HttpFileBuilder entityTag(boolean enabled)
Description copied from class:AbstractHttpFileBuilder
Sets whether to set the"etag"
header automatically based on the path and attributes of the file. By default, the"etag"
header is set automatically. UseAbstractHttpFileBuilder.entityTag(BiFunction)
to customize how an entity tag is generated.- Overrides:
entityTag
in classAbstractHttpFileBuilder
-
entityTag
public HttpFileBuilder entityTag(BiFunction<String,HttpFileAttributes,String> entityTagFunction)
Description copied from class:AbstractHttpFileBuilder
Sets the function which generates the entity tag that's used for setting the"etag"
header automatically.- Overrides:
entityTag
in classAbstractHttpFileBuilder
- Parameters:
entityTagFunction
- the entity tag function that generates the entity tag, ornull
to disable setting the"etag"
header.
-
addHeader
public HttpFileBuilder addHeader(CharSequence name, Object value)
Description copied from class:AbstractHttpFileBuilder
Adds the specified HTTP header.- Overrides:
addHeader
in classAbstractHttpFileBuilder
-
addHeaders
public HttpFileBuilder addHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
Description copied from class:AbstractHttpFileBuilder
Adds the specified HTTP headers.- Overrides:
addHeaders
in classAbstractHttpFileBuilder
-
setHeader
public HttpFileBuilder setHeader(CharSequence name, Object value)
Description copied from class:AbstractHttpFileBuilder
Sets the specified HTTP header.- Overrides:
setHeader
in classAbstractHttpFileBuilder
-
setHeaders
public HttpFileBuilder setHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
Description copied from class:AbstractHttpFileBuilder
Sets the specified HTTP headers.- Overrides:
setHeaders
in classAbstractHttpFileBuilder
-
contentType
public HttpFileBuilder contentType(MediaType contentType)
Description copied from class:AbstractHttpFileBuilder
Sets the"content-type"
header. This method is a shortcut of:builder.autoDetectedContentType(false); builder.setHeader(HttpHeaderNames.CONTENT_TYPE, contentType);
- Overrides:
contentType
in classAbstractHttpFileBuilder
-
contentType
public HttpFileBuilder contentType(CharSequence contentType)
Description copied from class:AbstractHttpFileBuilder
Sets the"content-type"
header. This method is a shortcut of:builder.autoDetectedContentType(false); builder.setHeader(HttpHeaderNames.CONTENT_TYPE, contentType);
- Overrides:
contentType
in classAbstractHttpFileBuilder
-
cacheControl
public HttpFileBuilder cacheControl(CacheControl cacheControl)
Description copied from class:AbstractHttpFileBuilder
Sets the"cache-control"
header. This method is a shortcut of:builder.setHeader(HttpHeaderNames.CACHE_CONTROL, cacheControl);
- Overrides:
cacheControl
in classAbstractHttpFileBuilder
-
cacheControl
public HttpFileBuilder cacheControl(CharSequence cacheControl)
Description copied from class:AbstractHttpFileBuilder
Sets the"cache-control"
header. This method is a shortcut of:builder.setHeader(HttpHeaderNames.CACHE_CONTROL, cacheControl);
- Overrides:
cacheControl
in classAbstractHttpFileBuilder
-
-