Package com.nimbusds.oauth2.sdk.http
Class HTTPRequest
- java.lang.Object
-
- com.nimbusds.oauth2.sdk.http.HTTPRequest
-
@ThreadSafe public class HTTPRequest extends Object
HTTP request with support for the parameters required to construct anOAuth 2.0 request message.Supported HTTP methods:
Supported request headers:
- Content-Type
- Authorization
- Accept
- Etc.
Supported timeouts:
- On HTTP connect
- On HTTP response read
HTTP 3xx redirection: follow (default) / don't follow
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classHTTPRequest.MethodEnumeration of the HTTP methods used in OAuth 2.0 requests.
-
Constructor Summary
Constructors Constructor Description HTTPRequest(HTTPRequest.Method method, URL url)Creates a new minimally specified HTTP request.
-
Method Summary
All Methods Static 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.voidensureMethod(HTTPRequest.Method expectedMethod)Ensures this HTTP request has the specified method.StringgetAccept()Gets theAcceptheader value.StringgetAuthorization()Gets theAuthorizationheader value.StringgetClientIPAddress()Gets the client IP address.X509CertificategetClientX509Certificate()Gets the received validated client X.509 certificate for a received HTTPS request.StringgetClientX509CertificateRootDN()Gets the root issuer DN of a received validated client X.509 certificate for a received HTTPS request.StringgetClientX509CertificateSubjectDN()Gets the subject DN of a received validated client X.509 certificate for a received HTTPS request.intgetConnectTimeout()Gets the HTTP connect timeout.javax.mail.internet.ContentTypegetContentType()Deprecated.static HostnameVerifiergetDefaultHostnameVerifier()Returns the default hostname verifier for all outgoing HTTPS requests.static SSLSocketFactorygetDefaultSSLSocketFactory()Returns the default SSL socket factory for all outgoing HTTPS requests.com.nimbusds.common.contenttype.ContentTypegetEntityContentType()Gets theContent-Typeheader value.booleangetFollowRedirects()Gets the boolean setting whether HTTP redirects (requests with response code 3xx) should be automatically followed.StringgetFragment()Gets the raw (undecoded) request fragment.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).HostnameVerifiergetHostnameVerifier()Gets the hostname verifier for outgoing HTTPS requests.HTTPRequest.MethodgetMethod()Gets the request method.StringgetQuery()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.JSONObjectgetQueryAsJSONObject()Gets the request query or entity body as a JSON Object.Map<String,List<String>>getQueryParameters()Gets the request query as a parameter map.intgetReadTimeout()Gets the HTTP response read timeout.SSLSocketFactorygetSSLSocketFactory()Gets the SSL factory for outgoing HTTPS requests.URIgetURI()Gets the request URL as URI.URLgetURL()Gets the request URL.HTTPResponsesend()Sends this HTTP request to the request URL and retrieves the resulting HTTP response.HTTPResponsesend(HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory)Deprecated.voidsetAccept(String accept)Sets theAcceptheader value.voidsetAuthorization(String authz)Sets theAuthorizationheader value.voidsetClientIPAddress(String clientIPAddress)Sets the client IP address.voidsetClientX509Certificate(X509Certificate clientX509Certificate)Sets the received validated client X.509 certificate for a received HTTPS request.voidsetClientX509CertificateRootDN(String rootDN)Sets the root issuer DN of a received validated client X.509 certificate for a received HTTPS request.voidsetClientX509CertificateSubjectDN(String subjectDN)Sets the subject DN of a received validated client X.509 certificate for a received HTTPS request.voidsetConnectTimeout(int connectTimeout)Sets the HTTP connect timeout.voidsetContentType(String ct)Sets theContent-Typeheader value.voidsetContentType(javax.mail.internet.ContentType ct)Deprecated.static voidsetDefaultHostnameVerifier(HostnameVerifier defaultHostnameVerifier)Sets the default hostname verifier for all outgoing HTTPS requests.static voidsetDefaultSSLSocketFactory(SSLSocketFactory sslSocketFactory)Sets the default SSL socket factory for all outgoing HTTPS requests.voidsetEntityContentType(com.nimbusds.common.contenttype.ContentType ct)Sets theContent-Typeheader value.voidsetFollowRedirects(boolean follow)Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed.voidsetFragment(String fragment)Sets the raw (undecoded) request fragment.voidsetHeader(String name, String... values)Sets an HTTP header.voidsetHostnameVerifier(HostnameVerifier hostnameVerifier)Sets the hostname verifier for outgoing HTTPS requests.voidsetQuery(String query)Sets the raw (undecoded) query string if the request is HTTP GET or the entity body if the request is HTTP POST.voidsetReadTimeout(int readTimeout)Sets the HTTP response read timeout.voidsetSSLSocketFactory(SSLSocketFactory sslSocketFactory)Sets the SSL factory for outgoing HTTPS requests.HttpURLConnectiontoHttpURLConnection()Returns an established HTTP URL connection for this HTTP request.HttpURLConnectiontoHttpURLConnection(HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory)Deprecated.
-
-
-
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 benull.url- The HTTP request URL. Must not benull.
-
-
Method Detail
-
getMethod
public HTTPRequest.Method getMethod()
Gets the request method.- Returns:
- The request method.
-
ensureMethod
public void ensureMethod(HTTPRequest.Method expectedMethod) throws ParseException
Ensures this HTTP request has the specified method.- Parameters:
expectedMethod- The expected method. Must not benull.- Throws:
ParseException- If the method doesn't match the expected.
-
getAuthorization
public String getAuthorization()
Gets theAuthorizationheader value.- Returns:
- The
Authorizationheader value,nullif not specified.
-
setAuthorization
public void setAuthorization(String authz)
Sets theAuthorizationheader value.- Parameters:
authz- TheAuthorizationheader value,nullif not specified.
-
getAccept
public String getAccept()
Gets theAcceptheader value.- Returns:
- The
Acceptheader value,nullif not specified.
-
setAccept
public void setAccept(String accept)
Sets theAcceptheader value.- Parameters:
accept- TheAcceptheader value,nullif 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.
nullif 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.nullif not specified.
-
getQueryParameters
public Map<String,List<String>> getQueryParameters()
Gets the request query as a parameter map. The parameters are decoded according toapplication/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'tapplication/json, the request query or entity body isnull, empty or couldn't be parsed to a valid JSON object.
-
getFragment
public String getFragment()
Gets the raw (undecoded) request fragment.- Returns:
- The request fragment,
nullif not specified.
-
setFragment
public void setFragment(String fragment)
Sets the raw (undecoded) request fragment.- Parameters:
fragment- The request fragment,nullif not specified.
-
getConnectTimeout
public int getConnectTimeout()
Gets the HTTP connect timeout.- Returns:
- The HTTP connect timeout, in milliseconds. Zero implies no timeout.
-
setConnectTimeout
public void setConnectTimeout(int connectTimeout)
Sets the HTTP connect timeout.- Parameters:
connectTimeout- The HTTP connect timeout, in milliseconds. Zero implies no timeout. Must not be negative.
-
getReadTimeout
public int getReadTimeout()
Gets the HTTP response read timeout.- Returns:
- The HTTP response read timeout, in milliseconds. Zero implies no timeout.
-
setReadTimeout
public void setReadTimeout(int readTimeout)
Sets the HTTP response read timeout.- Parameters:
readTimeout- The HTTP response read timeout, in milliseconds. Zero implies no timeout. Must not be negative.
-
getFollowRedirects
public boolean getFollowRedirects()
Gets the boolean setting whether HTTP redirects (requests with response code 3xx) should be automatically followed.- Returns:
trueif HTTP redirects are automatically followed, elsefalse.
-
setFollowRedirects
public void setFollowRedirects(boolean follow)
Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed.- Parameters:
follow- Whether or not to follow HTTP redirects.
-
getClientX509Certificate
public X509Certificate getClientX509Certificate()
Gets the received validated client X.509 certificate for a received HTTPS request.- Returns:
- The client X.509 certificate,
nullif not specified.
-
setClientX509Certificate
public void setClientX509Certificate(X509Certificate clientX509Certificate)
Sets the received validated client X.509 certificate for a received HTTPS request.- Parameters:
clientX509Certificate- The client X.509 certificate,nullif not specified.
-
getClientX509CertificateSubjectDN
public String getClientX509CertificateSubjectDN()
Gets the subject DN of a received validated client X.509 certificate for a received HTTPS request.- Returns:
- The subject DN,
nullif not specified.
-
setClientX509CertificateSubjectDN
public void setClientX509CertificateSubjectDN(String subjectDN)
Sets the subject DN of a received validated client X.509 certificate for a received HTTPS request.- Parameters:
subjectDN- The subject DN,nullif not specified.
-
getClientX509CertificateRootDN
public String getClientX509CertificateRootDN()
Gets the root issuer DN of a received validated client X.509 certificate for a received HTTPS request.- Returns:
- The root DN,
nullif not specified.
-
setClientX509CertificateRootDN
public void setClientX509CertificateRootDN(String rootDN)
Sets the root issuer DN of a received validated client X.509 certificate for a received HTTPS request.- Parameters:
rootDN- The root DN,nullif not specified.
-
getHostnameVerifier
public HostnameVerifier getHostnameVerifier()
Gets the hostname verifier for outgoing HTTPS requests.- Returns:
- The hostname verifier,
nullimplies use of thedefault one.
-
setHostnameVerifier
public void setHostnameVerifier(HostnameVerifier hostnameVerifier)
Sets the hostname verifier for outgoing HTTPS requests.- Parameters:
hostnameVerifier- The hostname verifier,nullimplies use of thedefault one.
-
getSSLSocketFactory
public SSLSocketFactory getSSLSocketFactory()
Gets the SSL factory for outgoing HTTPS requests.- Returns:
- The SSL factory,
nullimplies of the default one.
-
setSSLSocketFactory
public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory)
Sets the SSL factory for outgoing HTTPS requests. Use theTLS utilityto set a custom trust store for server and CA certificates and / or a custom key store for client private keys and certificates, also to select a specific TLS protocol version.- Parameters:
sslSocketFactory- The SSL factory,nullimplies use of the default one.
-
getDefaultHostnameVerifier
public static HostnameVerifier getDefaultHostnameVerifier()
Returns the default hostname verifier for all outgoing HTTPS requests.- Returns:
- The hostname verifier.
-
setDefaultHostnameVerifier
public static void setDefaultHostnameVerifier(HostnameVerifier defaultHostnameVerifier)
Sets the default hostname verifier for all outgoing HTTPS requests. Can be overridden on a individual request basis.- Parameters:
defaultHostnameVerifier- The hostname verifier. Must not benull.
-
getDefaultSSLSocketFactory
public static SSLSocketFactory getDefaultSSLSocketFactory()
Returns the default SSL socket factory for all outgoing HTTPS requests.- Returns:
- The SSL socket factory.
-
setDefaultSSLSocketFactory
public static void setDefaultSSLSocketFactory(SSLSocketFactory sslSocketFactory)
Sets the default SSL socket factory for all outgoing HTTPS requests. Can be overridden on a individual request basis. Use theTLS utilityto set a custom trust store for server and CA certificates and / or a custom key store for client private keys and certificates, also to select a specific TLS protocol version.- Parameters:
sslSocketFactory- The SSL socket factory. Must not benull.
-
toHttpURLConnection
@Deprecated public HttpURLConnection toHttpURLConnection(HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory) throws IOException
Deprecated.Returns an established HTTP URL connection for this HTTP request. Deprecated as of v5.31, usetoHttpURLConnection()withsetHostnameVerifier(javax.net.ssl.HostnameVerifier)andsetSSLSocketFactory(javax.net.ssl.SSLSocketFactory)instead.- Parameters:
hostnameVerifier- The hostname verifier for outgoing HTTPS requests,nullimplies use of thedefault one.sslSocketFactory- The SSL socket factory for HTTPS requests,nullimplies use of thedefault one.- Returns:
- The HTTP URL connection, with the request sent and ready to read the response.
- Throws:
IOException- If the HTTP request couldn't be made, due to a network or other error.
-
toHttpURLConnection
public HttpURLConnection toHttpURLConnection() throws IOException
Returns an established HTTP URL connection for this HTTP request.- Returns:
- The HTTP URL connection, with the request sent and ready to read the response.
- Throws:
IOException- If the HTTP request couldn't be made, due to a network or other error.
-
send
@Deprecated public HTTPResponse send(HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory) throws IOException
Deprecated.Sends this HTTP request to the request URL and retrieves the resulting HTTP response. Deprecated as of v5.31, usetoHttpURLConnection()withsetHostnameVerifier(javax.net.ssl.HostnameVerifier)andsetSSLSocketFactory(javax.net.ssl.SSLSocketFactory)instead.- Parameters:
hostnameVerifier- The hostname verifier for outgoing HTTPS requests,nullimplies use of thedefault one.sslSocketFactory- The SSL socket factory for HTTPS requests,nullimplies use of thedefault one.- Returns:
- The resulting HTTP response.
- Throws:
IOException- If the HTTP request couldn't be made, due to a network or other error.
-
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.
-
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.
-
-