KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > servlet > ServletRequestImpl


1 /*
2  * Copyright 2004-2005 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.cocoon.portal.pluto.servlet;
17
18 import java.io.UnsupportedEncodingException JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
27
28 import org.apache.cocoon.portal.pluto.PortletURLProviderImpl;
29 import org.apache.pluto.om.window.PortletWindow;
30
31 /**
32  * Our request wrapper
33  *
34  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
35  * @version CVS $Id: ServletRequestImpl.java 328004 2005-10-24 09:13:34Z cziegeler $
36  */

37 public class ServletRequestImpl extends HttpServletRequestWrapper JavaDoc {
38
39     /** Cache the parameter map. */
40     protected Map JavaDoc portletParameterMap;
41
42     /** Request object used for {@link #portletParameterMap}. */
43     protected HttpServletRequest JavaDoc cachedRequest;
44
45     /** Original Request. */
46     final protected HttpServletRequest JavaDoc originalRequest;
47
48     final protected PortletURLProviderImpl provider;
49
50     protected PortletWindow window;
51
52     public ServletRequestImpl(HttpServletRequest JavaDoc request,
53                               PortletURLProviderImpl provider) {
54         super(request);
55         this.provider = provider;
56         this.originalRequest = request;
57     }
58
59     public ServletRequestImpl(HttpServletRequest JavaDoc request,
60                               PortletURLProviderImpl provider,
61                               PortletWindow window) {
62         super(request);
63         this.provider = provider;
64         this.window = window;
65         this.originalRequest = request;
66     }
67
68     public ServletRequestImpl getRequest(PortletWindow window) {
69         return new ServletRequestImpl((HttpServletRequest JavaDoc)this.getRequest(), provider, window);
70     }
71
72     /**
73      * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
74      */

75     public void setCharacterEncoding(String JavaDoc arg0)
76     throws UnsupportedEncodingException JavaDoc {
77         //this.request.setCharacterEncoding(arg0);
78
}
79
80     /**
81      * @see javax.servlet.ServletRequest#getContentType()
82      */

83     public String JavaDoc getContentType() {
84         String JavaDoc contentType = "text/html";
85         if (getCharacterEncoding() != null) {
86             contentType += ";" + getCharacterEncoding();
87         }
88         return contentType;
89     }
90
91     /**
92      * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
93      */

94     public String JavaDoc getParameter(String JavaDoc name) {
95         final String JavaDoc[] values = (String JavaDoc[])this.getParameterMap().get(name);
96         if ( values != null && values.length > 0 ) {
97             return values[0];
98         }
99         return null;
100
101     }
102
103     /**
104      * @see javax.servlet.ServletRequest#getParameterMap()
105      */

106     public Map JavaDoc getParameterMap() {
107         HttpServletRequest JavaDoc currentRequest = (HttpServletRequest JavaDoc)this.getRequest();
108         if ( this.portletParameterMap == null
109              || currentRequest != this.cachedRequest ) {
110             this.cachedRequest = currentRequest;
111
112             //get control params
113
this.portletParameterMap = new HashMap JavaDoc();
114
115             if (this.provider != null
116                 && this.provider.getPortletWindow().equals(this.window)) {
117                 // get render parameters
118
Iterator JavaDoc i = this.provider.getParameters().entrySet().iterator();
119                 while (i.hasNext()) {
120                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
121                     // convert simple values to string arrays
122
if (entry.getValue() instanceof String JavaDoc) {
123                         this.portletParameterMap.put(
124                             entry.getKey(),
125                             new String JavaDoc[] {(String JavaDoc) entry.getValue()});
126                     } else {
127                         this.portletParameterMap.put(
128                             entry.getKey(),
129                             entry.getValue());
130                     }
131                 }
132
133                 // get request params from the original Cocoon request
134
// but filter all cocoon portal request parameters.
135
Enumeration JavaDoc parameters = currentRequest.getParameterNames();
136                 while (parameters.hasMoreElements()) {
137                     String JavaDoc paramName = (String JavaDoc) parameters.nextElement();
138                     String JavaDoc[] paramValues = this.getRequest().getParameterValues(paramName);
139                     String JavaDoc[] values = (String JavaDoc[]) this.portletParameterMap.get(paramName);
140
141                     if ( !paramName.startsWith("cocoon-") ) {
142                         if (values != null) {
143                             String JavaDoc[] temp = new String JavaDoc[paramValues.length + values.length];
144                             System.arraycopy(paramValues, 0, temp, 0, paramValues.length);
145                             System.arraycopy(values, 0, temp, paramValues.length, values.length);
146                             paramValues = temp;
147                         }
148                         this.portletParameterMap.put(paramName, paramValues);
149                     }
150                 }
151             }
152         }
153
154         return this.portletParameterMap;
155     }
156
157     /**
158      * @see javax.servlet.ServletRequest#getParameterNames()
159      */

160     public Enumeration JavaDoc getParameterNames() {
161         return Collections.enumeration(this.getParameterMap().keySet());
162     }
163
164     /**
165      * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
166      */

167     public String JavaDoc[] getParameterValues(String JavaDoc name) {
168         return (String JavaDoc[]) this.getParameterMap().get(name);
169     }
170     /**
171      * JST-168 PLT.16.3.3 cxxix
172      * @see javax.servlet.ServletRequest#getProtocol()
173      */

174     public String JavaDoc getProtocol() {
175         return null;
176     }
177
178     /**
179      * JST-168 PLT.16.3.3 cxxix
180      * @see javax.servlet.ServletRequest#getRemoteAddr()
181      */

182     public String JavaDoc getRemoteAddr() {
183         return null;
184     }
185
186     /**
187      * JST-168 PLT.16.3.3 cxxix
188      * @see javax.servlet.ServletRequest#getRemoteHost()
189      */

190     public String JavaDoc getRemoteHost() {
191         return null;
192     }
193
194     /**
195      * JST-168 PLT.16.3.3 cxxix
196      * @see javax.servlet.http.HttpServletRequest#getRequestURL()
197      */

198     public StringBuffer JavaDoc getRequestURL() {
199         return null;
200     }
201
202     /**
203      * JST-168 PLT.16.3.3 cxxx
204      * @see javax.servlet.http.HttpServletRequest#getPathInfo()
205      */

206     public String JavaDoc getPathInfo() {
207         String JavaDoc attr = (String JavaDoc)super.getAttribute("javax.servlet.include.path_info");
208         return (attr != null) ? attr : super.getPathInfo();
209     }
210
211     /**
212      * JST-168 PLT.16.3.3 cxxx
213      * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
214      */

215     public String JavaDoc getPathTranslated() {
216         // TODO: Don't know yet how to implement this.
217
// A null value is a valid value.
218
return null;
219     }
220
221     /**
222      * JST-168 PLT.16.3.3 cxxx
223      * @see javax.servlet.http.HttpServletRequest#getQueryString()
224      */

225     public String JavaDoc getQueryString() {
226         String JavaDoc attr = (String JavaDoc)super.getAttribute("javax.servlet.include.query_string");
227         return (attr != null) ? attr : super.getQueryString();
228     }
229
230     /**
231      * JST-168 PLT.16.3.3 cxxx
232      * @see javax.servlet.http.HttpServletRequest#getRequestURI()
233      */

234     public String JavaDoc getRequestURI() {
235         String JavaDoc attr = (String JavaDoc)super.getAttribute("javax.servlet.include.request_uri");
236         return (attr != null) ? attr : super.getRequestURI();
237     }
238
239     /**
240      * JST-168 PLT.16.3.3 cxxx
241      * @see javax.servlet.http.HttpServletRequest#getServletPath()
242      */

243     public String JavaDoc getServletPath() {
244         String JavaDoc attr = (String JavaDoc)super.getAttribute("javax.servlet.include.servlet_path");
245         return (attr != null) ? attr : super.getServletPath();
246     }
247
248     /**
249      * JST-168 PLT.16.3.3 cxxxi
250      * @see javax.servlet.http.HttpServletRequest#getContextPath()
251      */

252     public String JavaDoc getContextPath() {
253         String JavaDoc attr = (String JavaDoc)super.getAttribute("javax.servlet.include.context_path");
254         return (attr != null) ? attr : super.getContextPath();
255     }
256 }
257
Popular Tags