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.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.HashMap;
026    import java.util.Map;
027    
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletRequestWrapper;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class EncryptedServletRequest extends HttpServletRequestWrapper {
035    
036            public EncryptedServletRequest(HttpServletRequest request, Key key) {
037                    super(request);
038    
039                    _params = new HashMap<String, String[]>();
040                    _key = key;
041    
042                    Map<String, String[]> parameters = request.getParameterMap();
043    
044                    for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
045                            String name = entry.getKey();
046                            String[] values = entry.getValue();
047    
048                            for (int i = 0; i < values.length; i++) {
049                                    if (Validator.isNotNull(values[i])) {
050                                            try {
051                                                    values[i] = Encryptor.decrypt(_key, values[i]);
052                                            }
053                                            catch (EncryptorException ee) {
054                                                    values[i] = StringPool.BLANK;
055                                            }
056                                    }
057                            }
058    
059                            _params.put(name, values);
060                    }
061            }
062    
063            @Override
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            @Override
076            public Map<String, String[]> getParameterMap() {
077                    return Collections.unmodifiableMap(_params);
078            }
079    
080            @Override
081            public String[] getParameterValues(String name) {
082                    return _params.get(name);
083            }
084    
085            private Key _key;
086            private Map<String, String[]> _params;
087    
088    }