001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 =
063                                    (HttpServletRequest)
064                                            ((HttpServletRequestWrapper)originalRequest).getRequest();
065                    }
066    
067                    return request.getSession();
068            }
069    
070    }