001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013: package com.sun.portal.portlet.impl;
014:
015: import java.io.BufferedReader;
016: import java.io.IOException;
017: import java.io.UnsupportedEncodingException;
018: import java.net.URLDecoder;
019: import java.security.Principal;
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Enumeration;
023: import java.util.HashMap;
024: import java.util.Locale;
025: import java.util.Map;
026: import java.util.StringTokenizer;
027:
028: import javax.portlet.RenderRequest;
029: import javax.servlet.ServletInputStream;
030: import javax.servlet.http.Cookie;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletRequestWrapper;
033:
034: /**
035: * RDRequestWrapper is a wrapped HttpServerRequest object that needs to be
036: * passed into the servlet request dispatcher.
037: */
038:
039: public class RDRequestWrapper extends HttpServletRequestWrapper {
040:
041: // constants
042: private static final String REQUEST_URI = "javax.servlet.include.request_uri";
043:
044: private static final String SERVLET_PATH = "javax.servlet.include.servlet_path";
045:
046: private static final String PATH_INFO = "javax.servlet.include.path_info";
047:
048: private static final String QUERY_STRING = "javax.servlet.include.query_string";
049:
050: private static final String ENC = "UTF-8";
051:
052: // private members
053: private RenderRequest _rRequest;
054:
055: private String _path;
056:
057: private boolean isParamMapInitialized;
058:
059: private Map paramMap;
060:
061: // constructor
062: public RDRequestWrapper(RenderRequest rReq, String path) {
063: super (
064: (HttpServletRequest) rReq
065: .getAttribute(PortletRequestConstants.HTTP_SERVLET_REQUEST));
066: _rRequest = rReq;
067: _path = path;
068: }
069:
070: /***************************************************************************
071: * The following methods must return null based on the portlet
072: * specification.
073: **************************************************************************/
074:
075: public String getProtocol() {
076:
077: return null;
078: }
079:
080: public String getRemoteAddr() {
081:
082: return null;
083: }
084:
085: public String getRemoteHost() {
086:
087: return null;
088: }
089:
090: public String getRealPath(String path) {
091:
092: return null;
093: }
094:
095: public StringBuffer getRequestURL() {
096:
097: return null;
098: }
099:
100: /***************************************************************************
101: * The following methods of the HttpServletRequest must return the path and
102: * query string information used to obtain the PortletRequestDispatcher
103: * object.
104: **************************************************************************/
105:
106: // TBD: a bug in S1AS???
107: public String getPathInfo() {
108:
109: return (String) getAttribute(PATH_INFO);
110: }
111:
112: // TBD: hot to get real path???
113: public String getPathTranslated() {
114:
115: return null;
116: }
117:
118: public String getQueryString() {
119:
120: return (String) getAttribute(QUERY_STRING);
121: }
122:
123: public String getRequestURI() {
124:
125: return (String) getAttribute(REQUEST_URI);
126: }
127:
128: public String getServletPath() {
129:
130: return (String) getAttribute(SERVLET_PATH);
131: }
132:
133: /***************************************************************************
134: * The following methods of the HttpServletRequest must be equivalent to the
135: * methods of the PortletRequest of similar names.
136: **************************************************************************/
137:
138: public String getScheme() {
139:
140: return _rRequest.getScheme();
141: }
142:
143: public String getServerName() {
144:
145: return _rRequest.getServerName();
146: }
147:
148: public int getServerPort() {
149: return _rRequest.getServerPort();
150: }
151:
152: public Object getAttribute(String name) {
153: Object retValue = null;
154: if (name.startsWith("javax")
155: && !name.equals("javax.portlet.request")
156: && !name.equals("javax.portlet.response")) {
157: retValue = super .getAttribute(name);
158: if (retValue == null) {
159: retValue = _rRequest.getAttribute(name);
160: }
161: } else {
162: retValue = _rRequest.getAttribute(name);
163: if (retValue == null) {
164: retValue = super .getAttribute(name);
165: }
166: }
167: return retValue;
168: }
169:
170: public Enumeration getAttributeNames() {
171: return _rRequest.getAttributeNames();
172: }
173:
174: public void setAttribute(String name, Object o) {
175: _rRequest.setAttribute(name, o);
176: super .setAttribute(name, o);
177: }
178:
179: public void removeAttribute(String name) {
180: _rRequest.removeAttribute(name);
181: super .removeAttribute(name);
182: }
183:
184: public Locale getLocale() {
185:
186: return _rRequest.getLocale();
187: }
188:
189: public Enumeration getLocales() {
190:
191: return _rRequest.getLocales();
192: }
193:
194: public boolean isSecure() {
195:
196: return _rRequest.isSecure();
197: }
198:
199: public String getAuthType() {
200:
201: return _rRequest.getAuthType();
202: }
203:
204: public String getContextPath() {
205:
206: return _rRequest.getContextPath();
207: }
208:
209: public String getRemoteUser() {
210:
211: return _rRequest.getRemoteUser();
212: }
213:
214: public Principal getUserPrincipal() {
215:
216: return _rRequest.getUserPrincipal();
217: }
218:
219: public String getRequestedSessionId() {
220:
221: return _rRequest.getRequestedSessionId();
222: }
223:
224: public boolean isRequestedSessionIdValid() {
225:
226: return _rRequest.isRequestedSessionIdValid();
227: }
228:
229: /**
230: * This private initializeParamMap() method initializes a local HashMap
231: * which merges the original RenderRequest's parameter map and the
232: * parameters specified in the query string used to create the
233: * PortletRerquestDispatcher (see PLT.16.1.1 and PLT.16.3.3. Some highlights
234: * here:
235: *
236: * 1. The parameters specified in the query string take precedence over
237: * other portlet render parameters of the same name passed to the included
238: * servlet/JSP.
239: *
240: * 2. The values in this local HashMap are from type String array
241: * (String[]). If it is a single-valued parameter, a StringArray of length 1
242: * is created to store the value. This is compliant with both the
243: * implementations of RenderRequestImpl and ContainerRequest classes.
244: */
245: private void initializeParamMap() {
246:
247: if (!isParamMapInitialized) {
248: paramMap = new HashMap(_rRequest.getParameterMap());
249: String queryString = getQueryString();
250:
251: if (queryString != null) {
252: StringTokenizer st = new StringTokenizer(queryString,
253: "&", false);
254: while (st.hasMoreTokens()) {
255: String next = st.nextToken();
256: String queryKey = next.substring(0, next
257: .indexOf("="));
258: String queryValue = next.substring(next
259: .indexOf("=") + 1, next.length());
260: // The key & value in query string might be encoded. So decode them.
261: String key;
262: try {
263: key = URLDecoder.decode(queryKey, ENC);
264: } catch (UnsupportedEncodingException ex) {
265: key = queryKey;
266: }
267: String value;
268: try {
269: value = URLDecoder.decode(queryValue, ENC);
270: } catch (UnsupportedEncodingException ex) {
271: value = queryValue;
272: }
273: String[] newValues = null;
274:
275: if (paramMap.containsKey(key)) {
276: String[] origValues = (String[]) paramMap
277: .get(key);
278: newValues = new String[origValues.length + 1];
279: newValues[0] = value;
280: for (int i = 1; i <= origValues.length; i++) {
281: newValues[i] = origValues[i - 1];
282: }
283: } else {
284: newValues = new String[1];
285: newValues[0] = value;
286: }
287: paramMap.put(key, newValues);
288: }
289: }
290: isParamMapInitialized = true;
291: }
292: }
293:
294: /***************************************************************************
295: * The following methods of the HttpServletRequest must be equivalent to the
296: * methods of the PortletRequest of similar names with the provision defined
297: * in Section PLT.16.1.1 "Query Strings in Request Dispatcher Paths".
298: **************************************************************************/
299: public String getParameter(String name) {
300:
301: if (!isParamMapInitialized) {
302: initializeParamMap();
303: }
304:
305: String retVal = null;
306: if (paramMap != null) {
307: String[] value = (String[]) paramMap.get(name);
308: if (value != null) {
309: retVal = ((String[]) value)[0];
310: }
311: }
312: return retVal;
313: }
314:
315: public Enumeration getParameterNames() {
316:
317: if (!isParamMapInitialized) {
318: initializeParamMap();
319: }
320: return Collections.enumeration(paramMap.keySet());
321: }
322:
323: public String[] getParameterValues(String name) {
324:
325: if (!isParamMapInitialized) {
326: initializeParamMap();
327: }
328: String[] retVal = null;
329:
330: if (paramMap != null) {
331: retVal = (String[]) paramMap.get(name);
332: }
333: return retVal;
334: }
335:
336: public Map getParameterMap() {
337:
338: if (!isParamMapInitialized) {
339: initializeParamMap();
340: }
341: return paramMap;
342: }
343:
344: /***************************************************************************
345: * The following methods of the HttpServletRequest must do no operations and
346: * return null. The getContentLength must return 0.
347: **************************************************************************/
348:
349: public String getCharacterEncoding() {
350:
351: return null;
352: }
353:
354: public void setCharacterEncoding(String env)
355: throws UnsupportedEncodingException {
356:
357: // no-op
358: }
359:
360: public int getContentLength() {
361:
362: return 0;
363: }
364:
365: public String getContentType() {
366:
367: return null;
368: }
369:
370: public ServletInputStream getInputStream() throws IOException {
371:
372: return null;
373: }
374:
375: public BufferedReader getReader() throws IOException {
376:
377: return null;
378: }
379:
380: /***************************************************************************
381: * The following methods of the HttpServletRequest must be based on the
382: * properties provided by the getProperties method of the PortletRequest
383: * interface.
384: **************************************************************************/
385:
386: public String getHeader(String name) {
387:
388: return super .getHeader(name);
389: }
390:
391: public Enumeration getHeaders(String name) {
392:
393: return super .getHeaders(name);
394: }
395:
396: public Enumeration getHeaderNames() {
397:
398: return super .getHeaderNames();
399: }
400:
401: public Cookie[] getCookies() {
402:
403: return super .getCookies();
404: }
405:
406: public long getDateHeader(String name) {
407:
408: return super .getDateHeader(name);
409: }
410:
411: public int getIntHeader(String name) {
412:
413: return super .getIntHeader(name);
414: }
415:
416: /***************************************************************************
417: * The following methods must be resolved according to the Servlet
418: * Specifications 2.3.
419: **************************************************************************/
420:
421: // public RequestDispatcher getRequestDispatcher(java.lang.String path)
422: // public boolean isUserInRole(java.lang.String role)
423: // public HttpSession getSession(boolean create)
424: // public boolean isRequestedSessionIdFromCookie()
425: // public boolean isRequestedSessionIdFromURL()
426: // public boolean isRequestedSessionIdFromUrl()
427: /***************************************************************************
428: * The getMethod method must always return 'GET'.
429: **************************************************************************/
430:
431: public String getMethod() {
432:
433: return "GET";
434: }
435: }
|