001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.messaging.webapp;
023    
024    import java.util.Collections;
025    import java.util.Enumeration;
026    import java.util.HashMap;
027    import java.util.Locale;
028    import java.util.Map;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletRequestWrapper;
032    
033    /**
034     * @author Venkat DANDA
035     */
036    @SuppressWarnings({ "unchecked", "rawtypes" })
037    public class HttpServletRequestParamWrapper extends HttpServletRequestWrapper {
038        
039        private final Locale locale;
040        private final Enumeration<Locale> locales;
041        private final Map requestParams;
042        private final Map requestAttributes;
043    
044        /**
045         * @param request
046         */
047        public HttpServletRequestParamWrapper(HttpServletRequest request) {
048            super(request);
049            this.locale = request.getLocale();
050            this.locales = request.getLocales();
051            this.requestParams = new HashMap(request.getParameterMap());
052            this.requestAttributes = new HashMap();
053        }
054    
055        /* (non-Javadoc)
056         * @see javax.servlet.ServletRequestWrapper#getParameter(java.lang.String)
057         */
058        @Override
059        public String getParameter(String name) {
060            String retValue = null;
061            String[] paramValues = getParameterValues(name);
062            if (paramValues != null && paramValues.length > 0) {
063                retValue = paramValues[0];
064            }
065            return retValue;
066        }
067    
068        /* (non-Javadoc)
069         * @see javax.servlet.ServletRequestWrapper#getParameterMap()
070         */
071        @Override
072        public Map getParameterMap() {
073            return Collections.unmodifiableMap(requestParams);
074        }
075    
076        /* (non-Javadoc)
077         * @see javax.servlet.ServletRequestWrapper#getParameterNames()
078         */
079        @Override
080        public Enumeration getParameterNames() {
081            return Collections.enumeration(requestParams.keySet());
082        }
083    
084        /* (non-Javadoc)
085         * @see javax.servlet.ServletRequestWrapper#getParameterValues(java.lang.String)
086         */
087        @Override
088        public String[] getParameterValues(String name) {
089            String[] retValues = null;
090            String[] tmpValues = (String[]) requestParams.get(name);
091            if (tmpValues != null) {
092                retValues = new String[tmpValues.length];
093                System.arraycopy(tmpValues, 0, retValues, 0, tmpValues.length);
094            }
095            return retValues;
096        }
097    
098        /**
099         * New method to set the parameter value.
100         * @param name
101         * @param value
102         */
103        public void setParameter(String name, String value) {
104            String[] param = { value };
105            setParameter(name, param);
106        }
107    
108        /**
109         * New method to set the parameter with multiple values.
110         * @param name
111         * @param values
112         */
113        public void setParameter(String name, String[] values) {
114            requestParams.put(name, values);
115        }
116    
117            @Override
118            public Object getAttribute(String name) {
119                    return requestAttributes.get(name);
120            }
121    
122            @Override
123            public Enumeration getAttributeNames() {
124                    return Collections.enumeration(requestAttributes.keySet());
125            }
126    
127            @Override
128            public void removeAttribute(String name) {
129                    requestAttributes.remove(name);
130            }
131    
132            @Override
133            public void setAttribute(String name, Object o) {
134                    requestAttributes.put(name, o);
135            }
136            
137            @Override
138            public Locale getLocale() {
139                return locale;
140            }
141            
142            @Override
143            public Enumeration getLocales() {
144                return locales;
145            }
146    }