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.util.servlet;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.util.Encryptor;
021    import com.liferay.util.EncryptorException;
022    
023    import java.security.Key;
024    
025    import java.util.Collections;
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                    Map<String, String[]> parameters = request.getParameterMap();
044    
045                    for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
046                            String name = entry.getKey();
047                            String[] values = entry.getValue();
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            @Override
065            public String getParameter(String name) {
066                    String[] values = _params.get(name);
067    
068                    if (ArrayUtil.isNotEmpty(values)) {
069                            return values[0];
070                    }
071                    else {
072                            return null;
073                    }
074            }
075    
076            @Override
077            public Map<String, String[]> getParameterMap() {
078                    return Collections.unmodifiableMap(_params);
079            }
080    
081            @Override
082            public String[] getParameterValues(String name) {
083                    return _params.get(name);
084            }
085    
086            private Key _key;
087            private Map<String, String[]> _params;
088    
089    }