001: /**
002: * $RCSfile$
003: * $Revision: 5574 $
004: * $Date: 2006-10-01 17:02:42 -0700 (Sun, 01 Oct 2006) $
005: *
006: * Copyright (C) 2004 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.util;
011:
012: import javax.servlet.http.HttpServletRequest;
013:
014: /**
015: * Assists JSP writers in getting parameters and attributes.
016: */
017: public class ParamUtils {
018:
019: /**
020: * Returns a parameter as a string.
021: *
022: * @param request the HttpServletRequest object, known as "request" in a
023: * JSP page.
024: * @param name the name of the parameter you want to get
025: * @return the value of the parameter or null if the parameter was not
026: * found or if the parameter is a zero-length string.
027: */
028: public static String getParameter(HttpServletRequest request,
029: String name) {
030: return getParameter(request, name, false);
031: }
032:
033: /**
034: * Returns a parameter as a string.
035: *
036: * @param request the HttpServletRequest object, known as "request" in a
037: * JSP page.
038: * @param name the name of the parameter you want to get
039: * @param emptyStringsOK return the parameter values even if it is an empty string.
040: * @return the value of the parameter or null if the parameter was not
041: * found.
042: */
043: public static String getParameter(HttpServletRequest request,
044: String name, boolean emptyStringsOK) {
045: String temp = request.getParameter(name);
046: if (temp != null) {
047: if (temp.equals("") && !emptyStringsOK) {
048: return null;
049: } else {
050: return temp;
051: }
052: } else {
053: return null;
054: }
055: }
056:
057: /**
058: * Returns a list of parameters of the same name
059: *
060: * @param request an HttpServletRequest object.
061: * @return an array of non-null, non-blank strings of the same name. This
062: * method will return an empty array if no parameters were found.
063: */
064: public static String[] getParameters(HttpServletRequest request,
065: String name) {
066: if (name == null) {
067: return new String[0];
068: }
069: String[] paramValues = request.getParameterValues(name);
070: if (paramValues == null || paramValues.length == 0) {
071: return new String[0];
072: } else {
073: java.util.List values = new java.util.ArrayList(
074: paramValues.length);
075: for (int i = 0; i < paramValues.length; i++) {
076: if (paramValues[i] != null
077: && !"".equals(paramValues[i])) {
078: values.add(paramValues[i]);
079: }
080: }
081: return (String[]) values.toArray(new String[] {});
082: }
083: }
084:
085: /**
086: * Returns a parameter as a boolean.
087: *
088: * @param request the HttpServletRequest object, known as "request" in a
089: * JSP page.
090: * @param name the name of the parameter you want to get
091: * @return true if the value of the parameter was "true", false otherwise.
092: */
093: public static boolean getBooleanParameter(
094: HttpServletRequest request, String name) {
095: return getBooleanParameter(request, name, false);
096: }
097:
098: /**
099: * Returns a parameter as a boolean.
100: *
101: * @param request the HttpServletRequest object, known as "request" in a
102: * JSP page.
103: * @param name the name of the parameter you want to get
104: * @return true if the value of the parameter was "true", false otherwise.
105: */
106: public static boolean getBooleanParameter(
107: HttpServletRequest request, String name, boolean defaultVal) {
108: String temp = request.getParameter(name);
109: if ("true".equals(temp) || "on".equals(temp)) {
110: return true;
111: } else if ("false".equals(temp) || "off".equals(temp)) {
112: return false;
113: } else {
114: return defaultVal;
115: }
116: }
117:
118: /**
119: * Returns a parameter as an int.
120: *
121: * @param request the HttpServletRequest object, known as "request" in a
122: * JSP page.
123: * @param name the name of the parameter you want to get
124: * @return the int value of the parameter specified or the default value if
125: * the parameter is not found.
126: */
127: public static int getIntParameter(HttpServletRequest request,
128: String name, int defaultNum) {
129: String temp = request.getParameter(name);
130: if (temp != null && !temp.equals("")) {
131: int num = defaultNum;
132: try {
133: num = Integer.parseInt(temp);
134: } catch (Exception ignored) {
135: }
136: return num;
137: } else {
138: return defaultNum;
139: }
140: }
141:
142: /**
143: * Returns a list of int parameters.
144: *
145: * @param request the HttpServletRequest object, known as "request" in a
146: * JSP page.
147: * @param name the name of the parameter you want to get
148: * @param defaultNum the default value of a parameter, if the parameter
149: * can't be converted into an int.
150: */
151: public static int[] getIntParameters(HttpServletRequest request,
152: String name, int defaultNum) {
153: String[] paramValues = request.getParameterValues(name);
154: if (paramValues == null || paramValues.length == 0) {
155: return new int[0];
156: }
157: int[] values = new int[paramValues.length];
158: for (int i = 0; i < paramValues.length; i++) {
159: try {
160: values[i] = Integer.parseInt(paramValues[i]);
161: } catch (Exception e) {
162: values[i] = defaultNum;
163: }
164: }
165: return values;
166: }
167:
168: /**
169: * Returns a parameter as a double.
170: *
171: * @param request the HttpServletRequest object, known as "request" in a
172: * JSP page.
173: * @param name the name of the parameter you want to get
174: * @return the double value of the parameter specified or the default value
175: * if the parameter is not found.
176: */
177: public static double getDoubleParameter(HttpServletRequest request,
178: String name, double defaultNum) {
179: String temp = request.getParameter(name);
180: if (temp != null && !temp.equals("")) {
181: double num = defaultNum;
182: try {
183: num = Double.parseDouble(temp);
184: } catch (Exception ignored) {
185: }
186: return num;
187: } else {
188: return defaultNum;
189: }
190: }
191:
192: /**
193: * Returns a parameter as a long.
194: *
195: * @param request the HttpServletRequest object, known as "request" in a
196: * JSP page.
197: * @param name the name of the parameter you want to get
198: * @return the long value of the parameter specified or the default value if
199: * the parameter is not found.
200: */
201: public static long getLongParameter(HttpServletRequest request,
202: String name, long defaultNum) {
203: String temp = request.getParameter(name);
204: if (temp != null && !temp.equals("")) {
205: long num = defaultNum;
206: try {
207: num = Long.parseLong(temp);
208: } catch (Exception ignored) {
209: }
210: return num;
211: } else {
212: return defaultNum;
213: }
214: }
215:
216: /**
217: * Returns a list of long parameters.
218: *
219: * @param request the HttpServletRequest object, known as "request" in a
220: * JSP page.
221: * @param name the name of the parameter you want to get
222: * @param defaultNum the default value of a parameter, if the parameter
223: * can't be converted into a long.
224: */
225: public static long[] getLongParameters(HttpServletRequest request,
226: String name, long defaultNum) {
227: String[] paramValues = request.getParameterValues(name);
228: if (paramValues == null || paramValues.length == 0) {
229: return new long[0];
230: }
231: long[] values = new long[paramValues.length];
232: for (int i = 0; i < paramValues.length; i++) {
233: try {
234: values[i] = Long.parseLong(paramValues[i]);
235: } catch (Exception e) {
236: values[i] = defaultNum;
237: }
238: }
239: return values;
240: }
241:
242: /**
243: * Returns an attribute as a string.
244: *
245: * @param request the HttpServletRequest object, known as "request" in a JSP page.
246: * @param name the name of the parameter you want to get
247: * @return the value of the parameter or null if the parameter was not
248: * found or if the parameter is a zero-length string.
249: */
250: public static String getAttribute(HttpServletRequest request,
251: String name) {
252: return getAttribute(request, name, false);
253: }
254:
255: /**
256: * Returns an attribute as a string.
257: *
258: * @param request the HttpServletRequest object, known as "request" in a JSP page.
259: * @param name the name of the parameter you want to get.
260: * @param emptyStringsOK return the parameter values even if it is an empty string.
261: * @return the value of the parameter or null if the parameter was not
262: * found.
263: */
264: public static String getAttribute(HttpServletRequest request,
265: String name, boolean emptyStringsOK) {
266: String temp = (String) request.getAttribute(name);
267: if (temp != null) {
268: if (temp.equals("") && !emptyStringsOK) {
269: return null;
270: } else {
271: return temp;
272: }
273: } else {
274: return null;
275: }
276: }
277:
278: /**
279: * Returns an attribute as a boolean.
280: *
281: * @param request the HttpServletRequest object, known as "request" in a JSP page.
282: * @param name the name of the attribute you want to get.
283: * @return true if the value of the attribute is "true", false otherwise.
284: */
285: public static boolean getBooleanAttribute(
286: HttpServletRequest request, String name) {
287: String temp = (String) request.getAttribute(name);
288: if (temp != null && temp.equals("true")) {
289: return true;
290: } else {
291: return false;
292: }
293: }
294:
295: /**
296: * Returns an attribute as a int.
297: *
298: * @param request the HttpServletRequest object, known as "request" in a JSP page.
299: * @param name the name of the attribute you want to get.
300: * @return the int value of the attribute or the default value if the
301: * attribute is not found or is a zero length string.
302: */
303: public static int getIntAttribute(HttpServletRequest request,
304: String name, int defaultNum) {
305: String temp = (String) request.getAttribute(name);
306: if (temp != null && !temp.equals("")) {
307: int num = defaultNum;
308: try {
309: num = Integer.parseInt(temp);
310: } catch (Exception ignored) {
311: }
312: return num;
313: } else {
314: return defaultNum;
315: }
316: }
317:
318: /**
319: * Returns an attribute as a long.
320: *
321: * @param request the HttpServletRequest object, known as "request" in a JSP page.
322: * @param name the name of the attribute you want to get.
323: * @return the long value of the attribute or the default value if the
324: * attribute is not found or is a zero length string.
325: */
326: public static long getLongAttribute(HttpServletRequest request,
327: String name, long defaultNum) {
328: String temp = (String) request.getAttribute(name);
329: if (temp != null && !temp.equals("")) {
330: long num = defaultNum;
331: try {
332: num = Long.parseLong(temp);
333: } catch (Exception ignored) {
334: }
335: return num;
336: } else {
337: return defaultNum;
338: }
339: }
340: }
|