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.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.util.Mergeable;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.WebKeys;
024    
025    import java.util.Collections;
026    import java.util.Enumeration;
027    import java.util.HashMap;
028    import java.util.HashSet;
029    import java.util.Map;
030    import java.util.Set;
031    import java.util.concurrent.locks.Lock;
032    
033    import javax.servlet.ServletRequest;
034    import javax.servlet.http.HttpServletRequest;
035    
036    /**
037     * @author Shuyang Zhou
038     */
039    public class RestrictPortletServletRequest
040            extends PersistentHttpServletRequestWrapper {
041    
042            public RestrictPortletServletRequest(HttpServletRequest request) {
043                    super(request);
044            }
045    
046            @Override
047            public Object getAttribute(String name) {
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                    Lock lock = (Lock)servletRequest.getAttribute(
100                            WebKeys.PARALLEL_RENDERING_MERGE_LOCK);
101    
102                    if (lock != null) {
103                            lock.lock();
104                    }
105    
106                    try {
107                            doMergeSharedAttributes(servletRequest);
108                    }
109                    finally {
110                            if (lock != null) {
111                                    lock.unlock();
112                            }
113                    }
114            }
115    
116            @Override
117            public void removeAttribute(String name) {
118                    _attributes.put(name, _nullValue);
119            }
120    
121            @Override
122            public void setAttribute(String name, Object value) {
123                    if (value == null) {
124                            value = _nullValue;
125                    }
126    
127                    _attributes.put(name, value);
128            }
129    
130            protected void doMergeSharedAttributes(ServletRequest servletRequest) {
131                    for (Map.Entry<String, Object> entry : _attributes.entrySet()) {
132                            String name = entry.getKey();
133                            Object value = entry.getValue();
134    
135                            doMergeSharedAttributes(servletRequest, name, value);
136                    }
137            }
138    
139            protected void doMergeSharedAttributes(
140                    ServletRequest servletRequest, String name, Object value) {
141    
142                    if (isSharedRequestAttribute(name)) {
143                            if (value == _nullValue) {
144                                    servletRequest.removeAttribute(name);
145    
146                                    if (_log.isDebugEnabled()) {
147                                            _log.debug("Remove shared attribute " + name);
148                                    }
149                            }
150                            else {
151                                    Object masterValue = servletRequest.getAttribute(name);
152    
153                                    if ((masterValue == null) || !(value instanceof Mergeable)) {
154                                            servletRequest.setAttribute(name, value);
155    
156                                            if (_log.isDebugEnabled()) {
157                                                    _log.debug("Set shared attribute " + name);
158                                            }
159                                    }
160                                    else {
161                                            Mergeable<Object> masterMergeable =
162                                                    (Mergeable<Object>)masterValue;
163                                            Mergeable<Object> slaveMergeable = (Mergeable<Object>)value;
164    
165                                            masterMergeable.merge(slaveMergeable);
166    
167                                            if (_log.isDebugEnabled()) {
168                                                    _log.debug("Merge shared attribute " + name);
169                                            }
170                                    }
171                            }
172                    }
173                    else {
174                            if ((value != _nullValue) && _log.isDebugEnabled()) {
175                                    _log.debug("Ignore setting restricted attribute " + name);
176                            }
177                    }
178            }
179    
180            protected boolean isSharedRequestAttribute(String name) {
181                    for (String requestSharedAttribute : _REQUEST_SHARED_ATTRIBUTES) {
182                            if (name.startsWith(requestSharedAttribute)) {
183                                    return true;
184                            }
185                    }
186    
187                    return false;
188            }
189    
190            private static final String[] _REQUEST_SHARED_ATTRIBUTES =
191                    PropsUtil.getArray(PropsKeys.REQUEST_SHARED_ATTRIBUTES);
192    
193            private static Log _log = LogFactoryUtil.getLog(
194                    RestrictPortletServletRequest.class);
195    
196            private static Object _nullValue = new Object();
197    
198            private Map<String, Object> _attributes = new HashMap<String, Object>();
199    
200    }