001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.servlet;
022:
023: import java.util.Collections;
024: import java.util.Iterator;
025: import java.util.LinkedHashMap;
026: import java.util.Map;
027:
028: import javax.portlet.PortletRequest;
029: import javax.portlet.PortletSession;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpSession;
033:
034: /**
035: * <a href="SessionErrors.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Brian Wing Shun Chan
038: *
039: */
040: public class SessionErrors {
041:
042: public static final String KEY = SessionErrors.class.getName();
043:
044: // Servlet Request
045:
046: public static void add(HttpServletRequest req, String key) {
047: add(req.getSession(), key);
048: }
049:
050: public static void add(HttpSession ses, String key) {
051: Map errors = _getErrors(ses);
052:
053: errors.put(key, key);
054: }
055:
056: public static void add(HttpServletRequest req, String key,
057: Object value) {
058: add(req.getSession(), key, value);
059: }
060:
061: public static void add(HttpSession ses, String key, Object value) {
062: Map errors = _getErrors(ses);
063:
064: errors.put(key, value);
065: }
066:
067: public static void clear(HttpServletRequest req) {
068: clear(req.getSession());
069: }
070:
071: public static void clear(HttpSession ses) {
072: Map errors = _getErrors(ses);
073:
074: errors.clear();
075: }
076:
077: public static boolean contains(HttpServletRequest req, String key) {
078: return contains(req.getSession(), key);
079: }
080:
081: public static boolean contains(HttpSession ses, String key) {
082: Map errors = _getErrors(ses);
083:
084: return errors.containsKey(key);
085: }
086:
087: public static Object get(HttpServletRequest req, String key) {
088: return get(req.getSession(), key);
089: }
090:
091: public static Object get(HttpSession ses, String key) {
092: Map errors = _getErrors(ses);
093:
094: return errors.get(key);
095: }
096:
097: public static boolean isEmpty(HttpServletRequest req) {
098: return isEmpty(req.getSession());
099: }
100:
101: public static boolean isEmpty(HttpSession ses) {
102: Map errors = _getErrors(ses);
103:
104: return errors.isEmpty();
105: }
106:
107: public static Iterator iterator(HttpServletRequest req) {
108: return iterator(req.getSession());
109: }
110:
111: public static Iterator iterator(HttpSession ses) {
112: Map errors = _getErrors(ses);
113:
114: return Collections.unmodifiableSet(errors.keySet()).iterator();
115: }
116:
117: public static void print(HttpServletRequest req) {
118: print(req.getSession());
119: }
120:
121: public static void print(HttpSession ses) {
122: Iterator itr = iterator(ses);
123:
124: while (itr.hasNext()) {
125: System.out.println(itr.next());
126: }
127: }
128:
129: public static int size(HttpServletRequest req) {
130: return size(req.getSession());
131: }
132:
133: public static int size(HttpSession ses) {
134: Map errors = _getErrors(ses);
135:
136: return errors.size();
137: }
138:
139: private static Map _getErrors(HttpSession ses) {
140: Map errors = null;
141:
142: try {
143: errors = (Map) ses.getAttribute(KEY);
144:
145: if (errors == null) {
146: errors = new LinkedHashMap();
147:
148: ses.setAttribute(KEY, errors);
149: }
150: } catch (IllegalStateException ise) {
151: errors = new LinkedHashMap();
152: }
153:
154: return errors;
155: }
156:
157: // Portlet Request
158:
159: public static void add(PortletRequest req, String key) {
160: add(req.getPortletSession(), key);
161: }
162:
163: public static void add(PortletSession ses, String key) {
164: Map errors = _getErrors(ses);
165:
166: errors.put(key, key);
167: }
168:
169: public static void add(PortletRequest req, String key, Object value) {
170: add(req.getPortletSession(), key, value);
171: }
172:
173: public static void add(PortletSession ses, String key, Object value) {
174: Map errors = _getErrors(ses);
175:
176: errors.put(key, value);
177: }
178:
179: public static void clear(PortletRequest req) {
180: clear(req.getPortletSession());
181: }
182:
183: public static void clear(PortletSession ses) {
184: Map errors = _getErrors(ses);
185:
186: errors.clear();
187: }
188:
189: public static boolean contains(PortletRequest req, String key) {
190: return contains(req.getPortletSession(), key);
191: }
192:
193: public static boolean contains(PortletSession ses, String key) {
194: Map errors = _getErrors(ses);
195:
196: return errors.containsKey(key);
197: }
198:
199: public static Object get(PortletRequest req, String key) {
200: return get(req.getPortletSession(), key);
201: }
202:
203: public static Object get(PortletSession ses, String key) {
204: Map errors = _getErrors(ses);
205:
206: return errors.get(key);
207: }
208:
209: public static boolean isEmpty(PortletRequest req) {
210: return isEmpty(req.getPortletSession());
211: }
212:
213: public static boolean isEmpty(PortletSession ses) {
214: Map errors = _getErrors(ses);
215:
216: return errors.isEmpty();
217: }
218:
219: public static Iterator iterator(PortletRequest req) {
220: return iterator(req.getPortletSession());
221: }
222:
223: public static Iterator iterator(PortletSession ses) {
224: Map errors = _getErrors(ses);
225:
226: return Collections.unmodifiableSet(errors.keySet()).iterator();
227: }
228:
229: public static void print(PortletRequest req) {
230: print(req.getPortletSession());
231: }
232:
233: public static void print(PortletSession ses) {
234: Iterator itr = iterator(ses);
235:
236: while (itr.hasNext()) {
237: System.out.println(itr.next());
238: }
239: }
240:
241: public static int size(PortletRequest req) {
242: return size(req.getPortletSession());
243: }
244:
245: public static int size(PortletSession ses) {
246: Map errors = _getErrors(ses);
247:
248: return errors.size();
249: }
250:
251: private static Map _getErrors(PortletSession ses) {
252: Map errors = null;
253:
254: try {
255: errors = (Map) ses.getAttribute(KEY);
256:
257: if (errors == null) {
258: errors = new LinkedHashMap();
259:
260: ses.setAttribute(KEY, errors);
261: }
262: } catch (IllegalStateException ise) {
263: errors = new LinkedHashMap();
264: }
265:
266: return errors;
267: }
268:
269: }
|