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.servlet;
016    
017    import java.util.Collections;
018    import java.util.Enumeration;
019    import java.util.HashMap;
020    import java.util.HashSet;
021    import java.util.Map;
022    import java.util.Set;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletRequestWrapper;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class TempAttributesServletRequest extends HttpServletRequestWrapper {
031    
032            public TempAttributesServletRequest(HttpServletRequest request) {
033                    super(request);
034            }
035    
036            @Override
037            public Object getAttribute(String name) {
038                    Object value = _attributes.get(name);
039    
040                    if (value == _nullValue) {
041                            return null;
042                    }
043    
044                    if (value != null) {
045                            return value;
046                    }
047    
048                    return super.getAttribute(name);
049            }
050    
051            @Override
052            public Enumeration<String> getAttributeNames() {
053                    Enumeration<String> superEnumeration = super.getAttributeNames();
054    
055                    if (_attributes.isEmpty()) {
056                            return superEnumeration;
057                    }
058    
059                    Set<String> names = new HashSet<String>();
060    
061                    while (superEnumeration.hasMoreElements()) {
062                            names.add(superEnumeration.nextElement());
063                    }
064    
065                    names.addAll(_attributes.keySet());
066    
067                    return Collections.enumeration(names);
068            }
069    
070            @Override
071            public void removeAttribute(String name) {
072                    _attributes.remove(name);
073    
074                    super.removeAttribute(name);
075            }
076    
077            public void setTempAttribute(String name, Object value) {
078                    if (value == null) {
079                            value = _nullValue;
080                    }
081    
082                    _attributes.put(name, value);
083            }
084    
085            private static Object _nullValue = new Object();
086    
087            private Map<String, Object> _attributes = new HashMap<String, Object>();
088    
089    }