public class RestUtils
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
RestUtils.Headers
Ambry specific HTTP headers.
|
static class |
RestUtils.InternalKeys
Ambry specific keys used internally in a
RestRequest . |
static class |
RestUtils.MultipartPost |
static class |
RestUtils.SubResource
Permitted sub-resources of a blob.
|
static class |
RestUtils.TrackingHeaders |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
BYTE_RANGE_UNITS |
static java.lang.String |
HTTP_DATE_FORMAT |
static java.lang.String |
JSON_CONTENT_TYPE |
static java.lang.String |
SIGNED_ID_PREFIX |
Constructor and Description |
---|
RestUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
accountAndContainerNamePreconditionCheck(RestRequest restRequest)
Check preconditions for request if the
restRequest contains the target account and container. |
static BlobProperties |
buildBlobProperties(java.util.Map<java.lang.String,java.lang.Object> args)
Builds
BlobProperties given the arguments associated with a request. |
static com.github.ambry.utils.Pair<java.lang.String,java.lang.Long> |
buildContentRangeAndLength(ByteRange range,
long blobSize)
Build the value for the Content-Range header that corresponds to the provided range and blob size.
|
static GetBlobOptions |
buildGetBlobOptions(java.util.Map<java.lang.String,java.lang.Object> args,
RestUtils.SubResource subResource,
GetOption getOption,
int blobSegmentIdx)
Build a
GetBlobOptions object from an argument map for a certain sub-resource. |
static java.util.Map<java.lang.String,java.lang.String> |
buildUserMetadata(byte[] userMetadata)
Gets deserialized metadata from the byte array if possible
|
static byte[] |
buildUserMetadata(java.util.Map<java.lang.String,java.lang.Object> args)
Builds user metadata given the arguments associated with a request.
|
static void |
ensureRequiredHeadersOrThrow(RestRequest restRequest,
java.util.Set<java.lang.String> requiredHeaders)
Ensures the required headers are present.
|
static Account |
getAccountFromArgs(java.util.Map<java.lang.String,java.lang.Object> args)
Extract the injected
Account from a map of arguments. |
static boolean |
getBooleanHeader(java.util.Map<java.lang.String,java.lang.Object> args,
java.lang.String header,
boolean required)
Gets the value of a header as a
boolean . |
static Container |
getContainerFromArgs(java.util.Map<java.lang.String,java.lang.Object> args)
Extract the injected
Container from a map of arguments. |
static GetOption |
getGetOption(RestRequest restRequest,
GetOption defaultGetOption)
Gets the
GetOption required by the request. |
static java.lang.String |
getHeader(java.util.Map<java.lang.String,?> args,
java.lang.String header,
boolean required)
Gets the value of the header
header in args . |
static java.lang.Long |
getLongHeader(java.util.Map<java.lang.String,?> args,
java.lang.String header,
boolean required)
Gets the value of a header as a
Long |
static <T extends java.lang.Number> |
getNumericalHeader(java.util.Map<java.lang.String,?> args,
java.lang.String header,
boolean required,
java.util.function.Function<java.lang.String,T> converter)
Gets the value of a header as a
Number , using the provided converter function to parse the string. |
static RequestPath |
getRequestPath(RestRequest restRequest)
Extract the
RequestPath object from RestRequest arguments. |
static java.lang.Long |
getTimeFromDateString(java.lang.String dateString)
Fetch time in ms for the
dateString passed in, since epoch |
static boolean |
isChunkUpload(java.util.Map<java.lang.String,java.lang.Object> args)
Determine if
RestUtils.Headers.CHUNK_UPLOAD is set in the request args. |
static boolean |
isPrivate(java.util.Map<java.lang.String,java.lang.Object> args)
Gets the isPrivate setting from the args.
|
static boolean |
setUserMetadataHeaders(byte[] userMetadata,
RestResponseChannel restResponseChannel)
Sets the user metadata in the headers of the response.
|
static long |
toSecondsPrecisionInMs(long ms)
Reduces the precision of a time in milliseconds to seconds precision.
|
public static final java.lang.String HTTP_DATE_FORMAT
public static final java.lang.String BYTE_RANGE_UNITS
public static final java.lang.String SIGNED_ID_PREFIX
public static final java.lang.String JSON_CONTENT_TYPE
public static BlobProperties buildBlobProperties(java.util.Map<java.lang.String,java.lang.Object> args) throws RestServiceException
BlobProperties
given the arguments associated with a request.args
- the arguments associated with the request. Cannot be null
.BlobProperties
extracted from the arguments.RestServiceException
- if required arguments aren't present or if they aren't in the format or number
expected.public static byte[] buildUserMetadata(java.util.Map<java.lang.String,java.lang.Object> args) throws RestServiceException
The following binary format will be used:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | | size | total | | | | | | | | | version | excluding | no of | key1 size | key1 | value1 size | value 1 | key2 size | ... | Crc | |(2 bytes)| ver & crc | entries | (4 bytes) |(key1 size| (4 bytes) |(value1 size| (4 bytes) | ... | (8 bytes) | | | (4 bytes) | (4 bytes)| | bytes) | | bytes) | | | | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - version - The version of the user metadata record size exluding ver & CRC - The size of the user metadata content excluding the version and the CRC total no of entries - Total number of entries in user metadata key1 size - Size of 1st key key1 - Content of key1 value1 size - Size of 1st value value1 - Content of value1 key2 size - Size of 2nd key crc - The crc of the user metadata record
args
- the arguments associated with the request.RestServiceException
- if usermetadata arguments have null values.public static java.util.Map<java.lang.String,java.lang.String> buildUserMetadata(byte[] userMetadata)
userMetadata
- the byte array which has the user metadatanull
if the userMetadata
cannot be
parsed in expected formatpublic static GetBlobOptions buildGetBlobOptions(java.util.Map<java.lang.String,java.lang.Object> args, RestUtils.SubResource subResource, GetOption getOption, int blobSegmentIdx) throws RestServiceException
GetBlobOptions
object from an argument map for a certain sub-resource.args
- the arguments associated with the request. This is typically a map of header names and query string
arguments to values.subResource
- the RestUtils.SubResource
for the request, or null
if no sub-resource is requested.getOption
- the GetOption
required.blobSegmentIdx
- index of blob segment one wishes to GET, GetBlobOptions.NO_BLOB_SEGMENT_IDX_SPECIFIED
if not usedGetBlobOptions
object.RestServiceException
- if the GetBlobOptions
could not be constructed.public static com.github.ambry.utils.Pair<java.lang.String,java.lang.Long> buildContentRangeAndLength(ByteRange range, long blobSize) throws RestServiceException
{a}-{b}/{c}
, where {a}
is the inclusive
start byte offset of the returned range, {b}
is the inclusive end byte offset of the returned range, and
{c}
is the total size of the blob in bytes. This function also generates the range length in bytes.range
- a ByteRange
used to generate the Content-Range header.blobSize
- the total size of the associated blob in bytes.Pair
containing the content range header value and the content length in bytes.RestServiceException
public static RequestPath getRequestPath(RestRequest restRequest)
RequestPath
object from RestRequest
arguments.restRequest
- the RestRequest
.RequestPath
public static java.lang.Long getTimeFromDateString(java.lang.String dateString)
dateString
passed in, since epochdateString
- the String representation of the date that needs to be parseddateString
is not in the expected format or could not be parsedpublic static long toSecondsPrecisionInMs(long ms)
ms
- time that needs to be parsedpublic static GetOption getGetOption(RestRequest restRequest, GetOption defaultGetOption) throws RestServiceException
GetOption
required by the request.restRequest
- the representation of the request.defaultGetOption
- the GetOption
to use if the restRequest
doesn't have one. Can be
null
.GetOption
. Defaults to GetOption.None
.RestServiceException
- if the RestUtils.Headers.GET_OPTION
is present but not recognized.public static boolean isPrivate(java.util.Map<java.lang.String,java.lang.Object> args) throws RestServiceException
args
- The args where to include the isPrivate setting.RestServiceException
- if exception occurs during parsing the arg.public static boolean isChunkUpload(java.util.Map<java.lang.String,java.lang.Object> args) throws RestServiceException
RestUtils.Headers.CHUNK_UPLOAD
is set in the request args.args
- The request arguments.true
if RestUtils.Headers.CHUNK_UPLOAD
is set.RestServiceException
- if exception occurs during parsing the arg.public static void ensureRequiredHeadersOrThrow(RestRequest restRequest, java.util.Set<java.lang.String> requiredHeaders) throws RestServiceException
restRequest
- The RestRequest
to ensure header presence. Cannot be null
.requiredHeaders
- A set of headers to check presence. Cannot be null
.RestServiceException
- if any of the headers is missing.public static java.lang.String getHeader(java.util.Map<java.lang.String,?> args, java.lang.String header, boolean required) throws RestServiceException
header
in args
.args
- a map of arguments to be used to look for header
.header
- the name of the header.required
- if true
, IllegalArgumentException
will be thrown if header
is not present
in args
.header
in args
if it exists. If it does not exist and required
is
false
, then returns null.RestServiceException
- if required
is true
and header
does not exist in
args
or if there is more than one value for header
in
args
.public static java.lang.Long getLongHeader(java.util.Map<java.lang.String,?> args, java.lang.String header, boolean required) throws RestServiceException
Long
args
- a map of arguments to be used to look for header
.header
- the name of the header.required
- if true
, RestServiceException
will be thrown if header
is not present
in args
.header
in args
if it exists. If it does not exist and required
is
false
, then returns null.RestServiceException
- same as cases of getHeader(Map, String, boolean)
and if the value cannot be
converted to a Long
.public static <T extends java.lang.Number> T getNumericalHeader(java.util.Map<java.lang.String,?> args, java.lang.String header, boolean required, java.util.function.Function<java.lang.String,T> converter) throws RestServiceException
Number
, using the provided converter function to parse the string.args
- a map of arguments to be used to look for header
.header
- the name of the header.required
- if true
, RestServiceException
will be thrown if header
is not present
in args
.converter
- a function to convert from a String
to the desired numerical type. This should throw
NumberFormatException
if the argument is not a valid number.header
in args
if it exists. If it does not exist and required
is
false
, then returns null.RestServiceException
- same as cases of getHeader(Map, String, boolean)
and if the value cannot be
converted to a Long
.public static boolean getBooleanHeader(java.util.Map<java.lang.String,java.lang.Object> args, java.lang.String header, boolean required) throws RestServiceException
boolean
.args
- a map of arguments to be used to look for header
.header
- the name of the header.required
- if true
, RestServiceException
will be thrown if header
is not present
in args
.true
if the header's value is "true"
(case-insensitive), or false
if the header's
value is "false
(case-insensitive) or the header is not present and required
is
false
.RestServiceException
- same as cases of getHeader(Map, String, boolean)
and if the value cannot be
converted to a boolean
.public static Account getAccountFromArgs(java.util.Map<java.lang.String,java.lang.Object> args) throws RestServiceException
Account
from a map of arguments.args
- a map of arguments that possibly contains the injected Account
and Container
.Account
RestServiceException
public static Container getContainerFromArgs(java.util.Map<java.lang.String,java.lang.Object> args) throws RestServiceException
Container
from a map of arguments.args
- a map of arguments that possibly contains the injected Account
and Container
.Container
RestServiceException
public static void accountAndContainerNamePreconditionCheck(RestRequest restRequest) throws RestServiceException
restRequest
contains the target account and container.restRequest
- the RestRequest
that contains the Account
and Container
details.RestServiceException
- if preconditions check failed.public static boolean setUserMetadataHeaders(byte[] userMetadata, RestResponseChannel restResponseChannel) throws RestServiceException
userMetadata
- the user metadata that needs to be sent.restResponseChannel
- the RestResponseChannel
that is used for sending the response.true
if the user metadata was successfully deserialized into headers, false
if not.RestServiceException
- if there are any problems setting the header.