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 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
-
-
Field Summary
Fields Modifier and Type Field Description static intSC_BAD_REQUESTHTTP status code (400) indicating a bad request.static intSC_CREATEDHTTP status code (201) indicating the request succeeded with a new resource being created.static intSC_FORBIDDENHTTP status code (403) indicating that access to the resource was forbidden.static intSC_FOUNDHTTP status code (302) indicating that the resource resides temporarily under a different URI (redirect).static intSC_OKHTTP status code (200) indicating the request succeeded.static intSC_SERVER_ERRORHTTP status code (500) indicating an internal server error.static intSC_SERVICE_UNAVAILABLEHTTP status code (503) indicating the server is unavailable.static intSC_UNAUTHORIZEDHTTP 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 voidensureContentType()Deprecated.voidensureContentType(javax.mail.internet.ContentType contentType)Deprecated.voidensureEntityContentType()Ensures this HTTP message has aContent-Typeheader value.voidensureEntityContentType(com.nimbusds.common.contenttype.ContentType contentType)Ensures this HTTP message has the specifiedContent-Typeheader value.voidensureStatusCode(int... expectedStatusCode)Ensures this HTTP response has the specified status code.voidensureStatusCodeNotOK()Ensures this HTTP response does not have a200 OKstatus code.StringgetCacheControl()Gets theCache-Controlheader value.StringgetClientIPAddress()Gets the client IP address.StringgetContent()Gets the raw response content.net.minidev.json.JSONArraygetContentAsJSONArray()Gets the response content as a JSON array.net.minidev.json.JSONObjectgetContentAsJSONObject()Gets the response content as a JSON object.com.nimbusds.jwt.JWTgetContentAsJWT()Gets the response content as a JSON Web Token (JWT).javax.mail.internet.ContentTypegetContentType()Deprecated.com.nimbusds.common.contenttype.ContentTypegetEntityContentType()Gets theContent-Typeheader value.Map<String,List<String>>getHeaderMap()Returns the HTTP headers.StringgetHeaderValue(String name)Gets an HTTP header's value.List<String>getHeaderValues(String name)Gets an HTTP header's value(s).URIgetLocation()Gets theLocationheader value (for redirects).StringgetPragma()Gets thePragmaheader value.intgetStatusCode()Gets the HTTP status code.StringgetStatusMessage()Gets the HTTP status message.StringgetWWWAuthenticate()Gets theWWW-Authenticateheader value.booleanindicatesSuccess()Returnstrueif the HTTP status code indicates success (2xx).voidsetCacheControl(String cacheControl)Sets theCache-Controlheader value.voidsetClientIPAddress(String clientIPAddress)Sets the client IP address.voidsetContent(String content)Sets the raw response content.voidsetContentType(String ct)Sets theContent-Typeheader value.voidsetContentType(javax.mail.internet.ContentType ct)Deprecated.voidsetEntityContentType(com.nimbusds.common.contenttype.ContentType ct)Sets theContent-Typeheader value.voidsetHeader(String name, String... values)Sets an HTTP header.voidsetLocation(URI location)Sets theLocationheader value (for redirects).voidsetPragma(String pragma)Sets thePragmaheader value.voidsetStatusMessage(String message)Sets the HTTP status message.voidsetWWWAuthenticate(String wwwAuthenticate)Sets theWWW-Authenticateheader 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()
Returnstrueif the HTTP status code indicates success (2xx).- Returns:
trueif 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 OKstatus 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,
nullif not specified.
-
setStatusMessage
public void setStatusMessage(String message)
Sets the HTTP status message.- Parameters:
message- The HTTP status message,nullif not specified.
-
getLocation
public URI getLocation()
Gets theLocationheader value (for redirects).- Returns:
- The header value,
nullif not specified.
-
setLocation
public void setLocation(URI location)
Sets theLocationheader value (for redirects).- Parameters:
location- The header value,nullif not specified.
-
getCacheControl
public String getCacheControl()
Gets theCache-Controlheader value.- Returns:
- The header value,
nullif not specified.
-
setCacheControl
public void setCacheControl(String cacheControl)
Sets theCache-Controlheader value.- Parameters:
cacheControl- The header value,nullif not specified.
-
getPragma
public String getPragma()
Gets thePragmaheader value.- Returns:
- The header value,
nullif not specified.
-
setPragma
public void setPragma(String pragma)
Sets thePragmaheader value.- Parameters:
pragma- The header value,nullif not specified.
-
getWWWAuthenticate
public String getWWWAuthenticate()
Gets theWWW-Authenticateheader value.- Returns:
- The header value,
nullif not specified.
-
setWWWAuthenticate
public void setWWWAuthenticate(String wwwAuthenticate)
Sets theWWW-Authenticateheader value.- Parameters:
wwwAuthenticate- The header value,nullif not specified.
-
getContent
public String getContent()
Gets the raw response content.- Returns:
- The raw response content,
nullif 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,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
public void setContentType(String ct) throws ParseException
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.
-
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 ParseExceptionEnsures this HTTP message has aContent-Typeheader value.- Throws:
ParseException- If theContent-Typeheader 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 ParseExceptionEnsures 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.
-
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,
nullif 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),
nullif 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). Ifnulland 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,
nullif not specified.
-
setClientIPAddress
public void setClientIPAddress(String clientIPAddress)
Sets the client IP address.- Parameters:
clientIPAddress- The client IP address,nullif not specified.
-
-