@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 and 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 and Description |
---|
HTTPResponse(int statusCode)
Creates a new minimal HTTP response with the specified status code.
|
Modifier and Type | Method and 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 |
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. |
String |
getHeader(String name)
Gets a HTTP header value.
|
Map<String,String> |
getHeaders()
Returns the HTTP headers.
|
URI |
getLocation()
Gets the
Location header value (for redirects). |
String |
getPragma()
Gets the
Pragma header value. |
int |
getStatusCode()
Gets the HTTP status code.
|
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 |
setContent(String content)
Sets the raw response content.
|
void |
setContentType(javax.mail.internet.ContentType ct)
Sets the
Content-Type header value. |
void |
setContentType(String ct)
Sets the
Content-Type header value. |
void |
setHeader(String name,
String value)
Sets a HTTP header value.
|
void |
setLocation(URI location)
Sets the
Location header value (for redirects). |
void |
setPragma(String pragma)
Sets the
Pragma header value. |
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 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 getHeader(String name)
name
- The header name. Must not be null
.null
if not specified.public void setHeader(String name, String value)
name
- The header name. Must not be null
.value
- The header value. If null
and a header with the
same name is specified, it will be deleted.public Map<String,String> getHeaders()
Copyright © 2015 Connect2id Ltd.. All Rights Reserved.