001: package com.icesoft.faces.webapp.http.portlet;
002:
003: import com.icesoft.faces.context.AbstractAttributeMap;
004: import com.icesoft.faces.context.AbstractCopyingAttributeMap;
005: import com.icesoft.faces.context.BridgeExternalContext;
006: import com.icesoft.faces.util.EnumerationIterator;
007: import com.icesoft.faces.webapp.command.CommandQueue;
008: import com.icesoft.faces.webapp.http.common.Configuration;
009: import com.icesoft.faces.webapp.http.servlet.SessionDispatcher;
010: import com.icesoft.jasper.Constants;
011:
012: import javax.faces.FacesException;
013: import javax.portlet.PortletConfig;
014: import javax.portlet.PortletContext;
015: import javax.portlet.PortletException;
016: import javax.portlet.PortletSession;
017: import javax.portlet.RenderRequest;
018: import javax.portlet.RenderResponse;
019: import javax.servlet.http.Cookie;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.Writer;
025: import java.net.MalformedURLException;
026: import java.net.URL;
027: import java.util.Collections;
028: import java.util.Enumeration;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Locale;
032: import java.util.Map;
033: import java.util.Set;
034:
035: public class PortletExternalContext extends BridgeExternalContext {
036: private PortletContext context;
037: private PortletConfig config;
038: private RenderRequest request;
039: private RenderResponse response;
040: private PortletSession session;
041:
042: public PortletExternalContext(String viewIdentifier,
043: final Object request, Object response,
044: CommandQueue commandQueue, Configuration configuration,
045: final SessionDispatcher.Listener.Monitor monitor,
046: Object config) {
047: super (viewIdentifier, commandQueue, configuration);
048: this .config = (PortletConfig) config;
049: this .request = (RenderRequest) request;
050: this .response = (RenderResponse) response;
051: this .session = new ProxyPortletSession(this .request
052: .getPortletSession()) {
053: public void invalidate() {
054: monitor.shutdown();
055: }
056: };
057: this .context = this .session.getPortletContext();
058: this .initParameterMap = new AbstractAttributeMap() {
059: protected Object getAttribute(String key) {
060: return context.getInitParameter(key);
061: }
062:
063: protected void setAttribute(String key, Object value) {
064: throw new IllegalAccessError("Read only map.");
065: }
066:
067: protected void removeAttribute(String key) {
068: throw new IllegalAccessError("Read only map.");
069: }
070:
071: protected Enumeration getAttributeNames() {
072: return context.getInitParameterNames();
073: }
074: };
075: this .applicationMap = new AbstractAttributeMap() {
076: protected Object getAttribute(String key) {
077: return context.getAttribute(key);
078: }
079:
080: protected void setAttribute(String key, Object value) {
081: context.setAttribute(key, value);
082: }
083:
084: protected void removeAttribute(String key) {
085: context.removeAttribute(key);
086: }
087:
088: protected Enumeration getAttributeNames() {
089: return context.getAttributeNames();
090: }
091: };
092: this .sessionMap = new AbstractAttributeMap() {
093: protected Object getAttribute(String key) {
094: return session.getAttribute(key);
095: }
096:
097: protected void setAttribute(String key, Object value) {
098: session.setAttribute(key, value);
099: }
100:
101: protected void removeAttribute(String key) {
102: session.removeAttribute(key);
103: }
104:
105: protected Enumeration getAttributeNames() {
106: return session.getAttributeNames();
107: }
108: };
109: this .requestMap = new RequestAttributeMap();
110: this .requestCookieMap = new HashMap();
111:
112: this .update(this .request, this .response);
113: this .insertNewViewrootToken();
114: }
115:
116: public Object getSession(boolean create) {
117: return session;
118: }
119:
120: public Object getContext() {
121: return context;
122: }
123:
124: public Object getRequest() {
125: return request;
126: }
127:
128: public Object getResponse() {
129: return response;
130: }
131:
132: //todo: try to reuse functionality from the next method
133: public void update(HttpServletRequest request,
134: HttpServletResponse response) {
135: //update parameters
136: boolean persistSeamKey = isSeamLifecycleShortcut();
137:
138: requestParameterMap = Collections
139: .synchronizedMap(new HashMap());
140: requestParameterValuesMap = Collections
141: .synchronizedMap(new HashMap());
142: insertPostbackKey();
143: Enumeration parameterNames = request.getParameterNames();
144: while (parameterNames.hasMoreElements()) {
145: String name = (String) parameterNames.nextElement();
146: Object value = request.getParameter(name);
147: requestParameterMap.put(name, value);
148: requestParameterValuesMap.put(name, request
149: .getParameterValues(name));
150: }
151:
152: applySeamLifecycleShortcut(persistSeamKey);
153:
154: requestCookieMap = Collections.synchronizedMap(new HashMap());
155: Cookie[] cookies = request.getCookies();
156: if (cookies != null) {
157: for (int i = 0; i < cookies.length; i++) {
158: Cookie cookie = cookies[i];
159: requestCookieMap.put(cookie.getName(), cookie);
160: }
161: }
162: responseCookieMap = Collections.synchronizedMap(new HashMap());
163: }
164:
165: public void update(RenderRequest request, RenderResponse response) {
166: //update parameters
167: boolean persistSeamKey = isSeamLifecycleShortcut();
168: requestParameterMap = new HashMap();
169: requestParameterValuesMap = new HashMap();
170: insertPostbackKey();
171: Enumeration parameterNames = request.getParameterNames();
172: while (parameterNames.hasMoreElements()) {
173: String name = (String) parameterNames.nextElement();
174: Object value = request.getParameter(name);
175: requestParameterMap.put(name, value);
176: requestParameterValuesMap.put(name, request
177: .getParameterValues(name));
178: }
179:
180: applySeamLifecycleShortcut(persistSeamKey);
181:
182: requestCookieMap = new HashMap();
183: responseCookieMap = new HashMap();
184:
185: this .response = response;
186: }
187:
188: public void updateOnReload(Object request, Object response) {
189: Map previousRequestMap = this .requestMap;
190: this .request = (RenderRequest) request;
191: this .requestMap = new RequestAttributeMap();
192: //propagate entries
193: this .requestMap.putAll(previousRequestMap);
194: this .update((RenderRequest) request, (RenderResponse) response);
195: }
196:
197: public Map getRequestHeaderMap() {
198: return Collections.EMPTY_MAP;
199: }
200:
201: public Map getRequestHeaderValuesMap() {
202: return Collections.EMPTY_MAP;
203: }
204:
205: public Locale getRequestLocale() {
206: return request.getLocale();
207: }
208:
209: public Iterator getRequestLocales() {
210: return new EnumerationIterator(request.getLocales());
211: }
212:
213: public String getRequestPathInfo() {
214: return (String) request.getAttribute(Constants.INC_PATH_INFO);
215: }
216:
217: public String getRequestURI() {
218: return (String) request.getAttribute(Constants.INC_REQUEST_URI);
219: }
220:
221: public String getRequestContextPath() {
222: return (String) request
223: .getAttribute(Constants.INC_CONTEXT_PATH);
224: }
225:
226: public String getRequestServletPath() {
227: return (String) request
228: .getAttribute(Constants.INC_SERVLET_PATH);
229: }
230:
231: public Set getResourcePaths(String path) {
232: return context.getResourcePaths(path);
233: }
234:
235: public URL getResource(String path) throws MalformedURLException {
236: return context.getResource(path);
237: }
238:
239: public InputStream getResourceAsStream(String path) {
240: return context.getResourceAsStream(path);
241: }
242:
243: public String encodeActionURL(String url) {
244: return encodeResourceURL(url);
245: }
246:
247: public String encodeResourceURL(String url) {
248: try {
249: return response.encodeURL(url);
250: } catch (Exception e) {
251: return url;
252: }
253: }
254:
255: public String encodeNamespace(String name) {
256: return response.getNamespace() + name;
257: }
258:
259: public void dispatch(String path) throws IOException,
260: FacesException {
261: try {
262: context.getRequestDispatcher(path).include(request,
263: response);
264: } catch (PortletException e) {
265: throw new FacesException(e);
266: }
267: }
268:
269: public void log(String message) {
270: context.log(message);
271: }
272:
273: public void log(String message, Throwable throwable) {
274: context.log(message, throwable);
275: }
276:
277: public String getAuthType() {
278: return request.getAuthType();
279: }
280:
281: public String getRemoteUser() {
282: return request.getRemoteUser();
283: }
284:
285: public java.security.Principal getUserPrincipal() {
286: return request.getUserPrincipal();
287: }
288:
289: public boolean isUserInRole(String role) {
290: return request.isUserInRole(role);
291: }
292:
293: public Writer getWriter(String encoding) throws IOException {
294: return response.getWriter();
295: }
296:
297: public PortletConfig getConfig() {
298: return config;
299: }
300:
301: public void switchToNormalMode() {
302: redirector = new PortletExternalContext.Redirector() {
303: public void redirect(String uri) {
304: //cannot redirect
305: }
306: };
307:
308: cookieTransporter = new PortletExternalContext.CookieTransporter() {
309: public void send(Cookie cookie) {
310: //cannot send cookie
311: }
312: };
313: }
314:
315: public void switchToPushMode() {
316: redirector = new CommandQueueRedirector();
317: cookieTransporter = new CommandQueueCookieTransporter();
318: resetRequestMap();
319: }
320:
321: private class RequestAttributeMap extends
322: AbstractCopyingAttributeMap {
323: public Enumeration getAttributeNames() {
324: return request.getAttributeNames();
325: }
326:
327: public Object getAttribute(String name) {
328: return request.getAttribute(name);
329: }
330:
331: public void setAttribute(String name, Object value) {
332: request.setAttribute(name, value);
333: }
334:
335: public void removeAttribute(String name) {
336: request.removeAttribute(name);
337: }
338: }
339: }
|