Package com.nimbusds.oauth2.sdk.http
Class HTTPResponse
- java.lang.Object
-
- com.nimbusds.oauth2.sdk.http.HTTPResponse
-
- All Implemented Interfaces:
ReadOnlyHTTPMessage
,ReadOnlyHTTPResponse
@ThreadSafe public class HTTPResponse extends Object implements ReadOnlyHTTPResponse
HTTP response with support for the parameters required to construct anOAuth 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
Fields Modifier and Type Field Description static int
SC_BAD_REQUEST
HTTP status code (400) indicating a bad request.static int
SC_CREATED
HTTP status code (201) indicating the request succeeded with a new resource being created.static int
SC_FORBIDDEN
HTTP status code (403) indicating that access to the resource was forbidden.static int
SC_FOUND
HTTP status code (302) indicating that the resource resides temporarily under a different URI (redirect).static int
SC_NOT_FOUND
HTTP status code (404) indicating the resource was not found.static int
SC_OK
HTTP status code (200) indicating the request succeeded.static int
SC_SERVER_ERROR
HTTP status code (500) indicating an internal server error.static int
SC_SERVICE_UNAVAILABLE
HTTP status code (503) indicating the server is unavailable.static int
SC_UNAUTHORIZED
HTTP status code (401) indicating that the request requires HTTP authentication.
-
Constructor Summary
Constructors Constructor Description HTTPResponse(int statusCode)
Creates a new minimal HTTP response with the specified status code.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
ensureEntityContentType()
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
ensureStatusCode(int... expectedStatusCode)
Ensures this HTTP response has the specified status code.void
ensureStatusCodeNotOK()
Ensures this HTTP response does not have a200 OK
status code.String
getBody()
Get the HTTP message body.Map<String,List<String>>
getBodyAsFormParameters()
Gets the response body as a form parameters map.net.minidev.json.JSONArray
getBodyAsJSONArray()
Gets the response content as a JSON array.net.minidev.json.JSONObject
getBodyAsJSONObject()
Gets the response body as a JSON object.com.nimbusds.jwt.JWT
getBodyAsJWT()
Gets the response body as a JSON Web Token (JWT).String
getCacheControl()
Gets theCache-Control
header value.String
getClientIPAddress()
Gets the client IP address.String
getContent()
Deprecated.net.minidev.json.JSONArray
getContentAsJSONArray()
Deprecated.UsegetBodyAsJSONArray()
.net.minidev.json.JSONObject
getContentAsJSONObject()
Deprecated.com.nimbusds.jwt.JWT
getContentAsJWT()
Deprecated.UsegetBodyAsJWT()
.Nonce
getDPoPNonce()
Gets theDPoP-Nonce
header value.com.nimbusds.common.contenttype.ContentType
getEntityContentType()
Gets theContent-Type
header value.Map<String,List<String>>
getHeaderMap()
Returns the HTTP headers.String
getHeaderValue(String name)
Gets an HTTP header's value.List<String>
getHeaderValues(String name)
Gets an HTTP header's value(s).URI
getLocation()
Gets theLocation
header value (for redirects).String
getPragma()
Gets thePragma
header value.int
getStatusCode()
Gets the HTTP status code.String
getStatusMessage()
Gets the HTTP status message.String
getWWWAuthenticate()
Gets theWWW-Authenticate
header value.boolean
indicatesSuccess()
Returnstrue
if the HTTP status code indicates success (2xx).void
setBody(String body)
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
setHeader(String name, String... values)
Sets an HTTP header.void
setLocation(URI location)
Sets theLocation
header value (for redirects).void
setPragma(String pragma)
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 Detail
-
SC_OK
public static final int SC_OK
HTTP status code (200) indicating the request succeeded.- See Also:
- Constant Field Values
-
SC_CREATED
public static final int SC_CREATED
HTTP status code (201) indicating the request succeeded with a new resource being created.- See Also:
- Constant Field Values
-
SC_FOUND
public static final int SC_FOUND
HTTP status code (302) indicating that the resource resides temporarily under a different URI (redirect).- See Also:
- Constant Field Values
-
SC_BAD_REQUEST
public static final int SC_BAD_REQUEST
HTTP status code (400) indicating a bad request.- See Also:
- Constant Field Values
-
SC_UNAUTHORIZED
public static final int SC_UNAUTHORIZED
HTTP status code (401) indicating that the request requires HTTP authentication.- See Also:
- Constant Field Values
-
SC_FORBIDDEN
public static final int SC_FORBIDDEN
HTTP status code (403) indicating that access to the resource was forbidden.- See Also:
- Constant Field Values
-
SC_NOT_FOUND
public static final int SC_NOT_FOUND
HTTP status code (404) indicating the resource was not found.- See Also:
- Constant Field Values
-
SC_SERVER_ERROR
public static final int SC_SERVER_ERROR
HTTP status code (500) indicating an internal server error.- See Also:
- Constant Field Values
-
SC_SERVICE_UNAVAILABLE
public static final int SC_SERVICE_UNAVAILABLE
HTTP status code (503) indicating the server is unavailable.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
HTTPResponse
public HTTPResponse(int statusCode)
Creates a new minimal HTTP response with the specified status code.- Parameters:
statusCode
- The HTTP status code.
-
-
Method Detail
-
getStatusCode
public int getStatusCode()
Gets the HTTP status code.- Specified by:
getStatusCode
in interfaceReadOnlyHTTPResponse
- Returns:
- The HTTP status code.
-
indicatesSuccess
public boolean indicatesSuccess()
Returnstrue
if the HTTP status code indicates success (2xx).- Returns:
true
if the HTTP status code indicates success, elsefalse
.
-
ensureStatusCode
public void ensureStatusCode(int... expectedStatusCode) throws ParseException
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
public void ensureStatusCodeNotOK() throws ParseException
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
public String 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
public void setStatusMessage(String message)
Sets the HTTP status message.- Parameters:
message
- The HTTP status message,null
if not specified.
-
getLocation
public URI getLocation()
Gets theLocation
header value (for redirects).- Returns:
- The header value,
null
if not specified.
-
setLocation
public void setLocation(URI location)
Sets theLocation
header value (for redirects).- Parameters:
location
- The header value,null
if not specified.
-
getCacheControl
public String getCacheControl()
Gets theCache-Control
header value.- Returns:
- The header value,
null
if not specified.
-
setCacheControl
public void setCacheControl(String cacheControl)
Sets theCache-Control
header value.- Parameters:
cacheControl
- The header value,null
if not specified.
-
getPragma
public String getPragma()
Gets thePragma
header value.- Returns:
- The header value,
null
if not specified.
-
setPragma
public void setPragma(String pragma)
Sets thePragma
header value.- Parameters:
pragma
- The header value,null
if not specified.
-
getWWWAuthenticate
public String getWWWAuthenticate()
Gets theWWW-Authenticate
header value.- Returns:
- The header value,
null
if not specified.
-
setWWWAuthenticate
public void setWWWAuthenticate(String wwwAuthenticate)
Sets theWWW-Authenticate
header value.- Parameters:
wwwAuthenticate
- The header value,null
if not specified.
-
getDPoPNonce
public Nonce getDPoPNonce()
Gets theDPoP-Nonce
header value.- Returns:
- The
DPoP-Nonce
header value,null
if not specified or parsing failed.
-
setDPoPNonce
public void setDPoPNonce(Nonce nonce)
Sets theDPoP-Nonce
header value.- Parameters:
nonce
- TheDPoP-Nonce
header value,null
if not specified.
-
getContent
@Deprecated public String getContent()
Deprecated.Gets the raw response content.- Returns:
- The raw response content,
null
if none.
-
getContentAsJSONObject
@Deprecated public net.minidev.json.JSONObject getContentAsJSONObject() throws ParseException
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 public net.minidev.json.JSONArray getContentAsJSONArray() throws ParseException
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 public com.nimbusds.jwt.JWT getContentAsJWT() throws ParseException
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 public void setContent(String content)
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
public void setContentType(String ct) throws ParseException
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
public void ensureEntityContentType() throws ParseException
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.
-
getHeaderValue
public String getHeaderValue(String name)
Gets an HTTP header's value.- Parameters:
name
- The header name. Must not benull
.- Returns:
- The first header value,
null
if not specified.
-
getHeaderValues
public List<String> getHeaderValues(String name)
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
public void setHeader(String name, String... values)
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
public Map<String,List<String>> getHeaderMap()
Description copied from interface:ReadOnlyHTTPMessage
Returns the HTTP headers.- Specified by:
getHeaderMap
in interfaceReadOnlyHTTPMessage
- Returns:
- The HTTP headers.
-
getBody
public String 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
public void setBody(String body)
Sets the HTTP message body.- Parameters:
body
- The HTTP message body,null
if not specified.
-
getBodyAsFormParameters
public Map<String,List<String>> getBodyAsFormParameters() throws ParseException
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
public net.minidev.json.JSONObject getBodyAsJSONObject() throws ParseException
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
, the response body isnull
, empty or couldn't be parsed to a valid JSON object.
-
getBodyAsJSONArray
public net.minidev.json.JSONArray getBodyAsJSONArray() throws ParseException
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.
-
getBodyAsJWT
public com.nimbusds.jwt.JWT getBodyAsJWT() throws ParseException
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
, the response content isnull
, empty or couldn't be parsed to a valid JSON Web Token (JWT).
-
getClientIPAddress
public String getClientIPAddress()
Gets the client IP address.- Returns:
- The client IP address,
null
if not specified.
-
setClientIPAddress
public void setClientIPAddress(String clientIPAddress)
Sets the client IP address.- Parameters:
clientIPAddress
- The client IP address,null
if not specified.
-
-