001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.util.servlet.SharedSessionServletRequest;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletRequestWrapper;
022    import javax.servlet.http.HttpSession;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class SessionLayoutClone implements LayoutClone {
028    
029            public String get(HttpServletRequest request, long plid) {
030                    HttpSession session = getPortalSession(request);
031    
032                    return (String) session.getAttribute(encodeKey(plid));
033            }
034    
035            public void update(HttpServletRequest request, long plid,
036                    String typeSettings) {
037    
038                    HttpSession session = getPortalSession(request);
039    
040                    session.setAttribute(encodeKey(plid), typeSettings);
041            }
042    
043            protected String encodeKey(long plid) {
044                    return SessionLayoutClone.class.getName().concat(
045                            StringPool.POUND).concat(String.valueOf(plid));
046            }
047    
048            protected HttpSession getPortalSession(HttpServletRequest request) {
049                    HttpServletRequest originalRequest = request;
050    
051                    while (originalRequest instanceof HttpServletRequestWrapper) {
052                            if (originalRequest instanceof SharedSessionServletRequest) {
053                                    SharedSessionServletRequest sharedSessionServletRequest =
054                                            (SharedSessionServletRequest)originalRequest;
055    
056                                    return sharedSessionServletRequest.getSharedSession();
057                            }
058    
059                            originalRequest = (HttpServletRequest)
060                                    ((HttpServletRequestWrapper)originalRequest).getRequest();
061                    }
062    
063                    return request.getSession();
064            }
065    
066    }