diff --git a/projects/samskivert/src/java/com/samskivert/servlet/util/ParameterUtil.java b/projects/samskivert/src/java/com/samskivert/servlet/util/ParameterUtil.java index a08df540..e5c7ea6a 100644 --- a/projects/samskivert/src/java/com/samskivert/servlet/util/ParameterUtil.java +++ b/projects/samskivert/src/java/com/samskivert/servlet/util/ParameterUtil.java @@ -1,5 +1,5 @@ // -// $Id: ParameterUtil.java,v 1.12 2003/09/17 18:50:07 ray Exp $ +// $Id: ParameterUtil.java,v 1.13 2003/12/16 01:11:50 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -140,6 +140,24 @@ public class ParameterUtil return parseIntParameter(value, invalidDataMessage); } + /** + * Fetches the supplied parameter from the request and converts it to + * a long. If the parameter does not exist, defval 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) + throws DataValidationException + { + String value = getParameter(req, name, false); + if (StringUtil.blank(value)) { + return defval; + } + return parseLongParameter(value, invalidDataMessage); + } + /** * Fetches the supplied parameter from the request. If the parameter * does not exist, a data validation exception is thrown with the @@ -270,6 +288,20 @@ public class ParameterUtil } } + /** + * Internal method to parse long values. + */ + protected static long parseLongParameter ( + String value, String invalidDataMessage) + throws DataValidationException + { + try { + return Long.parseLong(value); + } catch (NumberFormatException nfe) { + throw new DataValidationException(invalidDataMessage); + } + } + /** * Internal method to parse a float value. */