1 29 30 package nextapp.echo2.webrender; 31 32 import java.io.IOException ; 33 import java.io.OutputStream ; 34 import java.io.PrintWriter ; 35 import java.util.HashMap ; 36 import java.util.Map ; 37 38 import javax.servlet.ServletException ; 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 import javax.servlet.http.HttpSession ; 42 43 50 public class Connection { 51 52 55 private static final String USER_INSTANCE_SESSION_KEY_PREFIX = "Echo2UserInstance"; 56 57 private HttpServletRequest request; 58 private HttpServletResponse response; 59 private WebRenderServlet servlet; 60 private UserInstance userInstance; 61 private Map propertyMap; 62 63 72 Connection(WebRenderServlet servlet, HttpServletRequest request, HttpServletResponse response) throws IOException , 73 ServletException { 74 super(); 75 76 this.servlet = servlet; 77 this.request = request; 78 this.response = response; 79 80 String contentType = request.getContentType(); 82 if (contentType != null && contentType.startsWith(ContentType.MULTIPART_FORM_DATA.getMimeType())) { 83 if (WebRenderServlet.getMultipartRequestWrapper() == null) { 84 throw new WebRenderServletException("MultipartRequestWrapper was never set and client made an HTTP request " 85 + "encoded as multipart/form-data."); 86 } 87 this.request = WebRenderServlet.getMultipartRequestWrapper().getWrappedRequest(request); 88 } 89 90 HttpSession session = request.getSession(false); 91 if (session != null) { 92 userInstance = (UserInstance) session.getAttribute(getSessionKey()); 93 } 94 } 95 96 100 void disposeUserInstance() { 101 HttpSession session = request.getSession(false); 102 if (session != null) { 103 getUserInstance().setServletUri(null); 104 session.removeAttribute(getSessionKey()); 105 } 106 } 107 108 118 public OutputStream getOutputStream() { 119 try { 120 return response.getOutputStream(); 121 } catch (IOException ex) { 122 throw new WebRenderServletException("Unable to get PrintWriter.", ex); 123 } 124 } 125 126 136 public Object getProperty(String key) { 137 return propertyMap == null ? null : propertyMap.get(key); 138 } 139 140 147 public HttpServletRequest getRequest() { 148 return request; 149 } 150 151 158 public HttpServletResponse getResponse() { 159 return response; 160 } 161 162 168 private String getSessionKey() { 169 return USER_INSTANCE_SESSION_KEY_PREFIX + ":" + servlet.getServletName(); 170 } 171 172 179 public WebRenderServlet getServlet() { 180 return servlet; 181 } 182 183 191 public UserInstance getUserInstance() { 192 return userInstance; 193 } 194 195 205 public PrintWriter getWriter() { 206 try { 207 return response.getWriter(); 208 } catch (IOException ex) { 209 throw new WebRenderServletException("Unable to get PrintWriter.", ex); 210 } 211 } 212 213 220 void initUserInstance(UserInstance userInstance) { 221 this.userInstance = userInstance; 222 userInstance.setServletUri(request.getRequestURI()); 223 HttpSession session = request.getSession(true); 224 session.setAttribute(getSessionKey(), userInstance); 225 } 226 227 234 public void setContentType(ContentType contentType) { 235 UserInstance userInstance = getUserInstance(); 236 if (contentType.isBinary() || userInstance == null) { 237 response.setContentType(contentType.getMimeType()); 238 } else { 239 response.setContentType(contentType.getMimeType() + "; charset=" + userInstance.getCharacterEncoding()); 240 } 241 } 242 243 253 public void setProperty(String key, Object value) { 254 if (propertyMap == null) { 255 propertyMap = new HashMap (); 256 } 257 propertyMap.put(key, value); 258 } 259 } | Popular Tags |