public final class CommonUtils extends Object
Modifier and Type | Method and Description |
---|---|
static void |
assertFalse(boolean cond,
String message)
Assert that the statement is true, otherwise throw an exception with the
provided message.
|
static void |
assertNotEmpty(Collection<?> c,
String message)
Check whether the collection is null or empty.
|
static void |
assertNotNull(Object object,
String message)
Check whether the object is null or not.
|
static void |
assertTrue(boolean cond,
String message)
Assert that the statement is true, otherwise throw an exception with the
provided message.
|
static void |
closeQuietly(Closeable resource)
Unconditionally close a
Closeable . |
static String |
constructRedirectUrl(String casServerLoginUrl,
String serviceParameterName,
String serviceUrl,
boolean renew,
boolean gateway)
Constructs the URL to use to redirect to the CAS server.
|
static String |
constructServiceUrl(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
String service,
String serverNames,
String artifactParameterName,
boolean encode)
Deprecated.
|
static String |
constructServiceUrl(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
String service,
String serverNames,
String serviceParameterName,
String artifactParameterName,
boolean encode)
Constructs a service url from the HttpServletRequest or from the given
serviceUrl.
|
static ProxyList |
createProxyList(String proxies) |
protected static String |
findMatchingServerName(javax.servlet.http.HttpServletRequest request,
String serverName) |
static String |
getResponseFromServer(String constructedUrl,
String encoding)
Deprecated.
|
static String |
getResponseFromServer(URL constructedUrl,
HttpURLConnectionFactory factory,
String encoding)
Contacts the remote URL and returns the response.
|
static String |
getResponseFromServer(URL constructedUrl,
String encoding)
Deprecated.
|
static boolean |
isBlank(String string)
Determines if a String is blank or not.
|
static boolean |
isEmpty(String string)
Determines whether the String is null or of length 0.
|
static boolean |
isNotBlank(String string)
Determines if a string is not blank.
|
static boolean |
isNotEmpty(String string)
Determines if the String is not empty.
|
static void |
readAndRespondToProxyReceptorRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
ProxyGrantingTicketStorage proxyGrantingTicketStorage) |
static String |
safeGetParameter(javax.servlet.http.HttpServletRequest request,
String parameter) |
static String |
safeGetParameter(javax.servlet.http.HttpServletRequest request,
String parameter,
List<String> parameters)
Safe method for retrieving a parameter from the request without disrupting the reader UNLESS the parameter
actually exists in the query string.
|
static void |
sendRedirect(javax.servlet.http.HttpServletResponse response,
String url)
Sends the redirect message and captures the exceptions that we can't possibly do anything with.
|
static boolean |
toBoolean(String str)
Converts a String to a boolean (optimised for performance).
|
static Boolean |
toBooleanObject(String str)
Converts a String to a Boolean.
|
static int |
toInt(String str,
int defaultValue)
Convert a
String to an int , returning a
default value if the conversion fails. |
static long |
toLong(String str,
long defaultValue)
Convert a
String to a long , returning a
default value if the conversion fails. |
static String |
urlEncode(String value)
Url encode a value using UTF-8 encoding.
|
public static void assertNotNull(Object object, String message)
object
- the object to check.message
- the message to display if the object is null.public static void assertNotEmpty(Collection<?> c, String message)
c
- the collection to check.message
- the message to display if the object is null.public static void assertTrue(boolean cond, String message)
cond
- the condition to assert is true.message
- the message to display if the condition is not true.public static void assertFalse(boolean cond, String message)
cond
- the condition to assert is false.message
- the message to display if the condition is not false.public static boolean isEmpty(String string)
string
- the string to checkpublic static boolean isNotEmpty(String string)
string
- the string to checkpublic static boolean isBlank(String string)
string
- the string to checkpublic static boolean isNotBlank(String string)
string
- the string to check.public static String constructRedirectUrl(String casServerLoginUrl, String serviceParameterName, String serviceUrl, boolean renew, boolean gateway)
casServerLoginUrl
- the CAS Server login url.serviceParameterName
- the name of the parameter that defines the service.serviceUrl
- the actual service's url.renew
- whether we should send renew or not.gateway
- where we should send gateway or not.public static String urlEncode(String value)
value
- the value to encode.public static void readAndRespondToProxyReceptorRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, ProxyGrantingTicketStorage proxyGrantingTicketStorage) throws IOException
IOException
protected static String findMatchingServerName(javax.servlet.http.HttpServletRequest request, String serverName)
@Deprecated public static String constructServiceUrl(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String service, String serverNames, String artifactParameterName, boolean encode)
request
- the HttpServletRequestresponse
- the HttpServletResponseservice
- the configured service url (this will be used if not null)serverNames
- the server name to use to construct the service url if the service param is empty. Note, prior to CAS Client 3.3, this was a single value.
As of 3.3, it can be a space-separated value. We keep it as a single value, but will convert it to an array internally to get the matching value. This keeps backward compatability with anything using this public
method.artifactParameterName
- the artifact parameter name to remove (i.e. ticket)encode
- whether to encode the url or not (i.e. Jsession).public static String constructServiceUrl(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String service, String serverNames, String serviceParameterName, String artifactParameterName, boolean encode)
request
- the HttpServletRequestresponse
- the HttpServletResponseservice
- the configured service url (this will be used if not null)serverNames
- the server name to use to construct the service url if the service param is empty. Note, prior to CAS Client 3.3, this was a single value.
As of 3.3, it can be a space-separated value. We keep it as a single value, but will convert it to an array internally to get the matching value. This keeps backward compatability with anything using this public
method.serviceParameterName
- the service parameter name to remove (i.e. service)artifactParameterName
- the artifact parameter name to remove (i.e. ticket)encode
- whether to encode the url or not (i.e. Jsession).public static String safeGetParameter(javax.servlet.http.HttpServletRequest request, String parameter, List<String> parameters)
Note, this does not work for POST Requests for "logoutRequest". It works for all other CAS POST requests because the parameter is ALWAYS in the GET request.
If we see the "logoutRequest" parameter we MUST treat it as if calling the standard request.getParameter.
Note, that as of 3.3.0, we've made it more generic.
request
- the request to check.parameter
- the parameter to look for.public static String safeGetParameter(javax.servlet.http.HttpServletRequest request, String parameter)
@Deprecated public static String getResponseFromServer(String constructedUrl, String encoding)
constructedUrl
- the url to contact.encoding
- the encoding to use.@Deprecated public static String getResponseFromServer(URL constructedUrl, String encoding)
public static String getResponseFromServer(URL constructedUrl, HttpURLConnectionFactory factory, String encoding)
constructedUrl
- the url to contact.factory
- connection factory to prepare the URL connection instanceencoding
- the encoding to use.public static void sendRedirect(javax.servlet.http.HttpServletResponse response, String url)
response
- the HttpServletResponse. CANNOT be NULL.url
- the url to redirect to.public static void closeQuietly(Closeable resource)
Closeable
. Equivalent to Closeable.close()
close(), except any exceptions
will be ignored. This is typically used in finally blocks.resource
- the resource to closepublic static boolean toBoolean(String str)
Converts a String to a boolean (optimised for performance).
'true'
, 'on'
, 'y'
, 't'
or 'yes'
(case insensitive) will return true
. Otherwise,
false
is returned.
This method performs 4 times faster (JDK1.4) than
Boolean.valueOf(String)
. However, this method accepts
'on' and 'yes', 't', 'y' as true values.
BooleanUtils.toBoolean(null) = false BooleanUtils.toBoolean("true") = true BooleanUtils.toBoolean("TRUE") = true BooleanUtils.toBoolean("tRUe") = true BooleanUtils.toBoolean("on") = true BooleanUtils.toBoolean("yes") = true BooleanUtils.toBoolean("false") = false BooleanUtils.toBoolean("x gti") = false BooleanUtils.toBooleanObject("y") = true BooleanUtils.toBooleanObject("n") = false BooleanUtils.toBooleanObject("t") = true BooleanUtils.toBooleanObject("f") = false
str
- the String to checkfalse
if no match or the String is nullpublic static Boolean toBooleanObject(String str)
Converts a String to a Boolean.
'true'
, 'on'
, 'y'
, 't'
or 'yes'
(case insensitive) will return true
.
'false'
, 'off'
, 'n'
, 'f'
or 'no'
(case insensitive) will return false
.
Otherwise, null
is returned.
NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.
// N.B. case is not significant BooleanUtils.toBooleanObject(null) = null BooleanUtils.toBooleanObject("true") = Boolean.TRUE BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE] BooleanUtils.toBooleanObject("false") = Boolean.FALSE BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse] BooleanUtils.toBooleanObject("No") = Boolean.FALSE BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o] BooleanUtils.toBooleanObject("on") = Boolean.TRUE BooleanUtils.toBooleanObject("ON") = Boolean.TRUE BooleanUtils.toBooleanObject("off") = Boolean.FALSE BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE BooleanUtils.toBooleanObject("yes") = Boolean.TRUE BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES] BooleanUtils.toBooleanObject("blue") = null BooleanUtils.toBooleanObject("true ") = null // trailing space (too long) BooleanUtils.toBooleanObject("ono") = null // does not match on or no
str
- the String to check; upper and lower case are treated as the samenull
if no match or null
inputpublic static long toLong(String str, long defaultValue)
Convert a String
to a long
, returning a
default value if the conversion fails.
If the string is null
, the default value is returned.
NumberUtils.toLong(null, 1L) = 1L NumberUtils.toLong("", 1L) = 1L NumberUtils.toLong("1", 0L) = 1L
str
- the string to convert, may be nulldefaultValue
- the default valuepublic static int toInt(String str, int defaultValue)
Convert a String
to an int
, returning a
default value if the conversion fails.
If the string is null
, the default value is returned.
NumberUtils.toInt(null, 1) = 1 NumberUtils.toInt("", 1) = 1 NumberUtils.toInt("1", 0) = 1
str
- the string to convert, may be nulldefaultValue
- the default valueCopyright © 2006–2018 Jasig. All rights reserved.