001    /**
002     * Copyright (c) 2000-2010 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.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.ServletVersionDetector;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.util.PropsValues;
022    
023    import java.util.Enumeration;
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpSession;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Brian Myunghun Kim
033     * @author Shuyang Zhou
034     */
035    public class SharedSessionUtil {
036    
037            public static Map<String, Object> getSharedSessionAttributes(
038                    HttpServletRequest request) {
039    
040                    HttpSession session = request.getSession();
041    
042                    if (ServletVersionDetector.is2_5()) {
043                            Map<String, Object> attributes = new HashMap<String, Object>();
044    
045                            Enumeration<String> enu = session.getAttributeNames();
046    
047                            while (enu.hasMoreElements()) {
048                                    String name = enu.nextElement();
049    
050                                    Object value = session.getAttribute(name);
051    
052                                    if (value == null) {
053                                            continue;
054                                    }
055    
056                                    if (ArrayUtil.contains(
057                                                    PropsValues.SHARED_SESSION_ATTRIBUTES_EXCLUDES, name)) {
058    
059                                            continue;
060                                    }
061    
062                                    for (String sharedName :
063                                                    PropsValues.SHARED_SESSION_ATTRIBUTES) {
064    
065                                            if (!name.startsWith(sharedName)) {
066                                                    continue;
067                                            }
068    
069                                            if (_log.isDebugEnabled()) {
070                                                    _log.debug("Sharing " + name);
071                                            }
072    
073                                            attributes.put(name, value);
074    
075                                            break;
076                                    }
077                            }
078    
079                            return attributes;
080                    }
081                    else {
082                            SharedSessionAttributeCache sharedSessionAttributeCache =
083                                    SharedSessionAttributeCache.getInstance(session);
084    
085                            Map<String, Object> values =
086                                    sharedSessionAttributeCache.getValues();
087    
088                            if (_log.isDebugEnabled()) {
089                                    _log.debug("Shared session attributes " + values);
090                            }
091    
092                            return values;
093                    }
094            }
095    
096            private static Log _log = LogFactoryUtil.getLog(SharedSessionUtil.class);
097    
098    }