001: /* RenderHttpServletRequest.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Jan 17 00:58:56 2006, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.web.portlet;
020:
021: import java.util.Map;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Enumeration;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpSession;
028: import javax.portlet.RenderRequest;
029: import javax.portlet.PortletSession;
030:
031: import org.zkoss.util.CollectionsX;
032: import org.zkoss.web.Attributes;
033:
034: /**
035: * A facade of RenderRequest that implements HttpServletRespose.
036: *
037: * @author tomyeh
038: */
039: public class RenderHttpServletRequest implements HttpServletRequest {
040: private final RenderRequest _req;
041: private String _enc = "UTF-8";
042: private final Map _attrs = new HashMap(9);
043:
044: public static HttpServletRequest getInstance(RenderRequest req) {
045: if (req instanceof HttpServletRequest)
046: return (HttpServletRequest) req;
047: return new RenderHttpServletRequest(req);
048: }
049:
050: private RenderHttpServletRequest(RenderRequest req) {
051: if (req == null)
052: throw new IllegalArgumentException("null");
053: _req = req;
054:
055: String ctxpath = req.getContextPath();
056: if (ctxpath == null)
057: ctxpath = "";
058: _attrs.put(Attributes.INCLUDE_CONTEXT_PATH, ctxpath);
059: _attrs.put(Attributes.INCLUDE_SERVLET_PATH, "");
060: _attrs.put(Attributes.INCLUDE_PATH_INFO, "");
061: _attrs.put(Attributes.INCLUDE_QUERY_STRING, "");
062: _attrs.put(Attributes.INCLUDE_REQUEST_URI, ctxpath);
063: }
064:
065: //-- ServletRequest --//
066: public Object getAttribute(String name) {
067: final String val = (String) _attrs.get(name);
068: return val != null ? val : _req.getAttribute(name);
069: }
070:
071: public Enumeration getAttributeNames() {
072: final Enumeration _e = _req.getAttributeNames();
073: final Iterator _it = _attrs.keySet().iterator();
074: return new Enumeration() {
075: Object _next;
076: {
077: next();
078: }
079:
080: public boolean hasMoreElements() {
081: return _next != null;
082: }
083:
084: public Object nextElement() {
085: Object next = _next;
086: next();
087: return next;
088: }
089:
090: private void next() {
091: _next = null;
092: while (_e.hasMoreElements()) {
093: Object next = _e.nextElement();
094: if (!_attrs.containsKey(next)) {
095: _next = next;
096: return; //done
097: }
098: }
099: if (_it.hasNext())
100: _next = _it.next();
101: }
102: };
103: }
104:
105: public String getCharacterEncoding() {
106: return _enc;
107: }
108:
109: public int getContentLength() {
110: return -1;
111: }
112:
113: public String getContentType() {
114: final String ct = _req.getResponseContentType();
115: return ct != null ? ct : "text/html";
116: }
117:
118: public javax.servlet.ServletInputStream getInputStream() {
119: return new javax.servlet.ServletInputStream() {
120: public int read() {
121: return -1;
122: }
123: };
124: }
125:
126: public String getLocalAddr() {
127: return "";
128: }
129:
130: public java.util.Locale getLocale() {
131: return _req.getLocale();
132: }
133:
134: public java.util.Enumeration getLocales() {
135: return _req.getLocales();
136: }
137:
138: public String getLocalName() {
139: return "";
140: }
141:
142: public int getLocalPort() {
143: return -1;
144: }
145:
146: public String getParameter(String name) {
147: return _req.getParameter(name);
148: }
149:
150: public java.util.Map getParameterMap() {
151: return _req.getParameterMap();
152: }
153:
154: public java.util.Enumeration getParameterNames() {
155: return _req.getParameterNames();
156: }
157:
158: public String[] getParameterValues(String name) {
159: return _req.getParameterValues(name);
160: }
161:
162: public String getProtocol() {
163: return "HTTP/1.0";
164: }
165:
166: public java.io.BufferedReader getReader() {
167: return new java.io.BufferedReader(new java.io.StringReader(""));
168: }
169:
170: /**
171: * @deprecated
172: */
173: public String getRealPath(String path) {
174: return null;
175: }
176:
177: public String getRemoteAddr() {
178: return "";
179: }
180:
181: public String getRemoteHost() {
182: return "";
183: }
184:
185: public int getRemotePort() {
186: return -1;
187: }
188:
189: public javax.servlet.RequestDispatcher getRequestDispatcher(
190: String path) {
191: return null; //implies we don't support relative URI
192: }
193:
194: public String getScheme() {
195: return _req.getScheme();
196: }
197:
198: public String getServerName() {
199: return _req.getServerName();
200: }
201:
202: public int getServerPort() {
203: return _req.getServerPort();
204: }
205:
206: public boolean isSecure() {
207: return _req.isSecure();
208: }
209:
210: public void removeAttribute(String name) {
211: _req.removeAttribute(name);
212: }
213:
214: public void setAttribute(String name, Object o) {
215: _req.setAttribute(name, o);
216: }
217:
218: public void setCharacterEncoding(String enc)
219: throws java.io.UnsupportedEncodingException {
220: //Ensure the specified encoding is valid
221: byte buffer[] = new byte[1];
222: buffer[0] = (byte) 'a';
223: String dummy = new String(buffer, enc);
224:
225: _enc = enc;
226: }
227:
228: //-- HttpServletRequest --//
229: public String getAuthType() {
230: return _req.getAuthType();
231: }
232:
233: public String getContextPath() {
234: return (String) _attrs.get(Attributes.INCLUDE_CONTEXT_PATH);
235: }
236:
237: public javax.servlet.http.Cookie[] getCookies() {
238: return new javax.servlet.http.Cookie[0];
239: }
240:
241: public long getDateHeader(String name) {
242: return -1; //not available
243: }
244:
245: public String getHeader(String name) {
246: return null;
247: }
248:
249: public java.util.Enumeration getHeaderNames() {
250: return CollectionsX.EMPTY_ENUMERATION;
251: }
252:
253: public java.util.Enumeration getHeaders(String name) {
254: return CollectionsX.EMPTY_ENUMERATION;
255: }
256:
257: public int getIntHeader(String name) {
258: return -1; //not available
259: }
260:
261: public String getMethod() {
262: return "GET";
263: }
264:
265: public String getPathInfo() {
266: return (String) _attrs.get(Attributes.INCLUDE_PATH_INFO);
267: }
268:
269: public String getPathTranslated() {
270: return null;
271: }
272:
273: public String getQueryString() {
274: return (String) _attrs.get(Attributes.INCLUDE_QUERY_STRING);
275: }
276:
277: public String getRemoteUser() {
278: return _req.getRemoteUser();
279: }
280:
281: public String getRequestedSessionId() {
282: return _req.getRequestedSessionId();
283: }
284:
285: public String getRequestURI() {
286: return (String) _attrs.get(Attributes.INCLUDE_REQUEST_URI);
287: }
288:
289: public StringBuffer getRequestURL() {
290: return new StringBuffer();
291: }
292:
293: public String getServletPath() {
294: return (String) _attrs.get(Attributes.INCLUDE_SERVLET_PATH);
295: }
296:
297: public HttpSession getSession() {
298: return PortletHttpSession.getInstance(_req.getPortletSession());
299: }
300:
301: public HttpSession getSession(boolean create) {
302: final PortletSession sess = _req.getPortletSession(create);
303: return sess != null ? PortletHttpSession.getInstance(sess)
304: : null;
305: }
306:
307: public java.security.Principal getUserPrincipal() {
308: return _req.getUserPrincipal();
309: }
310:
311: public boolean isRequestedSessionIdFromCookie() {
312: return false;
313: }
314:
315: /**
316: * @deprecated
317: */
318: public boolean isRequestedSessionIdFromUrl() {
319: return isRequestedSessionIdFromURL();
320: }
321:
322: public boolean isRequestedSessionIdFromURL() {
323: return false;
324: }
325:
326: public boolean isRequestedSessionIdValid() {
327: return _req.isRequestedSessionIdValid();
328: }
329:
330: public boolean isUserInRole(String role) {
331: return _req.isUserInRole(role);
332: }
333: }
|