001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.Locale;
018    
019    import javax.portlet.PortletRequest;
020    import javax.portlet.PortletSession;
021    
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpSession;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class SessionParamUtil {
029    
030            public static boolean getBoolean(HttpServletRequest request, String param) {
031                    return getBoolean(request, param, GetterUtil.DEFAULT_BOOLEAN);
032            }
033    
034            public static boolean getBoolean(
035                    HttpServletRequest request, String param, boolean defaultValue) {
036    
037                    HttpSession session = request.getSession(false);
038    
039                    String requestValue = request.getParameter(param);
040    
041                    if (requestValue != null) {
042                            boolean value = GetterUtil.getBoolean(requestValue);
043    
044                            if (session != null) {
045                                    session.setAttribute(param, value);
046                            }
047    
048                            return value;
049                    }
050    
051                    if (session != null) {
052                            Boolean sessionValue = (Boolean)session.getAttribute(param);
053    
054                            if (sessionValue != null) {
055                                    return sessionValue;
056                            }
057                    }
058    
059                    return defaultValue;
060            }
061    
062            public static boolean getBoolean(
063                    PortletRequest portletRequest, String param) {
064    
065                    return getBoolean(portletRequest, param, GetterUtil.DEFAULT_BOOLEAN);
066            }
067    
068            public static boolean getBoolean(
069                    PortletRequest portletRequest, String param, boolean defaultValue) {
070    
071                    PortletSession portletSession = portletRequest.getPortletSession(false);
072    
073                    String portletRequestValue = portletRequest.getParameter(param);
074    
075                    if (portletRequestValue != null) {
076                            boolean value = GetterUtil.getBoolean(portletRequestValue);
077    
078                            portletSession.setAttribute(param, value);
079    
080                            return value;
081                    }
082    
083                    if (portletSession != null) {
084                            Boolean portletSessionValue = (Boolean)portletSession.getAttribute(
085                                    param);
086    
087                            if (portletSessionValue != null) {
088                                    return portletSessionValue;
089                            }
090                    }
091    
092                    return defaultValue;
093            }
094    
095            public static double getDouble(HttpServletRequest request, String param) {
096                    return getDouble(request, param, GetterUtil.DEFAULT_DOUBLE);
097            }
098    
099            public static double getDouble(
100                    HttpServletRequest request, String param, double defaultValue) {
101    
102                    HttpSession session = request.getSession(false);
103    
104                    String requestValue = request.getParameter(param);
105    
106                    if (requestValue != null) {
107                            double value = GetterUtil.getDouble(requestValue);
108    
109                            if (session != null) {
110                                    session.setAttribute(param, value);
111                            }
112    
113                            return value;
114                    }
115    
116                    if (session != null) {
117                            Double sessionValue = (Double)session.getAttribute(param);
118    
119                            if (sessionValue != null) {
120                                    return sessionValue;
121                            }
122                    }
123    
124                    return defaultValue;
125            }
126    
127            public static double getDouble(
128                    HttpServletRequest request, String param, double defaultValue,
129                    Locale locale) {
130    
131                    HttpSession session = request.getSession(false);
132    
133                    String requestValue = request.getParameter(param);
134    
135                    if (requestValue != null) {
136                            double value = GetterUtil.getDouble(requestValue, locale);
137    
138                            if (session != null) {
139                                    session.setAttribute(param, value);
140                            }
141    
142                            return value;
143                    }
144    
145                    if (session != null) {
146                            Double sessionValue = (Double)session.getAttribute(param);
147    
148                            if (sessionValue != null) {
149                                    return sessionValue;
150                            }
151                    }
152    
153                    return defaultValue;
154            }
155    
156            public static double getDouble(
157                    HttpServletRequest request, String param, Locale locale) {
158    
159                    return getDouble(request, param, GetterUtil.DEFAULT_DOUBLE, locale);
160            }
161    
162            public static double getDouble(
163                    PortletRequest portletRequest, String param) {
164    
165                    return getDouble(portletRequest, param, GetterUtil.DEFAULT_DOUBLE);
166            }
167    
168            public static double getDouble(
169                    PortletRequest portletRequest, String param, double defaultValue) {
170    
171                    PortletSession portletSession = portletRequest.getPortletSession(false);
172    
173                    String portletRequestValue = portletRequest.getParameter(param);
174    
175                    if (portletRequestValue != null) {
176                            double value = GetterUtil.getDouble(portletRequestValue);
177    
178                            portletSession.setAttribute(param, value);
179    
180                            return value;
181                    }
182    
183                    if (portletSession != null) {
184                            Double portletSessionValue = (Double)portletSession.getAttribute(
185                                    param);
186    
187                            if (portletSessionValue != null) {
188                                    return portletSessionValue;
189                            }
190                    }
191    
192                    return defaultValue;
193            }
194    
195            public static double getDouble(
196                    PortletRequest portletRequest, String param, double defaultValue,
197                    Locale locale) {
198    
199                    PortletSession portletSession = portletRequest.getPortletSession(false);
200    
201                    String portletRequestValue = portletRequest.getParameter(param);
202    
203                    if (portletRequestValue != null) {
204                            double value = GetterUtil.getDouble(portletRequestValue, locale);
205    
206                            portletSession.setAttribute(param, value);
207    
208                            return value;
209                    }
210    
211                    if (portletSession != null) {
212                            Double portletSessionValue = (Double)portletSession.getAttribute(
213                                    param);
214    
215                            if (portletSessionValue != null) {
216                                    return portletSessionValue;
217                            }
218                    }
219    
220                    return defaultValue;
221            }
222    
223            public static double getDouble(
224                    PortletRequest portletRequest, String param, Locale locale) {
225    
226                    return getDouble(
227                            portletRequest, param, GetterUtil.DEFAULT_DOUBLE, locale);
228            }
229    
230            public static int getInteger(HttpServletRequest request, String param) {
231                    return getInteger(request, param, GetterUtil.DEFAULT_INTEGER);
232            }
233    
234            public static int getInteger(
235                    HttpServletRequest request, String param, int defaultValue) {
236    
237                    HttpSession session = request.getSession(false);
238    
239                    String requestValue = request.getParameter(param);
240    
241                    if (requestValue != null) {
242                            int value = GetterUtil.getInteger(requestValue);
243    
244                            if (session != null) {
245                                    session.setAttribute(param, value);
246                            }
247    
248                            return value;
249                    }
250    
251                    if (session != null) {
252                            Integer sessionValue = (Integer)session.getAttribute(param);
253    
254                            if (sessionValue != null) {
255                                    return sessionValue;
256                            }
257                    }
258    
259                    return defaultValue;
260            }
261    
262            public static int getInteger(PortletRequest portletRequest, String param) {
263                    return getInteger(portletRequest, param, GetterUtil.DEFAULT_INTEGER);
264            }
265    
266            public static int getInteger(
267                    PortletRequest portletRequest, String param, int defaultValue) {
268    
269                    PortletSession portletSession = portletRequest.getPortletSession(false);
270    
271                    String portletRequestValue = portletRequest.getParameter(param);
272    
273                    if (portletRequestValue != null) {
274                            int value = GetterUtil.getInteger(portletRequestValue);
275    
276                            portletSession.setAttribute(param, value);
277    
278                            return value;
279                    }
280    
281                    if (portletSession != null) {
282                            Integer portletSessionValue = (Integer)portletSession.getAttribute(
283                                    param);
284    
285                            if (portletSessionValue != null) {
286                                    return portletSessionValue;
287                            }
288                    }
289    
290                    return defaultValue;
291            }
292    
293            public static long getLong(HttpServletRequest request, String param) {
294                    return getLong(request, param, GetterUtil.DEFAULT_LONG);
295            }
296    
297            public static long getLong(
298                    HttpServletRequest request, String param, long defaultValue) {
299    
300                    HttpSession session = request.getSession(false);
301    
302                    String requestValue = request.getParameter(param);
303    
304                    if (requestValue != null) {
305                            long value = GetterUtil.getLong(requestValue);
306    
307                            if (session != null) {
308                                    session.setAttribute(param, value);
309                            }
310    
311                            return value;
312                    }
313    
314                    if (session != null) {
315                            Long sessionValue = (Long)session.getAttribute(param);
316    
317                            if (sessionValue != null) {
318                                    return sessionValue;
319                            }
320                    }
321    
322                    return defaultValue;
323            }
324    
325            public static long getLong(PortletRequest portletRequest, String param) {
326                    return getLong(portletRequest, param, GetterUtil.DEFAULT_LONG);
327            }
328    
329            public static long getLong(
330                    PortletRequest portletRequest, String param, long defaultValue) {
331    
332                    PortletSession portletSession = portletRequest.getPortletSession(false);
333    
334                    String portletRequestValue = portletRequest.getParameter(param);
335    
336                    if (portletRequestValue != null) {
337                            long value = GetterUtil.getLong(portletRequestValue);
338    
339                            portletSession.setAttribute(param, value);
340    
341                            return value;
342                    }
343    
344                    if (portletSession != null) {
345                            Long portletSessionValue = (Long)portletSession.getAttribute(param);
346    
347                            if (portletSessionValue != null) {
348                                    return portletSessionValue;
349                            }
350                    }
351    
352                    return defaultValue;
353            }
354    
355            public static short getShort(HttpServletRequest request, String param) {
356                    return getShort(request, param, GetterUtil.DEFAULT_SHORT);
357            }
358    
359            public static short getShort(
360                    HttpServletRequest request, String param, short defaultValue) {
361    
362                    HttpSession session = request.getSession(false);
363    
364                    String requestValue = request.getParameter(param);
365    
366                    if (requestValue != null) {
367                            short value = GetterUtil.getShort(requestValue);
368    
369                            if (session != null) {
370                                    session.setAttribute(param, value);
371                            }
372    
373                            return value;
374                    }
375    
376                    if (session != null) {
377                            Short sessionValue = (Short)session.getAttribute(param);
378    
379                            if (sessionValue != null) {
380                                    return sessionValue;
381                            }
382                    }
383    
384                    return defaultValue;
385            }
386    
387            public static short getShort(PortletRequest portletRequest, String param) {
388                    return getShort(portletRequest, param, GetterUtil.DEFAULT_SHORT);
389            }
390    
391            public static short getShort(
392                    PortletRequest portletRequest, String param, short defaultValue) {
393    
394                    PortletSession portletSession = portletRequest.getPortletSession(false);
395    
396                    String portletRequestValue = portletRequest.getParameter(param);
397    
398                    if (portletRequestValue != null) {
399                            short value = GetterUtil.getShort(portletRequestValue);
400    
401                            portletSession.setAttribute(param, value);
402    
403                            return value;
404                    }
405    
406                    if (portletSession != null) {
407                            Short portletSessionValue = (Short)portletSession.getAttribute(
408                                    param);
409    
410                            if (portletSessionValue != null) {
411                                    return portletSessionValue;
412                            }
413                    }
414    
415                    return defaultValue;
416            }
417    
418            public static String getString(HttpServletRequest request, String param) {
419                    return getString(request, param, GetterUtil.DEFAULT_STRING);
420            }
421    
422            public static String getString(
423                    HttpServletRequest request, String param, String defaultValue) {
424    
425                    HttpSession session = request.getSession(false);
426    
427                    String requestValue = request.getParameter(param);
428    
429                    if (requestValue != null) {
430                            String value = GetterUtil.getString(requestValue);
431    
432                            if (session != null) {
433                                    session.setAttribute(param, value);
434                            }
435    
436                            return value;
437                    }
438    
439                    if (session != null) {
440                            String sessionValue = (String)session.getAttribute(param);
441    
442                            if (sessionValue != null) {
443                                    return sessionValue;
444                            }
445                    }
446    
447                    return defaultValue;
448            }
449    
450            public static String getString(
451                    PortletRequest portletRequest, String param) {
452    
453                    return getString(portletRequest, param, GetterUtil.DEFAULT_STRING);
454            }
455    
456            public static String getString(
457                    PortletRequest portletRequest, String param, String defaultValue) {
458    
459                    PortletSession portletSession = portletRequest.getPortletSession(false);
460    
461                    String portletRequestValue = portletRequest.getParameter(param);
462    
463                    if (portletRequestValue != null) {
464                            String value = GetterUtil.getString(portletRequestValue);
465    
466                            portletSession.setAttribute(param, value);
467    
468                            return value;
469                    }
470    
471                    if (portletSession != null) {
472                            String portletSessionValue = (String)portletSession.getAttribute(
473                                    param);
474    
475                            if (portletSessionValue != null) {
476                                    return portletSessionValue;
477                            }
478                    }
479    
480                    return defaultValue;
481            }
482    
483    }