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