1 16 package org.directwebremoting.impl; 17 18 import java.io.IOException ; 19 import java.io.StringWriter ; 20 21 import javax.servlet.ServletConfig ; 22 import javax.servlet.ServletContext ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 28 import org.directwebremoting.Container; 29 import org.directwebremoting.ScriptSession; 30 import org.directwebremoting.WebContext; 31 import org.directwebremoting.extend.RealScriptSession; 32 import org.directwebremoting.extend.ScriptSessionManager; 33 import org.directwebremoting.util.SwallowingHttpServletResponse; 34 import org.directwebremoting.util.VersionUtil; 35 36 40 public class DefaultWebContext extends DefaultServerContext implements WebContext 41 { 42 51 public DefaultWebContext(HttpServletRequest request, HttpServletResponse response, ServletConfig config, ServletContext context, Container container) 52 { 53 super(config, context, container); 54 55 this.request = request; 56 this.response = response; 57 } 58 59 62 public void setCurrentPageInformation(String page, String scriptSessionId) 63 { 64 this.scriptSessionId = scriptSessionId; 65 this.page = page; 66 } 67 68 71 public String getCurrentPage() 72 { 73 return page; 74 } 75 76 79 public ScriptSession getScriptSession() 80 { 81 ScriptSessionManager manager = getScriptSessionManager(); 82 83 RealScriptSession scriptSession = manager.getScriptSession(scriptSessionId); 84 manager.setPageForScriptSession(scriptSession, page); 85 86 return scriptSession; 87 } 88 89 92 public HttpSession getSession() 93 { 94 return request.getSession(); 95 } 96 97 100 public HttpSession getSession(boolean create) 101 { 102 return request.getSession(create); 103 } 104 105 108 public HttpServletRequest getHttpServletRequest() 109 { 110 return request; 111 } 112 113 116 public HttpServletResponse getHttpServletResponse() 117 { 118 return response; 119 } 120 121 124 public String forwardToString(String url) throws ServletException , IOException 125 { 126 StringWriter sout = new StringWriter (); 127 StringBuffer buffer = sout.getBuffer(); 128 129 HttpServletResponse realResponse = getHttpServletResponse(); 130 HttpServletResponse fakeResponse = new SwallowingHttpServletResponse(realResponse, sout, realResponse.getCharacterEncoding()); 131 132 HttpServletRequest realRequest = getHttpServletRequest(); 133 realRequest.setAttribute(WebContext.ATTRIBUTE_DWR, Boolean.TRUE); 134 135 getServletContext().getRequestDispatcher(url).forward(realRequest, fakeResponse); 136 137 return buffer.toString(); 138 } 139 140 143 public String getVersion() 144 { 145 return VersionUtil.getVersion(); 146 } 147 148 151 private String scriptSessionId = null; 152 153 156 private String page = null; 157 158 161 private HttpServletRequest request = null; 162 163 166 private HttpServletResponse response = null; 167 } 168 | Popular Tags |