Class Request
Instances can be acquired via the withRawUrl(HttpMethod, String) (e.g. provided by clients on a "raw" HTTP/1.1 request line, un-decoded) and withPath(HttpMethod, String) (e.g. manually-constructed during integration testing, understood to be already-decoded) builder factory methods.
Any necessary decoding (path, URL parameter, Content-Type: application/x-www-form-urlencoded, etc.) will be automatically performed. Unless otherwise indicated, all accessor methods will return decoded data.
Detailed documentation available at https://www.soklet.com/docs/request-handling.
- Author:
- Mark Allen
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic final classBuilder used to construct instances ofRequestviawithPath(HttpMethod, String).static final classBuilder used to construct instances ofRequestviawithRawUrl(HttpMethod, String). -
Method Summary
Modifier and TypeMethodDescriptioncopy()Vends a mutable copier seeded with this instance's data, suitable for building new instances.booleanOptional<byte[]> getBody()The raw bytes of the request body - callers should not modify this array; it is not defensively copied for performance reasons.Convenience method that provides thegetBody()bytes as aStringencoded using the client-specified character set pergetCharset().The request's character encoding, as specified by the client in theContent-Typeheader value.TheContent-Typeheader value, as specified by the client.Convenience method to access a cookie's value when at most one is expected for the givenname.The cookies provided by the client for this request.getCors()Non-preflight CORS request data.CORS preflight-related request data.getFormParameter(String name) Convenience method to access a decoded form parameter's value when at most one is expected for the givenname.The decoded HTMLapplication/x-www-form-urlencodedform parameters provided by the client for this request.Convenience method to access a header's value when at most one is expected for the givenname.The headers provided by the client for this request.The HTTP method for this request.getId()An application-specific identifier for this request.Locale.LanguageRangeinformation for this request as specified byAccept-Languageheader value[s].Locale information for this request as specified byAccept-Languageheader value[s] and ordered by weight as defined by RFC 7231, Section 5.3.5.getMultipartField(String name) Convenience method to access a decoded multipart field when at most one is expected for the givenname.The decoded HTMLmultipart/form-datafields provided by the client for this request.getPath()The percent-decoded path component of this request (no query string).getQueryParameter(String name) Convenience method to access a decoded query parameter's value when at most one is expected for the givenname.The decoded query parameters provided by the client for this request.The raw (un-decoded) path component of this request exactly as the client specified.The raw (un-decoded) path and query components of this request exactly as the client specified.The raw (un-decoded) query component of this request exactly as the client specified.Convenience method to acquire aResourcePathrepresentation ofgetPath().inthashCode()Was this request too large for the server to handle?Is this a request withContent-Typeofmultipart/form-data?toString()static Request.PathBuilderwithPath(HttpMethod httpMethod, String path) Acquires a builder forRequestinstances from already-decoded path and query components - useful for manual construction, e.g. integration tests.static Request.RawBuilderwithRawUrl(HttpMethod httpMethod, String rawUrl) Acquires a builder forRequestinstances from the URL provided by clients on a "raw" HTTP/1.1 request line.
-
Method Details
-
withRawUrl
@Nonnull public static Request.RawBuilder withRawUrl(@Nonnull HttpMethod httpMethod, @Nonnull String rawUrl) Acquires a builder forRequestinstances from the URL provided by clients on a "raw" HTTP/1.1 request line.The provided
rawUrlmust be un-decoded and in either "path-and-query" form (i.e. starts with a/character) or an absolute URL (i.e. starts withhttp://orhttps://). It might include un-decoded query parameters, e.g.https://www.example.com/one?two=thr%20eeor/one?two=thr%20ee. An exception to this rule isOPTIONS *requests, where the URL is the*"splat" symbol.Paths will be percent-decoded.
Query parameters are parsed and decoded using RFC 3986 semantics - see
QueryFormat.RFC_3986_STRICT.Request body form parameters with
Content-Type: application/x-www-form-urlencodedare parsed and decoded by usingQueryFormat.X_WWW_FORM_URLENCODED.- Parameters:
httpMethod- the HTTP method for this request (GET, POST, etc.)rawUrl- the raw (un-decoded) URL for this request- Returns:
- the builder
-
withPath
@Nonnull public static Request.PathBuilder withPath(@Nonnull HttpMethod httpMethod, @Nonnull String path) Acquires a builder forRequestinstances from already-decoded path and query components - useful for manual construction, e.g. integration tests.The provided
pathmust start with the/character and already be decoded (e.g."/my path", not"/my%20path"). It must not include query parameters. ForOPTIONS *requests, thepathmust be*- the "splat" symbol.Query parameters must be specified via
Request.PathBuilder.queryParameters(Map)and are assumed to be already-decoded.Request body form parameters with
Content-Type: application/x-www-form-urlencodedare parsed and decoded by usingQueryFormat.X_WWW_FORM_URLENCODED.- Parameters:
httpMethod- the HTTP method for this request (GET, POST, etc.)path- the decoded URL path for this request- Returns:
- the builder
-
copy
Vends a mutable copier seeded with this instance's data, suitable for building new instances.- Returns:
- a copier for this instance
-
toString
-
equals
-
hashCode
-
getId
-
getHttpMethod
The HTTP method for this request.- Returns:
- the request's HTTP method
-
getPath
-
getResourcePath
Convenience method to acquire aResourcePathrepresentation ofgetPath().- Returns:
- the resource path for this request
-
getCookies
The cookies provided by the client for this request.The keys are the
Cookieheader names and the values areCookieheader values (it is possible for a client to send multipleCookieheaders with the same name).Note that
Cookieheaders, like all request headers, have case-insensitive names per the HTTP spec.Use
getCookie(String)for a convenience method to access cookie values when only one is expected.- Returns:
- the request's cookies
-
getQueryParameters
The decoded query parameters provided by the client for this request.The keys are the query parameter names and the values are query parameter values (it is possible for a client to send multiple query parameters with the same name, e.g.
?test=1&test=2).Note that query parameters have case-sensitive names per the HTTP spec.
Use
getQueryParameter(String)for a convenience method to access query parameter values when only one is expected.- Returns:
- the request's query parameters
-
getFormParameters
The decoded HTMLapplication/x-www-form-urlencodedform parameters provided by the client for this request.The keys are the form parameter names and the values are form parameter values (it is possible for a client to send multiple form parameters with the same name, e.g.
?test=1&test=2).Note that form parameters have case-sensitive names per the HTTP spec.
Use
getFormParameter(String)for a convenience method to access form parameter values when only one is expected.- Returns:
- the request's form parameters
-
getRawPath
The raw (un-decoded) path component of this request exactly as the client specified.For example,
"/a%20b"(never decoded).Note: For requests constructed via
withPath(HttpMethod, String), this value is generated by encoding the decoded path, which may not exactly match the original wire format.- Returns:
- the raw path for this request
-
getRawQuery
The raw (un-decoded) query component of this request exactly as the client specified.For example,
"a=b&c=d+e"(never decoded).This is useful for special cases like HMAC signature verification, which relies on the exact client format.
Note: For requests constructed via
withPath(HttpMethod, String), this value is generated by encoding the decoded query parameters, which may not exactly match the original wire format.- Returns:
- the raw query for this request, or
Optional.empty()if none was specified
-
getRawPathAndQuery
The raw (un-decoded) path and query components of this request exactly as the client specified.For example,
"/my%20path?a=b&c=d%20e"(never decoded).Note: For requests constructed via
withPath(HttpMethod, String), this value is generated by encoding the decoded path and query parameters, which may not exactly match the original wire format.- Returns:
- the raw path and query for this request
-
getHeaders
The headers provided by the client for this request.The keys are the header names and the values are header values (it is possible for a client to send multiple headers with the same name).
Note that request headers have case-insensitive names per the HTTP spec.
Use
getHeader(String)for a convenience method to access header values when only one is expected.- Returns:
- the request's headers
-
getContentType
TheContent-Typeheader value, as specified by the client.- Returns:
- the request's
Content-Typeheader value, orOptional.empty()if not specified
-
getCharset
The request's character encoding, as specified by the client in theContent-Typeheader value.- Returns:
- the request's character encoding, or
Optional.empty()if not specified
-
isMultipart
Is this a request withContent-Typeofmultipart/form-data?- Returns:
trueif this is amultipart/form-datarequest,falseotherwise
-
getMultipartFields
The decoded HTMLmultipart/form-datafields provided by the client for this request.The keys are the multipart field names and the values are multipart field values (it is possible for a client to send multiple multipart fields with the same name).
Note that multipart fields have case-sensitive names per the HTTP spec.
Use
getMultipartField(String)for a convenience method to access a multipart parameter field value when only one is expected.When using Soklet's default
Server, multipart fields are parsed using theMultipartParseras configured byServer.Builder.multipartParser(MultipartParser).- Returns:
- the request's multipart fields, or the empty map if none are present
-
getBody
The raw bytes of the request body - callers should not modify this array; it is not defensively copied for performance reasons.For convenience,
getBodyAsString()is available if you expect your request body to be of typeString.- Returns:
- the request body bytes, or
Optional.empty()if none was supplied
-
isContentTooLarge
Was this request too large for the server to handle?If so, this request might have incomplete sets of headers/cookies. It will always have a zero-length body.
Soklet is designed to power systems that exchange small "transactional" payloads that live entirely in memory. It is not appropriate for handling multipart files at scale, buffering uploads to disk, streaming, etc.
When using Soklet's default
Server, maximum request size is configured byServer.Builder.maximumRequestSizeInBytes(Integer).- Returns:
trueif this request is larger than the server is able to handle,falseotherwise
-
getBodyAsString
Convenience method that provides thegetBody()bytes as aStringencoded using the client-specified character set pergetCharset().If no character set is specified,
StandardCharsets.UTF_8is used to perform the encoding.This method will lazily convert the raw bytes as specified by
getBody()to an instance ofStringwhen first invoked. TheStringrepresentation is then cached and re-used for subsequent invocations.This method is threadsafe.
- Returns:
- a
Stringrepresentation of this request's body, orOptional.empty()if no request body was specified by the client
-
getCors
Non-preflight CORS request data.See https://www.soklet.com/docs/cors for details.
- Returns:
- non-preflight CORS request data, or
Optional.empty()if none was specified
-
getCorsPreflight
CORS preflight-related request data.See https://www.soklet.com/docs/cors for details.
- Returns:
- preflight CORS request data, or
Optional.empty()if none was specified
-
getLocales
Locale information for this request as specified byAccept-Languageheader value[s] and ordered by weight as defined by RFC 7231, Section 5.3.5.This method will lazily parse
Accept-Languageheader values into to an orderedListofLocalewhen first invoked. This representation is then cached and re-used for subsequent invocations.This method is threadsafe.
See
getLanguageRanges()for a variant that pullsLocale.LanguageRangevalues.- Returns:
- locale information for this request, or the empty list if none was specified
-
getLanguageRanges
Locale.LanguageRangeinformation for this request as specified byAccept-Languageheader value[s].This method will lazily parse
Accept-Languageheader values into to an orderedListofLocale.LanguageRangewhen first invoked. This representation is then cached and re-used for subsequent invocations.This method is threadsafe.
See
getLocales()for a variant that pullsLocalevalues.- Returns:
- language range information for this request, or the empty list if none was specified
-
getQueryParameter
Convenience method to access a decoded query parameter's value when at most one is expected for the givenname.If a query parameter
namecan support multiple values,getQueryParameters()should be used instead of this method.If this method is invoked for a query parameter
namewith multiple values, Soklet will throwIllegalQueryParameterException.Note that query parameters have case-sensitive names per the HTTP spec.
- Parameters:
name- the name of the query parameter- Returns:
- the value for the query parameter, or
Optional.empty()if none is present - Throws:
IllegalQueryParameterException- if the query parameter with the givennamehas multiple values
-
getFormParameter
Convenience method to access a decoded form parameter's value when at most one is expected for the givenname.If a form parameter
namecan support multiple values,getFormParameters()should be used instead of this method.If this method is invoked for a form parameter
namewith multiple values, Soklet will throwIllegalFormParameterException.Note that form parameters have case-sensitive names per the HTTP spec.
- Parameters:
name- the name of the form parameter- Returns:
- the value for the form parameter, or
Optional.empty()if none is present - Throws:
IllegalFormParameterException- if the form parameter with the givennamehas multiple values
-
getHeader
Convenience method to access a header's value when at most one is expected for the givenname.If a header
namecan support multiple values,getHeaders()should be used instead of this method.If this method is invoked for a header
namewith multiple values, Soklet will throwIllegalRequestHeaderException.Note that request headers have case-insensitive names per the HTTP spec.
- Parameters:
name- the name of the header- Returns:
- the value for the header, or
Optional.empty()if none is present - Throws:
IllegalRequestHeaderException- if the header with the givennamehas multiple values
-
getCookie
Convenience method to access a cookie's value when at most one is expected for the givenname.If a cookie
namecan support multiple values,getCookies()should be used instead of this method.If this method is invoked for a cookie
namewith multiple values, Soklet will throwIllegalRequestCookieException.Note that
Cookieheaders, like all request headers, have case-insensitive names per the HTTP spec.- Parameters:
name- the name of the cookie- Returns:
- the value for the cookie, or
Optional.empty()if none is present - Throws:
IllegalRequestCookieException- if the cookie with the givennamehas multiple values
-
getMultipartField
Convenience method to access a decoded multipart field when at most one is expected for the givenname.If a
namecan support multiple multipart fields,getMultipartFields()should be used instead of this method.If this method is invoked for a
namewith multiple multipart field values, Soklet will throwIllegalMultipartFieldException.Note that multipart fields have case-sensitive names per the HTTP spec.
- Parameters:
name- the name of the multipart field- Returns:
- the multipart field value, or
Optional.empty()if none is present - Throws:
IllegalMultipartFieldException- if the multipart field with the givennamehas multiple values
-