Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: ru/gammalabs/ice/framework/SessionUtil.java


1   /*
2    *  $Id: SessionUtil.java,v 1.5 2002/12/07 09:22:08 dimitry Exp $
3    *
4    *  Copyright (c) 2002 GammaLabs.
5    * ==================================================================
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19   * ==================================================================
20   */
21  package ru.gammalabs.ice.framework;
22  
23  import ru.gammalabs.ice.framework.Actor;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpSession;
27  
28  public class SessionUtil
29  {
30    private static String SESSION_KEY_ACTOR = "session.actor";
31  
32    public static Actor createActor(HttpServletRequest request)
33    {
34      return createActor(request.getSession());
35    }
36  
37    public static Actor createActor(HttpSession session)
38    {
39      Actor actor = getActorFromSession(session);
40      if (actor != null)
41        actor.logout();
42      actor = new Actor(session);
43      session.setAttribute(SESSION_KEY_ACTOR, actor);
44      return actor;
45    }
46  
47    private static Actor getActorFromSession(HttpSession session)
48    {
49      return (Actor) session.getAttribute(SESSION_KEY_ACTOR);
50    }
51  
52    /**
53     * Получить актера по запросу.
54     *
55     * @param request
56     * @return Текущего актера.
57     * @throws UnauthorizedAccessException Если актера еще не авторизовывали.
58     */
59    public static Actor getActor(HttpServletRequest request)
60      throws UnauthorizedAccessException
61    {
62      return getActor(request.getSession());
63    }
64  
65    /**
66     * Получить актора по сессии.
67     *
68     * @param session
69     * @return Текущего актера.
70     * @throws UnauthorizedAccessException Если актера еще не создавали.
71     */
72    public static Actor getActor(HttpSession session)
73      throws UnauthorizedAccessException
74    {
75      return getActor(session,false);
76    }
77  
78    /**
79     * Получить актера, с возможностью создать если такового нет.
80     *
81     * @param session Сессия
82     * @param create если этот параметр == true и актер не существует - он будет создан.
83     * @return Текущий актер.
84     * @throws UnauthorizedAccessException в случае если create==false и актера не существует.
85     */
86    public static Actor getActor(HttpSession session, boolean create)
87      throws UnauthorizedAccessException
88    {
89      Actor actor = getActorFromSession(session);
90  
91      if (actor == null)
92      {
93        if (create)
94        {
95          actor = createActor(session);
96        }
97        else
98        {
99          throw new UnauthorizedAccessException();
100       }
101     }
102 
103     return actor;
104   }
105 }