001
014
015 package com.liferay.portal.kernel.servlet;
016
017 import java.util.Collections;
018 import java.util.Iterator;
019 import java.util.LinkedHashMap;
020 import java.util.Map;
021 import java.util.Set;
022
023 import javax.portlet.PortletRequest;
024 import javax.portlet.PortletSession;
025
026 import javax.servlet.http.HttpServletRequest;
027 import javax.servlet.http.HttpSession;
028
029
032 public class SessionErrors {
033
034 public static final String KEY = SessionErrors.class.getName();
035
036
037
038 public static void add(HttpServletRequest request, String key) {
039 add(request.getSession(), key);
040 }
041
042 public static void add(HttpSession session, String key) {
043 Map<String, Object> errors = _getErrors(session);
044
045 errors.put(key, key);
046 }
047
048 public static void add(
049 HttpServletRequest request, String key, Object value) {
050
051 add(request.getSession(), key, value);
052 }
053
054 public static void add(HttpSession session, String key, Object value) {
055 Map<String, Object> errors = _getErrors(session);
056
057 errors.put(key, value);
058 }
059
060 public static void clear(HttpServletRequest request) {
061 clear(request.getSession());
062 }
063
064 public static void clear(HttpSession session) {
065 Map<String, Object> errors = _getErrors(session);
066
067 errors.clear();
068 }
069
070 public static boolean contains(HttpServletRequest request, String key) {
071 return contains(request.getSession(), key);
072 }
073
074 public static boolean contains(HttpSession session, String key) {
075 Map<String, Object> errors = _getErrors(session);
076
077 return errors.containsKey(key);
078 }
079
080 public static Object get(HttpServletRequest request, String key) {
081 return get(request.getSession(), key);
082 }
083
084 public static Object get(HttpSession session, String key) {
085 Map<String, Object> errors = _getErrors(session);
086
087 return errors.get(key);
088 }
089
090 public static boolean isEmpty(HttpServletRequest request) {
091 return isEmpty(request.getSession());
092 }
093
094 public static boolean isEmpty(HttpSession session) {
095 Map<String, Object> errors = _getErrors(session);
096
097 return errors.isEmpty();
098 }
099
100 public static Iterator<String> iterator(HttpServletRequest request) {
101 return iterator(request.getSession());
102 }
103
104 public static Iterator<String> iterator(HttpSession session) {
105 Map<String, Object> errors = _getErrors(session);
106
107 return Collections.unmodifiableSet(errors.keySet()).iterator();
108 }
109
110 public static Set<String> keySet(HttpServletRequest request) {
111 return keySet(request.getSession());
112 }
113
114 public static Set<String> keySet(HttpSession session) {
115 Map<String, Object> errors = _getErrors(session);
116
117 return Collections.unmodifiableSet(errors.keySet());
118 }
119
120 public static void print(HttpServletRequest request) {
121 print(request.getSession());
122 }
123
124 public static void print(HttpSession session) {
125 Iterator<String> itr = iterator(session);
126
127 while (itr.hasNext()) {
128 System.out.println(itr.next());
129 }
130 }
131
132 public static int size(HttpServletRequest request) {
133 return size(request.getSession());
134 }
135
136 public static int size(HttpSession session) {
137 Map<String, Object> errors = _getErrors(session);
138
139 return errors.size();
140 }
141
142 private static Map<String, Object> _getErrors(HttpSession session) {
143 Map<String, Object> errors = null;
144
145 try {
146 errors = (Map<String, Object>)session.getAttribute(KEY);
147
148 if (errors == null) {
149 errors = new LinkedHashMap<String, Object>();
150
151 session.setAttribute(KEY, errors);
152 }
153 }
154 catch (IllegalStateException ise) {
155 errors = new LinkedHashMap<String, Object>();
156 }
157
158 return errors;
159 }
160
161
162
163 public static void add(PortletRequest portletRequest, String key) {
164 add(portletRequest.getPortletSession(), key);
165 }
166
167 public static void add(PortletSession portletSession, String key) {
168 Map<String, Object> errors = _getErrors(portletSession);
169
170 errors.put(key, key);
171 }
172
173 public static void add(
174 PortletRequest portletRequest, String key, Object value) {
175
176 add(portletRequest.getPortletSession(), key, value);
177 }
178
179 public static void add(
180 PortletSession portletSession, String key, Object value) {
181
182 Map<String, Object> errors = _getErrors(portletSession);
183
184 errors.put(key, value);
185 }
186
187 public static void clear(PortletRequest portletRequest) {
188 clear(portletRequest.getPortletSession());
189 }
190
191 public static void clear(PortletSession portletSession) {
192 Map<String, Object> errors = _getErrors(portletSession);
193
194 errors.clear();
195 }
196
197 public static boolean contains(PortletRequest portletRequest, String key) {
198 return contains(portletRequest.getPortletSession(), key);
199 }
200
201 public static boolean contains(PortletSession portletSession, String key) {
202 Map<String, Object> errors = _getErrors(portletSession);
203
204 return errors.containsKey(key);
205 }
206
207 public static Object get(PortletRequest portletRequest, String key) {
208 return get(portletRequest.getPortletSession(), key);
209 }
210
211 public static Object get(PortletSession portletSession, String key) {
212 Map<String, Object> errors = _getErrors(portletSession);
213
214 return errors.get(key);
215 }
216
217 public static boolean isEmpty(PortletRequest portletRequest) {
218 return isEmpty(portletRequest.getPortletSession());
219 }
220
221 public static boolean isEmpty(PortletSession portletSession) {
222 Map<String, Object> errors = _getErrors(portletSession);
223
224 return errors.isEmpty();
225 }
226
227 public static Iterator<String> iterator(PortletRequest portletRequest) {
228 return iterator(portletRequest.getPortletSession());
229 }
230
231 public static Iterator<String> iterator(PortletSession portletSession) {
232 Map<String, Object> errors = _getErrors(portletSession);
233
234 return Collections.unmodifiableSet(errors.keySet()).iterator();
235 }
236
237 public static Set<String> keySet(PortletRequest portletRequest) {
238 return keySet(portletRequest.getPortletSession());
239 }
240
241 public static Set<String> keySet(PortletSession portletSession) {
242 Map<String, Object> errors = _getErrors(portletSession);
243
244 return Collections.unmodifiableSet(errors.keySet());
245 }
246
247 public static void print(PortletRequest portletRequest) {
248 print(portletRequest.getPortletSession());
249 }
250
251 public static void print(PortletSession portletSession) {
252 Iterator<String> itr = iterator(portletSession);
253
254 while (itr.hasNext()) {
255 System.out.println(itr.next());
256 }
257 }
258
259 public static int size(PortletRequest portletRequest) {
260 return size(portletRequest.getPortletSession());
261 }
262
263 public static int size(PortletSession portletSession) {
264 Map<String, Object> errors = _getErrors(portletSession);
265
266 return errors.size();
267 }
268
269 private static Map<String, Object> _getErrors(
270 PortletSession portletSession) {
271
272 Map<String, Object> errors = null;
273
274 try {
275 errors = (Map<String, Object>)portletSession.getAttribute(KEY);
276
277 if (errors == null) {
278 errors = new LinkedHashMap<String, Object>();
279
280 portletSession.setAttribute(KEY, errors);
281 }
282 }
283 catch (IllegalStateException ise) {
284 errors = new LinkedHashMap<String, Object>();
285 }
286
287 return errors;
288 }
289
290 }