com.nimbusds.oauth2.sdk.http
Class HTTPResponse

java.lang.Object
  extended by com.nimbusds.oauth2.sdk.http.HTTPResponse

@ThreadSafe
public final class HTTPResponse
extends Object

HTTP response with support for the parameters required to construct an OAuth 2.0 response message. This class is thread-safe.

Provided HTTP status code constants:

Supported response headers:

Author:
Vladimir Dzhuvinov

Field Summary
static int SC_BAD_REQUEST
          HTTP status code (400) indicating a bad request.
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_UNAUTHORIZED
          HTTP status code (401) indicating that the request requires HTTP authentication.
 
Constructor Summary
HTTPResponse(int statusCode)
          Creates a new minimal HTTP response with the specified status code.
 
Method Summary
 void applyTo(javax.servlet.http.HttpServletResponse sr)
          Applies the status code, headers and content of this HTTP response object to the specified HTTP servlet response.
 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 statusCode)
          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.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.
 URL 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.
 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 setLocation(URL 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SC_OK

public static final int SC_OK
HTTP status code (200) indicating the request succeeded.

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
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.

ensureStatusCode

public void ensureStatusCode(int statusCode)
                      throws ParseException
Ensures this HTTP response has the specified status code.

Parameters:
statusCode - The expected status code.
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 a 200 OK status code.

Throws:
ParseException - If the status code of this HTTP response is 200 OK.

getLocation

public URL getLocation()
Gets the Location header value (for redirects).

Returns:
The header value, null if not specified.

setLocation

public void setLocation(URL location)
Sets the Location header value (for redirects).

Parameters:
location - The header value, null if not specified.

getCacheControl

public String getCacheControl()
Gets the Cache-Control header value.

Returns:
The header value, null if not specified.

setCacheControl

public void setCacheControl(String cacheControl)
Sets the Cache-Control header value.

Parameters:
cacheControl - The header value, null if not specified.

getPragma

public String getPragma()
Gets the Pragma header value.

Returns:
The header value, null if not specified.

setPragma

public void setPragma(String pragma)
Sets the Pragma header value.

Parameters:
pragma - The header value, null if not specified.

getWWWAuthenticate

public String getWWWAuthenticate()
Gets the WWW-Authenticate header value.

Returns:
The header value, null if not specified.

setWWWAuthenticate

public void setWWWAuthenticate(String wwwAuthenticate)
Sets the WWW-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't application/json, the response content is null, empty or couldn't be parsed to a valid JSON object.

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't application/jwt, the response content is null, 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.

applyTo

public void applyTo(javax.servlet.http.HttpServletResponse sr)
             throws IOException
Applies the status code, headers and content of this HTTP response object to the specified HTTP servlet response.

Parameters:
sr - The HTTP servlet response to have the properties of this HTTP request applied to. Must not be null.
Throws:
IOException - If the response content couldn't be written.

getContentType

public javax.mail.internet.ContentType getContentType()
Gets the Content-Type header value.

Returns:
The Content-Type header value, null if not specified.

setContentType

public void setContentType(javax.mail.internet.ContentType ct)
Sets the Content-Type header value.

Parameters:
ct - The Content-Type header value, null if not specified.

setContentType

public void setContentType(String ct)
                    throws ParseException
Sets the Content-Type header value.

Parameters:
ct - The Content-Type header value, null if not specified.
Throws:
ParseException - If the header value couldn't be parsed to a valid content type.

ensureContentType

public void ensureContentType()
                       throws ParseException
Ensures this HTTP message has a Content-Type header value.

Throws:
ParseException - If the Content-Type header is missing.

ensureContentType

public void ensureContentType(javax.mail.internet.ContentType contentType)
                       throws ParseException
Ensures this HTTP message has the specified Content-Type header value. This method compares only the primary type and subtype; any content type parameters, such as charset, are ignored.

Parameters:
contentType - The expected content type. Must not be null.
Throws:
ParseException - If the Content-Type header is missing or its primary and subtype don't match.


Copyright © 2013 NimbusDS. All Rights Reserved.