|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
com.sas.servlet.util.Util
public class Util
Utility methods for use within JSP pages and/or servlets.
The majority of the methods in the Util class deal with sharing
objects within the server: stored Connection
connections
and shared Rocf
object factories. There are also
generic methods to store session bound data; i.e. data that
is associated with the servlet session, as well as
global data that has a timeout. This utility class allow
you to find Connection/Rocf objects which are stored on
and bound to an HTTP session. These objects are automatically
stopped when the HTTP session ends. This is done through
the BoundConnection
and BoundRocf
wrapper objects.
synchronized (getLock()
) { ... }
block. For example, if you wish to cache some computed
result, such as the values from a data table on a SAS server,
use:
synchronized (Util.getLock()) { SimpleTable table = (SimpleTable) Util.getGlobalObject(yourUniqueDataName); if (table == null) table = ...; // code to populate table here Util.putGlobalObject(yourUniqueDataName, table); }However, you should be careful that the code within the critical section does not take a long time to execute, since on a server, many other sessions may block on this lock. If the code is potentially time consuming, and it is not an problem to generate the same data twice if multiple threads access it, is to compute the data first, if the global data does not yet exist, then store it in a critical section only if another thread has not already done so:
SimpleTable table = (SimpleTable) Util.getGlobalObject(yourUniqueDataName); if (table == null) table = ...; // code to populate table here else return table; synchronized (Util.getLock()) { SimpleTable gTable = (SimpleTable) Util.getGlobalObject(yourUniqueDataName); if (gTable == null) // no other thread created the data. Save new global data. { gTable = table; Util.putGlobalObject(yourUniqueDataName, gTable); } else // although we computed a table, another thrad did as well // and beat us to the punch for storing the global data, // so we discard our version and use the global data version. table = gTable; // discard }
Although session based objects have less potential for
asynchronous thread contention on shared session data
in Util, you should still guard the get/put operations
with a synchronized block on getLock()
since some servlets may use multiple threads per session.
BoundConnection
,
BoundRocf
Field Summary | |
---|---|
static java.lang.String |
CONNECTION
|
static java.lang.String |
ROCF
|
Constructor Summary | |
---|---|
Util()
|
Method Summary | |
---|---|
static com.sas.collection.StringCollection |
createStringCollection(com.sas.collection.StaticOrderedCollectionInterface model)
Creates a copy of the data provided by the model implementing StaticOrderedCollectionInterface. |
static com.sas.collection.StringCollection |
createStringCollection(com.sas.collection.StaticOrderedCollectionInterface model,
boolean sort)
Creates a copy of the data provided by the model implementing StaticOrderedCollectionInterface. |
static java.lang.String |
decodeBase64(java.lang.String s)
Decode a base64 encoded string. |
static java.lang.String[] |
decodeURLParameter(java.lang.String[] value,
javax.servlet.http.HttpServletResponse response,
javax.servlet.http.HttpServletRequest request)
Decodes an ascii string value to the specified character encoding |
static java.lang.String[] |
decodeURLParameter(java.lang.String[] value,
java.lang.String encode)
Decodes an ascii string value to the specified character encoding |
static java.lang.String |
decodeURLParameter(java.lang.String value,
javax.servlet.http.HttpServletResponse response,
javax.servlet.http.HttpServletRequest request)
Decodes an ascii string value to the specified character encoding |
static java.lang.String |
decodeURLParameter(java.lang.String value,
java.lang.String encode)
Decodes an ascii string value to the specified character encoding |
static com.sas.rmi.Rocf |
getClassFactory(javax.servlet.http.HttpServletRequest req)
Returns the default webAF class factory. |
static com.sas.rmi.Rocf |
getClassFactory(javax.servlet.http.HttpServletRequest req,
boolean create)
Returns the default webAF class factory |
static com.sas.rmi.Rocf |
getClassFactory(javax.servlet.http.HttpServletRequest req,
boolean create,
java.lang.String name)
Returns the named webAF class factory |
static com.sas.rmi.Connection |
getConnection(javax.servlet.http.HttpServletRequest req)
Returns the session's default webAF connection. |
static com.sas.rmi.Connection |
getConnection(javax.servlet.http.HttpServletRequest req,
boolean create)
Returns the session's default webAF connection |
static com.sas.rmi.Connection |
getConnection(javax.servlet.http.HttpServletRequest req,
boolean create,
java.lang.String name)
Returns the named webAF connection associated with a servlet session. |
static java.lang.Object |
getGlobalObject(java.lang.String name)
Returns the named object from the global object pool or null if the named object does not exist. |
static long |
getGlobalObjectLastUsedMillis(java.lang.String name)
Returns the last time the named object from the global object pool was used. |
static java.util.Enumeration |
getGlobalObjectNames()
Returns an enumeration of all the global object names. |
static java.lang.Object |
getLock()
Return a lock object which may be used as a monitor for critical sections which get and put either session objects or global objects. |
static com.sas.collection.hlist.HListInterface |
getParametersAsList(javax.servlet.http.HttpServletRequest req)
Creates an HList containing all of the request parameters. |
static java.lang.Object |
getSessionObject(javax.servlet.http.HttpServletRequest req,
java.lang.String name)
Returns the named object from the session or null if the named object does not exist See also the important discussion above about synchronizing access to session and global data for the proper way to use putSessionObject(HttpServletRequest, String, Object) and getSessionObject |
protected static boolean |
isEncodingSet(javax.servlet.http.HttpServletRequest request)
Checks if characterEncoding is set on the request object. |
static java.lang.Object |
newInstance(javax.servlet.http.HttpServletRequest req,
java.lang.Class c)
Creates a new instance of a remote model using the default class factory and connection |
static java.lang.Object |
newInstance(com.sas.rmi.Rocf rocf,
com.sas.rmi.Connection connection,
java.lang.Class c)
Creates a new instance of a remote model using the class factory and connection given. |
static void |
putGlobalObject(java.lang.String name,
java.lang.Object value)
Puts the named object in the global object pool. |
static void |
putGlobalObject(java.lang.String name,
java.lang.Object value,
long seconds)
Puts the named object in the global object pool. |
static void |
putSessionObject(javax.servlet.http.HttpServletRequest req,
java.lang.String name,
java.lang.Object value)
Puts the named object in the session. |
static java.lang.String |
replaceSpecialCharacters(java.lang.String inputString,
int browserType)
Utility method for replacing special characters (&,$,<,>,',"). |
static void |
resetBeanStringProperties(java.lang.Object bean,
javax.servlet.ServletRequest request,
java.lang.String properties,
boolean alwaysReset,
java.lang.String defaultValue)
Reset String properties of a bean to a default value. |
static void |
setClassFactory(javax.servlet.http.HttpServletRequest req,
com.sas.rmi.Rocf rocf)
Sets the default webAF class factory. |
static void |
setClassFactory(javax.servlet.http.HttpServletRequest req,
com.sas.rmi.Rocf rocf,
java.lang.String name)
Sets the named webAF class factory associated with the session. |
static void |
setConnection(javax.servlet.http.HttpServletRequest req,
com.sas.rmi.Connection con)
Sets the default webAF connection |
static void |
setConnection(javax.servlet.http.HttpServletRequest req,
com.sas.rmi.Connection con,
java.lang.String name)
Sets the named webAF connection. |
Field Detail |
---|
public static final java.lang.String ROCF
public static final java.lang.String CONNECTION
Constructor Detail |
---|
public Util()
Method Detail |
---|
public static com.sas.rmi.Rocf getClassFactory(javax.servlet.http.HttpServletRequest req) throws javax.servlet.ServletException, java.io.IOException
req
- The request object. If req is null,
this method does nothing and returns null.
The object factory will be bound to this req's
session.
javax.servlet.ServletException
- thrown if some type of servlet
error occurs
java.io.IOException
- thrown if some type of I/O error occurspublic static com.sas.rmi.Rocf getClassFactory(javax.servlet.http.HttpServletRequest req, boolean create) throws javax.servlet.ServletException, java.io.IOException
req
- The request object. If req is null,
this method does nothing and returns null.
The object factory will be bound to this req's
session.create
- Set to true if the object should be created
javax.servlet.ServletException
- thrown if some type of servlet
error occurs
java.io.IOException
public static com.sas.rmi.Rocf getClassFactory(javax.servlet.http.HttpServletRequest req, boolean create, java.lang.String name) throws javax.servlet.ServletException, java.io.IOException
req
- The request object. If req is null,
this method does nothing and returns null.
The object factory will be bound to this req's
session.create
- Set to true if the object should be createdname
- The object name
javax.servlet.ServletException
- if some type of servlet
error occurs
java.io.IOException
- if some type of I/O error occurspublic static java.lang.Object getLock()
java.util.ArrayList data = null; synchronized ( Util.getLock() ) { data = (java.util.ArrayList) Util.getGlobalObject("myGlobalData"); if (data == null) { data = new java.util.ArrayList() Util.putGlobalData(MY_KEY, data); } }
public static void setClassFactory(javax.servlet.http.HttpServletRequest req, com.sas.rmi.Rocf rocf) throws javax.servlet.ServletException, java.io.IOException
req
- The request object. If req is null,
this method does nothing.rocf
- The class factory, or null to remove the current value
javax.servlet.ServletException
- if some type of servlet
error occurs
java.io.IOException
- if some type of I/O error occurspublic static void setClassFactory(javax.servlet.http.HttpServletRequest req, com.sas.rmi.Rocf rocf, java.lang.String name) throws javax.servlet.ServletException, java.io.IOException
req
- The request object. If req is null,
this method does nothing.rocf
- The class factory, or null to remove the current valuename
- The object name
javax.servlet.ServletException
- if some type of servlet
error occurs
java.io.IOException
- if some type of I/O error occurspublic static com.sas.rmi.Connection getConnection(javax.servlet.http.HttpServletRequest req) throws javax.servlet.ServletException, java.io.IOException
req
- The request object. If req is null,
this method does nothing and returns null.
The connection will be bound to this req's
session.
javax.servlet.ServletException
- if some type of servlet
error occurs
java.io.IOException
- if some type of I/O error occurspublic static com.sas.rmi.Connection getConnection(javax.servlet.http.HttpServletRequest req, boolean create) throws javax.servlet.ServletException, java.io.IOException
req
- The request object. If req is null,
this method does nothing and returns null.
The connection will be bound to this req's
session.create
- Set to true if the object should be created
javax.servlet.ServletException
- if some type of servlet
error occurs
java.io.IOException
- if some type of I/O error occurspublic static com.sas.rmi.Connection getConnection(javax.servlet.http.HttpServletRequest req, boolean create, java.lang.String name) throws javax.servlet.ServletException, java.io.IOException
req
- The request object. If req is null,
this method does nothing and returns null.
The connection will be bound to this req's
session.create
- Set to true if the object should be createdname
- The object name
javax.servlet.ServletException
- if some type of servlet
error occurs
java.io.IOException
- if some type of I/O error occurspublic static void setConnection(javax.servlet.http.HttpServletRequest req, com.sas.rmi.Connection con) throws javax.servlet.ServletException, java.io.IOException
req
- The request object. If req is null,
this method does nothing.con
- The connection, or null to remove the current value
javax.servlet.ServletException
- if some type of servlet
error occurs
java.io.IOException
- if some type of I/O error occurspublic static void setConnection(javax.servlet.http.HttpServletRequest req, com.sas.rmi.Connection con, java.lang.String name) throws javax.servlet.ServletException, java.io.IOException
req
- The request object. If req is null,
this method does nothing.con
- The connection, or null to remove the current valuename
- The object name
javax.servlet.ServletException
- if some type of servlet
error occurs
java.io.IOException
- if some type of I/O error occurspublic static java.lang.Object newInstance(javax.servlet.http.HttpServletRequest req, java.lang.Class c) throws javax.servlet.ServletException, java.io.IOException
req
- The request objectclass
- The class to create
javax.servlet.ServletException
- if some type of servlet
error occurs
java.io.IOException
- if some type of I/O error occurspublic static java.lang.Object newInstance(com.sas.rmi.Rocf rocf, com.sas.rmi.Connection connection, java.lang.Class c) throws javax.servlet.ServletException, java.io.IOException
rocf
- The class factoryconnection
- The connectionclass
- The class to create
javax.servlet.ServletException
- if some type of servlet
error occurs
java.io.IOException
- if some type of I/O error occurspublic static java.lang.Object getSessionObject(javax.servlet.http.HttpServletRequest req, java.lang.String name) throws javax.servlet.ServletException
See also the important discussion above about
synchronizing access to session and global data
for the proper way to use putSessionObject(HttpServletRequest, String, Object)
and getSessionObject
req
- The request objectname
- The name of the object
javax.servlet.ServletException
- if some type of servlet
error occursputSessionObject(HttpServletRequest, String, Object)
,
putGlobalObject(String, Object)
,
putGlobalObject(String, Object, long)
public static void putSessionObject(javax.servlet.http.HttpServletRequest req, java.lang.String name, java.lang.Object value) throws javax.servlet.ServletException
See also the important discussion above about
synchronizing access to session and global data
for the proper way to use putSessionObject
and getSessionObject(HttpServletRequest, String)
req
- The request objectname
- The name of the objectvalue
- The value of the object
javax.servlet.ServletException
- if some type of servlet
error occursgetSessionObject(HttpServletRequest,String)
public static java.lang.Object getGlobalObject(java.lang.String name)
See also the important discussion above about
synchronizing access to session and global data
for the proper way to use putGlobalObject(String, Object)
and getGLobalObject(String)
name
- The name of the object
putGlobalObject(String, Object, long)
,
putGlobalObject(String, Object)
public static long getGlobalObjectLastUsedMillis(java.lang.String name)
name
- The name of the object
public static java.util.Enumeration getGlobalObjectNames()
public static void putGlobalObject(java.lang.String name, java.lang.Object value)
See also the important discussion above about
synchronizing access to session and global data
for the proper way to use putGlobalObject
and getGlobalObject(String)
name
- The name of the objectvalue
- The value of the object, or null to remove the object from
the poolgetGlobalObject(String)
,
putGlobalObject(String,Object,long)
public static void putGlobalObject(java.lang.String name, java.lang.Object value, long seconds)
See also the important discussion above about
synchronizing access to session and global data
for the proper way to use putGlobalObject
and getGlobalObject(String)
name
- The name of the objectvalue
- The value of the object, or null to remove the object fromtimeoutSeconds
- The number of seconds before timing out
the poolgetGlobalObject(java.lang.String)
public static com.sas.collection.StringCollection createStringCollection(com.sas.collection.StaticOrderedCollectionInterface model)
model
- The model containing the data
public static com.sas.collection.StringCollection createStringCollection(com.sas.collection.StaticOrderedCollectionInterface model, boolean sort)
model
- The model containing the datasort
- true if the data should be sorted
public static com.sas.collection.hlist.HListInterface getParametersAsList(javax.servlet.http.HttpServletRequest req)
Creates an HList containing all of the request parameters. This is useful for passing the request parameters to SCL methods, for example. Single-value parameters will be stored in a StringItem (name-value pair). Multiple-value parameters will be stored as a named ListItem with each value being an unnamed StringItem within the sublist.
For example, the parameters from a simple form with 3 input fields (first name, last name, and favorite colors) might return the list:
Name | Value | Object Type |
---|---|---|
first | Bugs | StringItem |
last | Bunny | StringItem |
color | Red | StringItem |
Another simple form with 3 input fields, one if which is a list box that allows multiple selections, might return the list:
Name | Value | Object Type |
---|---|---|
first | Bugs | StringItem |
last | Bunny | StringItem |
color | (object) | ListItem |
With the 'color' list object containing:
Name | Value | Object Type |
---|---|---|
null | Red | StringItem |
null | Green | StringItem |
null | Orange | StringItem |
request
- The HTTP request
public static java.lang.String decodeBase64(java.lang.String s)
s
- The base64 encoded string
public static void resetBeanStringProperties(java.lang.Object bean, javax.servlet.ServletRequest request, java.lang.String properties, boolean alwaysReset, java.lang.String defaultValue) throws javax.servlet.ServletException
This method works around a certain documented but often undesirable behavior
of the <jsp:setProperty ...>
JSP tag. The JSP spec for setProperty
says that property set methods are not called when
the corresponding servlet request parameters
are empty. Thus, if a user blanks out a value on
an HTML form, the property will not get set to "".
This method provides a way to reset such String properties of a bean
to blank.
A sample call is shown below.
This call is used to unconditionally reset the properties
named title and author to null before invoking
<jsp:setProperty>
This operation can also be done with JSP snippet code such as the following:<jsp:usBean id="book" scope="session" type="com.unknown.publishing.Book"/> <%Util.resetBeanStringProperties(book, null, "author,title", true, null;);%> <jsp:setProperty name="book" property="*" />
book.setTitle(""): book.setAuthor("");However, a JSP page may not want to unconditionally reset the properties. This is true if the corresponding parameters may not exist in the request (i.e. no
title
or author
servlet parameters), or when it may be expensive to set the property,
especially when the <jsp:setProperty ...>
code may then
turn around and set it to a non-blank value. Thus, the default behvaior
of resetBeanStringProperties
is to only reset a property if
the property is listed in the servlet parameters and if the value there
is blank.
For example, this call is used to reset the properties
named title and author to blank before invoking
<jsp:setProperty>
, but only if the corresponding
servlet request parameters are not blank:
See the JSP spec for more details on how<jsp:usBean id="book" scope="session" type="com.unknown.publishing.Book" /> <% Util.resetBeanStringProperties(book, request, "author,title", false, ""); %> <jsp:setProperty name="book" property="*" />
<jsp:setProperty>
works.
bean
- A bean whose String properties you wish to reset to blank or null values.
This is typically the subject of a following <jsp:setProperty>
tag.request
- The servlet request containing the servlet parameters, typically from a HTML form.
If null
, then all candidate properties are reset.properties
- A String containing a comma separated list of candidate properties,
such as "firstName,lastName"
, to reset.
If properties is null
or "*"
, then all writable String properties
will be candidates for being reset. Otherwise, only those listed in this String
will be candidates.alwaysReset
- controls which candidate properties are actually reset.
If true
, then
all candidate String properties with a write method will be reset.
If false
,
then only those candidate String properties which have blank parameter
values in the request are reset.defaultValue
- the value to assign to a property when resetting it. This value
is passed to the bean's property write method. Normally, this should
be the empty string ""
, but you can use any other String or
null
javax.servlet.ServletException
public static java.lang.String replaceSpecialCharacters(java.lang.String inputString, int browserType)
inputString
- The String on which to perform the replacementsbrowserType
- The type of browser
replaced with.
public static java.lang.String decodeURLParameter(java.lang.String value, java.lang.String encode)
value
- The string to decodeencode
- encode value
public static java.lang.String[] decodeURLParameter(java.lang.String[] value, java.lang.String encode)
value
- The string(s) to decodeencode
- encode value
public static java.lang.String decodeURLParameter(java.lang.String value, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpServletRequest request)
value
- The string to decoderesponse
- The response objectrequest
- The request object
public static java.lang.String[] decodeURLParameter(java.lang.String[] value, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpServletRequest request)
value
- The string to decoderesponse
- The response objectrequest
- The request object
protected static boolean isEncodingSet(javax.servlet.http.HttpServletRequest request)
request
- The request object
|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |