ParamUtils.java 11.6 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10 11 12 13 14 15 16
 */

package org.jivesoftware.util;

import javax.servlet.http.HttpServletRequest;

/**
Matt Tucker's avatar
Matt Tucker committed
17
 * Assists JSP writers in getting parameters and attributes.
Matt Tucker's avatar
Matt Tucker committed
18 19 20 21
 */
public class ParamUtils {

    /**
Matt Tucker's avatar
Matt Tucker committed
22
     * Returns a parameter as a string.
Matt Tucker's avatar
Matt Tucker committed
23
     *
Matt Tucker's avatar
Matt Tucker committed
24 25 26 27 28
     * @param request the HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name the name of the parameter you want to get
     * @return the value of the parameter or null if the parameter was not
     *      found or if the parameter is a zero-length string.
Matt Tucker's avatar
Matt Tucker committed
29 30 31 32 33 34
     */
    public static String getParameter(HttpServletRequest request, String name) {
        return getParameter(request, name, false);
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
35
     * Returns a parameter as a string.
Matt Tucker's avatar
Matt Tucker committed
36
     *
Matt Tucker's avatar
Matt Tucker committed
37 38 39
     * @param request the HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name the name of the parameter you want to get
Matt Tucker's avatar
Matt Tucker committed
40
     * @param emptyStringsOK return the parameter values even if it is an empty string.
Matt Tucker's avatar
Matt Tucker committed
41 42
     * @return the value of the parameter or null if the parameter was not
     *      found.
Matt Tucker's avatar
Matt Tucker committed
43
     */
Matt Tucker's avatar
Matt Tucker committed
44 45 46
    public static String getParameter(HttpServletRequest request, String name,
            boolean emptyStringsOK)
    {
Matt Tucker's avatar
Matt Tucker committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        String temp = request.getParameter(name);
        if (temp != null) {
            if (temp.equals("") && !emptyStringsOK) {
                return null;
            }
            else {
                return temp;
            }
        }
        else {
            return null;
        }
    }

    /**
     * Returns a list of parameters of the same name
     *
     * @param request an HttpServletRequest object.
     * @return an array of non-null, non-blank strings of the same name. This
     *         method will return an empty array if no parameters were found.
     */
    public static String[] getParameters(HttpServletRequest request, String name) {
        if (name == null) {
            return new String[0];
        }
        String[] paramValues = request.getParameterValues(name);
        if (paramValues == null || paramValues.length == 0) {
            return new String[0];
        }
        else {
            java.util.List values = new java.util.ArrayList(paramValues.length);
            for (int i = 0; i < paramValues.length; i++) {
                if (paramValues[i] != null && !"".equals(paramValues[i])) {
                    values.add(paramValues[i]);
                }
            }
            return (String[])values.toArray(new String[]{});
        }
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
88
     * Returns a parameter as a boolean.
Matt Tucker's avatar
Matt Tucker committed
89
     *
Matt Tucker's avatar
Matt Tucker committed
90 91 92 93
     * @param request the HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name the name of the parameter you want to get
     * @return true if the value of the parameter was "true", false otherwise.
Matt Tucker's avatar
Matt Tucker committed
94
     */
Matt Tucker's avatar
Matt Tucker committed
95
    public static boolean getBooleanParameter(HttpServletRequest request, String name) {
Matt Tucker's avatar
Matt Tucker committed
96 97 98 99
        return getBooleanParameter(request, name, false);
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
100
     * Returns a parameter as a boolean.
Matt Tucker's avatar
Matt Tucker committed
101
     *
Matt Tucker's avatar
Matt Tucker committed
102 103 104 105
     * @param request the HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name the name of the parameter you want to get
     * @return true if the value of the parameter was "true", false otherwise.
Matt Tucker's avatar
Matt Tucker committed
106 107
     */
    public static boolean getBooleanParameter(HttpServletRequest request,
Matt Tucker's avatar
Matt Tucker committed
108 109
            String name, boolean defaultVal)
    {
Matt Tucker's avatar
Matt Tucker committed
110 111 112 113 114 115 116 117 118 119 120 121 122
        String temp = request.getParameter(name);
        if ("true".equals(temp) || "on".equals(temp)) {
            return true;
        }
        else if ("false".equals(temp) || "off".equals(temp)) {
            return false;
        }
        else {
            return defaultVal;
        }
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
123
     * Returns a parameter as an int.
Matt Tucker's avatar
Matt Tucker committed
124
     *
Matt Tucker's avatar
Matt Tucker committed
125 126 127 128 129
     * @param request the HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name the name of the parameter you want to get
     * @return the int value of the parameter specified or the default value if
     *      the parameter is not found.
Matt Tucker's avatar
Matt Tucker committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
     */
    public static int getIntParameter(HttpServletRequest request,
                                      String name, int defaultNum) {
        String temp = request.getParameter(name);
        if (temp != null && !temp.equals("")) {
            int num = defaultNum;
            try {
                num = Integer.parseInt(temp);
            }
            catch (Exception ignored) {
            }
            return num;
        }
        else {
            return defaultNum;
        }
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
149
     * Returns a list of int parameters.
Matt Tucker's avatar
Matt Tucker committed
150
     *
Matt Tucker's avatar
Matt Tucker committed
151 152 153 154 155
     * @param request the HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name the name of the parameter you want to get
     * @param defaultNum the default value of a parameter, if the parameter
     *      can't be converted into an int.
Matt Tucker's avatar
Matt Tucker committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
     */
    public static int[] getIntParameters(HttpServletRequest request,
                                         String name, int defaultNum) {
        String[] paramValues = request.getParameterValues(name);
        if (paramValues == null || paramValues.length == 0) {
            return new int[0];
        }
        int[] values = new int[paramValues.length];
        for (int i = 0; i < paramValues.length; i++) {
            try {
                values[i] = Integer.parseInt(paramValues[i]);
            }
            catch (Exception e) {
                values[i] = defaultNum;
            }
        }
        return values;
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
176
     * Returns a parameter as a double.
Matt Tucker's avatar
Matt Tucker committed
177
     *
Matt Tucker's avatar
Matt Tucker committed
178 179 180 181 182
     * @param request the HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name the name of the parameter you want to get
     * @return the double value of the parameter specified or the default value
     *      if the parameter is not found.
Matt Tucker's avatar
Matt Tucker committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
     */
    public static double getDoubleParameter(HttpServletRequest request, String name, double defaultNum) {
        String temp = request.getParameter(name);
        if (temp != null && !temp.equals("")) {
            double num = defaultNum;
            try {
                num = Double.parseDouble(temp);
            }
            catch (Exception ignored) {
            }
            return num;
        }
        else {
            return defaultNum;
        }
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
201
     * Returns a parameter as a long.
Matt Tucker's avatar
Matt Tucker committed
202
     *
Matt Tucker's avatar
Matt Tucker committed
203 204 205 206 207
     * @param request the HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name the name of the parameter you want to get
     * @return the long value of the parameter specified or the default value if
     *      the parameter is not found.
Matt Tucker's avatar
Matt Tucker committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
     */
    public static long getLongParameter(HttpServletRequest request, String name, long defaultNum) {
        String temp = request.getParameter(name);
        if (temp != null && !temp.equals("")) {
            long num = defaultNum;
            try {
                num = Long.parseLong(temp);
            }
            catch (Exception ignored) {
            }
            return num;
        }
        else {
            return defaultNum;
        }
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
226
     * Returns a list of long parameters.
Matt Tucker's avatar
Matt Tucker committed
227
     *
Matt Tucker's avatar
Matt Tucker committed
228 229 230 231 232
     * @param request the HttpServletRequest object, known as "request" in a
     *      JSP page.
     * @param name the name of the parameter you want to get
     * @param defaultNum the default value of a parameter, if the parameter
     *      can't be converted into a long.
Matt Tucker's avatar
Matt Tucker committed
233
     */
Matt Tucker's avatar
Matt Tucker committed
234 235 236
    public static long[] getLongParameters(HttpServletRequest request, String name,
            long defaultNum)
    {
Matt Tucker's avatar
Matt Tucker committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        String[] paramValues = request.getParameterValues(name);
        if (paramValues == null || paramValues.length == 0) {
            return new long[0];
        }
        long[] values = new long[paramValues.length];
        for (int i = 0; i < paramValues.length; i++) {
            try {
                values[i] = Long.parseLong(paramValues[i]);
            }
            catch (Exception e) {
                values[i] = defaultNum;
            }
        }
        return values;
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
254
     * Returns an attribute as a string.
Matt Tucker's avatar
Matt Tucker committed
255
     *
Matt Tucker's avatar
Matt Tucker committed
256 257 258 259
     * @param request the HttpServletRequest object, known as "request" in a JSP page.
     * @param name the name of the parameter you want to get
     * @return the value of the parameter or null if the parameter was not
     *      found or if the parameter is a zero-length string.
Matt Tucker's avatar
Matt Tucker committed
260 261 262 263 264 265
     */
    public static String getAttribute(HttpServletRequest request, String name) {
        return getAttribute(request, name, false);
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
266
     * Returns an attribute as a string.
Matt Tucker's avatar
Matt Tucker committed
267
     *
Matt Tucker's avatar
Matt Tucker committed
268 269 270 271 272
     * @param request the HttpServletRequest object, known as "request" in a JSP page.
     * @param name the name of the parameter you want to get.
     * @param emptyStringsOK return the parameter values even if it is an empty string.
     * @return the value of the parameter or null if the parameter was not
     *      found.
Matt Tucker's avatar
Matt Tucker committed
273
     */
Matt Tucker's avatar
Matt Tucker committed
274 275 276
    public static String getAttribute(HttpServletRequest request, String name,
            boolean emptyStringsOK)
    {
Matt Tucker's avatar
Matt Tucker committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        String temp = (String)request.getAttribute(name);
        if (temp != null) {
            if (temp.equals("") && !emptyStringsOK) {
                return null;
            }
            else {
                return temp;
            }
        }
        else {
            return null;
        }
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
292
     * Returns an attribute as a boolean.
Matt Tucker's avatar
Matt Tucker committed
293
     *
Matt Tucker's avatar
Matt Tucker committed
294 295 296
     * @param request the HttpServletRequest object, known as "request" in a JSP page.
     * @param name the name of the attribute you want to get.
     * @return true if the value of the attribute is "true", false otherwise.
Matt Tucker's avatar
Matt Tucker committed
297
     */
Matt Tucker's avatar
Matt Tucker committed
298
    public static boolean getBooleanAttribute(HttpServletRequest request, String name) {
Matt Tucker's avatar
Matt Tucker committed
299 300 301 302 303 304 305 306 307 308
        String temp = (String)request.getAttribute(name);
        if (temp != null && temp.equals("true")) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
309
     * Returns an attribute as a int.
Matt Tucker's avatar
Matt Tucker committed
310
     *
Matt Tucker's avatar
Matt Tucker committed
311 312 313 314
     * @param request the HttpServletRequest object, known as "request" in a JSP page.
     * @param name the name of the attribute you want to get.
     * @return the int value of the attribute or the default value if the
     *      attribute is not found or is a zero length string.
Matt Tucker's avatar
Matt Tucker committed
315
     */
Matt Tucker's avatar
Matt Tucker committed
316
    public static int getIntAttribute(HttpServletRequest request, String name, int defaultNum) {
Matt Tucker's avatar
Matt Tucker committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
        String temp = (String)request.getAttribute(name);
        if (temp != null && !temp.equals("")) {
            int num = defaultNum;
            try {
                num = Integer.parseInt(temp);
            }
            catch (Exception ignored) {
            }
            return num;
        }
        else {
            return defaultNum;
        }
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
333
     * Returns an attribute as a long.
Matt Tucker's avatar
Matt Tucker committed
334
     *
Matt Tucker's avatar
Matt Tucker committed
335 336 337 338
     * @param request the HttpServletRequest object, known as "request" in a JSP page.
     * @param name the name of the attribute you want to get.
     * @return the long value of the attribute or the default value if the
     *      attribute is not found or is a zero length string.
Matt Tucker's avatar
Matt Tucker committed
339
     */
Matt Tucker's avatar
Matt Tucker committed
340
    public static long getLongAttribute(HttpServletRequest request, String name, long defaultNum) {
Matt Tucker's avatar
Matt Tucker committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
        String temp = (String)request.getAttribute(name);
        if (temp != null && !temp.equals("")) {
            long num = defaultNum;
            try {
                num = Long.parseLong(temp);
            }
            catch (Exception ignored) {
            }
            return num;
        }
        else {
            return defaultNum;
        }
    }
}