001    /**
002     * Copyright (c) 2000-2011 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.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    }