Top Description Fields Constructors Methods
org.eclipse.jetty.http

public final Class EtagUtils

extends Object
Class Inheritance
Imports
java.io.IOException, java.nio.file.Files, .Path, java.time.Instant, java.util.Base64, .Objects, org.eclipse.jetty.util.StringUtil, org.eclipse.jetty.util.resource.Resource

Utility classes for Entity Tag behaviors as outlined in RFC9110 : Section 8.8.3 - Entity Tag

Field Summary

Modifier and TypeField and Description
public static final String
ETAG_SEPARATOR

The separator within an etag used to indicate a compressed variant.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

the weak etag
computeWeakEtag
(Resource
the resource to calculate etag from
resource
)

Compute the weak etag for the provided Resource reference.

public static String

Returns:

the weak etag
computeWeakEtag
(Resource
the resource to calculate etag from
resource
,
String
the suffix for the etag
etagSuffix
)

Compute the weak etag for the provided Resource reference.

public static String

Returns:

the weak etag
computeWeakEtag
(Path
the path to calculate from
path
)

Compute the weak etag for the provided Path reference.

public static String

Returns:

the weak etag
computeWeakEtag
(Path
the path to calculate from
path
,
String
the optional suffix for the ETag
suffix
)

Compute the weak etag for the provided Path reference.

private static String

Returns:

the calculated etag for the resource
computeWeakEtag
(String
the name of the resource
name
,
Instant
the last modified time of the resource
lastModified
,
long
the size of the resource
size
,
String
the optional etag suffix
etagSuffix
)

Main algorithm of how Jetty builds a unique Etag.

public static HttpField

Returns:

the field if possible to create etag, or null if not possible.
createWeakEtagField
(Resource
the resource to represent with an Etag.
resource
)

Create a new HttpField HttpHeader#ETAG field suitable to represent the provided Resource.

public static HttpField

Returns:

the field if possible to create etag, or null if not possible.
createWeakEtagField
(Resource
the resource to represent with an Etag.
resource
,
String
the suffix for the etag
etagSuffix
)

Create a new HttpField HttpHeader#ETAG field suitable to represent the provided Resource.

public static boolean

Returns:

true if strong (not weak).
isStrong
(String
the etag to test
etag
)

Test if the given Etag is considered strong (not weak).

public static boolean

Returns:

true if weak.
isWeak
(String
the etag to test
etag
)

Test if the given Etag is considered weak.

private static byte[]
longToBytes(long value)

public static boolean

Returns:

True if the tags are equal.
matches
(String
An etag without a compression suffix
etag
,
String
An etag optionally with a compression suffix.
etagWithOptionalSuffix
)

Test if etag matches against an etagWithOptionalSuffix.

public static String

Returns:

the new etag,
rewriteWithSuffix
(String
the original etag
etag
,
String
the new suffix to add or change (if a preexisting suffix exists)
newSuffix
)

Rewrite etag with a new suffix, satisfying quoting rules, and preserving optional weak flag.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ETAG_SEPARATORback to summary
public static final String ETAG_SEPARATOR

The separator within an etag used to indicate a compressed variant.

Default separator is --

Adding a suffix of gzip to an etag of W/"28c772d6" will result in W/"28c772d6--gzip"

The separator may be changed by the org.eclipse.jetty.http.EtagUtil.ETAG_SEPARATOR System property. If changed, it should be changed to a string that will not be found in a normal etag or at least is very unlikely to be a substring of a normal etag.

Constructor Detail

EtagUtilsback to summary
private EtagUtils()

Method Detail

computeWeakEtagback to summary
public static String computeWeakEtag(Resource resource)

Compute the weak etag for the provided Resource reference.

Will use the Resource#getPath() if the resource provides it, otherwise it will use the Resource provided details from Resource#getName(), Resource#lastModified(), and Resource#length() to build the ETag.

Parameters
resource:Resource

the resource to calculate etag from

Returns:String

the weak etag

computeWeakEtagback to summary
public static String computeWeakEtag(Resource resource, String etagSuffix)

Compute the weak etag for the provided Resource reference.

Will use the Resource#getPath() if the resource provides it, otherwise it will use the Resource provided details from Resource#getName(), Resource#lastModified(), and Resource#length() to build the ETag.

Parameters
resource:Resource

the resource to calculate etag from

etagSuffix:String

the suffix for the etag

Returns:String

the weak etag

computeWeakEtagback to summary
public static String computeWeakEtag(Path path)

Compute the weak etag for the provided Path reference.

This supports relative path references as well. Which can be useful to establish a reliable etag if the base changes.

Parameters
path:Path

the path to calculate from

Returns:String

the weak etag

computeWeakEtagback to summary
public static String computeWeakEtag(Path path, String suffix)

Compute the weak etag for the provided Path reference.

Parameters
path:Path

the path to calculate from

suffix:String

the optional suffix for the ETag

Returns:String

the weak etag

computeWeakEtagback to summary
private static String computeWeakEtag(String name, Instant lastModified, long size, String etagSuffix)

Main algorithm of how Jetty builds a unique Etag.

Parameters
name:String

the name of the resource

lastModified:Instant

the last modified time of the resource

size:long

the size of the resource

etagSuffix:String

the optional etag suffix

Returns:String

the calculated etag for the resource

createWeakEtagFieldback to summary
public static HttpField createWeakEtagField(Resource resource)

Create a new HttpField HttpHeader#ETAG field suitable to represent the provided Resource.

Parameters
resource:Resource

the resource to represent with an Etag.

Returns:HttpField

the field if possible to create etag, or null if not possible.

createWeakEtagFieldback to summary
public static HttpField createWeakEtagField(Resource resource, String etagSuffix)

Create a new HttpField HttpHeader#ETAG field suitable to represent the provided Resource.

Parameters
resource:Resource

the resource to represent with an Etag.

etagSuffix:String

the suffix for the etag

Returns:HttpField

the field if possible to create etag, or null if not possible.

isStrongback to summary
public static boolean isStrong(String etag)

Test if the given Etag is considered strong (not weak).

Parameters
etag:String

the etag to test

Returns:boolean

true if strong (not weak).

isWeakback to summary
public static boolean isWeak(String etag)

Test if the given Etag is considered weak.

Parameters
etag:String

the etag to test

Returns:boolean

true if weak.

longToBytesback to summary
private static byte[] longToBytes(long value)
matchesback to summary
public static boolean matches(String etag, String etagWithOptionalSuffix)

Test if etag matches against an etagWithOptionalSuffix.

Parameters
etag:String

An etag without a compression suffix

etagWithOptionalSuffix:String

An etag optionally with a compression suffix.

Returns:boolean

True if the tags are equal.

See Also
RFC9110: Section 8.8.3.2 - Etag Comparison
rewriteWithSuffixback to summary
public static String rewriteWithSuffix(String etag, String newSuffix)

Rewrite etag with a new suffix, satisfying quoting rules, and preserving optional weak flag.

Parameters
etag:String

the original etag

newSuffix:String

the new suffix to add or change (if a preexisting suffix exists)

Returns:String

the new etag,