org.jasig.cas.client.util
Class CommonUtils

java.lang.Object
  extended by org.jasig.cas.client.util.CommonUtils

public final class CommonUtils
extends Object

Common utilities so that we don't need to include Commons Lang.

Since:
3.0
Version:
$Revision: 11729 $ $Date: 2007-09-26 14:22:30 -0400 (Tue, 26 Sep 2007) $
Author:
Scott Battaglia

Method Summary
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

assertNotNull

public static void assertNotNull(Object object,
                                 String message)
Check whether the object is null or not. If it is, throw an exception and display the message.

Parameters:
object - the object to check.
message - the message to display if the object is null.

assertNotEmpty

public static void assertNotEmpty(Collection<?> c,
                                  String message)
Check whether the collection is null or empty. If it is, throw an exception and display the message.

Parameters:
c - the collection to check.
message - the message to display if the object is null.

assertTrue

public static void assertTrue(boolean cond,
                              String message)
Assert that the statement is true, otherwise throw an exception with the provided message.

Parameters:
cond - the condition to assert is true.
message - the message to display if the condition is not true.

assertFalse

public static void assertFalse(boolean cond,
                               String message)
Assert that the statement is true, otherwise throw an exception with the provided message.

Parameters:
cond - the condition to assert is false.
message - the message to display if the condition is not false.

isEmpty

public static boolean isEmpty(String string)
Determines whether the String is null or of length 0.

Parameters:
string - the string to check
Returns:
true if its null or length of 0, false otherwise.

isNotEmpty

public static boolean isNotEmpty(String string)
Determines if the String is not empty. A string is not empty if it is not null and has a length > 0.

Parameters:
string - the string to check
Returns:
true if it is not empty, false otherwise.

isBlank

public static boolean isBlank(String string)
Determines if a String is blank or not. A String is blank if its empty or if it only contains spaces.

Parameters:
string - the string to check
Returns:
true if its blank, false otherwise.

isNotBlank

public static boolean isNotBlank(String string)
Determines if a string is not blank. A string is not blank if it contains at least one non-whitespace character.

Parameters:
string - the string to check.
Returns:
true if its not blank, false otherwise.

constructRedirectUrl

public static String constructRedirectUrl(String casServerLoginUrl,
                                          String serviceParameterName,
                                          String serviceUrl,
                                          boolean renew,
                                          boolean gateway)
Constructs the URL to use to redirect to the CAS server.

Parameters:
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.
Returns:
the fully constructed redirect url.

urlEncode

public static String urlEncode(String value)
Url encode a value using UTF-8 encoding.

Parameters:
value - the value to encode.
Returns:
the encoded value.

readAndRespondToProxyReceptorRequest

public static void readAndRespondToProxyReceptorRequest(javax.servlet.http.HttpServletRequest request,
                                                        javax.servlet.http.HttpServletResponse response,
                                                        ProxyGrantingTicketStorage proxyGrantingTicketStorage)
                                                 throws IOException
Throws:
IOException

findMatchingServerName

protected static String findMatchingServerName(javax.servlet.http.HttpServletRequest request,
                                               String serverName)

constructServiceUrl

@Deprecated
public static String constructServiceUrl(javax.servlet.http.HttpServletRequest request,
                                                    javax.servlet.http.HttpServletResponse response,
                                                    String service,
                                                    String serverNames,
                                                    String artifactParameterName,
                                                    boolean encode)
Deprecated. 

Constructs a service url from the HttpServletRequest or from the given serviceUrl. Prefers the serviceUrl provided if both a serviceUrl and a serviceName. Compiles a list of all service parameters for supported protocols and removes them all from the query string.

Parameters:
request - the HttpServletRequest
response - the HttpServletResponse
service - 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).
Returns:
the service url to use.

constructServiceUrl

public 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. Prefers the serviceUrl provided if both a serviceUrl and a serviceName.

Parameters:
request - the HttpServletRequest
response - the HttpServletResponse
service - 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).
Returns:
the service url to use.

safeGetParameter

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

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.

Parameters:
request - the request to check.
parameter - the parameter to look for.
Returns:
the value of the parameter.

safeGetParameter

public static String safeGetParameter(javax.servlet.http.HttpServletRequest request,
                                      String parameter)

getResponseFromServer

@Deprecated
public static String getResponseFromServer(String constructedUrl,
                                                      String encoding)
Deprecated. 

Contacts the remote URL and returns the response.

Parameters:
constructedUrl - the url to contact.
encoding - the encoding to use.
Returns:
the response.

getResponseFromServer

@Deprecated
public static String getResponseFromServer(URL constructedUrl,
                                                      String encoding)
Deprecated. 


getResponseFromServer

public static String getResponseFromServer(URL constructedUrl,
                                           HttpURLConnectionFactory factory,
                                           String encoding)
Contacts the remote URL and returns the response.

Parameters:
constructedUrl - the url to contact.
factory - connection factory to prepare the URL connection instance
encoding - the encoding to use.
Returns:
the response.

createProxyList

public static ProxyList createProxyList(String proxies)

sendRedirect

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

Parameters:
response - the HttpServletResponse. CANNOT be NULL.
url - the url to redirect to.

closeQuietly

public static void closeQuietly(Closeable resource)
Unconditionally close a Closeable. Equivalent to Closeable.close()close(), except any exceptions will be ignored. This is typically used in finally blocks.

Parameters:
resource - the resource to close

toBoolean

public 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
 

Parameters:
str - the String to check
Returns:
the boolean value of the string, false if no match or the String is null

toBooleanObject

public 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
 

Parameters:
str - the String to check; upper and lower case are treated as the same
Returns:
the Boolean value of the string, null if no match or null input

toLong

public 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
 

Parameters:
str - the string to convert, may be null
defaultValue - the default value
Returns:
the long represented by the string, or the default if conversion fails

toInt

public 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
 

Parameters:
str - the string to convert, may be null
defaultValue - the default value
Returns:
the int represented by the string, or the default if conversion fails


Copyright © 2006-2015 Jasig. All Rights Reserved.