001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Portal/Sources/es/udc/mypersonalizer/portal/controller/session/SessionManager.java,v 1.1.1.1 2004/03/25 12:08:39 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:39 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.portal.controller.session;
042:
043: import es.udc.mypersonalizer.kernel.model.editors.EditorFactory;
044: import es.udc.mypersonalizer.kernel.model.editors.DesktopLayoutEditor;
045: import es.udc.mypersonalizer.kernel.model.editors.WorkspaceLayoutEditor;
046: import es.udc.mypersonalizer.kernel.model.repository.interfaces.DesktopLayout;
047: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceButtonsState;
048: import es.udc.mypersonalizer.kernel.model.repository.interfaces.WorkspaceLayout;
049: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
050: import es.udc.mypersonalizer.portal.conventions.SessionAttributeNames;
051: import es.udc.mypersonalizer.portal.model.permissions.UserCredentials;
052: import es.udc.mypersonalizer.portal.model.types.user.SignInResult;
053: import java.util.*;
054: import javax.servlet.http.HttpServletRequest;
055: import javax.servlet.http.HttpSession;
056:
057: /**
058: * This class provides via its static methods a way to store and retrieve
059: * information needed to be available sesion-wide.<p>
060: *
061: * The names established for storing objects into the request are defined
062: * in {@link SessionAttributeNames}.
063: *
064: * @author Daniel Fernandez
065: * @since 1.0
066: */
067: public class SessionManager {
068:
069: /**
070: * Avoids instantiation of this class
071: */
072: private SessionManager() {
073: }
074:
075: /**
076: * This method initializes the session with information coming from a Sign
077: * In operation, in a {@link SignInResult} object.<p>
078: * The user credentials, desktop layout, available workspace types and
079: * current workspace layout are put into the session, and also a flag
080: * indicating if the user is authenticated or not (as the
081: * <tt>anonymous</tt> user can be logged in but not having authenticated
082: * and thus not being able to perform most editing operations.
083: *
084: * @param request the request object
085: * @param signInResult the SignInResult which contains the needed data.
086: * @param userAuthenticated true if the user has authenticated through the
087: * sign in form, false if not.
088: */
089: public static void initializeSession(HttpServletRequest request,
090: SignInResult signInResult, boolean userAuthenticated) {
091:
092: String loginName = signInResult.getLoginName();
093: Collection userGroupIdentifiers = signInResult
094: .getUserGroupIdentifiers();
095: UserCredentials userCredentials = new UserCredentials(
096: loginName, userGroupIdentifiers);
097:
098: DesktopLayout desktopLayout = signInResult.getDesktopLayout();
099:
100: List availableWorkspaces = signInResult
101: .getAvailableWorkspaces();
102:
103: setUserCredentials(request, userCredentials);
104: setDesktopLayout(request, desktopLayout);
105: setAvailableWorkspaces(request, availableWorkspaces);
106:
107: /*
108: * We must check if the user has valid workspaces.
109: */
110: if (availableWorkspaces.isEmpty()) {
111:
112: setUserHasWorkspaces(request, false);
113:
114: } else {
115:
116: setUserHasWorkspaces(request, true);
117:
118: WorkspaceLayout currentWorkspaceLayout = signInResult
119: .getCurrentWorkspaceLayout();
120: Collection serviceButtonsStates = signInResult
121: .getServiceButtonsStates();
122:
123: setCurrentWorkspace(request, currentWorkspaceLayout,
124: serviceButtonsStates);
125:
126: }
127:
128: setUserAuthenticated(request, userAuthenticated);
129:
130: }
131:
132: /**
133: * Invalidates the session.
134: *
135: * @param request the request object.
136: */
137: public static void invalidateSession(HttpServletRequest request) {
138: HttpSession session = request.getSession(false);
139: if (session != null) {
140: session.invalidate();
141: }
142: }
143:
144: /**
145: * Checks if the session has been initialized by testing the existance
146: * of the user credentials in the session.
147: *
148: * @param request the reques object.
149: * @return true if the session is already initialized,
150: * false if not.
151: */
152: public static boolean isSessionInitialized(
153: HttpServletRequest request) {
154: HttpSession session = request.getSession(false);
155: if (session != null) {
156: if (getUserCredentials(request) != null) {
157: return true;
158: } else {
159: return false;
160: }
161: } else {
162: return false;
163: }
164: }
165:
166: /**
167: * This method checks the value of the <tt>userAuthenticated</tt> sesion
168: * parameter and returns it. This is useful because the
169: * <tt>anonymous</tt> user can be signed in without going through the
170: * sign in form, and many actions should therefore be forbidden.
171: *
172: * @param request the request object.
173: * @return true is the user is authenticated, false if not.
174: */
175: public static boolean isUserAuthenticated(HttpServletRequest request) {
176: HttpSession session = request.getSession(false);
177: if (session != null) {
178: Boolean result = (Boolean) session
179: .getAttribute(SessionAttributeNames.USER_AUTHENTICATED_SESSION_NAME);
180: if (result != null) {
181: return result.booleanValue();
182: } else {
183: return false;
184: }
185: } else {
186: return false;
187: }
188: }
189:
190: /**
191: * Sets a new value for the <tt>userAuthenticated</tt> session parameter.
192: *
193: * @param request the request object.
194: * @param userAuthenticated true if the user has authenticated through
195: * a sign in form, false if not.
196: */
197: protected static void setUserAuthenticated(
198: HttpServletRequest request, boolean userAuthenticated) {
199: HttpSession session = request.getSession(true);
200: session.setAttribute(
201: SessionAttributeNames.USER_AUTHENTICATED_SESSION_NAME,
202: new Boolean(userAuthenticated));
203: }
204:
205: /**
206: * Returns the user credentials stored in the session, null if the
207: * attribute does not exist.
208: *
209: * @param request the request object.
210: * @return the user credentials, null if they're not in
211: * the session.
212: */
213: public static UserCredentials getUserCredentials(
214: HttpServletRequest request) {
215: HttpSession session = request.getSession(false);
216: if (session != null) {
217: return (UserCredentials) session
218: .getAttribute(SessionAttributeNames.USER_CREDENTIALS_SESSION_NAME);
219: } else {
220: return null;
221: }
222: }
223:
224: /**
225: * Puts the user credentials into the session.
226: *
227: * @param request the request object.
228: * @param userCredentials the user credentials to be stored.
229: */
230: public static void setUserCredentials(HttpServletRequest request,
231: UserCredentials userCredentials) {
232: HttpSession session = request.getSession(true);
233: if (userCredentials == null) {
234: session
235: .removeAttribute(SessionAttributeNames.USER_CREDENTIALS_SESSION_NAME);
236: } else {
237: session
238: .setAttribute(
239: SessionAttributeNames.USER_CREDENTIALS_SESSION_NAME,
240: userCredentials);
241: }
242: }
243:
244: /**
245: * Returns the user's desktop layout stored in the session as an attribute,
246: * or null if does not exist.
247: *
248: * @param request the request object.
249: * @return the user's desktop layout stored in the request,
250: * null if it does not exist.
251: */
252: public static DesktopLayout getDesktopLayout(
253: HttpServletRequest request) {
254: HttpSession session = request.getSession(false);
255: if (session != null) {
256: return (DesktopLayout) session
257: .getAttribute(SessionAttributeNames.DESKTOP_LAYOUT_SESSION_NAME);
258: } else {
259: return null;
260: }
261: }
262:
263: /**
264: * Stores the desktop layout object into the session.
265: *
266: * @param request the request object.
267: * @param desktopLayout the desktop layout to be stored.
268: */
269: public static void setDesktopLayout(HttpServletRequest request,
270: DesktopLayout desktopLayout) {
271: HttpSession session = request.getSession(true);
272: if (desktopLayout == null) {
273: session
274: .removeAttribute(SessionAttributeNames.DESKTOP_LAYOUT_SESSION_NAME);
275: } else {
276: session.setAttribute(
277: SessionAttributeNames.DESKTOP_LAYOUT_SESSION_NAME,
278: desktopLayout);
279: }
280: }
281:
282: /**
283: * Returns the List of workspaces available to the user, stored as
284: * an attribute in the session, or null if the attribute does not exist.<p>
285: * This List is made of Map objects with the entries <tt>workspaceName</tt>,
286: * <tt>workspaceType</tt> and <tt>workspaceLayoutIdentifier</tt>.
287: *
288: * @param request the request object.
289: * @return the List of available workspaces.
290: */
291: public static List getAvailableWorkspaces(HttpServletRequest request) {
292: HttpSession session = request.getSession(false);
293: if (session != null) {
294: return (List) session
295: .getAttribute(SessionAttributeNames.AVAILABLE_WORKSPACES_SESSION_NAME);
296: } else {
297: return null;
298: }
299: }
300:
301: /**
302: * Puts the List of workspaces available to the user into the
303: * session as an attribute.
304: *
305: * @param request the request object.
306: * @param availableWorkspaces the List of available workspaces.
307: */
308: public static void setAvailableWorkspaces(
309: HttpServletRequest request, List availableWorkspaces) {
310: HttpSession session = request.getSession(true);
311: if (availableWorkspaces == null) {
312: session
313: .removeAttribute(SessionAttributeNames.AVAILABLE_WORKSPACES_SESSION_NAME);
314: } else {
315: session
316: .setAttribute(
317: SessionAttributeNames.AVAILABLE_WORKSPACES_SESSION_NAME,
318: availableWorkspaces);
319: }
320: if (availableWorkspaces.isEmpty()) {
321: setUserHasWorkspaces(request, false);
322: } else {
323: setUserHasWorkspaces(request, true);
324: }
325: }
326:
327: /**
328: * Adds a new workspaces to the list of available workspaces.
329: *
330: * @param request the request object.
331: * @param newWorkspaceLayout the new workspace layout.
332: * @throws InternalErrorException if any fatal errors happen.
333: */
334: public static void addAvailableWorkspace(
335: HttpServletRequest request,
336: WorkspaceLayout newWorkspaceLayout)
337: throws InternalErrorException {
338: HttpSession session = request.getSession(false);
339: if (session != null) {
340: List availableWorkspaces = getAvailableWorkspaces(request);
341: if (availableWorkspaces != null) {
342:
343: WorkspaceLayoutEditor newWorkspaceLayoutEditor = EditorFactory
344: .getInstance().createWorkspaceLayoutEditor(
345: newWorkspaceLayout.getProperty());
346:
347: Map newEntry = new HashMap();
348: newEntry.put("workspaceName", newWorkspaceLayoutEditor
349: .getWorkspaceName());
350: newEntry.put("workspaceLayoutIdentifier",
351: newWorkspaceLayout.getIdentifier());
352:
353: availableWorkspaces.add(newEntry);
354:
355: setAvailableWorkspaces(request, availableWorkspaces);
356: setUserHasWorkspaces(request, true);
357:
358: }
359: }
360: }
361:
362: /**
363: * Removes a workspace from the list of available workspaces.
364: *
365: * @param request the request object.
366: * @param removedWorkspaceLayoutIdentifier the identifier of the
367: * workspace layout to be removed.
368: * @throws InternalErrorException if any fatal errors happen.
369: */
370: public static void removeAvailableWorkspace(
371: HttpServletRequest request,
372: Long removedWorkspaceLayoutIdentifier)
373: throws InternalErrorException {
374: HttpSession session = request.getSession(false);
375: if (session != null) {
376: List availableWorkspaces = getAvailableWorkspaces(request);
377: if (availableWorkspaces != null) {
378:
379: Map workspaceToRemove = null;
380: Iterator availableWorkspacesIter = availableWorkspaces
381: .iterator();
382: while (availableWorkspacesIter.hasNext()) {
383: Map currentWorkspace = (Map) availableWorkspacesIter
384: .next();
385: if (currentWorkspace.get(
386: "workspaceLayoutIdentifier").equals(
387: removedWorkspaceLayoutIdentifier)) {
388: workspaceToRemove = currentWorkspace;
389: }
390: }
391: availableWorkspaces.remove(workspaceToRemove);
392:
393: setAvailableWorkspaces(request, availableWorkspaces);
394:
395: if (availableWorkspaces.isEmpty()) {
396: setUserHasWorkspaces(request, false);
397: }
398:
399: }
400: }
401: }
402:
403: /**
404: * Reorders the available workspaces as referenced in the desktop layout.
405: *
406: * @param request the request object.
407: * @throws InternalErrorException if any fatal errors happen.
408: */
409: public static void reOrderAvailableWorkspaces(
410: HttpServletRequest request) throws InternalErrorException {
411: HttpSession session = request.getSession(false);
412: if (session != null) {
413: List availableWorkspaces = getAvailableWorkspaces(request);
414: if (availableWorkspaces != null) {
415:
416: /*
417: * First, we create a Map of the current available workspaces
418: */
419: Map availableWorkspacesMap = new HashMap();
420: Iterator availableWorkspacesIter = availableWorkspaces
421: .iterator();
422: while (availableWorkspacesIter.hasNext()) {
423:
424: Map currentWorkspace = (Map) availableWorkspacesIter
425: .next();
426: availableWorkspacesMap.put(currentWorkspace
427: .get("workspaceLayoutIdentifier"),
428: currentWorkspace);
429:
430: }
431:
432: /*
433: * Now, we get the right order from the desktop layout,
434: * and create a new list in that order.
435: */
436: List newAvailableWorkspaces = new ArrayList();
437: DesktopLayout desktopLayout = getDesktopLayout(request);
438: DesktopLayoutEditor desktopLayoutEditor = EditorFactory
439: .getInstance().createDesktopLayoutEditor(
440: desktopLayout.getProperty());
441: List newWorkspacesOrder = desktopLayoutEditor
442: .getWorkspaceLayoutIdentifiers();
443: Iterator newWorkspacesOrderIter = newWorkspacesOrder
444: .iterator();
445: while (newWorkspacesOrderIter.hasNext()) {
446:
447: Long currentWorkspaceIdentifier = (Long) newWorkspacesOrderIter
448: .next();
449: newAvailableWorkspaces.add(availableWorkspacesMap
450: .get(currentWorkspaceIdentifier));
451:
452: }
453:
454: /*
455: * Set the available workspaces in the new order into the
456: * request.
457: */
458: setAvailableWorkspaces(request, newAvailableWorkspaces);
459:
460: }
461: }
462: }
463:
464: /**
465: * Tries to get from the session the workspace which identifier is
466: * equal to the one provided. Returns null if it is not found.
467: *
468: * @param request the request object.
469: * @param identifier the identifier of the desired workspace layout
470: * @return the WorkspaceLayout, null if it is not in the session.
471: */
472: public static WorkspaceLayout getWorkspaceLayout(
473: HttpServletRequest request, Long identifier) {
474: HttpSession session = request.getSession(false);
475: if (session != null) {
476: if (getUserHasWorkspaces(request)) {
477: return (WorkspaceLayout) session
478: .getAttribute(getWorkspaceLayoutSessionName(identifier));
479: } else {
480: return null;
481: }
482: } else {
483: return null;
484: }
485: }
486:
487: /**
488: * Sets the value of the session attribute indicating the current
489: * workspace layout to be the one provided in the
490: * <tt>workspaceLayoutIdentifier</tt> parameter.
491: *
492: * @param request the request object.
493: * @param workspaceLayoutIdentifier the identifier of the new current
494: * workspace.
495: */
496: public static void setCurrentWorkspaceLayout(
497: HttpServletRequest request, Long workspaceLayoutIdentifier) {
498:
499: HttpSession session = request.getSession(true);
500: session
501: .setAttribute(
502: SessionAttributeNames.CURRENT_WORKSPACE_LAYOUT_IDENTIFIER_SESSION_NAME,
503: workspaceLayoutIdentifier);
504:
505: }
506:
507: /**
508: * Puts all the data related to a workspace (this is, the workspaceLayout
509: * and the Collection of service buttons states of its services) into the
510: * session.
511: *
512: * @param request the request object
513: * @param workspaceLayout the workspace layout.
514: * @param serviceButtonsStates the Collection of service buttons states of
515: * the services.
516: */
517: public static void setCurrentWorkspace(HttpServletRequest request,
518: WorkspaceLayout workspaceLayout,
519: Collection serviceButtonsStates) {
520: HttpSession session = request.getSession(true);
521: Long identifier = workspaceLayout.getIdentifier();
522: session.setAttribute(getWorkspaceLayoutSessionName(identifier),
523: workspaceLayout);
524: Iterator buttonsStateIter = serviceButtonsStates.iterator();
525: while (buttonsStateIter.hasNext()) {
526: ServiceButtonsState serviceButtonsState = (ServiceButtonsState) buttonsStateIter
527: .next();
528: setServiceButtonsState(request, serviceButtonsState);
529: }
530: setCurrentWorkspaceLayout(request, identifier);
531: }
532:
533: /**
534: * Returns the current workspace layout by checking the value of the
535: * session attribute <tt>currentWorkspaceLayout</tt> and then getting
536: * the workspace layout itself also from the session.
537: *
538: * @param request the request object.
539: * @return the current workspace layout
540: */
541: public static WorkspaceLayout getCurrentWorkspaceLayout(
542: HttpServletRequest request) {
543: HttpSession session = request.getSession(false);
544: if (session != null) {
545: if (getUserHasWorkspaces(request)) {
546: Long identifier = (Long) session
547: .getAttribute(SessionAttributeNames.CURRENT_WORKSPACE_LAYOUT_IDENTIFIER_SESSION_NAME);
548: return (WorkspaceLayout) session
549: .getAttribute(getWorkspaceLayoutSessionName(identifier));
550: } else {
551: return null;
552: }
553: } else {
554: return null;
555: }
556: }
557:
558: /**
559: * Tries to get the service buttons state indicated by the identifier
560: * provided from the session. Returns null if it is not found.
561: *
562: * @param request the request object.
563: * @param identifier the identifier desired.
564: * @return the service buttons state desired, or null if
565: * it does not exist.
566: */
567: public static ServiceButtonsState getServiceButtonsState(
568: HttpServletRequest request, Long identifier) {
569: HttpSession session = request.getSession(false);
570: if (session != null) {
571: if (getUserHasWorkspaces(request)) {
572: return (ServiceButtonsState) session
573: .getAttribute(getServiceButtonsStateSessionName(identifier));
574: } else {
575: return null;
576: }
577: } else {
578: return null;
579: }
580: }
581:
582: /**
583: * Puts a new service buttons state object into the request. If it
584: * already existed, the new object replaces the old.
585: *
586: * @param request the request object.
587: * @param serviceButtonsState the service buttons state
588: */
589: public static void setServiceButtonsState(
590: HttpServletRequest request,
591: ServiceButtonsState serviceButtonsState) {
592:
593: HttpSession session = request.getSession(true);
594: Long identifier = serviceButtonsState.getPropertyIdentifier();
595: session.setAttribute(
596: getServiceButtonsStateSessionName(identifier),
597: serviceButtonsState);
598:
599: }
600:
601: /**
602: * Returns the flag that indicates if a user has workspaces in his / her
603: * desktop.
604: *
605: * @param request the request object.
606: * @return <tt>true</tt> if the user has workspaces, <tt>false</tt> if not.
607: */
608: public static boolean getUserHasWorkspaces(
609: HttpServletRequest request) {
610: HttpSession session = request.getSession(false);
611: if (session != null) {
612: return ((Boolean) session
613: .getAttribute(SessionAttributeNames.USER_HAS_WORKSPACES_SESSION_NAME))
614: .booleanValue();
615: } else {
616: return false;
617: }
618: }
619:
620: /**
621: * Sets a new value for the flag that indicates if a user has workspaces
622: * or not.
623: *
624: * @param request the request object.
625: * @param userHasWorkspaces the new value for the flag.
626: */
627: public static void setUserHasWorkspaces(HttpServletRequest request,
628: boolean userHasWorkspaces) {
629:
630: HttpSession session = request.getSession(true);
631: session.setAttribute(
632: SessionAttributeNames.USER_HAS_WORKSPACES_SESSION_NAME,
633: new Boolean(userHasWorkspaces));
634:
635: }
636:
637: private static String getWorkspaceLayoutSessionName(Long identifier) {
638: return (SessionAttributeNames.WORKSPACE_LAYOUT_SESSION_NAME_PREFIX + identifier
639: .toString());
640: }
641:
642: private static String getServiceButtonsStateSessionName(
643: Long identifier) {
644: return (SessionAttributeNames.SESSION_BUTTONS_STATE_SESSION_NAME_PREFIX + identifier
645: .toString());
646: }
647:
648: }
|