Package com.nimbusds.oauth2.sdk.http
Class HTTPRequest
java.lang.Object
com.nimbusds.oauth2.sdk.http.HTTPRequest
- All Implemented Interfaces:
ReadOnlyHTTPMessage,ReadOnlyHTTPRequest
HTTP request with support for the parameters required to construct an
OAuth 2.0 request message.
Supported HTTP methods:
Supported request headers:
- Content-Type
- Authorization
- Accept
- Etc.
Supported timeouts:
- On HTTP connect
- On HTTP response read
HTTP 3xx redirection: follow (default) / don't follow
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnumeration of the HTTP methods used in OAuth 2.0 requests. -
Constructor Summary
ConstructorsConstructorDescriptionHTTPRequest(HTTPRequest.Method method, URI uri) Creates a new minimally specified HTTP request.HTTPRequest(HTTPRequest.Method method, URL url) Creates a new minimally specified HTTP request. -
Method Summary
Modifier and TypeMethodDescriptionvoidappendQueryParameters(Map<String, List<String>> queryParams) Appends the specified query parameters to the current HTTP requestURLquery.voidappendQueryString(String queryString) Appends the specified raw (encoded) query string to the current HTTP requestURLquery.voidEnsures this HTTP message has aContent-Typeheader value.voidensureEntityContentType(com.nimbusds.common.contenttype.ContentType contentType) Ensures this HTTP message has the specifiedContent-Typeheader value.voidensureEntityContentType(com.nimbusds.common.contenttype.ContentType contentType, String subTypeSuffix) Ensures this HTTP message has the specifiedContent-Typeheader value.voidensureMethod(HTTPRequest.Method expectedMethod) Ensures this HTTP request has the specified method.Gets theAcceptheader value.Gets theAuthorizationheader value.getBody()Get the HTTP message body.Gets the response body as a form parameters map.net.minidev.json.JSONArrayGets the response content as a JSON array.net.minidev.json.JSONObjectGets the response body as a JSON object.com.nimbusds.jwt.JWTGets the response body as a JSON Web Token (JWT).Gets the client IP address.Gets the received validated client X.509 certificate for a received HTTPS request.Gets the root issuer DN of a received validated client X.509 certificate for a received HTTPS request.Gets the subject DN of a received validated client X.509 certificate for a received HTTPS request.intGets the HTTP connect timeout.static HostnameVerifierReturns the default hostname verifier for all outgoing HTTPS requests.static SSLSocketFactoryReturns the default SSL socket factory for all outgoing HTTPS requests.com.nimbusds.jwt.SignedJWTgetDPoP()Gets theDPoPheader value.com.nimbusds.common.contenttype.ContentTypeGets theContent-Typeheader value.booleanGets the boolean setting whether HTTP redirects (requests with response code 3xx) should be automatically followed.Deprecated.Returns the HTTP headers.getHeaderValue(String name) Gets an HTTP header's value.getHeaderValues(String name) Gets an HTTP header's value(s).Gets the hostname verifier for outgoing HTTPS requests.Gets the request method.com.nimbusds.jwt.SignedJWTGets theDPoPheader value.getProxy()Returns the proxy to use for this HTTP request.getQuery()Deprecated.UsegetURL().net.minidev.json.JSONObjectDeprecated.Deprecated.Gets the query string as a parameter map.intGets the HTTP response read timeout.Gets the SSL factory for outgoing HTTPS requests.getURI()Gets the request URL as URI.getURL()Gets the request URL.send()Sends this HTTP request to theURLand retrieves the resulting HTTP response.send(HTTPRequestSender httpRequestSender) Sends this HTTP request to theURLand retrieves the resulting HTTP response.send(HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory) Deprecated.voidSets theAcceptheader value.voidsetAuthorization(String authz) Sets theAuthorizationheader value.voidSets the HTTP message body.voidsetClientIPAddress(String clientIPAddress) Sets the client IP address.voidsetClientX509Certificate(X509Certificate clientX509Certificate) Sets the received validated client X.509 certificate for a received HTTPS request.voidsetClientX509CertificateRootDN(String rootDN) Sets the root issuer DN of a received validated client X.509 certificate for a received HTTPS request.voidsetClientX509CertificateSubjectDN(String subjectDN) Sets the subject DN of a received validated client X.509 certificate for a received HTTPS request.voidsetConnectTimeout(int connectTimeout) Sets the HTTP connect timeout.voidsetContentType(String ct) Sets theContent-Typeheader value.static voidsetDefaultHostnameVerifier(HostnameVerifier defaultHostnameVerifier) Sets the default hostname verifier for all outgoing HTTPS requests.static voidsetDefaultSSLSocketFactory(SSLSocketFactory sslSocketFactory) Sets the default SSL socket factory for all outgoing HTTPS requests.voidsetDPoP(com.nimbusds.jwt.SignedJWT dPoPJWT) Sets theDPoPheader value.voidsetEntityContentType(com.nimbusds.common.contenttype.ContentType ct) Sets theContent-Typeheader value.voidsetFollowRedirects(boolean follow) Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed.voidsetFragment(String fragment) Sets the raw (encoded) fragment of the URL.voidSets an HTTP header.voidsetHostnameVerifier(HostnameVerifier hostnameVerifier) Sets the hostname verifier for outgoing HTTPS requests.voidTunnels this HTTP request via the specifiedProxyby directly configuring the proxy on theURLConnection.voidDeprecated.voidsetReadTimeout(int readTimeout) Sets the HTTP response read timeout.voidsetSSLSocketFactory(SSLSocketFactory sslSocketFactory) Sets the SSL factory for outgoing HTTPS requests.Returns an established HTTP URL connection for this HTTP request.toHttpURLConnection(HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory) Deprecated.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.nimbusds.oauth2.sdk.http.ReadOnlyHTTPMessage
getBody, getHeaderMap
-
Constructor Details
-
HTTPRequest
Creates a new minimally specified HTTP request.- Parameters:
method- The HTTP request method. Must not benull.url- The HTTP request URL. Must not benull.
-
HTTPRequest
Creates a new minimally specified HTTP request.- Parameters:
method- The HTTP request method. Must not benull.uri- The HTTP request URI. Must be a URL and notnull.
-
-
Method Details
-
getMethod
Gets the request method.- Specified by:
getMethodin interfaceReadOnlyHTTPRequest- Returns:
- The request method.
-
getURL
Gets the request URL.- Specified by:
getURLin interfaceReadOnlyHTTPRequest- Returns:
- The request URL.
-
getURI
Gets the request URL as URI.- Specified by:
getURIin interfaceReadOnlyHTTPRequest- Returns:
- The request URL as URI.
-
ensureMethod
Ensures this HTTP request has the specified method.- Parameters:
expectedMethod- The expected method. Must not benull.- Throws:
ParseException- If the method doesn't match the expected.
-
getAuthorization
Gets theAuthorizationheader value.- Returns:
- The
Authorizationheader value,nullif not specified.
-
setAuthorization
Sets theAuthorizationheader value.- Parameters:
authz- TheAuthorizationheader value,nullif not specified.
-
getDPoP
Gets theDPoPheader value.- Returns:
- The
DPoPheader value,nullif not specified or parsing failed.
-
getPoPWithException
Gets theDPoPheader value.- Returns:
- The
DPoPheader value,nullif not specified. - Throws:
ParseException- If JWT parsing failed.
-
setDPoP
Sets theDPoPheader value.- Parameters:
dPoPJWT- TheDPoPheader value,nullif not specified.
-
getAccept
Gets theAcceptheader value.- Returns:
- The
Acceptheader value,nullif not specified.
-
setAccept
Sets theAcceptheader value.- Parameters:
accept- TheAcceptheader value,nullif not specified.
-
appendQueryParameters
Appends the specified query parameters to the current HTTP requestURLquery.If the current URL has a query string the new query is appended with `&` in front.
- Parameters:
queryParams- The query parameters to append, empty ornullif nothing to append.- Throws:
IllegalArgumentException- If the URL composition failed.
-
appendQueryString
Appends the specified raw (encoded) query string to the current HTTP requestURLquery.If the current URL has a query string the new query is appended with `&` in front.
The '?' character preceding the query string must not be included.
Example query string to append:
client_id=123&logout_hint=eepaeph8siot&state=shah2key
- Parameters:
queryString- The query string to append, blank ornullif nothing to append.- Throws:
IllegalArgumentException- If the URL composition failed.
-
getQuery
Deprecated.UsegetURL().Gets the raw (encoded) query string if the request is HTTP GET or the entity body if the request is HTTP POST.Note that the '?' character preceding the query string in GET requests is not included in the returned string.
Example query string (line breaks for clarity):
response_type=code &client_id=s6BhdRkqt3 &state=xyz &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
- Returns:
- For HTTP GET requests the URL query string, for HTTP POST
requests the body.
nullif not specified.
-
setQuery
Deprecated.Sets the raw (encoded) query string if the request is HTTP GET or the entity body if the request is HTTP POST.Note that the '?' character preceding the query string in GET requests must not be included.
Example query string (line breaks for clarity):
response_type=code &client_id=s6BhdRkqt3 &state=xyz &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
- Parameters:
query- For HTTP GET requests the URL query string, for HTTP POST requests the body.nullif not specified.
-
getQueryStringParameters
Gets the query string as a parameter map. The parameters are decoded according toapplication/x-www-form-urlencoded.- Returns:
- The query string parameters to, decoded. If none the map will be empty.
-
getQueryParameters
Deprecated.Gets the request query as a parameter map. The parameters are decoded according toapplication/x-www-form-urlencoded.- Returns:
- The request query parameters, decoded. If none the map will be empty.
-
getQueryAsJSONObject
Deprecated.Gets the request query or entity body as a JSON Object.- Returns:
- The request query or entity body as a JSON object.
- Throws:
ParseException- If the Content-Type header isn'tapplication/json, the request query or entity body isnull, empty or couldn't be parsed to a valid JSON object.
-
getFragment
Deprecated.UsegetURL().Gets the raw (encoded) fragment of the URL.- Returns:
- The fragment,
nullif not specified.
-
setFragment
Sets the raw (encoded) fragment of the URL.- Parameters:
fragment- The fragment,nullif not specified.
-
getConnectTimeout
Description copied from interface:ReadOnlyHTTPRequestGets the HTTP connect timeout.- Specified by:
getConnectTimeoutin interfaceReadOnlyHTTPRequest- Returns:
- The HTTP connect timeout, in milliseconds. Zero implies no timeout.
-
setConnectTimeout
Sets the HTTP connect timeout.- Parameters:
connectTimeout- The HTTP connect timeout, in milliseconds. Zero implies no timeout. Must not be negative.
-
getReadTimeout
Description copied from interface:ReadOnlyHTTPRequestGets the HTTP response read timeout.- Specified by:
getReadTimeoutin interfaceReadOnlyHTTPRequest- Returns:
- The HTTP response read timeout, in milliseconds. Zero implies no timeout.
-
setReadTimeout
Sets the HTTP response read timeout.- Parameters:
readTimeout- The HTTP response read timeout, in milliseconds. Zero implies no timeout. Must not be negative.
-
getProxy
Returns the proxy to use for this HTTP request.- Returns:
- The connection specific proxy for this request,
nullfor the default proxy strategy.
-
setProxy
Tunnels this HTTP request via the specifiedProxyby directly configuring the proxy on theURLConnection. The proxy is only used for this instance and bypasses any other proxy settings (such as set via System properties orProxySelector). Supplyingnull(the default) reverts to the default proxy strategy ofURLConnection. If the goal is to avoid using a proxy at all supplyProxy.NO_PROXY.- Parameters:
proxy- The connection specific proxy to use,nullto use the default proxy strategy.- See Also:
-
getFollowRedirects
Gets the boolean setting whether HTTP redirects (requests with response code 3xx) should be automatically followed.- Returns:
trueif HTTP redirects are automatically followed, elsefalse.
-
setFollowRedirects
Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed.- Parameters:
follow-trueif HTTP redirects are automatically followed, elsefalse.
-
getClientX509Certificate
Gets the received validated client X.509 certificate for a received HTTPS request.- Returns:
- The client X.509 certificate,
nullif not specified.
-
setClientX509Certificate
Sets the received validated client X.509 certificate for a received HTTPS request.- Parameters:
clientX509Certificate- The client X.509 certificate,nullif not specified.
-
getClientX509CertificateSubjectDN
Gets the subject DN of a received validated client X.509 certificate for a received HTTPS request.- Returns:
- The subject DN,
nullif not specified.
-
setClientX509CertificateSubjectDN
Sets the subject DN of a received validated client X.509 certificate for a received HTTPS request.- Parameters:
subjectDN- The subject DN,nullif not specified.
-
getClientX509CertificateRootDN
Gets the root issuer DN of a received validated client X.509 certificate for a received HTTPS request.- Returns:
- The root DN,
nullif not specified.
-
setClientX509CertificateRootDN
Sets the root issuer DN of a received validated client X.509 certificate for a received HTTPS request.- Parameters:
rootDN- The root DN,nullif not specified.
-
getHostnameVerifier
Gets the hostname verifier for outgoing HTTPS requests.- Returns:
- The hostname verifier,
nullimplies use of thedefault one.
-
setHostnameVerifier
Sets the hostname verifier for outgoing HTTPS requests.- Parameters:
hostnameVerifier- The hostname verifier,nullimplies use of thedefault one.
-
getSSLSocketFactory
Gets the SSL factory for outgoing HTTPS requests.- Returns:
- The SSL factory,
nullimplies of the default one.
-
setSSLSocketFactory
Sets the SSL factory for outgoing HTTPS requests. Use theTLS utilityto set a custom trust store for server and CA certificates and / or a custom key store for client private keys and certificates, also to select a specific TLS protocol version.- Parameters:
sslSocketFactory- The SSL factory,nullimplies use of the default one.
-
getDefaultHostnameVerifier
Returns the default hostname verifier for all outgoing HTTPS requests.- Returns:
- The hostname verifier.
-
setDefaultHostnameVerifier
Sets the default hostname verifier for all outgoing HTTPS requests. Can be overridden on a individual request basis.- Parameters:
defaultHostnameVerifier- The hostname verifier. Must not benull.
-
getDefaultSSLSocketFactory
Returns the default SSL socket factory for all outgoing HTTPS requests.- Returns:
- The SSL socket factory.
-
setDefaultSSLSocketFactory
Sets the default SSL socket factory for all outgoing HTTPS requests. Can be overridden on a individual request basis. Use theTLS utilityto set a custom trust store for server and CA certificates and / or a custom key store for client private keys and certificates, also to select a specific TLS protocol version.- Parameters:
sslSocketFactory- The SSL socket factory. Must not benull.
-
toHttpURLConnection
@Deprecated public HttpURLConnection toHttpURLConnection(HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory) throws IOException Deprecated.Returns an established HTTP URL connection for this HTTP request. Deprecated as of v5.31, usetoHttpURLConnection()withsetHostnameVerifier(javax.net.ssl.HostnameVerifier)andsetSSLSocketFactory(javax.net.ssl.SSLSocketFactory)instead.- Parameters:
hostnameVerifier- The hostname verifier for outgoing HTTPS requests,nullimplies use of thedefault one.sslSocketFactory- The SSL socket factory for HTTPS requests,nullimplies use of thedefault one.- Returns:
- The HTTP URL connection, with the request sent and ready to read the response.
- Throws:
IOException- If the HTTP request couldn't be made, due to a network or other error.
-
toHttpURLConnection
Returns an established HTTP URL connection for this HTTP request.- Returns:
- The HTTP URL connection, with the request sent and ready to read the response.
- Throws:
IOException- If the HTTP request couldn't be made, due to a network or other error.
-
send
@Deprecated public HTTPResponse send(HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory) throws IOException Deprecated.Sends this HTTP request to the request URL and retrieves the resulting HTTP response. Deprecated as of v5.31, usetoHttpURLConnection()withsetHostnameVerifier(javax.net.ssl.HostnameVerifier)andsetSSLSocketFactory(javax.net.ssl.SSLSocketFactory)instead.- Parameters:
hostnameVerifier- The hostname verifier for outgoing HTTPS requests,nullimplies use of thedefault one.sslSocketFactory- The SSL socket factory for HTTPS requests,nullimplies use of thedefault one.- Returns:
- The resulting HTTP response.
- Throws:
IOException- If the HTTP request couldn't be made, due to a network or other error.
-
send
Sends this HTTP request to theURLand retrieves the resulting HTTP response.- Returns:
- The resulting HTTP response.
- Throws:
IOException- If the HTTP request couldn't be sent, due to a network or another error.
-
send
Sends this HTTP request to theURLand retrieves the resulting HTTP response.- Parameters:
httpRequestSender- The HTTP request sender. Must not benull.- Returns:
- The resulting HTTP response.
- Throws:
IOException- If the HTTP request couldn't be sent, due to a network or another error.
-
getEntityContentType
public com.nimbusds.common.contenttype.ContentType getEntityContentType()Gets theContent-Typeheader value.- Returns:
- The
Content-Typeheader value,nullif not specified or parsing failed.
-
setEntityContentType
public void setEntityContentType(com.nimbusds.common.contenttype.ContentType ct) Sets theContent-Typeheader value.- Parameters:
ct- TheContent-Typeheader value,nullif not specified.
-
setContentType
Sets theContent-Typeheader value.- Parameters:
ct- TheContent-Typeheader value,nullif not specified.- Throws:
ParseException- If the header value couldn't be parsed to a valid content type.
-
ensureEntityContentType
Ensures this HTTP message has aContent-Typeheader value.- Throws:
ParseException- If theContent-Typeheader is missing.
-
ensureEntityContentType
public void ensureEntityContentType(com.nimbusds.common.contenttype.ContentType contentType) throws ParseException Ensures this HTTP message has the specifiedContent-Typeheader value. This method compares only the primary type and subtype; any content type parameters, such ascharset, are ignored.- Parameters:
contentType- The expected content type. Must not benull.- Throws:
ParseException- If theContent-Typeheader is missing or its primary and subtype don't match.
-
ensureEntityContentType
public void ensureEntityContentType(com.nimbusds.common.contenttype.ContentType contentType, String subTypeSuffix) throws ParseException Ensures this HTTP message has the specifiedContent-Typeheader value. This method compares only the primary type and subtype; any content type parameters, such ascharset, are ignored.- Parameters:
contentType- The expected content type. Must not benull.subTypeSuffix- Acceptable sub type suffix if the expected content type doesn't match,nullif not specified.- Throws:
ParseException- If theContent-Typeheader is missing or its primary and subtype don't match.
-
getHeaderValue
Gets an HTTP header's value.- Parameters:
name- The header name. Must not benull.- Returns:
- The first header value,
nullif not specified.
-
getHeaderValues
Gets an HTTP header's value(s).- Parameters:
name- The header name. Must not benull.- Returns:
- The header value(s),
nullif not specified.
-
setHeader
Sets an HTTP header.- Parameters:
name- The header name. Must not benull.values- The header value(s). Ifnulland a header with the same name is specified, it will be deleted.
-
getHeaderMap
Description copied from interface:ReadOnlyHTTPMessageReturns the HTTP headers.- Specified by:
getHeaderMapin interfaceReadOnlyHTTPMessage- Returns:
- The HTTP headers.
-
getBody
Description copied from interface:ReadOnlyHTTPMessageGet the HTTP message body.- Specified by:
getBodyin interfaceReadOnlyHTTPMessage- Returns:
- The HTTP message body,
nullif not specified.
-
setBody
Sets the HTTP message body.- Parameters:
body- The HTTP message body,nullif not specified.
-
getBodyAsFormParameters
Gets the response body as a form parameters map.- Returns:
- The form parameters as a map.
- Throws:
ParseException- If the Content-Type header isn'tapplication/x-www-form-urlencodedor the response couldn't be parsed to a valid form.
-
getBodyAsJSONObject
Gets the response body as a JSON object.- Returns:
- The response body as a JSON object.
- Throws:
ParseException- If the Content-Type header isn'tapplication/jsonor has no+jsonsuffix, the response body isnull, empty or couldn't be parsed to a valid JSON object.
-
getBodyAsJSONArray
Gets the response content as a JSON array.- Returns:
- The response content as a JSON array.
- Throws:
ParseException- If the Content-Type header isn'tapplication/jsonor has no+jsonsuffix, the response content isnull, empty or couldn't be parsed to a valid JSON array.
-
getBodyAsJWT
Gets the response body as a JSON Web Token (JWT).- Returns:
- The response body as a JSON Web Token (JWT).
- Throws:
ParseException- If the Content-Type header isn'tapplication/jwtor has no+jwtsuffix, the response content isnull, empty or couldn't be parsed to a valid JSON Web Token (JWT).
-
getClientIPAddress
Gets the client IP address.- Returns:
- The client IP address,
nullif not specified.
-
setClientIPAddress
Sets the client IP address.- Parameters:
clientIPAddress- The client IP address,nullif not specified.
-
getURL().