public class CsrfGuardUtils extends Object
Modifier and Type | Field and Description |
---|---|
static Object[] |
EMPTY_OBJECT_ARRAY
An empty immutable
Object array. |
Constructor and Description |
---|
CsrfGuardUtils() |
Modifier and Type | Method and Description |
---|---|
static String |
abbreviate(String str,
int maxWidth)
Abbreviates a String using ellipses.
|
static String |
abbreviate(String str,
int offset,
int maxWidth)
Abbreviates a String using ellipses.
|
static void |
assignField(Class theClass,
Object invokeOn,
String fieldName,
Object dataToAssign,
boolean callOnSupers,
boolean overrideSecurity,
boolean typeCast,
Class<? extends Annotation> annotationWithValueOverride)
assign data to a field
|
static void |
assignField(Field field,
Object invokeOn,
Object dataToAssign,
boolean overrideSecurity,
boolean typeCast)
assign data to a field
|
static void |
assignField(Field field,
Object invokeOn,
Object dataToAssign,
boolean overrideSecurity,
boolean typeCast,
Class<? extends Annotation> annotationWithValueOverride)
assign data to a field
|
static void |
assignField(Object invokeOn,
String fieldName,
Object dataToAssign)
assign data to a field.
|
static Object |
callMethod(Class theClass,
Object invokeOn,
String methodName)
helper method for calling a method with no params (could be in
superclass)
|
static Object |
callMethod(Class theClass,
Object invokeOn,
String methodName,
Object paramTypesOrArrayOrList,
Object paramsOrListOrArray)
helper method for calling a method (could be in superclass)
|
static Object |
callMethod(Class theClass,
Object invokeOn,
String methodName,
Object paramTypesOrArrayOrList,
Object paramsOrListOrArray,
boolean callOnSupers)
helper method for calling a method
|
static Object |
callMethod(Class theClass,
Object invokeOn,
String methodName,
Object paramTypesOrArrayOrList,
Object paramsOrListOrArray,
boolean callOnSupers,
boolean overrideSecurity)
helper method for calling a method
|
static String |
className(Object object)
null safe classname method, gets the unenhanced name
|
static String |
classNameCollection(Object object)
null safe classname method, max out at 20
|
static void |
closeQuietly(Connection connection)
close a connection null safe and dont throw exception
|
static void |
closeQuietly(InputStream input)
Unconditionally close an
InputStream . |
static void |
closeQuietly(OutputStream output)
Unconditionally close an
OutputStream . |
static void |
closeQuietly(Reader input)
Unconditionally close an
Reader . |
static void |
closeQuietly(ResultSet resultSet)
close a resultSet null safe and dont throw exception
|
static void |
closeQuietly(Statement statement)
close a statement null safe and dont throw exception
|
static void |
closeQuietly(Writer writer)
close a writer quietly
|
static void |
closeQuietly(XMLStreamWriter writer)
close a writer quietly
|
static String |
defaultString(String str)
Returns either the passed in String,
or if the String is
null , an empty String (""). |
static Field |
field(Class theClass,
String fieldName,
boolean callOnSupers,
boolean throwExceptionIfNotFound)
get a field object for a class, potentially in superclasses
|
static Class |
forName(String origClassName)
Returns the class object.
|
static Throwable |
getCause(Throwable throwable)
Introspects the
Throwable to obtain the cause. |
static Throwable |
getCause(Throwable throwable,
String[] methodNames)
Introspects the
Throwable to obtain the cause. |
static String |
getFullStackTrace(Throwable throwable)
A way to get the entire nested stack-trace of an throwable.
|
static String |
getInitParameter(javax.servlet.ServletConfig servletConfig,
String name,
String configFileDefaultParamValue,
String defaultValue) |
static Throwable[] |
getThrowables(Throwable throwable)
Returns the list of
Throwable objects in the
exception chain. |
static String |
httpProtocolAndDomain(String url)
for a url, get the protocol and domain, e.g.
|
static boolean |
injectInException(Throwable t,
String message)
If we can, inject this into the exception, else return false
|
static Object |
invokeMethod(Method method,
Object invokeOn)
Safely invoke a reflection method that takes no args
|
static Object |
invokeMethod(Method method,
Object invokeOn,
Object paramsOrListOrArray)
Safely invoke a reflection method
|
static boolean |
isBlank(Object input)
See if the input is null or if string, if it is empty or blank (whitespace)
|
static boolean |
isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
|
static boolean |
isNestedThrowable(Throwable throwable)
Checks whether this
Throwable class can store a cause. |
static boolean |
isThrowableNested()
Checks if the Throwable class has a
getCause method. |
static Iterator |
iterator(Object collection)
null safe iterator getter if the type if collection
|
static int |
length(Object arrayOrCollection)
Null safe array length or map
|
static <T> T |
newInstance(Class<T> theClass)
Construct a class
|
static Object |
next(Object arrayOrCollection,
Iterator iterator,
int index)
If array, get the element based on index, if Collection, get it based on
iterator.
|
static String |
readFileContent(String fileName) |
static String |
readInputStreamContent(InputStream is) |
static String |
readResourceFileContent(String resourceName,
boolean errorIfNotFound) |
static <T> T[] |
toArray(Collection collection,
Class<T> theClass)
convert a list into an array of type of theClass
|
static Object |
toArray(Object objectOrArrayOrCollection)
Convert a list to an array with the type of the first element e.g.
|
static String |
toStringForLog(Object object)
print out various types of objects
|
static String |
toStringForLog(Object object,
int maxChars)
print out various types of objects
|
static <T> T |
typeCast(Object value,
Class<T> theClass)
If necessary, convert an object to another type.
|
static <T> T |
typeCast(Object value,
Class<T> theClass,
boolean convertNullToDefaultPrimitive,
boolean useNewInstanceHooks)
If necessary, convert an object to another type.
|
public static final Object[] EMPTY_OBJECT_ARRAY
Object
array.public static String httpProtocolAndDomain(String url)
url
- public static Object callMethod(Class theClass, Object invokeOn, String methodName)
theClass
- the class which has the methodinvokeOn
- to call on or null for staticmethodName
- method name to callpublic static Object callMethod(Class theClass, Object invokeOn, String methodName, Object paramTypesOrArrayOrList, Object paramsOrListOrArray)
theClass
- the class which has the methodinvokeOn
- to call on or null for staticmethodName
- method name to callparamTypesOrArrayOrList
- types of the paramsparamsOrListOrArray
- datapublic static Object callMethod(Class theClass, Object invokeOn, String methodName, Object paramTypesOrArrayOrList, Object paramsOrListOrArray, boolean callOnSupers)
theClass
- the class which has the methodinvokeOn
- to call on or null for staticmethodName
- method name to callparamTypesOrArrayOrList
- types of the paramsparamsOrListOrArray
- datacallOnSupers
- if static and method not exists, try on superspublic static Object callMethod(Class theClass, Object invokeOn, String methodName, Object paramTypesOrArrayOrList, Object paramsOrListOrArray, boolean callOnSupers, boolean overrideSecurity)
theClass
- the class which has the methodinvokeOn
- to call on or null for staticmethodName
- method name to callparamTypesOrArrayOrList
- types of the paramsparamsOrListOrArray
- datacallOnSupers
- if static and method not exists, try on supersoverrideSecurity
- true to call on protected or private etc methodspublic static Class forName(String origClassName)
Returns the class object.
origClassName
- is fully qualifiedpublic static String getInitParameter(javax.servlet.ServletConfig servletConfig, String name, String configFileDefaultParamValue, String defaultValue)
public static String readResourceFileContent(String resourceName, boolean errorIfNotFound)
public static String readInputStreamContent(InputStream is)
public static boolean injectInException(Throwable t, String message)
t
- message
- public static boolean isBlank(Object input)
input
- public static boolean isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
isBlank(null) = true isBlank("") = true isBlank(" ") = true isBlank("bob") = false isBlank(" bob ") = false
str
- the String to check, may be nulltrue
if the String is null, empty or whitespacepublic static void assignField(Class theClass, Object invokeOn, String fieldName, Object dataToAssign, boolean callOnSupers, boolean overrideSecurity, boolean typeCast, Class<? extends Annotation> annotationWithValueOverride)
theClass
- the class which has the methodinvokeOn
- to call on or null for staticfieldName
- method name to calldataToAssign
- datacallOnSupers
- if static and method not exists, try on supersoverrideSecurity
- true to call on protected or private etc methodstypeCast
- true if we should typecastannotationWithValueOverride
- annotation with value of overridepublic static Object toArray(Object objectOrArrayOrCollection)
objectOrArrayOrCollection
- is a listpublic static int length(Object arrayOrCollection)
arrayOrCollection
- public static <T> T[] toArray(Collection collection, Class<T> theClass)
T
- is the type of the arraycollection
- list to converttheClass
- type of array to returnpublic static void assignField(Object invokeOn, String fieldName, Object dataToAssign)
invokeOn
- to call on or null for staticfieldName
- method name to calldataToAssign
- datapublic static Object invokeMethod(Method method, Object invokeOn)
method
- to invokeinvokeOn
- if NO_PARAMS then will not pass in params.public static Object invokeMethod(Method method, Object invokeOn, Object paramsOrListOrArray)
method
- to invokeinvokeOn
- paramsOrListOrArray
- must be an arg. If null, will pass null.
if NO_PARAMS then will not pass in params.public static String className(Object object)
object
- public static String classNameCollection(Object object)
object
- public static Iterator iterator(Object collection)
collection
- public static Object next(Object arrayOrCollection, Iterator iterator, int index)
arrayOrCollection
- iterator
- index
- public static void assignField(Field field, Object invokeOn, Object dataToAssign, boolean overrideSecurity, boolean typeCast, Class<? extends Annotation> annotationWithValueOverride)
field
- is the field to assign toinvokeOn
- to call on or null for staticdataToAssign
- dataoverrideSecurity
- true to call on protected or private etc methodstypeCast
- true if we should typecastannotationWithValueOverride
- annotation with value of override, or null if nonepublic static Field field(Class theClass, String fieldName, boolean callOnSupers, boolean throwExceptionIfNotFound)
theClass
- fieldName
- callOnSupers
- true if superclasses should be looked in for the fieldthrowExceptionIfNotFound
- will throw runtime exception if not foundpublic static void assignField(Field field, Object invokeOn, Object dataToAssign, boolean overrideSecurity, boolean typeCast)
field
- is the field to assign toinvokeOn
- to call on or null for staticdataToAssign
- dataoverrideSecurity
- true to call on protected or private etc methodstypeCast
- true if we should typecastpublic static <T> T typeCast(Object value, Class<T> theClass)
T
- is template typevalue
- theClass
- public static <T> T typeCast(Object value, Class<T> theClass, boolean convertNullToDefaultPrimitive, boolean useNewInstanceHooks)
T
- is the type to returnvalue
- theClass
- convertNullToDefaultPrimitive
- if the value is null, and theClass is primitive, should we
convert the null to a primitive default valueuseNewInstanceHooks
- if theClass is not recognized, then honor the string "null", "newInstance",
or get a constructor with one param, and call itpublic static <T> T newInstance(Class<T> theClass)
T
- template typetheClass
- public static void closeQuietly(Connection connection)
connection
- public static void closeQuietly(InputStream input)
InputStream
.
Equivalent to InputStream.close()
, except any exceptions will be ignored.input
- A (possibly null) InputStreampublic static void closeQuietly(OutputStream output)
OutputStream
.
Equivalent to OutputStream.close()
, except any exceptions will be ignored.output
- A (possibly null) OutputStreampublic static void closeQuietly(Reader input)
Reader
.
Equivalent to Reader.close()
, except any exceptions will be ignored.input
- A (possibly null) Readerpublic static void closeQuietly(ResultSet resultSet)
resultSet
- public static void closeQuietly(Statement statement)
statement
- public static void closeQuietly(Writer writer)
writer
- public static void closeQuietly(XMLStreamWriter writer)
writer
- public static String toStringForLog(Object object)
object
- public static String toStringForLog(Object object, int maxChars)
object
- maxChars
- is the max chars that should be returned (abbreviate if longer), or -1 for any amountpublic static String abbreviate(String str, int maxWidth)
Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "Now is the time for..."
Specifically:
str
is less than maxWidth
characters
long, return it.(substring(str, 0, max-3) + "...")
.maxWidth
is less than 4
, throw an
IllegalArgumentException
.maxWidth
.StringUtils.abbreviate(null, *) = null StringUtils.abbreviate("", 4) = "" StringUtils.abbreviate("abcdefg", 6) = "abc..." StringUtils.abbreviate("abcdefg", 7) = "abcdefg" StringUtils.abbreviate("abcdefg", 8) = "abcdefg" StringUtils.abbreviate("abcdefg", 4) = "a..." StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException
str
- the String to check, may be nullmaxWidth
- maximum length of result String, must be at least 4null
if null String inputIllegalArgumentException
- if the width is too smallpublic static String abbreviate(String str, int offset, int maxWidth)
Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "...is the time for..."
Works like abbreviate(String, int)
, but allows you to specify
a "left edge" offset. Note that this left edge is not necessarily going to
be the leftmost character in the result, or the first character following the
ellipses, but it will appear somewhere in the result.
In no case will it return a String of length greater than
maxWidth
.
StringUtils.abbreviate(null, *, *) = null StringUtils.abbreviate("", 0, 4) = "" StringUtils.abbreviate("abcdefghijklmno", -1, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 0, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 1, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 4, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 5, 10) = "...fghi..." StringUtils.abbreviate("abcdefghijklmno", 6, 10) = "...ghij..." StringUtils.abbreviate("abcdefghijklmno", 8, 10) = "...ijklmno" StringUtils.abbreviate("abcdefghijklmno", 10, 10) = "...ijklmno" StringUtils.abbreviate("abcdefghijklmno", 12, 10) = "...ijklmno" StringUtils.abbreviate("abcdefghij", 0, 3) = IllegalArgumentException StringUtils.abbreviate("abcdefghij", 5, 6) = IllegalArgumentException
str
- the String to check, may be nulloffset
- left edge of source StringmaxWidth
- maximum length of result String, must be at least 4null
if null String inputIllegalArgumentException
- if the width is too smallpublic static String getFullStackTrace(Throwable throwable)
A way to get the entire nested stack-trace of an throwable.
throwable
- the Throwable
to be examinedpublic static Throwable[] getThrowables(Throwable throwable)
Returns the list of Throwable
objects in the
exception chain.
A throwable without cause will return an array containing
one element - the input throwable.
A throwable with one cause will return an array containing
two elements. - the input throwable and the cause throwable.
A null
throwable will return an array size zero.
throwable
- the throwable to inspect, may be nullpublic static boolean isNestedThrowable(Throwable throwable)
Checks whether this Throwable
class can store a cause.
This method does not check whether it actually does store a cause.
throwable
- the Throwable
to examine, may be nulltrue
if nested otherwise false
public static Throwable getCause(Throwable throwable)
Introspects the Throwable
to obtain the cause.
The method searches for methods with specific names that return a
Throwable
object. This will pick up most wrapping exceptions,
including those from JDK 1.4, and
NestableException
.
The default list searched for are:
getCause()
getNextException()
getTargetException()
getException()
getSourceException()
getRootCause()
getCausedByException()
getNested()
In the absence of any such method, the object is inspected for a
detail
field assignable to a Throwable
.
If none of the above is found, returns null
.
throwable
- the throwable to introspect for a cause, may be nullThrowable
,
null
if none found or null throwable inputpublic static Throwable getCause(Throwable throwable, String[] methodNames)
Introspects the Throwable
to obtain the cause.
A null
set of method names means use the default set.
A null
in the set of method names will be ignored.
throwable
- the throwable to introspect for a cause, may be nullmethodNames
- the method names, null treated as default setThrowable
,
null
if none found or null throwable inputpublic static boolean isThrowableNested()
Checks if the Throwable class has a getCause
method.
This is true for JDK 1.4 and above.
public static String defaultString(String str)
Returns either the passed in String,
or if the String is null
, an empty String ("").
StringUtils.defaultString(null) = "" StringUtils.defaultString("") = "" StringUtils.defaultString("bat") = "bat"
str
- the String to check, may be nullnull
String.valueOf(Object)
Copyright © 2015 OWASP. All Rights Reserved.