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