Package com.nimbusds.oauth2.sdk.http
Class HTTPResponse
java.lang.Object
com.nimbusds.oauth2.sdk.http.HTTPResponse
- All Implemented Interfaces:
ReadOnlyHTTPMessage
,ReadOnlyHTTPResponse
HTTP response with support for the parameters required to construct an
OAuth 2.0 response message
.
Provided HTTP status code constants:
HTTP 200 OK
HTTP 201 Created
HTTP 302 Redirect
HTTP 400 Bad request
HTTP 401 Unauthorized
HTTP 403 Forbidden
HTTP 500 Server error
Supported response headers:
- Location
- Content-Type
- Cache-Control
- Pragma
- Www-Authenticate
- DPoP-Nonce
- Etc.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
HTTP status code (400) indicating a bad request.static final int
HTTP status code (201) indicating the request succeeded with a new resource being created.static final int
HTTP status code (403) indicating that access to the resource was forbidden.static final int
HTTP status code (302) indicating that the resource resides temporarily under a different URI (redirect).static final int
HTTP status code (404) indicating the resource was not found.static final int
HTTP status code (200) indicating the request succeeded.static final int
HTTP status code (500) indicating an internal server error.static final int
HTTP status code (503) indicating the server is unavailable.static final int
HTTP status code (401) indicating that the request requires HTTP authentication. -
Constructor Summary
ConstructorsConstructorDescriptionHTTPResponse
(int statusCode) Creates a new minimal HTTP response with the specified status code. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Ensures this HTTP message has aContent-Type
header value.void
ensureEntityContentType
(com.nimbusds.common.contenttype.ContentType contentType) Ensures this HTTP message has the specifiedContent-Type
header value.void
ensureEntityContentType
(com.nimbusds.common.contenttype.ContentType contentType, String subTypeSuffix) Ensures this HTTP message has the specifiedContent-Type
header value.void
ensureStatusCode
(int... expectedStatusCode) Ensures this HTTP response has the specified status code.void
Ensures this HTTP response does not have a200 OK
status code.getBody()
Get the HTTP message body.Gets the response body as a form parameters map.net.minidev.json.JSONArray
Gets the response content as a JSON array.net.minidev.json.JSONObject
Gets the response body as a JSON object.com.nimbusds.jwt.JWT
Gets the response body as a JSON Web Token (JWT).Gets theCache-Control
header value.Gets the client IP address.Deprecated.net.minidev.json.JSONArray
Deprecated.UsegetBodyAsJSONArray()
.net.minidev.json.JSONObject
Deprecated.com.nimbusds.jwt.JWT
Deprecated.UsegetBodyAsJWT()
.Gets theDPoP-Nonce
header value.com.nimbusds.common.contenttype.ContentType
Gets theContent-Type
header value.Returns the HTTP headers.getHeaderValue
(String name) Gets an HTTP header's value.getHeaderValues
(String name) Gets an HTTP header's value(s).Gets theLocation
header value (for redirects).Gets thePragma
header value.int
Gets the HTTP status code.Gets the HTTP status message.Gets theWWW-Authenticate
header value.boolean
Returnstrue
if the HTTP status code indicates success (2xx).void
Sets the HTTP message body.void
setCacheControl
(String cacheControl) Sets theCache-Control
header value.void
setClientIPAddress
(String clientIPAddress) Sets the client IP address.void
setContent
(String content) Deprecated.UsesetBody(String)
.void
setContentType
(String ct) Sets theContent-Type
header value.void
setDPoPNonce
(Nonce nonce) Sets theDPoP-Nonce
header value.void
setEntityContentType
(com.nimbusds.common.contenttype.ContentType ct) Sets theContent-Type
header value.void
Sets an HTTP header.void
setLocation
(URI location) Sets theLocation
header value (for redirects).void
Sets thePragma
header value.void
setStatusMessage
(String message) Sets the HTTP status message.void
setWWWAuthenticate
(String wwwAuthenticate) Sets theWWW-Authenticate
header value.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.nimbusds.oauth2.sdk.http.ReadOnlyHTTPMessage
getBody, getHeaderMap
-
Field Details
-
SC_OK
HTTP status code (200) indicating the request succeeded.- See Also:
-
SC_CREATED
HTTP status code (201) indicating the request succeeded with a new resource being created.- See Also:
-
SC_FOUND
HTTP status code (302) indicating that the resource resides temporarily under a different URI (redirect).- See Also:
-
SC_BAD_REQUEST
HTTP status code (400) indicating a bad request.- See Also:
-
SC_UNAUTHORIZED
HTTP status code (401) indicating that the request requires HTTP authentication.- See Also:
-
SC_FORBIDDEN
HTTP status code (403) indicating that access to the resource was forbidden.- See Also:
-
SC_NOT_FOUND
HTTP status code (404) indicating the resource was not found.- See Also:
-
SC_SERVER_ERROR
HTTP status code (500) indicating an internal server error.- See Also:
-
SC_SERVICE_UNAVAILABLE
HTTP status code (503) indicating the server is unavailable.- See Also:
-
-
Constructor Details
-
HTTPResponse
Creates a new minimal HTTP response with the specified status code.- Parameters:
statusCode
- The HTTP status code.
-
-
Method Details
-
getStatusCode
Gets the HTTP status code.- Specified by:
getStatusCode
in interfaceReadOnlyHTTPResponse
- Returns:
- The HTTP status code.
-
indicatesSuccess
Returnstrue
if the HTTP status code indicates success (2xx).- Returns:
true
if the HTTP status code indicates success, elsefalse
.
-
ensureStatusCode
Ensures this HTTP response has the specified status code.- Parameters:
expectedStatusCode
- The expected status code(s).- Throws:
ParseException
- If the status code of this HTTP response doesn't match the expected.
-
ensureStatusCodeNotOK
Ensures this HTTP response does not have a200 OK
status code.- Throws:
ParseException
- If the status code of this HTTP response is 200 OK.
-
getStatusMessage
Description copied from interface:ReadOnlyHTTPResponse
Gets the HTTP status message.- Specified by:
getStatusMessage
in interfaceReadOnlyHTTPResponse
- Returns:
- The HTTP status message,
null
if not specified.
-
setStatusMessage
Sets the HTTP status message.- Parameters:
message
- The HTTP status message,null
if not specified.
-
getLocation
Gets theLocation
header value (for redirects).- Returns:
- The header value,
null
if not specified.
-
setLocation
Sets theLocation
header value (for redirects).- Parameters:
location
- The header value,null
if not specified.
-
getCacheControl
Gets theCache-Control
header value.- Returns:
- The header value,
null
if not specified.
-
setCacheControl
Sets theCache-Control
header value.- Parameters:
cacheControl
- The header value,null
if not specified.
-
getPragma
Gets thePragma
header value.- Returns:
- The header value,
null
if not specified.
-
setPragma
Sets thePragma
header value.- Parameters:
pragma
- The header value,null
if not specified.
-
getWWWAuthenticate
Gets theWWW-Authenticate
header value.- Returns:
- The header value,
null
if not specified.
-
setWWWAuthenticate
Sets theWWW-Authenticate
header value.- Parameters:
wwwAuthenticate
- The header value,null
if not specified.
-
getDPoPNonce
Gets theDPoP-Nonce
header value.- Returns:
- The
DPoP-Nonce
header value,null
if not specified or parsing failed.
-
setDPoPNonce
Sets theDPoP-Nonce
header value.- Parameters:
nonce
- TheDPoP-Nonce
header value,null
if not specified.
-
getContent
Deprecated.Gets the raw response content.- Returns:
- The raw response content,
null
if none.
-
getContentAsJSONObject
Deprecated.Gets the response content as a JSON object.- Returns:
- The response content as a JSON object.
- Throws:
ParseException
- If the Content-Type header isn'tapplication/json
, the response content isnull
, empty or couldn't be parsed to a valid JSON object.
-
getContentAsJSONArray
Deprecated.UsegetBodyAsJSONArray()
.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/json
, the response content isnull
, empty or couldn't be parsed to a valid JSON array.
-
getContentAsJWT
Deprecated.UsegetBodyAsJWT()
.Gets the response content as a JSON Web Token (JWT).- Returns:
- The response content as a JSON Web Token (JWT).
- Throws:
ParseException
- If the Content-Type header isn'tapplication/jwt
, the response content isnull
, empty or couldn't be parsed to a valid JSON Web Token (JWT).
-
setContent
Deprecated.UsesetBody(String)
.Sets the raw response content.- Parameters:
content
- The raw response content,null
if none.
-
getEntityContentType
public com.nimbusds.common.contenttype.ContentType getEntityContentType()Gets theContent-Type
header value.- Returns:
- The
Content-Type
header value,null
if not specified or parsing failed.
-
setEntityContentType
public void setEntityContentType(com.nimbusds.common.contenttype.ContentType ct) Sets theContent-Type
header value.- Parameters:
ct
- TheContent-Type
header value,null
if not specified.
-
setContentType
Sets theContent-Type
header value.- Parameters:
ct
- TheContent-Type
header value,null
if not specified.- Throws:
ParseException
- If the header value couldn't be parsed to a valid content type.
-
ensureEntityContentType
Ensures this HTTP message has aContent-Type
header value.- Throws:
ParseException
- If theContent-Type
header is missing.
-
ensureEntityContentType
public void ensureEntityContentType(com.nimbusds.common.contenttype.ContentType contentType) throws ParseException Ensures this HTTP message has the specifiedContent-Type
header 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-Type
header 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-Type
header 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,null
if not specified.- Throws:
ParseException
- If theContent-Type
header 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,
null
if not specified.
-
getHeaderValues
Gets an HTTP header's value(s).- Parameters:
name
- The header name. Must not benull
.- Returns:
- The header value(s),
null
if not specified.
-
setHeader
Sets an HTTP header.- Parameters:
name
- The header name. Must not benull
.values
- The header value(s). Ifnull
and a header with the same name is specified, it will be deleted.
-
getHeaderMap
Description copied from interface:ReadOnlyHTTPMessage
Returns the HTTP headers.- Specified by:
getHeaderMap
in interfaceReadOnlyHTTPMessage
- Returns:
- The HTTP headers.
-
getBody
Description copied from interface:ReadOnlyHTTPMessage
Get the HTTP message body.- Specified by:
getBody
in interfaceReadOnlyHTTPMessage
- Returns:
- The HTTP message body,
null
if not specified.
-
setBody
Sets the HTTP message body.- Parameters:
body
- The HTTP message body,null
if 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-urlencoded
or 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/json
or has no+json
suffix, 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/json
or has no+json
suffix, 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/jwt
or has no+jwt
suffix, 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,
null
if not specified.
-
setClientIPAddress
Sets the client IP address.- Parameters:
clientIPAddress
- The client IP address,null
if not specified.
-
ReadOnlyHTTPMessage.getBody()
.