Widening.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@2161 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -31,14 +31,12 @@ import com.samskivert.util.ArrayIntSet;
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
/**
|
||||
* Utility functions for fetching and manipulating request parameters
|
||||
* (form fields).
|
||||
* Utility functions for fetching and manipulating request parameters (form fields).
|
||||
*/
|
||||
public class ParameterUtil
|
||||
{
|
||||
/** A default date that can be placed in fields to communicate the
|
||||
* appropriate format. Is treated the same as the empty string by {@link
|
||||
* #getDateParameter}. */
|
||||
/** A default date that can be placed in fields to communicate the appropriate format. Is
|
||||
* treated the same as the empty string by {@link #getDateParameter}. */
|
||||
public static final String DATE_TEMPLATE = "YYYY-MM-DD";
|
||||
|
||||
/**
|
||||
@@ -51,12 +49,11 @@ public class ParameterUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the supplied parameter from the request. If the parameter
|
||||
* does not exist, either null or the empty string will be returned
|
||||
* depending on the value of the <code>returnNull</code> parameter.
|
||||
* Fetches the supplied parameter from the request. If the parameter does not exist, either
|
||||
* null or the empty string will be returned depending on the value of the
|
||||
* <code>returnNull</code> parameter.
|
||||
*/
|
||||
public static String getParameter (
|
||||
HttpServletRequest req, String name, boolean returnNull)
|
||||
public static String getParameter (HttpServletRequest req, String name, boolean returnNull)
|
||||
{
|
||||
String value = req.getParameter(name);
|
||||
if (value == null) {
|
||||
@@ -67,42 +64,36 @@ public class ParameterUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the supplied parameter from the request and converts it to
|
||||
* a float. If the parameter does not exist or is not a well-formed
|
||||
* float, a data validation exception is thrown with the supplied
|
||||
* message.
|
||||
* Fetches the supplied parameter from the request and converts it to a float. If the parameter
|
||||
* does not exist or is not a well-formed float, a data validation exception is thrown with the
|
||||
* supplied message.
|
||||
*/
|
||||
public static float requireFloatParameter (
|
||||
HttpServletRequest req, String name, String invalidDataMessage)
|
||||
throws DataValidationException
|
||||
{
|
||||
return parseFloatParameter(
|
||||
getParameter(req, name, false), invalidDataMessage);
|
||||
return parseFloatParameter(getParameter(req, name, false), invalidDataMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the supplied parameter from the request and converts it to
|
||||
* an integer. If the parameter does not exist or is not a well-formed
|
||||
* integer, a data validation exception is thrown with the supplied
|
||||
* message.
|
||||
* Fetches the supplied parameter from the request and converts it to an integer. If the
|
||||
* parameter does not exist or is not a well-formed integer, a data validation exception is
|
||||
* thrown with the supplied message.
|
||||
*/
|
||||
public static int requireIntParameter (
|
||||
HttpServletRequest req, String name, String invalidDataMessage)
|
||||
throws DataValidationException
|
||||
{
|
||||
return parseIntParameter(
|
||||
getParameter(req, name, false), invalidDataMessage);
|
||||
return parseIntParameter(getParameter(req, name, false), invalidDataMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the supplied parameter from the request and converts it to
|
||||
* an integer. If the parameter does not exist or is not a well-formed
|
||||
* integer, a data validation exception is thrown with the supplied
|
||||
* message.
|
||||
* Fetches the supplied parameter from the request and converts it to an integer. If the
|
||||
* parameter does not exist or is not a well-formed integer, a data validation exception is
|
||||
* thrown with the supplied message.
|
||||
*/
|
||||
public static int requireIntParameter (
|
||||
HttpServletRequest req, String name, String invalidDataMessage,
|
||||
ParameterValidator validator)
|
||||
public static int requireIntParameter (HttpServletRequest req, String name,
|
||||
String invalidDataMessage, ParameterValidator validator)
|
||||
throws DataValidationException
|
||||
{
|
||||
String value = getParameter(req, name, false);
|
||||
@@ -111,25 +102,22 @@ public class ParameterUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the supplied parameter from the request and converts it to
|
||||
* an integer. If the parameter does not exist or is not a well-formed
|
||||
* integer, a data validation exception is thrown with the supplied
|
||||
* message.
|
||||
* Fetches the supplied parameter from the request and converts it to an integer. If the
|
||||
* parameter does not exist or is not a well-formed integer, a data validation exception is
|
||||
* thrown with the supplied message.
|
||||
*/
|
||||
public static int requireIntParameter (
|
||||
HttpServletRequest req, String name, int low, int high,
|
||||
String invalidDataMessage)
|
||||
public static int requireIntParameter (HttpServletRequest req, String name, int low, int high,
|
||||
String invalidDataMessage)
|
||||
throws DataValidationException
|
||||
{
|
||||
return requireIntParameter(req, name, invalidDataMessage,
|
||||
new IntRangeValidator(low, high, invalidDataMessage));
|
||||
return requireIntParameter(
|
||||
req, name, invalidDataMessage, new IntRangeValidator(low, high, invalidDataMessage));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all the values from the request with the specified name and
|
||||
* converts them to an IntSet. If the parameter does not exist or is
|
||||
* not a well-formed integer, a data validation exception is thrown
|
||||
* with the supplied message.
|
||||
* Fetches all the values from the request with the specified name and converts them to an
|
||||
* IntSet. If the parameter does not exist or is not a well-formed integer, a data validation
|
||||
* exception is thrown with the supplied message.
|
||||
*/
|
||||
public static ArrayIntSet getIntParameters (
|
||||
HttpServletRequest req, String name, String invalidDataMessage)
|
||||
@@ -148,11 +136,10 @@ public class ParameterUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all the values from the request with the specified name and
|
||||
* converts them to a HashSet.
|
||||
* Fetches all the values from the request with the specified name and converts them to a
|
||||
* HashSet.
|
||||
*/
|
||||
public static HashSet<String> getParameters (
|
||||
HttpServletRequest req, String name)
|
||||
public static HashSet<String> getParameters (HttpServletRequest req, String name)
|
||||
throws DataValidationException
|
||||
{
|
||||
HashSet<String> set = new HashSet<String>();
|
||||
@@ -168,25 +155,22 @@ public class ParameterUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the supplied parameter from the request. If the parameter does
|
||||
* not exist, <code>defval</code> is returned.
|
||||
* Fetches the supplied parameter from the request. If the parameter does not exist,
|
||||
* <code>defval</code> is returned.
|
||||
*/
|
||||
public static String getParameter (
|
||||
HttpServletRequest req, String name, String defval)
|
||||
public static String getParameter (HttpServletRequest req, String name, String defval)
|
||||
{
|
||||
String value = req.getParameter(name);
|
||||
return StringUtil.isBlank(value) ? defval : value.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the supplied parameter from the request and converts it to
|
||||
* an integer. If the parameter does not exist, <code>defval</code> is
|
||||
* returned. If the parameter is not a well-formed integer, a data
|
||||
* validation exception is thrown with the supplied message.
|
||||
* Fetches the supplied parameter from the request and converts it to an integer. If the
|
||||
* parameter does not exist, <code>defval</code> is returned. If the parameter is not a
|
||||
* well-formed integer, a data validation exception is thrown with the supplied message.
|
||||
*/
|
||||
public static int getIntParameter (
|
||||
HttpServletRequest req, String name, int defval,
|
||||
String invalidDataMessage)
|
||||
HttpServletRequest req, String name, int defval, String invalidDataMessage)
|
||||
throws DataValidationException
|
||||
{
|
||||
String value = getParameter(req, name, false);
|
||||
@@ -197,14 +181,12 @@ public class ParameterUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the supplied parameter from the request and converts it to
|
||||
* a long. If the parameter does not exist, <code>defval</code> is
|
||||
* returned. If the parameter is not a well-formed integer, a data
|
||||
* validation exception is thrown with the supplied message.
|
||||
* Fetches the supplied parameter from the request and converts it to a long. If the parameter
|
||||
* does not exist, <code>defval</code> is returned. If the parameter is not a well-formed
|
||||
* integer, a data validation exception is thrown with the supplied message.
|
||||
*/
|
||||
public static long getLongParameter (
|
||||
HttpServletRequest req, String name, long defval,
|
||||
String invalidDataMessage)
|
||||
HttpServletRequest req, String name, long defval, String invalidDataMessage)
|
||||
throws DataValidationException
|
||||
{
|
||||
String value = getParameter(req, name, false);
|
||||
@@ -215,9 +197,8 @@ public class ParameterUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the supplied parameter from the request. If the parameter
|
||||
* does not exist, a data validation exception is thrown with the
|
||||
* supplied message.
|
||||
* Fetches the supplied parameter from the request. If the parameter does not exist, a data
|
||||
* validation exception is thrown with the supplied message.
|
||||
*/
|
||||
public static String requireParameter (
|
||||
HttpServletRequest req, String name, String missingDataMessage)
|
||||
@@ -231,19 +212,16 @@ public class ParameterUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the supplied parameter from the request and ensures that
|
||||
* it is no longer than maxLength.
|
||||
* Fetches the supplied parameter from the request and ensures that it is no longer than
|
||||
* maxLength.
|
||||
*
|
||||
* Note that use of this method could be dangerous. If the specified
|
||||
* HttpServletRequest is used to pre-fill in values on a form, it will
|
||||
* not know to use the truncated version. A user may enter a version that
|
||||
* is too long, your code will see a truncated version, but then the
|
||||
* user will see on the page again the full-length reproduction of what
|
||||
* they typed in. Be careful.
|
||||
* Note that use of this method could be dangerous. If the specified HttpServletRequest is used
|
||||
* to pre-fill in values on a form, it will not know to use the truncated version. A user may
|
||||
* enter a version that is too long, your code will see a truncated version, but then the user
|
||||
* will see on the page again the full-length reproduction of what they typed in. Be careful.
|
||||
*/
|
||||
public static String requireParameter (
|
||||
HttpServletRequest req, String name, String missingDataMessage,
|
||||
int maxLength)
|
||||
HttpServletRequest req, String name, String missingDataMessage, int maxLength)
|
||||
throws DataValidationException
|
||||
{
|
||||
return StringUtil.truncate(
|
||||
@@ -251,18 +229,15 @@ public class ParameterUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the supplied parameter from the request and converts it to
|
||||
* a date. The value of the parameter should be a date formatted like
|
||||
* so: 2001-12-25. If the parameter does not exist or is not a
|
||||
* well-formed date, a data validation exception is thrown with the
|
||||
* supplied message.
|
||||
* Fetches the supplied parameter from the request and converts it to a date. The value of the
|
||||
* parameter should be a date formatted like so: 2001-12-25. If the parameter does not exist or
|
||||
* is not a well-formed date, a data validation exception is thrown with the supplied message.
|
||||
*/
|
||||
public static Date requireDateParameter (
|
||||
HttpServletRequest req, String name, String invalidDataMessage)
|
||||
throws DataValidationException
|
||||
{
|
||||
return parseDateParameter(getParameter(req, name, false),
|
||||
invalidDataMessage);
|
||||
return parseDateParameter(getParameter(req, name, false), invalidDataMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -272,16 +247,14 @@ public class ParameterUtil
|
||||
HttpServletRequest req, String name, String invalidDataMessage)
|
||||
throws DataValidationException
|
||||
{
|
||||
return new java.sql.Date(
|
||||
requireDateParameter(req, name, invalidDataMessage).getTime());
|
||||
return new java.sql.Date(requireDateParameter(req, name, invalidDataMessage).getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the supplied parameter from the request and converts it to
|
||||
* a date. The value of the parameter should be a date formatted like
|
||||
* so: 2001-12-25. If the parameter does not exist, null is
|
||||
* returned. If the parameter is not a well-formed date, a data
|
||||
* validation exception is thrown with the supplied message.
|
||||
* Fetches the supplied parameter from the request and converts it to a date. The value of the
|
||||
* parameter should be a date formatted like so: 2001-12-25. If the parameter does not exist,
|
||||
* null is returned. If the parameter is not a well-formed date, a data validation exception is
|
||||
* thrown with the supplied message.
|
||||
*/
|
||||
public static Date getDateParameter (
|
||||
HttpServletRequest req, String name, String invalidDataMessage)
|
||||
@@ -309,8 +282,7 @@ public class ParameterUtil
|
||||
/**
|
||||
* Returns true if the specified parameter is set in the request.
|
||||
*
|
||||
* @return true if the specified parameter is set in the request
|
||||
* context, false otherwise.
|
||||
* @return true if the specified parameter is set in the request context, false otherwise.
|
||||
*/
|
||||
public static boolean isSet (HttpServletRequest req, String name)
|
||||
{
|
||||
@@ -318,37 +290,28 @@ public class ParameterUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the specified parameter is set to a non-blank value
|
||||
* in the request, false if the value is blank, or
|
||||
* <code>defaultValue</code> if the parameter is unspecified.
|
||||
* Returns true if the specified parameter is set to a non-blank value in the request, false if
|
||||
* the value is blank, or <code>defaultValue</code> if the parameter is unspecified.
|
||||
*
|
||||
* @return true if the specified parameter is set to a non-blank value
|
||||
* in the request, false if the value is blank, or
|
||||
* <code>defaultValue</code> if the parameter is unspecified.
|
||||
* @return true if the specified parameter is set to a non-blank value in the request, false if
|
||||
* the value is blank, or <code>defaultValue</code> if the parameter is unspecified.
|
||||
*/
|
||||
public static boolean isSet (
|
||||
HttpServletRequest req, String name, boolean defaultValue)
|
||||
public static boolean isSet (HttpServletRequest req, String name, boolean defaultValue)
|
||||
{
|
||||
String value = getParameter(req, name, true);
|
||||
return (value == null) ? defaultValue :
|
||||
!StringUtil.isBlank(req.getParameter(name));
|
||||
return (value == null) ? defaultValue : !StringUtil.isBlank(req.getParameter(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the specified parameter is equal to the supplied
|
||||
* value. If the parameter is not set in the request, false is
|
||||
* returned.
|
||||
* Returns true if the specified parameter is equal to the supplied value. If the parameter is
|
||||
* not set in the request, false is returned.
|
||||
*
|
||||
* @param name The parameter whose value should be compared with the
|
||||
* supplied value.
|
||||
* @param value The value to which the parameter may be equal. This
|
||||
* should not be null.
|
||||
* @param name The parameter whose value should be compared with the supplied value.
|
||||
* @param value The value to which the parameter may be equal. This should not be null.
|
||||
*
|
||||
* @return true if the specified parameter is equal to the supplied
|
||||
* parameter, false otherwise.
|
||||
* @return true if the specified parameter is equal to the supplied parameter, false otherwise.
|
||||
*/
|
||||
public static boolean parameterEquals (
|
||||
HttpServletRequest req, String name, String value)
|
||||
public static boolean parameterEquals (HttpServletRequest req, String name, String value)
|
||||
{
|
||||
return value.equals(getParameter(req, name, false));
|
||||
}
|
||||
@@ -356,8 +319,7 @@ public class ParameterUtil
|
||||
/**
|
||||
* Internal method to parse integer values.
|
||||
*/
|
||||
protected static int parseIntParameter (
|
||||
String value, String invalidDataMessage)
|
||||
protected static int parseIntParameter (String value, String invalidDataMessage)
|
||||
throws DataValidationException
|
||||
{
|
||||
try {
|
||||
@@ -370,8 +332,7 @@ public class ParameterUtil
|
||||
/**
|
||||
* Internal method to parse long values.
|
||||
*/
|
||||
protected static long parseLongParameter (
|
||||
String value, String invalidDataMessage)
|
||||
protected static long parseLongParameter (String value, String invalidDataMessage)
|
||||
throws DataValidationException
|
||||
{
|
||||
try {
|
||||
@@ -384,8 +345,7 @@ public class ParameterUtil
|
||||
/**
|
||||
* Internal method to parse a float value.
|
||||
*/
|
||||
protected static float parseFloatParameter (
|
||||
String value, String invalidDataMessage)
|
||||
protected static float parseFloatParameter (String value, String invalidDataMessage)
|
||||
throws DataValidationException
|
||||
{
|
||||
try {
|
||||
@@ -398,8 +358,7 @@ public class ParameterUtil
|
||||
/**
|
||||
* Internal method to parse a date.
|
||||
*/
|
||||
protected static Date parseDateParameter (
|
||||
String value, String invalidDataMessage)
|
||||
protected static Date parseDateParameter (String value, String invalidDataMessage)
|
||||
throws DataValidationException
|
||||
{
|
||||
try {
|
||||
@@ -409,24 +368,16 @@ public class ParameterUtil
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure integers are within a range.
|
||||
*/
|
||||
/** Makes sure integers are within a range. */
|
||||
protected static class IntRangeValidator implements ParameterValidator
|
||||
{
|
||||
/**
|
||||
*/
|
||||
public IntRangeValidator (int low, int high, String outOfRangeError)
|
||||
{
|
||||
public IntRangeValidator (int low, int high, String outOfRangeError) {
|
||||
_low = low;
|
||||
_high = high;
|
||||
_err = outOfRangeError;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void validateParameter (String name, String value)
|
||||
throws DataValidationException
|
||||
{
|
||||
public void validateParameter (String name, String value) throws DataValidationException {
|
||||
try {
|
||||
int ivalue = Integer.parseInt(value);
|
||||
if ((ivalue >= _low) && (ivalue <= _high)) {
|
||||
@@ -435,7 +386,6 @@ public class ParameterUtil
|
||||
} catch (Exception e) {
|
||||
// fall through
|
||||
}
|
||||
|
||||
throw new DataValidationException(_err);
|
||||
}
|
||||
|
||||
@@ -444,6 +394,5 @@ public class ParameterUtil
|
||||
}
|
||||
|
||||
/** We use this to parse dates in requireDateParameter(). */
|
||||
protected static SimpleDateFormat _dparser =
|
||||
new SimpleDateFormat("yyyy-MM-dd");
|
||||
protected static SimpleDateFormat _dparser = new SimpleDateFormat("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user