001: /*
002: * Copyright (c) 2005 Your Corporation. All Rights Reserved.
003: */
004: package org.wings.jsp;
005:
006: import org.wings.Renderable;
007: import org.wings.RequestURL;
008: import org.wings.SComponent;
009: import org.wings.SForm;
010: import org.wings.SFrame;
011: import org.wings.event.SRequestEvent;
012: import org.wings.event.SRequestListener;
013: import org.wings.externalizer.ExternalizedResource;
014: import org.wings.io.StringBuilderDevice;
015: import org.wings.plaf.css.Utils;
016: import org.wings.resource.ResourceNotFoundException;
017: import org.wings.session.Session;
018: import org.wings.session.SessionManager;
019:
020: import javax.servlet.ServletException;
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpServletResponse;
023: import javax.servlet.jsp.JspWriter;
024: import java.io.IOException;
025: import java.util.Enumeration;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: /**
030: * @author hengels
031: */
032: public class WingsSession extends Session {
033: public static WingsSession getSession(HttpServletRequest request,
034: HttpServletResponse response) throws ServletException {
035: synchronized (request.getSession()) {
036: String key = "Session:"
037: + request.getSession().getServletContext()
038: .getServletContextName();
039: WingsSession wingsSession = (WingsSession) request
040: .getSession().getAttribute(key);
041: if (wingsSession == null) {
042: wingsSession = new WingsSession();
043: request.getSession().setAttribute(key, wingsSession);
044: SessionManager.setSession(wingsSession);
045:
046: wingsSession.init(request);
047: RequestURL requestURL = new RequestURL("", response
048: .encodeURL("foo").substring(3));
049: wingsSession.setProperty("request.url", requestURL);
050: }
051:
052: SessionManager.setSession(wingsSession);
053: wingsSession.setServletRequest(request);
054: wingsSession.setServletResponse(response);
055:
056: return wingsSession;
057: }
058: }
059:
060: public static void removeSession() {
061: SessionManager.removeSession();
062: }
063:
064: public static SFrame getFrame(WingsSession wingsSession)
065: throws ServletException {
066: String path = wingsSession.getServletRequest().getServletPath();
067: int pos = path.lastIndexOf('/');
068: path = path.substring(pos + 1);
069:
070: synchronized (wingsSession.getServletRequest().getSession()) {
071: Map frames = getFrames(wingsSession);
072: SFrame frame = (SFrame) frames.get(path);
073: if (frame == null) {
074: frame = new SFrame(path);
075: frame.setTargetResource(path);
076: frame.show();
077: frames.put(path, frame);
078: }
079: return frame;
080: }
081: }
082:
083: private static Map getFrames(WingsSession wingsSession) {
084: Map frames = (Map) wingsSession.getProperty("frames");
085: if (frames == null) {
086: frames = new HashMap();
087: wingsSession.setProperty("frames", frames);
088: }
089: return frames;
090: }
091:
092: public static void dispatchEvents(HttpServletRequest request,
093: HttpServletResponse response) throws ServletException {
094: synchronized (request.getSession()) {
095: WingsSession wingsSession = getSession(request, response);
096: try {
097: SForm.clearArmedComponents();
098:
099: Enumeration en = request.getParameterNames();
100: if (en.hasMoreElements()) {
101: wingsSession
102: .fireRequestEvent(SRequestEvent.DISPATCH_START);
103:
104: while (en.hasMoreElements()) {
105: String paramName = (String) en.nextElement();
106: String[] value = request
107: .getParameterValues(paramName);
108: wingsSession.getDispatcher().dispatch(
109: paramName, value);
110: }
111: SForm.fireEvents();
112:
113: wingsSession
114: .fireRequestEvent(SRequestEvent.DISPATCH_DONE);
115: }
116:
117: wingsSession.getReloadManager().invalidateFrames();
118: wingsSession.getReloadManager().notifyCGs();
119: } finally {
120: wingsSession.getReloadManager().clear();
121: SessionManager.removeSession();
122: SForm.clearArmedComponents();
123: }
124: }
125: }
126:
127: public void addComponent(String name, SComponent component)
128: throws ServletException {
129: SFrame frame = getFrame(this );
130: frame.getContentPane().add(component, name);
131: setProperty(name, component);
132: }
133:
134: public SComponent getComponent(String name) {
135: return (SComponent) getProperty(name);
136: }
137:
138: public SComponent removeComponent(String name)
139: throws ServletException {
140: SFrame frame = getFrame(this );
141: SComponent component = (SComponent) removeProperty(name);
142: frame.getContentPane().remove(component);
143: return component;
144: }
145:
146: public static void writeHeaders(HttpServletRequest request,
147: HttpServletResponse response, JspWriter out)
148: throws IOException, ServletException,
149: ResourceNotFoundException {
150: synchronized (request.getSession()) {
151: WingsSession wingsSession = getSession(request, response);
152: SFrame frame = getFrame(wingsSession);
153:
154: StringBuilderDevice headerdev = new StringBuilderDevice(
155: 1024);
156: for (Object next : frame.getHeaders()) {
157: if (next instanceof Renderable) {
158: ((Renderable) next).write(headerdev);
159: } else {
160: Utils.write(headerdev, next.toString());
161: }
162: headerdev.write("\n".getBytes());
163: }
164: out.print(headerdev);
165:
166: SessionManager.removeSession();
167: }
168: }
169:
170: public static void writeComponent(HttpServletRequest request,
171: HttpServletResponse response, JspWriter out,
172: SComponent component) throws IOException, ServletException {
173: synchronized (request.getSession()) {
174: StringBuilderDevice outdev = new StringBuilderDevice(1024);
175: component.write(outdev);
176: out.print(outdev);
177: SessionManager.removeSession();
178: }
179: }
180:
181: public void setServletRequest(HttpServletRequest servletRequest) {
182: this .servletRequest = servletRequest;
183: }
184:
185: public void setServletResponse(HttpServletResponse servletResponse) {
186: this .servletResponse = servletResponse;
187: }
188:
189: public void fireRequestEvent(int type) {
190: fireRequestEvent(type, null);
191: }
192:
193: public void fireRequestEvent(int type, ExternalizedResource resource) {
194: SRequestEvent event = null;
195:
196: Object[] listeners = listenerList.getListenerList();
197: for (int i = listeners.length - 2; i >= 0; i -= 2) {
198: if (listeners[i] == SRequestListener.class) {
199: // Lazily create the event:
200: if (event == null) {
201: event = new SRequestEvent(this , type, resource);
202: }
203: ((SRequestListener) listeners[i + 1])
204: .processRequest(event);
205: }
206: }
207: }
208: }
|