1 4 package org.wings.jsp; 5 6 import org.wings.session.Session; 7 import org.wings.session.SessionManager; 8 import org.wings.externalizer.ExternalizedResource; 9 import org.wings.event.SRequestEvent; 10 import org.wings.event.SRequestListener; 11 import org.wings.*; 12 import org.wings.plaf.css.Utils; 13 import org.wings.io.StringBufferDevice; 14 15 import javax.servlet.http.HttpServletResponse ; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.ServletConfig ; 18 import javax.servlet.ServletException ; 19 import javax.servlet.jsp.JspWriter ; 20 import java.util.Map ; 21 import java.util.HashMap ; 22 import java.util.Enumeration ; 23 import java.util.Iterator ; 24 import java.io.IOException ; 25 26 30 public class WingsSession 31 extends Session 32 { 33 public static WingsSession getSession(ServletConfig servletConfig, HttpServletRequest request, HttpServletResponse response) throws ServletException { 34 synchronized (request.getSession()) { 35 String key = "Session:" + request.getSession().getServletContext().getServletContextName(); 36 WingsSession wingsSession = (WingsSession)request.getSession().getAttribute(key); 37 if (wingsSession == null) { 38 wingsSession = new WingsSession(); 39 request.getSession().setAttribute(key, wingsSession); 40 SessionManager.setSession(wingsSession); 41 42 wingsSession.init(servletConfig, request); 43 RequestURL requestURL = new RequestURL("", response.encodeURL("foo").substring(3)); 44 wingsSession.setProperty("request.url", requestURL); 45 } 46 47 SessionManager.setSession(wingsSession); 48 wingsSession.setServletRequest(request); 49 wingsSession.setServletResponse(response); 50 51 return wingsSession; 52 } 53 } 54 55 protected static WingsSession getSession(HttpServletRequest request, HttpServletResponse response) throws ServletException { 56 return getSession(null, request, response); 60 } 61 62 public static void removeSession() { 63 SessionManager.removeSession(); 64 } 65 66 public static SFrame getFrame(HttpServletRequest request, HttpServletResponse response) throws ServletException { 67 String path = request.getServletPath(); 68 int pos = path.lastIndexOf('/'); 69 path = path.substring(pos + 1); 70 71 synchronized (request.getSession()) { 72 WingsSession wingsSession = getSession(request, response); 73 Map frames = getFrames(wingsSession); 74 SFrame frame = (SFrame)frames.get(path); 75 if (frame == null) { 76 frame = new SFrame(path); 77 frame.setTargetResource(path); 78 frame.show(); 79 frames.put(path, frame); 80 } 81 SessionManager.removeSession(); 82 return frame; 83 } 84 } 85 86 private static Map getFrames(WingsSession wingsSession) { 87 Map frames = (Map ) wingsSession.getProperty("frames"); 88 if (frames == null) { 89 frames = new HashMap (); 90 wingsSession.setProperty("frames", frames); 91 } 92 return frames; 93 } 94 95 public static void dispatchEvents(HttpServletRequest request, HttpServletResponse response) throws ServletException { 96 synchronized (request.getSession()) { 97 WingsSession wingsSession = getSession(request, response); 98 SForm.clearArmedComponents(); 99 100 Enumeration en = request.getParameterNames(); 101 if (en.hasMoreElements()) { 102 wingsSession.fireRequestEvent(SRequestEvent.DISPATCH_START); 103 104 while (en.hasMoreElements()) { 105 String paramName = (String ) en.nextElement(); 106 String [] value = request.getParameterValues(paramName); 107 wingsSession.getDispatcher().dispatch(paramName, value); 108 } 109 SForm.fireEvents(); 110 111 wingsSession.fireRequestEvent(SRequestEvent.DISPATCH_DONE); 112 } 113 114 wingsSession.getReloadManager().invalidateResources(); 115 wingsSession.getReloadManager().clear(); 116 SessionManager.removeSession(); 117 } 118 } 119 120 public void addComponent(String name, SComponent component) throws ServletException { 121 SFrame frame = getFrame(getServletRequest(), getServletResponse()); 122 frame.getContentPane().add(component, name); 123 setProperty(name, component); 124 } 125 126 public SComponent getComponent(String name) { 127 return (SComponent)getProperty(name); 128 } 129 130 public SComponent removeComponent(String name) throws ServletException { 131 SFrame frame = getFrame(getServletRequest(), getServletResponse()); 132 SComponent component = (SComponent) getProperty(name); 133 frame.getContentPane().remove(component); 134 return component; 135 } 136 137 public static void writeHeaders(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws IOException , ServletException { 138 synchronized (request.getSession()) { 139 SFrame frame = getFrame(request, response); 140 WingsSession wingsSession = getSession(request, response); 141 142 StringBufferDevice headerdev = new StringBufferDevice(); 143 for (Iterator iterator = frame.headers().iterator(); iterator.hasNext();) { 144 Object next = iterator.next(); 145 if (next instanceof Renderable) { 146 ((Renderable) next).write(headerdev); 147 } else { 148 Utils.write(headerdev, next.toString()); 149 } 150 headerdev.write("\n".getBytes()); 151 } 152 out.print(headerdev); 153 154 SessionManager.removeSession(); 155 } 156 } 157 158 public static void writeComponent(HttpServletRequest request, HttpServletResponse response, JspWriter out, SComponent component) throws IOException , ServletException { 159 synchronized (request.getSession()) { 160 WingsSession wingsSession = getSession(request, response); 161 StringBufferDevice outdev = new StringBufferDevice(); 162 component.write(outdev); 163 out.print(outdev); 164 SessionManager.removeSession(); 165 } 166 } 167 168 public void setServletRequest(HttpServletRequest servletRequest) { 169 this.servletRequest = servletRequest; 170 } 171 172 public void setServletResponse(HttpServletResponse servletResponse) { 173 this.servletResponse = servletResponse; 174 } 175 176 public void fireRequestEvent(int type) { 177 fireRequestEvent(type, null); 178 } 179 180 public void fireRequestEvent(int type, ExternalizedResource resource) { 181 SRequestEvent event = null; 182 183 Object [] listeners = listenerList.getListenerList(); 184 for (int i = listeners.length - 2; i >= 0; i -= 2) { 185 if (listeners[i] == SRequestListener.class) { 186 if (event == null) { 188 event = new SRequestEvent(this, type, resource); 189 } 190 ((SRequestListener) listeners[i + 1]).processRequest(event); 191 } 192 } 193 } 194 } 195 | Popular Tags |