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