1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wsrp;
24  
25  import java.util.Hashtable;
26  import java.util.Map;
27  
28  import javax.portlet.PortletSession;
29  
30  import oasis.names.tc.wsrp.v1.intf.WSRP_v1_Markup_PortType;
31  
32  import org.apache.wsrp4j.consumer.GroupSessionMgr;
33  import org.apache.wsrp4j.consumer.driver.GenericUserSessionImpl;
34  import org.apache.wsrp4j.consumer.util.ConsumerConstants;
35  import org.apache.wsrp4j.exception.WSRPException;
36  
37  /**
38   * <a href="UserSessionImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Michael Young
41   *
42   */
43  public class UserSessionImpl extends GenericUserSessionImpl {
44  
45      public UserSessionImpl(String producerID, String userID,
46              String portletServicesURL, PortletSession portletSession)
47              throws WSRPException {
48  
49          super(producerID, userID, portletServicesURL);
50  
51          this._portletSession = portletSession;
52          _userSession = getUserSessionMap();
53          setGroupSessionTable(getGroupSessionTable());
54      }
55  
56      private Hashtable getGroupSessionTable() {
57  
58          if ((groupSessions = (Hashtable) _userSession
59                  .get(ConsumerConstants.WSRP_GROUPSESSIONS)) == null) {
60              groupSessions = new Hashtable();
61              _userSession
62                      .put(ConsumerConstants.WSRP_GROUPSESSIONS, groupSessions);
63          }
64  
65          return groupSessions;
66      }
67  
68      /**
69       * Get the group session for this group ID
70       *
71       * @param groupID
72       *            ID of the portlet application
73       * @return The group session for the provided group ID
74       */
75      public GroupSessionMgr getGroupSession(String groupID)
76          throws WSRPException {
77  
78          if (groupID != null) {
79              GroupSessionMgr groupSession = (GroupSessionMgr) this.groupSessions
80                      .get(groupID);
81              if (groupSession == null) {
82  
83                  groupSession = new GroupSessionImpl(groupID, this
84                          .getMarkupInterfaceURL());
85                  addGroupSession(groupSession);
86              }
87  
88              return groupSession;
89          }
90          return null;
91      }
92  
93      public WSRP_v1_Markup_PortType getWSRPBaseService() {
94          WSRP_v1_Markup_PortType markupPort = null;
95          if ((markupPort = (WSRP_v1_Markup_PortType) _userSession
96                  .get(ConsumerConstants.WSRP_MARKUP_PORT)) == null) {
97              markupPort = super.getWSRPBaseService();
98              _userSession.put(ConsumerConstants.WSRP_MARKUP_PORT, markupPort);
99          }
100 
101         this.setWSRPBaseService(markupPort);
102         return markupPort;
103     }
104 
105     public boolean isInitCookieRequired() {
106         Boolean initCookieReq = null;
107         if ((initCookieReq = (Boolean) _userSession
108                 .get(ConsumerConstants.WSRP_INIT_COOKIE_REQ)) == null) {
109             initCookieReq = Boolean.valueOf(super.isInitCookieRequired());
110             setInitCookieRequired(initCookieReq.booleanValue());
111         }
112 
113         return initCookieReq.booleanValue();
114     }
115 
116     public void setInitCookieRequired(boolean initCookieRequired) {
117         _userSession.put(ConsumerConstants.WSRP_INIT_COOKIE_REQ, Boolean.valueOf(
118                 initCookieRequired));
119         super.setInitCookieRequired(initCookieRequired);
120     }
121 
122     public boolean isInitCookieDone() {
123         Boolean initCookieDone = null;
124         if ((initCookieDone = (Boolean) _userSession
125                 .get(ConsumerConstants.WSRP_INIT_COOKIE_DONE)) == null) {
126             initCookieDone = Boolean.valueOf(super.isInitCookieDone());
127             setInitCookieDone(initCookieDone.booleanValue());
128         }
129 
130         return initCookieDone.booleanValue();
131     }
132 
133     public void setInitCookieDone(boolean initCookieDone) {
134         _userSession.put(ConsumerConstants.WSRP_INIT_COOKIE_DONE, Boolean.valueOf(
135                 initCookieDone));
136         super.setInitCookieRequired(initCookieDone);
137     }
138 
139     private Map getUserSessionMap() {
140         String key = createKey();
141         Map myMap = (Map) this._portletSession.getAttribute(key,
142                 PortletSession.APPLICATION_SCOPE);
143 
144         if (myMap == null) {
145             myMap = new Hashtable();
146             this._portletSession.setAttribute(key, myMap,
147                     PortletSession.APPLICATION_SCOPE);
148         }
149 
150         return myMap;
151     }
152 
153     private String createKey() {
154         return "user :" + this.getUserID() + " producer:"
155                 + this.getProducerID();
156     }
157 
158     private PortletSession _portletSession = null;
159 
160     private Map _userSession = null;
161 
162 }