|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.click.util.ClickUtils
Provides miscellaneous Form, String and Stream utility methods.
Field Summary | |
static String |
DEFAULT_APP_CONFIG
The default Click configuration filename: "/WEB-INF/click.xml". |
static String |
ENABLE_RESOURCE_VERSION
The resource versioning request attribute: key: enable-resource-version. |
static String |
RESOURCE_VERSION_INDICATOR
The static web resource version number indicator string. |
Constructor Summary | |
ClickUtils()
|
Method Summary | |
static void |
autoPostRedirect(HttpServletRequest request,
HttpServletResponse response,
String target,
Map params,
boolean compress)
Perform an auto post redirect to the specified target using the given response. |
static Document |
buildDocument(InputStream inputStream)
Return a new XML Document for the given input stream. |
static Document |
buildDocument(InputStream inputStream,
EntityResolver entityResolver)
Return a new XML Document for the given input stream and XML entity resolver. |
static Class |
classForName(String classname)
Returns the Class object associated with the class or
interface with the given string name, using the current Thread context
class loader. |
static void |
close(InputStream stream)
Close the given input stream and ignore any exceptions thrown. |
static void |
close(OutputStream stream)
Close the given output stream and ignore any exceptions thrown. |
static void |
close(Reader reader)
Close the given reader and ignore any exceptions thrown. |
static void |
copyFormToObject(Form form,
Object object,
boolean debug)
Populate the given object's attributes with the Form's field values. |
static void |
copyObjectToForm(Object object,
Form form,
boolean debug)
Populate the given Form field values with the object's attributes. |
static String |
createHtmlImport(String pattern,
Context context)
Create an HTML import statement from the given string pattern and versionIndicator, formatted with the request context path. |
static Object |
decode(String string)
Return an object from the encode(Object) string. |
static String[] |
decodePasswordCookie(String cookieVal,
int xorMask)
Decodes a cookie string containing a username and password. |
static String[] |
decodePasswordCookie(String cookieVal,
String encoding,
int xorMask)
Decodes a cookie string containing a username and password. |
static String |
decodeURL(Object value)
URL decode the specified value using the "UTF-8" encoding scheme. |
static void |
deployFile(ServletContext servletContext,
String resource,
String targetDir)
Deploy the specified classpath resource to the given target directory under the web application root directory. |
static void |
deployFileList(ServletContext servletContext,
Class controlClass,
String targetDir)
Deploys required files (from a file list) for a control that repsects a specific convention. |
static void |
deployFiles(ServletContext servletContext,
String[] resources,
String targetDir)
Deploy the specified classpath resources to the given target directory under the web application root directory. |
static String |
encode(Object object)
Return an encoded version of the Serializable object. |
static String |
encodePasswordCookie(String username,
String password,
int xorMask)
Builds a cookie string containing a username and password. |
static String |
encodePasswordCookie(String username,
String password,
String encoding,
int xorMask)
Builds a cookie string containing a username and password, using offsets to customize the encoding. |
static String |
encodeURL(Object value)
URL encode the specified value using the "UTF-8" encoding scheme. |
static String |
encodeUrl(Object object,
Context context)
Return an encoded URL value for the given object using the context request character encoding. |
static String |
escapeHtml(String value)
Return a HTML escaped string for the given string value. |
static ResourceBundle |
getBundle(String baseName)
Return a resource bundle using the specified base name. |
static ResourceBundle |
getBundle(String baseName,
Locale locale)
Return a resource bundle using the specified base name and locale. |
static Element |
getChild(Element parent,
String name)
Return the first XML child Element for the given parent Element and child Element name. |
static List |
getChildren(Element parent,
String name)
Return the list of XML child Element elements with the given name from the given parent Element. |
static InputStream |
getClickConfig(ServletContext servletContext)
Return the InputStream for the Click configuration file click.xml. |
static String |
getClickVersion()
Return the Click Framework version string. |
static ConfigService |
getConfigService(ServletContext servletContext)
Return the application configuration service instance from the given servlet context. |
static Cookie |
getCookie(HttpServletRequest request,
String name)
Returns the specified Cookie object, or null if the cookie does not exist. |
static String |
getCookieValue(HttpServletRequest request,
String name)
Returns the value of the specified cookie as a String. |
static List |
getFormFields(Form form)
Return the list of Fields for the given Form, including any Fields contained in FieldSets. |
static LogService |
getLogService()
Return the application LogService instance using thread local Context to perform the lookup. |
static String |
getMimeType(String filename)
Return the mime-type or content-type for the given filename. |
static String |
getParentMessage(Control control,
String name)
Return the given control's top level parent's localized message for the specified name. |
static Map |
getParentMessages(Control control)
Return the given control's top level parent's localized messages Map. |
static Page |
getParentPage(Control control)
Get the parent page of the given control. |
static Map |
getRequestParameterMap(HttpServletRequest request)
Return an ordered map of request parameters from the given request. |
static String |
getRequestURI(HttpServletRequest request)
Return the requestURI from the request. |
static URL |
getResource(String name,
Class aClass)
Finds a resource with a given name. |
static InputStream |
getResourceAsStream(String name,
Class aClass)
Finds a resource with a given name. |
static String |
getResourcePath(HttpServletRequest request)
Return the page resource path from the request. |
static String |
getResourceVersionIndicator(Context context)
Return a version indicator for static web resources (eg css, js and image files), if resource versioning is active, otherwise this method will return an empty string. |
static void |
invalidateCookie(HttpServletRequest request,
HttpServletResponse response,
String cookieName)
Invalidate the specified cookie and delete it from the response object. |
static void |
invalidateCookie(HttpServletRequest request,
HttpServletResponse response,
String cookieName,
String path)
Invalidate the specified cookie and delete it from the response object. |
static boolean |
invokeListener(Object listener,
String method)
Invoke the named method on the given object and return the boolean result. |
static boolean |
isEnableResourceVersion(Context context)
Return true if static web content resource versioning is enabled. |
static boolean |
isMultipartRequest(HttpServletRequest request)
Return true if the request is a multi-part content type POST request. |
static String |
limitLength(String value,
int maxlength)
Return the value string limited to maxlength characters. |
static String |
limitLength(String value,
int maxlength,
String suffix)
Return the value string limited to maxlength characters. |
static Cookie |
setCookie(HttpServletRequest request,
HttpServletResponse response,
String name,
String value,
int maxAge,
String path)
Sets the given cookie values in the servlet response. |
static String |
toGetterName(String property)
Return the getter method name for the given property name. |
static String |
toIsGetterName(String property)
Return the is getter method name for the given property name. |
static String |
toLabel(String name)
Return a field label string from the given field name. |
static String |
toMD5Hash(String plaintext)
Return an 32 char MD5 encoded string from the given plain text. |
static String |
toName(String label)
Return a field name string from the given field label. |
static String |
toSetterName(String property)
Return the setter method name for the given property name. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final String ENABLE_RESOURCE_VERSION
Control.getHtmlImports()
will have a
version indicator added to their path.
Control.getHtmlImports()
,
createHtmlImport(String, Context)
,
getResourceVersionIndicator(Context)
,
Constant Field Valuespublic static final String DEFAULT_APP_CONFIG
public static final String RESOURCE_VERSION_INDICATOR
Constructor Detail |
public ClickUtils()
Method Detail |
public static void autoPostRedirect(HttpServletRequest request, HttpServletResponse response, String target, Map params, boolean compress)
request
- the servlet requestresponse
- the servlet responsetarget
- the target URL to send the auto post redirect toparams
- the map of parameter values to postcompress
- the flag to specify whether to attempt gzip compression
of the response contentpublic static Document buildDocument(InputStream inputStream)
inputStream
- the input stream
RuntimeException
- if a parsing error occurspublic static Document buildDocument(InputStream inputStream, EntityResolver entityResolver)
inputStream
- the input streamentityResolver
- the XML entity resolver
RuntimeException
- if a parsing error occurspublic static Class classForName(String classname) throws ClassNotFoundException
Class
object associated with the class or
interface with the given string name, using the current Thread context
class loader.
classname
- the name of the class to load
ClassNotFoundException
- if the class cannot be locatedpublic static void close(InputStream stream)
stream
- the input stream to close.public static void close(OutputStream stream)
stream
- the output stream to close.public static void close(Reader reader)
reader
- the reader to close.public static String createHtmlImport(String pattern, Context context)
ENABLE_RESOURCE_VERSION
is set to "true".
pattern
- the HTML import pattern string to formatcontext
- the request context
public static void invalidateCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String path)
request
- the servlet requestresponse
- the servlet responsecookieName
- The name of the cookie you want to deletepath
- of the path the cookie you want to deletepublic static boolean isMultipartRequest(HttpServletRequest request)
request
- the page servlet request
public static void invalidateCookie(HttpServletRequest request, HttpServletResponse response, String cookieName)
invalidateCookie(HttpServletRequest, HttpServletResponse, String, String)
This method was derived from Atlassian CookieUtils method of
the same name, release under the Apache License.
request
- the servlet requestresponse
- the servlet responsecookieName
- The name of the cookie you want to delete.invalidateCookie(HttpServletRequest, HttpServletResponse, String, String)
public static ResourceBundle getBundle(String baseName)
baseName
- the base name of the resource bundle, a fully qualified class name
MissingResourceException
- if no resource bundle for the specified base name can be foundpublic static ResourceBundle getBundle(String baseName, Locale locale)
baseName
- the base name of the resource bundle, a fully qualified class namelocale
- the locale for which a resource bundle is desired
MissingResourceException
- if no resource bundle for the specified base name can be foundpublic static Element getChild(Element parent, String name)
parent
- the parent element to get the child fromname
- the name of the child element
public static List getChildren(Element parent, String name)
parent
- the parent element to get the child fromname
- the name of the child element
public static InputStream getClickConfig(ServletContext servletContext)
servletContext
- the servlet context to obtain the Click configuration
from
RuntimeException
- if the resource could not be foundpublic static ConfigService getConfigService(ServletContext servletContext)
servletContext
- the servlet context to get the config service instance
public static Cookie getCookie(HttpServletRequest request, String name)
request
- the servlet requestname
- the name of the cookie
public static Cookie setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, int maxAge, String path)
request
- the servlet requestresponse
- the servlet responsename
- the cookie namevalue
- the cookie valuemaxAge
- the maximum age of the cookie in seconds. A negative
value will expire the cookie at the end of the session, while 0 will delete
the cookie.path
- the cookie path
public static String getCookieValue(HttpServletRequest request, String name)
request
- the servlet requestname
- the name of the cookie
public static String getClickVersion()
public static String getResourceVersionIndicator(Context context)
context
- the request context
public static void copyFormToObject(Form form, Object object, boolean debug)
Map
. If a POJO is specified, its attributes are
populated from matching form fields. If a map is specified, its
key/value pairs are populated from matching form fields.
form
- the Form to obtain field values fromobject
- the object to populate with field valuesdebug
- log debug statements when populating the objectpublic static void copyObjectToForm(Object object, Form form, boolean debug)
Map
. If a POJO is specified, its attributes are
copied to matching form fields. If a map is specified, its key/value
pairs are copied to matching form fields.
object
- the object to obtain attribute values fromform
- the Form to populatedebug
- log debug statements when populating the formpublic static void deployFile(ServletContext servletContext, String resource, String targetDir)
servletContext
- the web applications servlet contextresource
- the classpath resource nametargetDir
- the target directory to deploy the resource topublic static void deployFiles(ServletContext servletContext, String[] resources, String targetDir)
servletContext
- the web applications servlet contextresources
- the array of classpath resource namestargetDir
- the target directory to deploy the resource topublic static void deployFileList(ServletContext servletContext, Class controlClass, String targetDir)
tools/standalone/dev-tasks/ListFilesTask
.
The files to deploy are all in a subdirectory placed in the same directory with the control.
See documentation for more details.
Usage:
In your Control simply use the code below, and everything should work automatically.
public void onDeploy(ServletContext servletContext) { ClickUtils.deployFileList(servletContext, HeavyControl.class, "click"); }
servletContext
- the web applications servlet contextcontrolClass
- the class of the Control that has files for deploymenttargetDir
- target directory where to deploy the files to. In most cases this
is only the reserved directory click
public static String encode(Object object) throws IOException
object
- the object to encode
IOException
- if an I/O error occurs
IllegalArgumentException
- if the object parameter is null, or if
the object is not Serializablepublic static Object decode(String string) throws ClassNotFoundException, IOException
encode(Object)
string.
string
- the encoded string
ClassNotFoundException
- if the class could not be instantiated
IOException
- if an data I/O error occurspublic static String encodePasswordCookie(String username, String password, int xorMask)
username
- the usernamepassword
- the passwordxorMask
- the XOR mask to encrypt the value with, must be same as
as the value used to decrypt the cookie password
null
public static String encodePasswordCookie(String username, String password, String encoding, int xorMask)
username
- the usernamepassword
- the passwordencoding
- a String used to customize cookie encoding (only the first 3 characters are used)xorMask
- the XOR mask to encrypt the value with, must be same as
as the value used to decrypt the cookie password
null
.public static String[] decodePasswordCookie(String cookieVal, int xorMask)
cookieVal
- the encoded cookie username and password valuexorMask
- the XOR mask to decrypt the value with, must be same as
as the value used to encrypt the cookie password
{ null, null }
if cookieVal equals
null
or the empty string.public static String[] decodePasswordCookie(String cookieVal, String encoding, int xorMask)
cookieVal
- the encoded cookie username and password valueencoding
- a String used to customize cookie encoding (only the first 3 characters are used)
- should be the same string you used to encode the cookie!xorMask
- the XOR mask to decrypt the value with, must be same as
as the value used to encrypt the cookie password
{ null, null }
if cookieVal equals
null
or the empty string.public static String encodeURL(Object value)
URLEncoder.encode(java.lang.String, java.lang.String)
internally.
value
- the value to encode using "UTF-8"
public static String decodeURL(Object value)
URLDecoder.decode(java.lang.String, java.lang.String)
internally.
value
- the value to decode using "UTF-8"
public static String encodeUrl(Object object, Context context)
URLEncoder.encode(java.lang.String, java.lang.String)
internally.
object
- the object value to encode as a URL stringcontext
- the context providing the request character encoding
public static String escapeHtml(String value)
value
- the string value to escape
public static boolean invokeListener(Object listener, String method)
listener
- the object with the method to invokemethod
- the name of the method to invoke
Control.setListener(Object, String)
public static boolean isEnableResourceVersion(Context context)
context
- the request context
public static String limitLength(String value, int maxlength)
value
- the string value to limit the length ofmaxlength
- the maximum string length
public static String limitLength(String value, int maxlength, String suffix)
value
- the string value to limit the length ofmaxlength
- the maximum string lengthsuffix
- the suffix to append to the length limited string
public static LogService getLogService()
public static List getFormFields(Form form)
form
- the form to obtain the fields from
public static String getMimeType(String filename)
filename
- the filename to obtain the mime-type for
public static Map getParentMessages(Control control)
control
- the control to get the parent messages Map for
public static String getParentMessage(Control control, String name)
myfield.label=Pageand MyForm.properties
myfield.label=Formand a the following snippet:
public MyPage extends Page { public void onInit() { MyForm form = new MyForm("form"); TextField field = new TextField("myfield"); form.add(field); //1. System.out.println(ClickUtils.getParentMessage(field, "myfield.label")); addControl(form); //2. System.out.println(ClickUtils.getParentMessage(field, "myfield.label")); } }The first (1.) println statement above will output Form because at that stage MyForm is the highest level parent of field. getParentMessage will find the property myfield.label in the MyForm message properties and return Form The second (2.) println statement will output Page as now MyPage is the highest level parent. On its first pass up the hierarchy, getParentMessage will find the property myfield.label in the MyForm message properties and on its second pass will find the same property in MyPage message properties. As MyPage is higher up the hierarchy than MyForm, MyPage will override MyForm and the property value will be Page.
control
- the control to get the parent message forname
- the specific property name to find
public static Page getParentPage(Control control)
control
- the control to get the parent page from
public static Map getRequestParameterMap(HttpServletRequest request)
request
- the servlet request to obtain request parameters from
public static String getResourcePath(HttpServletRequest request)
http://www.mycorp.com/banking/secure/login.htm -> /secure/login.htm
request
- the page servlet request
public static String getRequestURI(HttpServletRequest request)
http://www.mycorp.com/banking/secure/login.htm -> /banking/secure/login.htm
request
- the page servlet request
public static InputStream getResourceAsStream(String name, Class aClass)
This method uses the current Thread context ClassLoader to find the resource. If the resource is not found the class loader of the given class is then used to find the resource.
name
- the name of the resourceaClass
- the class lookup the resource against, if the resource is
not found using the current Thread context ClassLoader.
public static URL getResource(String name, Class aClass)
This method uses the current Thread context ClassLoader to find the resource. If the resource is not found the class loader of the given class is then used to find the resource.
name
- the name of the resourceaClass
- the class lookup the resource against, if the resource is
not found using the current Thread context ClassLoader.
public static String toGetterName(String property)
property
- the property name
public static String toIsGetterName(String property)
property
- the property name
public static String toLabel(String name)
faxNumber -> Fax NumberNote toLabel will return an empty String ("") if a null String name is specified.
name
- the field name
public static String toMD5Hash(String plaintext)
plaintext
- the plain text value to encode
public static String toName(String label)
label
- the field label or caption
public static String toSetterName(String property)
property
- the property name
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |