1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }