001    /**
002     * Copyright (c) 2000-2012 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            public String get(HttpServletRequest request, long plid) {
031                    HttpSession session = getPortalSession(request);
032    
033                    return (String) session.getAttribute(encodeKey(plid));
034            }
035    
036            public void update(HttpServletRequest request, long plid,
037                    String typeSettings) {
038    
039                    HttpSession session = getPortalSession(request);
040    
041                    session.setAttribute(encodeKey(plid), typeSettings);
042            }
043    
044            protected String encodeKey(long plid) {
045                    return SessionLayoutClone.class.getName().concat(
046                            StringPool.POUND).concat(StringUtil.toHexString(plid));
047            }
048    
049            protected HttpSession getPortalSession(HttpServletRequest request) {
050                    HttpServletRequest originalRequest = request;
051    
052                    while (originalRequest instanceof HttpServletRequestWrapper) {
053                            if (originalRequest instanceof SharedSessionServletRequest) {
054                                    SharedSessionServletRequest sharedSessionServletRequest =
055                                            (SharedSessionServletRequest)originalRequest;
056    
057                                    return sharedSessionServletRequest.getSharedSession();
058                            }
059    
060                            originalRequest = (HttpServletRequest)
061                                    ((HttpServletRequestWrapper)originalRequest).getRequest();
062                    }
063    
064                    return request.getSession();
065            }
066    
067    }