Package com.nimbusds.oauth2.sdk.http
Class HTTPResponse
- java.lang.Object
-
- com.nimbusds.oauth2.sdk.http.HTTPResponse
-
@ThreadSafe public class HTTPResponse extends Object
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
-
-
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_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
ensureContentType()
Deprecated.void
ensureContentType(javax.mail.internet.ContentType contentType)
Deprecated.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
getCacheControl()
Gets theCache-Control
header value.String
getClientIPAddress()
Gets the client IP address.String
getContent()
Gets the raw response content.net.minidev.json.JSONArray
getContentAsJSONArray()
Gets the response content as a JSON array.net.minidev.json.JSONObject
getContentAsJSONObject()
Gets the response content as a JSON object.com.nimbusds.jwt.JWT
getContentAsJWT()
Gets the response content as a JSON Web Token (JWT).javax.mail.internet.ContentType
getContentType()
Deprecated.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
setCacheControl(String cacheControl)
Sets theCache-Control
header value.void
setClientIPAddress(String clientIPAddress)
Sets the client IP address.void
setContent(String content)
Sets the raw response content.void
setContentType(String ct)
Sets theContent-Type
header value.void
setContentType(javax.mail.internet.ContentType ct)
Deprecated.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.
-
-
-
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_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.- 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()
Gets the HTTP status message.- 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.
-
getContent
public String getContent()
Gets the raw response content.- Returns:
- The raw response content,
null
if none.
-
getContentAsJSONObject
public net.minidev.json.JSONObject getContentAsJSONObject() throws ParseException
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
public net.minidev.json.JSONArray getContentAsJSONArray() 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.
-
getContentAsJWT
public com.nimbusds.jwt.JWT getContentAsJWT() throws ParseException
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
public void setContent(String content)
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.
-
getContentType
@Deprecated public javax.mail.internet.ContentType getContentType()
Deprecated.- See Also:
getEntityContentType()
-
setContentType
@Deprecated public void setContentType(javax.mail.internet.ContentType ct)
Deprecated.- See Also:
setEntityContentType(ContentType)
-
ensureEntityContentType
public void ensureEntityContentType() throws ParseException
Ensures this HTTP message has aContent-Type
header value.- Throws:
ParseException
- If theContent-Type
header is missing.
-
ensureContentType
@Deprecated public void ensureContentType() throws ParseException
Deprecated.- Throws:
ParseException
- See Also:
ensureEntityContentType()
-
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.
-
ensureContentType
@Deprecated public void ensureContentType(javax.mail.internet.ContentType contentType) throws ParseException
Deprecated.- Throws:
ParseException
- See Also:
ensureEntityContentType(ContentType)
-
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()
Returns the HTTP headers.- Returns:
- The HTTP headers.
-
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.
-
-