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="SessionMessages.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Brian Wing Shun Chan
038: *
039: */
040: public class SessionMessages {
041:
042: public static final String KEY = SessionMessages.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 messages = _getMessages(ses);
052:
053: messages.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 messages = _getMessages(ses);
063:
064: messages.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 messages = _getMessages(ses);
073:
074: messages.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 messages = _getMessages(ses);
083:
084: return messages.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 messages = _getMessages(ses);
093:
094: return messages.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 messages = _getMessages(ses);
103:
104: return messages.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 messages = _getMessages(ses);
113:
114: return Collections.unmodifiableSet(messages.keySet())
115: .iterator();
116: }
117:
118: public static void print(HttpServletRequest req) {
119: print(req.getSession());
120: }
121:
122: public static void print(HttpSession ses) {
123: Iterator itr = iterator(ses);
124:
125: while (itr.hasNext()) {
126: System.out.println(itr.next());
127: }
128: }
129:
130: public static int size(HttpServletRequest req) {
131: return size(req.getSession());
132: }
133:
134: public static int size(HttpSession ses) {
135: Map messages = _getMessages(ses);
136:
137: return messages.size();
138: }
139:
140: private static Map _getMessages(HttpSession ses) {
141: Map messages = null;
142:
143: try {
144: messages = (Map) ses.getAttribute(KEY);
145:
146: if (messages == null) {
147: messages = new LinkedHashMap();
148:
149: ses.setAttribute(KEY, messages);
150: }
151: } catch (IllegalStateException ise) {
152: messages = new LinkedHashMap();
153: }
154:
155: return messages;
156: }
157:
158: // Portlet Request
159:
160: public static void add(PortletRequest req, String key) {
161: add(req.getPortletSession(), key);
162: }
163:
164: public static void add(PortletSession ses, String key) {
165: Map messages = _getMessages(ses);
166:
167: messages.put(key, key);
168: }
169:
170: public static void add(PortletRequest req, String key, Object value) {
171: add(req.getPortletSession(), key, value);
172: }
173:
174: public static void add(PortletSession ses, String key, Object value) {
175: Map messages = _getMessages(ses);
176:
177: messages.put(key, value);
178: }
179:
180: public static void clear(PortletRequest req) {
181: clear(req.getPortletSession());
182: }
183:
184: public static void clear(PortletSession ses) {
185: Map messages = _getMessages(ses);
186:
187: messages.clear();
188: }
189:
190: public static boolean contains(PortletRequest req, String key) {
191: return contains(req.getPortletSession(), key);
192: }
193:
194: public static boolean contains(PortletSession ses, String key) {
195: Map messages = _getMessages(ses);
196:
197: return messages.containsKey(key);
198: }
199:
200: public static Object get(PortletRequest req, String key) {
201: return get(req.getPortletSession(), key);
202: }
203:
204: public static Object get(PortletSession ses, String key) {
205: Map messages = _getMessages(ses);
206:
207: return messages.get(key);
208: }
209:
210: public static boolean isEmpty(PortletRequest req) {
211: return isEmpty(req.getPortletSession());
212: }
213:
214: public static boolean isEmpty(PortletSession ses) {
215: Map messages = _getMessages(ses);
216:
217: return messages.isEmpty();
218: }
219:
220: public static Iterator iterator(PortletRequest req) {
221: return iterator(req.getPortletSession());
222: }
223:
224: public static Iterator iterator(PortletSession ses) {
225: Map messages = _getMessages(ses);
226:
227: return Collections.unmodifiableSet(messages.keySet())
228: .iterator();
229: }
230:
231: public static void print(PortletRequest req) {
232: print(req.getPortletSession());
233: }
234:
235: public static void print(PortletSession ses) {
236: Iterator itr = iterator(ses);
237:
238: while (itr.hasNext()) {
239: System.out.println(itr.next());
240: }
241: }
242:
243: public static int size(PortletRequest req) {
244: return size(req.getPortletSession());
245: }
246:
247: public static int size(PortletSession ses) {
248: Map messages = _getMessages(ses);
249:
250: return messages.size();
251: }
252:
253: private static Map _getMessages(PortletSession ses) {
254: Map messages = null;
255:
256: try {
257: messages = (Map) ses.getAttribute(KEY);
258:
259: if (messages == null) {
260: messages = new LinkedHashMap();
261:
262: ses.setAttribute(KEY, messages);
263: }
264: } catch (IllegalStateException ise) {
265: messages = new LinkedHashMap();
266: }
267:
268: return messages;
269: }
270:
271: }
|