ParamUtils.java 12 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Matt Tucker's avatar
Matt Tucker committed
19 20 21 22 23 24 25
 */

package org.jivesoftware.util;

import javax.servlet.http.HttpServletRequest;

/**
Matt Tucker's avatar
Matt Tucker committed
26
 * Assists JSP writers in getting parameters and attributes.
Matt Tucker's avatar
Matt Tucker committed
27 28 29 30
 */
public class ParamUtils {

    /**
Matt Tucker's avatar
Matt Tucker committed
31
     * Returns a parameter as a string.
Matt Tucker's avatar
Matt Tucker committed
32
     *
Matt Tucker's avatar
Matt Tucker committed
33 34 35 36 37
     * @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
38 39 40 41 42 43
     */
    public static String getParameter(HttpServletRequest request, String name) {
        return getParameter(request, name, false);
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
44
     * Returns a parameter as a string.
Matt Tucker's avatar
Matt Tucker committed
45
     *
Matt Tucker's avatar
Matt Tucker committed
46 47 48
     * @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
49
     * @param emptyStringsOK return the parameter values even if it is an empty string.
Matt Tucker's avatar
Matt Tucker committed
50 51
     * @return the value of the parameter or null if the parameter was not
     *      found.
Matt Tucker's avatar
Matt Tucker committed
52
     */
Matt Tucker's avatar
Matt Tucker committed
53 54 55
    public static String getParameter(HttpServletRequest request, String name,
            boolean emptyStringsOK)
    {
Matt Tucker's avatar
Matt Tucker committed
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 88 89 90 91 92 93 94 95 96
        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
97
     * Returns a parameter as a boolean.
Matt Tucker's avatar
Matt Tucker committed
98
     *
Matt Tucker's avatar
Matt Tucker committed
99 100 101 102
     * @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
103
     */
Matt Tucker's avatar
Matt Tucker committed
104
    public static boolean getBooleanParameter(HttpServletRequest request, String name) {
Matt Tucker's avatar
Matt Tucker committed
105 106 107 108
        return getBooleanParameter(request, name, false);
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
109
     * Returns a parameter as a boolean.
Matt Tucker's avatar
Matt Tucker committed
110
     *
Matt Tucker's avatar
Matt Tucker committed
111 112 113 114
     * @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
115 116
     */
    public static boolean getBooleanParameter(HttpServletRequest request,
Matt Tucker's avatar
Matt Tucker committed
117 118
            String name, boolean defaultVal)
    {
Matt Tucker's avatar
Matt Tucker committed
119 120 121 122 123 124 125 126 127 128 129 130 131
        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
132
     * Returns a parameter as an int.
Matt Tucker's avatar
Matt Tucker committed
133
     *
Matt Tucker's avatar
Matt Tucker committed
134 135 136 137 138
     * @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
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
     */
    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
158
     * Returns a list of int parameters.
Matt Tucker's avatar
Matt Tucker committed
159
     *
Matt Tucker's avatar
Matt Tucker committed
160 161 162 163 164
     * @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
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
     */
    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
185
     * Returns a parameter as a double.
Matt Tucker's avatar
Matt Tucker committed
186
     *
Matt Tucker's avatar
Matt Tucker committed
187 188 189 190 191
     * @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
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
     */
    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
210
     * Returns a parameter as a long.
Matt Tucker's avatar
Matt Tucker committed
211
     *
Matt Tucker's avatar
Matt Tucker committed
212 213 214 215 216
     * @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
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
     */
    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
235
     * Returns a list of long parameters.
Matt Tucker's avatar
Matt Tucker committed
236
     *
Matt Tucker's avatar
Matt Tucker committed
237 238 239 240 241
     * @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
242
     */
Matt Tucker's avatar
Matt Tucker committed
243 244 245
    public static long[] getLongParameters(HttpServletRequest request, String name,
            long defaultNum)
    {
Matt Tucker's avatar
Matt Tucker committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        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
263
     * Returns an attribute as a string.
Matt Tucker's avatar
Matt Tucker committed
264
     *
Matt Tucker's avatar
Matt Tucker committed
265 266 267 268
     * @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
269 270 271 272 273 274
     */
    public static String getAttribute(HttpServletRequest request, String name) {
        return getAttribute(request, name, false);
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
275
     * Returns an attribute as a string.
Matt Tucker's avatar
Matt Tucker committed
276
     *
Matt Tucker's avatar
Matt Tucker committed
277 278 279 280 281
     * @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
282
     */
Matt Tucker's avatar
Matt Tucker committed
283 284 285
    public static String getAttribute(HttpServletRequest request, String name,
            boolean emptyStringsOK)
    {
Matt Tucker's avatar
Matt Tucker committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
        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
301
     * Returns an attribute as a boolean.
Matt Tucker's avatar
Matt Tucker committed
302
     *
Matt Tucker's avatar
Matt Tucker committed
303 304 305
     * @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
306
     */
Matt Tucker's avatar
Matt Tucker committed
307
    public static boolean getBooleanAttribute(HttpServletRequest request, String name) {
Matt Tucker's avatar
Matt Tucker committed
308 309 310 311 312 313 314 315 316 317
        String temp = (String)request.getAttribute(name);
        if (temp != null && temp.equals("true")) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
318
     * Returns an attribute as a int.
Matt Tucker's avatar
Matt Tucker committed
319
     *
Matt Tucker's avatar
Matt Tucker committed
320 321 322 323
     * @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
324
     */
Matt Tucker's avatar
Matt Tucker committed
325
    public static int getIntAttribute(HttpServletRequest request, String name, int defaultNum) {
Matt Tucker's avatar
Matt Tucker committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
        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
342
     * Returns an attribute as a long.
Matt Tucker's avatar
Matt Tucker committed
343
     *
Matt Tucker's avatar
Matt Tucker committed
344 345 346 347
     * @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
348
     */
Matt Tucker's avatar
Matt Tucker committed
349
    public static long getLongAttribute(HttpServletRequest request, String name, long defaultNum) {
Matt Tucker's avatar
Matt Tucker committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
        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;
        }
    }
}