001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.util.servlet;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.util.Encryptor;
020    import com.liferay.util.EncryptorException;
021    
022    import java.security.Key;
023    
024    import java.util.Collections;
025    import java.util.Enumeration;
026    import java.util.HashMap;
027    import java.util.Map;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletRequestWrapper;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class EncryptedServletRequest extends HttpServletRequestWrapper {
036    
037            public EncryptedServletRequest(HttpServletRequest request, Key key) {
038                    super(request);
039    
040                    _params = new HashMap<String, String[]>();
041                    _key = key;
042    
043                    Enumeration<String> enu = getParameterNames();
044    
045                    while (enu.hasMoreElements()) {
046                            String name = enu.nextElement();
047                            String[] values = super.getParameterValues(name);
048    
049                            for (int i = 0; i < values.length; i++) {
050                                    if (Validator.isNotNull(values[i])) {
051                                            try {
052                                                    values[i] = Encryptor.decrypt(_key, values[i]);
053                                            }
054                                            catch (EncryptorException ee) {
055                                                    values[i] = StringPool.BLANK;
056                                            }
057                                    }
058                            }
059    
060                            _params.put(name, values);
061                    }
062            }
063    
064            public String getParameter(String name) {
065                    String[] values = _params.get(name);
066    
067                    if ((values != null) && (values.length > 0)) {
068                            return values[0];
069                    }
070                    else {
071                            return null;
072                    }
073            }
074    
075            public Map<String, String[]> getParameterMap() {
076                    return Collections.unmodifiableMap(_params);
077            }
078    
079            public String[] getParameterValues(String name) {
080                    return _params.get(name);
081            }
082    
083            private Map<String, String[]> _params;
084            private Key _key;
085    
086    }