1 | /* |
2 | * Copyright 2003,2004 The Apache Software Foundation. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.apache.pluto.driver.core; |
17 | |
18 | import java.util.Collections; |
19 | import java.util.Enumeration; |
20 | import java.util.HashMap; |
21 | import java.util.Iterator; |
22 | import java.util.Map; |
23 | |
24 | import javax.servlet.http.HttpServletRequest; |
25 | import javax.servlet.http.HttpServletRequestWrapper; |
26 | |
27 | import org.apache.pluto.PortletWindow; |
28 | import org.apache.pluto.driver.url.PortalURL; |
29 | import org.apache.pluto.driver.url.PortalURLParameter; |
30 | |
31 | public class PortalServletRequest extends HttpServletRequestWrapper { |
32 | |
33 | private PortletWindow portletWindow = null; |
34 | |
35 | private PortalURL url; |
36 | private Map portletParameters; |
37 | |
38 | public PortalServletRequest(HttpServletRequest request, |
39 | PortletWindow window) { |
40 | super(request); |
41 | this.portletWindow = window; |
42 | |
43 | url = |
44 | PortalEnvironment.getPortalEnvironment(request).getRequestedPortalURL(); |
45 | } |
46 | |
47 | |
48 | // HttpServletRequestWrapper overlay |
49 | |
50 | public java.lang.String getContentType() { |
51 | String contentType = super.getContentType(); |
52 | return contentType; |
53 | } |
54 | |
55 | // ServletRequestWrapper overlay |
56 | |
57 | public String getParameter(String name) { |
58 | String[] values = (String[]) this.getParameterMap().get(name); |
59 | if (values != null) { |
60 | return values[0]; |
61 | } |
62 | return null; |
63 | } |
64 | |
65 | /** |
66 | * Retreive the Parameters. |
67 | * @return Map of parameters targeted to the window associated with this |
68 | * request. |
69 | */ |
70 | public Map getParameterMap() { |
71 | if (portletParameters == null) { |
72 | initParameterMap(); |
73 | } |
74 | return Collections.unmodifiableMap(portletParameters); |
75 | } |
76 | |
77 | /** |
78 | * Initialize parameters for this request. We must be careful to make sure |
79 | * that render parameters are only made available if they were targeted for |
80 | * this specific window. |
81 | */ |
82 | private void initParameterMap() { |
83 | portletParameters = new HashMap(); |
84 | |
85 | Iterator iterator = url.getParameters().iterator(); |
86 | while (iterator.hasNext()) { |
87 | PortalURLParameter param = (PortalURLParameter) iterator.next(); |
88 | String name = param.getName(); |
89 | String[] values = param.getValues(); |
90 | if (param.getWindowId().equals(portletWindow.getId().getStringId())) { |
91 | portletParameters.put(name, values); |
92 | } |
93 | } |
94 | |
95 | String id = portletWindow.getId().getStringId(); |
96 | if (portletWindow.getId().getStringId().equals(id)) { |
97 | Enumeration params = super.getParameterNames(); |
98 | while (params.hasMoreElements()) { |
99 | String name = params.nextElement().toString(); |
100 | String[] values = super.getParameterValues(name); |
101 | if (portletParameters.containsKey(name)) { |
102 | String[] temp = (String[]) portletParameters.get(name); |
103 | String[] all = new String[values.length + temp.length]; |
104 | System.arraycopy(values, 0, all, 0, values.length); |
105 | System.arraycopy(temp, 0, all, values.length, temp.length); |
106 | } |
107 | portletParameters.put(name, values); |
108 | } |
109 | } |
110 | } |
111 | |
112 | /** |
113 | * Get an enumeration which contains each of the names for which parameters |
114 | * exist. |
115 | * @return an enumeration of all names bound as parameters. |
116 | */ |
117 | public Enumeration getParameterNames() { |
118 | return Collections.enumeration(getParameterMap().keySet()); |
119 | } |
120 | |
121 | /** |
122 | * Get the values associated with the given parameter key. |
123 | * @param name the Parameter name used to key the parameter. |
124 | * @return a String[] of all values bound to the given name as a parameter. |
125 | */ |
126 | public String[] getParameterValues(String name) { |
127 | return (String[]) getParameterMap().get(name); |
128 | } |
129 | |
130 | } |
131 | |
132 | |