1
22
23 package com.liferay.util.servlet;
24
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29
30 import javax.portlet.PortletRequest;
31 import javax.portlet.PortletSession;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpSession;
35
36
42 public class SessionErrors {
43
44 public static final String KEY = SessionErrors.class.getName();
45
46
48 public static void add(HttpServletRequest req, String key) {
49 add(req.getSession(), key);
50 }
51
52 public static void add(HttpSession ses, String key) {
53 Map errors = _getErrors(ses);
54
55 errors.put(key, key);
56 }
57
58 public static void add(HttpServletRequest req, String key, Object value) {
59 add(req.getSession(), key, value);
60 }
61
62 public static void add(HttpSession ses, String key, Object value) {
63 Map errors = _getErrors(ses);
64
65 errors.put(key, value);
66 }
67
68 public static void clear(HttpServletRequest req) {
69 clear(req.getSession());
70 }
71
72 public static void clear(HttpSession ses) {
73 Map errors = _getErrors(ses);
74
75 errors.clear();
76 }
77
78 public static boolean contains(HttpServletRequest req, String key) {
79 return contains(req.getSession(), key);
80 }
81
82 public static boolean contains(HttpSession ses, String key) {
83 Map errors = _getErrors(ses);
84
85 return errors.containsKey(key);
86 }
87
88 public static Object get(HttpServletRequest req, String key) {
89 return get(req.getSession(), key);
90 }
91
92 public static Object get(HttpSession ses, String key) {
93 Map errors = _getErrors(ses);
94
95 return errors.get(key);
96 }
97
98 public static boolean isEmpty(HttpServletRequest req) {
99 return isEmpty(req.getSession());
100 }
101
102 public static boolean isEmpty(HttpSession ses) {
103 Map errors = _getErrors(ses);
104
105 return errors.isEmpty();
106 }
107
108 public static Iterator iterator(HttpServletRequest req) {
109 return iterator(req.getSession());
110 }
111
112 public static Iterator iterator(HttpSession ses) {
113 Map errors = _getErrors(ses);
114
115 return Collections.unmodifiableSet(errors.keySet()).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 errors = _getErrors(ses);
136
137 return errors.size();
138 }
139
140 private static Map _getErrors(HttpSession ses) {
141 Map errors = null;
142
143 try {
144 errors = (Map)ses.getAttribute(KEY);
145
146 if (errors == null) {
147 errors = new LinkedHashMap();
148
149 ses.setAttribute(KEY, errors);
150 }
151 }
152 catch (IllegalStateException ise) {
153 errors = new LinkedHashMap();
154 }
155
156 return errors;
157 }
158
159
161 public static void add(PortletRequest req, String key) {
162 add(req.getPortletSession(), key);
163 }
164
165 public static void add(PortletSession ses, String key) {
166 Map errors = _getErrors(ses);
167
168 errors.put(key, key);
169 }
170
171 public static void add(PortletRequest req, String key, Object value) {
172 add(req.getPortletSession(), key, value);
173 }
174
175 public static void add(PortletSession ses, String key, Object value) {
176 Map errors = _getErrors(ses);
177
178 errors.put(key, value);
179 }
180
181 public static void clear(PortletRequest req) {
182 clear(req.getPortletSession());
183 }
184
185 public static void clear(PortletSession ses) {
186 Map errors = _getErrors(ses);
187
188 errors.clear();
189 }
190
191 public static boolean contains(PortletRequest req, String key) {
192 return contains(req.getPortletSession(), key);
193 }
194
195 public static boolean contains(PortletSession ses, String key) {
196 Map errors = _getErrors(ses);
197
198 return errors.containsKey(key);
199 }
200
201 public static Object get(PortletRequest req, String key) {
202 return get(req.getPortletSession(), key);
203 }
204
205 public static Object get(PortletSession ses, String key) {
206 Map errors = _getErrors(ses);
207
208 return errors.get(key);
209 }
210
211 public static boolean isEmpty(PortletRequest req) {
212 return isEmpty(req.getPortletSession());
213 }
214
215 public static boolean isEmpty(PortletSession ses) {
216 Map errors = _getErrors(ses);
217
218 return errors.isEmpty();
219 }
220
221 public static Iterator iterator(PortletRequest req) {
222 return iterator(req.getPortletSession());
223 }
224
225 public static Iterator iterator(PortletSession ses) {
226 Map errors = _getErrors(ses);
227
228 return Collections.unmodifiableSet(errors.keySet()).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 errors = _getErrors(ses);
249
250 return errors.size();
251 }
252
253 private static Map _getErrors(PortletSession ses) {
254 Map errors = null;
255
256 try {
257 errors = (Map)ses.getAttribute(KEY);
258
259 if (errors == null) {
260 errors = new LinkedHashMap();
261
262 ses.setAttribute(KEY, errors);
263 }
264 }
265 catch (IllegalStateException ise) {
266 errors = new LinkedHashMap();
267 }
268
269 return errors;
270 }
271
272 }