@ThreadSafe public class HTTPResponse extends Object
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:
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 | Description |
---|---|
HTTPResponse(int statusCode) |
Creates a new minimal HTTP response with the specified status code.
|
Modifier and Type | Method | Description |
---|---|---|
void |
ensureContentType() |
Ensures this HTTP message has a
Content-Type header value. |
void |
ensureContentType(javax.mail.internet.ContentType contentType) |
Ensures this HTTP message has the specified
Content-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 a
200 OK
status code. |
String |
getCacheControl() |
Gets the
Cache-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() |
Gets the
Content-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 the
Location header value (for redirects). |
String |
getPragma() |
Gets the
Pragma header value. |
int |
getStatusCode() |
Gets the HTTP status code.
|
String |
getStatusMessage() |
Gets the HTTP status message.
|
String |
getWWWAuthenticate() |
Gets the
WWW-Authenticate header value. |
boolean |
indicatesSuccess() |
Returns
true if the HTTP status code indicates success
(2xx). |
void |
setCacheControl(String cacheControl) |
Sets the
Cache-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 the
Content-Type header value. |
void |
setContentType(javax.mail.internet.ContentType ct) |
Sets the
Content-Type header value. |
void |
setHeader(String name,
String... values) |
Sets an HTTP header.
|
void |
setLocation(URI location) |
Sets the
Location header value (for redirects). |
void |
setPragma(String pragma) |
Sets the
Pragma header value. |
void |
setStatusMessage(String message) |
Sets the HTTP status message.
|
void |
setWWWAuthenticate(String wwwAuthenticate) |
Sets the
WWW-Authenticate header value. |
public static final int SC_OK
public static final int SC_CREATED
public static final int SC_FOUND
public static final int SC_BAD_REQUEST
public static final int SC_UNAUTHORIZED
public static final int SC_FORBIDDEN
public static final int SC_SERVER_ERROR
public static final int SC_SERVICE_UNAVAILABLE
public HTTPResponse(int statusCode)
statusCode
- The HTTP status code.public int getStatusCode()
public boolean indicatesSuccess()
true
if the HTTP status code indicates success
(2xx).true
if the HTTP status code indicates success, else
false
.public void ensureStatusCode(int... expectedStatusCode) throws ParseException
expectedStatusCode
- The expected status code(s).ParseException
- If the status code of this HTTP response
doesn't match the expected.public void ensureStatusCodeNotOK() throws ParseException
200 OK
status code.ParseException
- If the status code of this HTTP response is
200 OK.public String getStatusMessage()
null
if not specified.public void setStatusMessage(String message)
message
- The HTTP status message, null
if not
specified.public URI getLocation()
Location
header value (for redirects).null
if not specified.public void setLocation(URI location)
Location
header value (for redirects).location
- The header value, null
if not specified.public String getCacheControl()
Cache-Control
header value.null
if not specified.public void setCacheControl(String cacheControl)
Cache-Control
header value.cacheControl
- The header value, null
if not specified.public String getPragma()
Pragma
header value.null
if not specified.public void setPragma(String pragma)
Pragma
header value.pragma
- The header value, null
if not specified.public String getWWWAuthenticate()
WWW-Authenticate
header value.null
if not specified.public void setWWWAuthenticate(String wwwAuthenticate)
WWW-Authenticate
header value.wwwAuthenticate
- The header value, null
if not
specified.public String getContent()
null
if none.public net.minidev.json.JSONObject getContentAsJSONObject() throws ParseException
ParseException
- If the Content-Type header isn't
application/json
, the response
content is null
, empty or couldn't be
parsed to a valid JSON object.public net.minidev.json.JSONArray getContentAsJSONArray() throws ParseException
ParseException
- If the Content-Type header isn't
application/json
, the response
content is null
, empty or couldn't be
parsed to a valid JSON array.public com.nimbusds.jwt.JWT getContentAsJWT() throws ParseException
ParseException
- If the Content-Type header isn't
application/jwt
, the response content
is null
, empty or couldn't be parsed
to a valid JSON Web Token (JWT).public void setContent(String content)
content
- The raw response content, null
if none.public javax.mail.internet.ContentType getContentType()
Content-Type
header value.Content-Type
header value, null
if not
specified.public void setContentType(javax.mail.internet.ContentType ct)
Content-Type
header value.ct
- The Content-Type
header value, null
if not
specified.public void setContentType(String ct) throws ParseException
Content-Type
header value.ct
- The Content-Type
header value, null
if not
specified.ParseException
- If the header value couldn't be parsed to a
valid content type.public void ensureContentType() throws ParseException
Content-Type
header value.ParseException
- If the Content-Type
header is
missing.public void ensureContentType(javax.mail.internet.ContentType contentType) throws ParseException
Content-Type
header value. This method compares only the primary type and
subtype; any content type parameters, such as charset
, are
ignored.contentType
- The expected content type. Must not be
null
.ParseException
- If the Content-Type
header is missing
or its primary and subtype don't match.public String getHeaderValue(String name)
name
- The header name. Must not be null
.null
if not specified.public List<String> getHeaderValues(String name)
name
- The header name. Must not be null
.null
if not specified.public void setHeader(String name, String... values)
name
- The header name. Must not be null
.values
- The header value(s). If null
and a header with
the same name is specified, it will be deleted.public Map<String,List<String>> getHeaderMap()
public String getClientIPAddress()
null
if not specified.public void setClientIPAddress(String clientIPAddress)
clientIPAddress
- The client IP address, null
if not
specified.Copyright © 2019 Connect2id Ltd.. All rights reserved.