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 OKHTTP 201 CreatedHTTP 302 RedirectHTTP 400 Bad requestHTTP 401 UnauthorizedHTTP 403 ForbiddenHTTP 500 Server error
Supported response headers:
- Location
- Content-Type
- Cache-Control
- Pragma
- Www-Authenticate
- DPoP-Nonce
- Etc.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intHTTP status code (400) indicating a bad request.static final intHTTP status code (201) indicating the request succeeded with a new resource being created.static final intHTTP status code (403) indicating that access to the resource was forbidden.static final intHTTP status code (302) indicating that the resource resides temporarily under a different URI (redirect).static final intHTTP status code (404) indicating the resource was not found.static final intHTTP status code (200) indicating the request succeeded.static final intHTTP status code (500) indicating an internal server error.static final intHTTP status code (503) indicating the server is unavailable.static final intHTTP 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 TypeMethodDescriptionvoidEnsures 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.voidensureStatusCode(int... expectedStatusCode) Ensures this HTTP response has the specified status code.voidEnsures this HTTP response does not have a200 OKstatus code.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 theCache-Controlheader value.Gets the client IP address.Deprecated.net.minidev.json.JSONArrayDeprecated.UsegetBodyAsJSONArray().net.minidev.json.JSONObjectDeprecated.com.nimbusds.jwt.JWTDeprecated.UsegetBodyAsJWT().Gets theDPoP-Nonceheader value.com.nimbusds.common.contenttype.ContentTypeGets theContent-Typeheader 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 theLocationheader value (for redirects).Gets thePragmaheader value.intGets the HTTP status code.Gets the HTTP status message.Gets theWWW-Authenticateheader value.booleanReturnstrueif the HTTP status code indicates success (2xx).voidSets the HTTP message body.voidsetCacheControl(String cacheControl) Sets theCache-Controlheader value.voidsetClientIPAddress(String clientIPAddress) Sets the client IP address.voidsetContent(String content) Deprecated.UsesetBody(String).voidsetContentType(String ct) Sets theContent-Typeheader value.voidsetDPoPNonce(Nonce nonce) Sets theDPoP-Nonceheader value.voidsetEntityContentType(com.nimbusds.common.contenttype.ContentType ct) Sets theContent-Typeheader value.voidSets an HTTP header.voidsetLocation(URI location) Sets theLocationheader value (for redirects).voidSets thePragmaheader value.voidsetStatusMessage(String message) Sets the HTTP status message.voidsetWWWAuthenticate(String wwwAuthenticate) Sets theWWW-Authenticateheader value.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
-
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:
getStatusCodein interfaceReadOnlyHTTPResponse- Returns:
- The HTTP status code.
-
indicatesSuccess
Returnstrueif the HTTP status code indicates success (2xx).- Returns:
trueif 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 OKstatus code.- Throws:
ParseException- If the status code of this HTTP response is 200 OK.
-
getStatusMessage
Description copied from interface:ReadOnlyHTTPResponseGets the HTTP status message.- Specified by:
getStatusMessagein interfaceReadOnlyHTTPResponse- Returns:
- The HTTP status message,
nullif not specified.
-
setStatusMessage
Sets the HTTP status message.- Parameters:
message- The HTTP status message,nullif not specified.
-
getLocation
Gets theLocationheader value (for redirects).- Returns:
- The header value,
nullif not specified.
-
setLocation
Sets theLocationheader value (for redirects).- Parameters:
location- The header value,nullif not specified.
-
getCacheControl
Gets theCache-Controlheader value.- Returns:
- The header value,
nullif not specified.
-
setCacheControl
Sets theCache-Controlheader value.- Parameters:
cacheControl- The header value,nullif not specified.
-
getPragma
Gets thePragmaheader value.- Returns:
- The header value,
nullif not specified.
-
setPragma
Sets thePragmaheader value.- Parameters:
pragma- The header value,nullif not specified.
-
getWWWAuthenticate
Gets theWWW-Authenticateheader value.- Returns:
- The header value,
nullif not specified.
-
setWWWAuthenticate
Sets theWWW-Authenticateheader value.- Parameters:
wwwAuthenticate- The header value,nullif not specified.
-
getDPoPNonce
Gets theDPoP-Nonceheader value.- Returns:
- The
DPoP-Nonceheader value,nullif not specified or parsing failed.
-
setDPoPNonce
Sets theDPoP-Nonceheader value.- Parameters:
nonce- TheDPoP-Nonceheader value,nullif not specified.
-
getContent
Deprecated.Gets the raw response content.- Returns:
- The raw response content,
nullif 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,nullif none.
-
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.
-
ReadOnlyHTTPMessage.getBody().