com.nimbusds.oauth2.sdk.http
Class HTTPRequest

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

@ThreadSafe
public final class HTTPRequest
extends Object

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

Supported HTTP methods:

Supported request headers:

Author:
Vladimir Dzhuvinov

Nested Class Summary
static class HTTPRequest.Method
          Enumeration of the HTTP methods used in OAuth 2.0 requests.
 
Constructor Summary
HTTPRequest(HTTPRequest.Method method, URL url)
          Creates a new minimally specified HTTP request.
HTTPRequest(javax.servlet.http.HttpServletRequest sr)
          Creates a new HTTP request from the specified HTTP servlet request.
 
Method Summary
 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 ensureMethod(HTTPRequest.Method expectedMethod)
          Ensures this HTTP request has the specified method.
 String getAuthorization()
          Gets the Authorization header value.
 javax.mail.internet.ContentType getContentType()
          Gets the Content-Type header value.
 HTTPRequest.Method getMethod()
          Gets the request method.
 String getQuery()
          Gets the raw (undecoded) query string if the request is HTTP GET or the entity body if the request is HTTP POST.
 net.minidev.json.JSONObject getQueryAsJSONObject()
          Gets the request query or entity body as a JSON Object.
 Map<String,String> getQueryParameters()
          Gets the request query as a parameter map.
 URL getURL()
          Gets the request URL.
 HTTPResponse send()
          Sends this HTTP request to the request URL and retrieves the resulting HTTP response.
 void setAuthorization(String authz)
          Sets the Authorization header value.
 void setContentType(javax.mail.internet.ContentType ct)
          Sets the Content-Type header value.
 void setContentType(String ct)
          Sets the Content-Type header value.
 void setQuery(String query)
          Sets the raw (undecoded) query string if the request is HTTP GET or the entity body if the request is HTTP POST.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HTTPRequest

public HTTPRequest(HTTPRequest.Method method,
                   URL url)
Creates a new minimally specified HTTP request.

Parameters:
method - The HTTP request method. Must not be null.
url - The HTTP request URL. Must not be null.

HTTPRequest

public HTTPRequest(javax.servlet.http.HttpServletRequest sr)
            throws IOException
Creates a new HTTP request from the specified HTTP servlet request.

Parameters:
sr - The servlet request. Must not be null.
Throws:
IllegalArgumentException - The the servlet request method is not GET or POST, or the content type header value couldn't be parsed.
IOException - For a POST body that couldn't be read due to an I/O exception.
Method Detail

getMethod

public HTTPRequest.Method getMethod()
Gets the request method.

Returns:
The request method.

getURL

public URL getURL()
Gets the request URL.

Returns:
The request URL.

ensureMethod

public void ensureMethod(HTTPRequest.Method expectedMethod)
                  throws ParseException
Ensures this HTTP request has the specified method.

Parameters:
expectedMethod - The expected method. Must not be null.
Throws:
ParseException - If the method doesn't match the expected.

getAuthorization

public String getAuthorization()
Gets the Authorization header value.

Returns:
The Authorization header value, null if not specified.

setAuthorization

public void setAuthorization(String authz)
Sets the Authorization header value.

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

getQuery

public String getQuery()
Gets the raw (undecoded) query string if the request is HTTP GET or the entity body if the request is HTTP POST.

Note that the '?' character preceding the query string in GET requests is not included in the returned string.

Example query string (line breaks for clarity):

 response_type=code
 &client_id=s6BhdRkqt3
 &state=xyz
 &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
 

Returns:
For HTTP GET requests the URL query string, for HTTP POST requests the body. null if not specified.

setQuery

public void setQuery(String query)
Sets the raw (undecoded) query string if the request is HTTP GET or the entity body if the request is HTTP POST.

Note that the '?' character preceding the query string in GET requests must not be included.

Example query string (line breaks for clarity):

 response_type=code
 &client_id=s6BhdRkqt3
 &state=xyz
 &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
 

Parameters:
query - For HTTP GET requests the URL query string, for HTTP POST requests the body. null if not specified.

getQueryParameters

public Map<String,String> getQueryParameters()
Gets the request query as a parameter map. The parameters are decoded according to application/x-www-form-urlencoded.

Returns:
The request query parameters, decoded. If none the map will be empty.

getQueryAsJSONObject

public net.minidev.json.JSONObject getQueryAsJSONObject()
                                                 throws ParseException
Gets the request query or entity body as a JSON Object.

Returns:
The request query or entity body as a JSON object.
Throws:
ParseException - If the Content-Type header isn't application/json, the request query or entity body is null, empty or couldn't be parsed to a valid JSON object.

send

public HTTPResponse send()
                  throws IOException
Sends this HTTP request to the request URL and retrieves the resulting HTTP response.

Returns:
The resulting HTTP response.
Throws:
IOException - If the HTTP request couldn't be made, due to a network or other error.

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.