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