001    /**
002     * Copyright (c) 2000-2013 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.kernel.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.PersistentHttpServletRequestWrapper;
020    import com.liferay.portal.kernel.servlet.RequestDispatcherAttributeNames;
021    
022    import java.util.Collections;
023    import java.util.Enumeration;
024    import java.util.HashMap;
025    import java.util.HashSet;
026    import java.util.Map;
027    import java.util.Set;
028    
029    import javax.servlet.ServletRequest;
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class RestrictPortletServletRequest
036            extends PersistentHttpServletRequestWrapper {
037    
038            public RestrictPortletServletRequest(HttpServletRequest request) {
039                    super(request);
040            }
041    
042            @Override
043            public Object getAttribute(String name) {
044                    if (RequestDispatcherAttributeNames.contains(name)) {
045                            return super.getAttribute(name);
046                    }
047    
048                    Object value = _attributes.get(name);
049    
050                    if (value == _nullValue) {
051                            return null;
052                    }
053    
054                    if (value != null) {
055                            return value;
056                    }
057    
058                    return super.getAttribute(name);
059            }
060    
061            @Override
062            public Enumeration<String> getAttributeNames() {
063                    Enumeration<String> superEnumeration = super.getAttributeNames();
064    
065                    if (_attributes.isEmpty()) {
066                            return superEnumeration;
067                    }
068    
069                    Set<String> names = new HashSet<String>();
070    
071                    while (superEnumeration.hasMoreElements()) {
072                            names.add(superEnumeration.nextElement());
073                    }
074    
075                    for (Map.Entry<String, Object> entry : _attributes.entrySet()) {
076                            String key = entry.getKey();
077                            Object value = entry.getValue();
078    
079                            if (value == null) {
080                                    names.remove(key);
081                            }
082                            else {
083                                    names.add(key);
084                            }
085                    }
086    
087                    names.addAll(_attributes.keySet());
088    
089                    return Collections.enumeration(names);
090            }
091    
092            public Map<String, Object> getAttributes() {
093                    return _attributes;
094            }
095    
096            public void mergeSharedAttributes() {
097                    ServletRequest servletRequest = getRequest();
098    
099                    for (Map.Entry<String, Object> entry : _attributes.entrySet()) {
100                            String name = entry.getKey();
101                            Object value = entry.getValue();
102    
103                            if (name.startsWith("LIFERAY_SHARED_") ||
104                                    name.startsWith("javax.portlet.") ||
105                                    name.startsWith("javax.servlet.")) {
106    
107                                    if (value == _nullValue) {
108                                            servletRequest.removeAttribute(name);
109    
110                                            if (_log.isInfoEnabled()) {
111                                                    _log.info("Remove shared attribute " + name);
112                                            }
113                                    }
114                                    else {
115                                            servletRequest.setAttribute(name, value);
116    
117                                            if (_log.isInfoEnabled()) {
118                                                    _log.info("Set shared attribute " + name);
119                                            }
120                                    }
121                            }
122                            else {
123                                    if ((value != _nullValue) && _log.isDebugEnabled()) {
124                                            _log.debug("Ignore setting restricted attribute " + name);
125                                    }
126                            }
127                    }
128            }
129    
130            @Override
131            public void removeAttribute(String name) {
132                    if (RequestDispatcherAttributeNames.contains(name)) {
133                            super.removeAttribute(name);
134                    }
135                    else {
136                            _attributes.put(name, _nullValue);
137                    }
138            }
139    
140            @Override
141            public void setAttribute(String name, Object value) {
142                    if (RequestDispatcherAttributeNames.contains(name)) {
143                            super.setAttribute(name, value);
144                    }
145                    else {
146                            if (value == null) {
147                                    value = _nullValue;
148                            }
149    
150                            _attributes.put(name, value);
151                    }
152            }
153    
154            private static Log _log = LogFactoryUtil.getLog(
155                    RestrictPortletServletRequest.class);
156    
157            private static Object _nullValue = new Object();
158    
159            private Map<String, Object> _attributes = new HashMap<String, Object>();
160    
161    }