1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  import com.liferay.util.servlet.SharedSessionServletRequest;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletRequestWrapper;
22  import javax.servlet.http.HttpSession;
23  
24  /**
25   * <a href="SessionLayoutClone.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class SessionLayoutClone implements LayoutClone {
30  
31      public String get(HttpServletRequest request, long plid) {
32          HttpSession session = getPortalSession(request);
33  
34          return (String) session.getAttribute(encodeKey(plid));
35      }
36  
37      public void update(HttpServletRequest request, long plid,
38          String typeSettings) {
39  
40          HttpSession session = getPortalSession(request);
41  
42          session.setAttribute(encodeKey(plid), typeSettings);
43      }
44  
45      protected String encodeKey(long plid) {
46          return SessionLayoutClone.class.getName().concat(
47              StringPool.POUND).concat(String.valueOf(plid));
48      }
49  
50      protected HttpSession getPortalSession(HttpServletRequest request) {
51          HttpServletRequest originalRequest = request;
52  
53          while (originalRequest instanceof HttpServletRequestWrapper) {
54              if (originalRequest instanceof SharedSessionServletRequest) {
55                  SharedSessionServletRequest sharedSessionServletRequest =
56                      (SharedSessionServletRequest)originalRequest;
57  
58                  return sharedSessionServletRequest.getSharedSession();
59              }
60  
61              originalRequest = (HttpServletRequest)
62                  ((HttpServletRequestWrapper)originalRequest).getRequest();
63          }
64  
65          return request.getSession();
66      }
67  
68  }