Widening.

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