1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.util.servlet;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  import com.liferay.portal.kernel.util.Validator;
19  import com.liferay.util.Encryptor;
20  import com.liferay.util.EncryptorException;
21  
22  import java.security.Key;
23  
24  import java.util.Collections;
25  import java.util.Enumeration;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletRequestWrapper;
31  
32  /**
33   * <a href="EncryptedServletRequest.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class EncryptedServletRequest extends HttpServletRequestWrapper {
38  
39      public EncryptedServletRequest(HttpServletRequest request, Key key) {
40          super(request);
41  
42          _params = new HashMap<String, String[]>();
43          _key = key;
44  
45          Enumeration<String> enu = getParameterNames();
46  
47          while (enu.hasMoreElements()) {
48              String name = enu.nextElement();
49              String[] values = super.getParameterValues(name);
50  
51              for (int i = 0; i < values.length; i++) {
52                  if (Validator.isNotNull(values[i])) {
53                      try {
54                          values[i] = Encryptor.decrypt(_key, values[i]);
55                      }
56                      catch (EncryptorException ee) {
57                          values[i] = StringPool.BLANK;
58                      }
59                  }
60              }
61  
62              _params.put(name, values);
63          }
64      }
65  
66      public String getParameter(String name) {
67          String[] values = _params.get(name);
68  
69          if ((values != null) && (values.length > 0)) {
70              return values[0];
71          }
72          else {
73              return null;
74          }
75      }
76  
77      public Map<String, String[]> getParameterMap() {
78          return Collections.unmodifiableMap(_params);
79      }
80  
81      public String[] getParameterValues(String name) {
82          return _params.get(name);
83      }
84  
85      private Map<String, String[]> _params;
86      private Key _key;
87  
88  }