001    /**
002     * Copyright (c) 2000-2010 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.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    }