001    /**
002     * Copyright (c) 2000-2013 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.portal.kernel.util.StringUtil;
019    import com.liferay.portal.servlet.SharedSessionServletRequest;
020    
021    import javax.servlet.http.HttpServletRequest;
022    import javax.servlet.http.HttpServletRequestWrapper;
023    import javax.servlet.http.HttpSession;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class SessionLayoutClone implements LayoutClone {
029    
030            @Override
031            public String get(HttpServletRequest request, long plid) {
032                    HttpSession session = getPortalSession(request);
033    
034                    return (String)session.getAttribute(encodeKey(plid));
035            }
036    
037            @Override
038            public void update(
039                    HttpServletRequest request, long plid, String typeSettings) {
040    
041                    HttpSession session = getPortalSession(request);
042    
043                    session.setAttribute(encodeKey(plid), typeSettings);
044            }
045    
046            protected String encodeKey(long plid) {
047                    return SessionLayoutClone.class.getName().concat(
048                            StringPool.POUND).concat(StringUtil.toHexString(plid));
049            }
050    
051            protected HttpSession getPortalSession(HttpServletRequest request) {
052                    HttpServletRequest originalRequest = request;
053    
054                    while (originalRequest instanceof HttpServletRequestWrapper) {
055                            if (originalRequest instanceof SharedSessionServletRequest) {
056                                    SharedSessionServletRequest sharedSessionServletRequest =
057                                            (SharedSessionServletRequest)originalRequest;
058    
059                                    return sharedSessionServletRequest.getSharedSession();
060                            }
061    
062                            originalRequest = (HttpServletRequest)
063                                    ((HttpServletRequestWrapper)originalRequest).getRequest();
064                    }
065    
066                    return request.getSession();
067            }
068    
069    }