1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util.servlet;
21  
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.util.Encryptor;
25  import com.liferay.util.EncryptorException;
26  
27  import java.security.Key;
28  
29  import java.util.Collections;
30  import java.util.Enumeration;
31  import java.util.HashMap;
32  import java.util.Map;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletRequestWrapper;
36  
37  /**
38   * <a href="EncryptedServletRequest.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class EncryptedServletRequest extends HttpServletRequestWrapper {
44  
45      public EncryptedServletRequest(HttpServletRequest request, Key key) {
46          super(request);
47  
48          _params = new HashMap<String, String[]>();
49          _key = key;
50  
51          Enumeration<String> enu = getParameterNames();
52  
53          while (enu.hasMoreElements()) {
54              String name = enu.nextElement();
55              String[] values = super.getParameterValues(name);
56  
57              for (int i = 0; i < values.length; i++) {
58                  if (Validator.isNotNull(values[i])) {
59                      try {
60                          values[i] = Encryptor.decrypt(_key, values[i]);
61                      }
62                      catch (EncryptorException ee) {
63                          values[i] = StringPool.BLANK;
64                      }
65                  }
66              }
67  
68              _params.put(name, values);
69          }
70      }
71  
72      public String getParameter(String name) {
73          String[] values = _params.get(name);
74  
75          if ((values != null) && (values.length > 0)) {
76              return values[0];
77          }
78          else {
79              return null;
80          }
81      }
82  
83      public Map<String, String[]> getParameterMap() {
84          return Collections.unmodifiableMap(_params);
85      }
86  
87      public String[] getParameterValues(String name) {
88          return _params.get(name);
89      }
90  
91      private Map<String, String[]> _params;
92      private Key _key;
93  
94  }