View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: ServletContext.java,v 1.11 2002/09/05 15:41:51 ludovicc Exp $ 37 */ 38 package org.scopemvc.controller.servlet; 39 40 41 import java.io.IOException; 42 import java.io.PrintStream; 43 import java.io.PrintWriter; 44 import java.io.StringWriter; 45 import java.util.HashMap; 46 import javax.servlet.http.HttpServletRequest; 47 import javax.servlet.http.HttpServletResponse; 48 import org.apache.commons.logging.Log; 49 import org.apache.commons.logging.LogFactory; 50 import org.scopemvc.core.View; 51 import org.scopemvc.controller.basic.ViewContext; 52 53 /*** 54 * <P> 55 * 56 * A {@link org.scopemvc.controller.basic.ViewContext ViewContext} that handles 57 * showView(), hideView() and showError() for servlet implementations. </P> <P> 58 * 59 * The showView() expects a ServletView that is asked to stream the currently 60 * visible Page to the HTTP Response's OutputStream. </P> <P> 61 * 62 * Two interesting behaviours can be customized with a ServletContext subclass: 63 * 64 * <UL> 65 * <LI> To handle errors with a global error page override {@link #showError} 66 * to implement the required behaviour. </LI> 67 * </UL> 68 * Custom ServletContexts can be used for requests by overriding {@link 69 * ScopeServlet#createServletContext} in the application's ScopeServlet subclass 70 * to return an instance of the custom ServletContext. </P> 71 * 72 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 73 * @created 05 September 2002 74 * @version $Revision: 1.11 $ $Date: 2002/09/05 15:41:51 $ 75 * @see org.scopemvc.controller.servlet.jsp.JSPContext 76 * @see org.scopemvc.controller.servlet.xml.XSLServletContext 77 */ 78 public abstract class ServletContext extends ViewContext { 79 80 private final static Log LOG = LogFactory.getLog(ServletContext.class); 81 82 /*** 83 * The HttpServletResponse to use on showView. Set on creation then nulled 84 * after showView/showError. 85 */ 86 protected HttpServletResponse response; 87 88 /*** 89 * TODO: describe of the Field 90 */ 91 protected HttpServletRequest request; 92 93 /*** 94 * TODO: describe of the Field 95 */ 96 protected HashMap formParameters; 97 98 /*** 99 * TODO: describe of the Field 100 */ 101 protected ScopeServlet servlet; 102 103 104 /*** 105 * Create with an HttpServletResponse to use on a showView during 106 * initialisation, and a HttpServletRequest accessible to application code. 107 * 108 * @param inServlet TODO: Describe the Parameter 109 * @param inRequest TODO: Describe the Parameter 110 * @param inResponse TODO: Describe the Parameter 111 * @param inFormParameters TODO: Describe the Parameter 112 */ 113 public ServletContext(ScopeServlet inServlet, 114 HttpServletRequest inRequest, 115 HttpServletResponse inResponse, 116 HashMap inFormParameters) { 117 servlet = inServlet; 118 request = inRequest; 119 setHttpResponse(inResponse); 120 formParameters = inFormParameters; 121 } 122 123 124 /*** 125 * Allow access to the response object. Should rarely be used. 126 * 127 * @return The httpResponse value 128 */ 129 public final HttpServletResponse getHttpResponse() { 130 return response; 131 } 132 133 134 /*** 135 * Allow access to the request object. Can be used to get access to cookies, 136 * session etc. 137 * 138 * @return The httpRequest value 139 */ 140 public final HttpServletRequest getHttpRequest() { 141 return request; 142 } 143 144 145 /*** 146 * Return the ScopeServlet that handled this context's request. 147 * 148 * @return The servlet value 149 */ 150 public final ScopeServlet getServlet() { 151 return servlet; 152 } 153 154 155 /*** 156 * @return the form parameters for the current request. 157 */ 158 public final HashMap getFormParameters() { 159 return formParameters; 160 } 161 162 163 /*** 164 * TODO: document the method 165 * 166 * @return TODO: Describe the Return Value 167 */ 168 public final boolean hasShownView() { 169 return (getHttpResponse() == null); 170 } 171 172 173 /*** 174 * Show the ServletView passed. 175 * 176 * @param inView TODO: Describe the Parameter 177 */ 178 public abstract void showView(View inView); 179 180 181 /*** 182 * Don't do anything in this impl. <P> 183 * 184 * ***** A nicer impl of this view manager would maintain a "stack" of shown 185 * views and implement "doHideView" to step back through the stack if 186 * available. Could be tied to some rudimentary state management in 187 * ScopeServlet. </P> 188 * 189 * @param inView TODO: Describe the Parameter 190 */ 191 public void hideView(View inView) { 192 if (LOG.isDebugEnabled()) { 193 LOG.debug("hideView: " + inView); 194 } 195 // noop 196 } 197 198 199 /*** 200 * This is a very simple default error handler. To implement your own error 201 * handler, override this method and also override {@link 202 * ScopeServlet#createServletContext} to return an instance of your custom 203 * ServletContext that will handle servlet requests. 204 * 205 * @param inErrorTitle TODO: Describe the Parameter 206 * @param inErrorMessage TODO: Describe the Parameter 207 */ 208 public void showError(String inErrorTitle, String inErrorMessage) { 209 if (LOG.isDebugEnabled()) { 210 LOG.debug("showError: " + inErrorTitle + ", " + inErrorMessage); 211 } 212 213 response.setContentType("text/html"); 214 try { 215 PrintStream ps = new PrintStream(response.getOutputStream()); 216 ps.println("<HTML><H1>Error</H1><H3>" 217 + formatMessageToHTML(inErrorTitle) + "</H3><P>" 218 + formatMessageToHTML(inErrorMessage) 219 + "</P></HTML>"); 220 ps.flush(); 221 response.getOutputStream().close(); 222 } catch (IOException e) { 223 LOG.fatal("Failed to showError", e); 224 } finally { 225 setHttpResponse(null); 226 } 227 } 228 229 230 /*** 231 * Used by default BasicController's handler for the EXIT Control. Doesn't 232 * do anything in a servlet context. 233 */ 234 public void exit() { 235 // noop for servlets 236 } 237 238 239 /*** 240 * TODO: document the method 241 */ 242 public void startProgress() { 243 // noop for servlets 244 } 245 246 247 /*** 248 * TODO: document the method 249 */ 250 public void stopProgress() { 251 // noop for servlets 252 } 253 254 255 /*** 256 * The response to use for showView. Set in ctor and then nulled after 257 * showing a view. 258 * 259 * @param inResponse The new httpResponse value 260 */ 261 protected final void setHttpResponse(HttpServletResponse inResponse) { 262 if (LOG.isDebugEnabled()) { 263 LOG.debug("setHttpResponse: " + this); 264 } 265 response = inResponse; 266 } 267 268 269 /*** 270 * Got an error while streaming the view into the response OutputStream. The 271 * stream could be corrupt by this point, but it hasn't been closed so do 272 * the best you can. 273 * 274 * @param t TODO: Describe the Parameter 275 * @throws IOException TODO: Describe the Exception 276 */ 277 protected void handleInternalError(Throwable t) throws IOException { 278 if (LOG.isDebugEnabled()) { 279 LOG.debug("handleInternalError: ", t); 280 } 281 282 try { 283 StringWriter stringWriter = new StringWriter(); 284 PrintWriter writer = new PrintWriter(stringWriter); 285 t.printStackTrace(writer); 286 String dump = stringWriter.toString(); 287 288 String formattedMessage = formatMessageToHTML(dump); 289 290 PrintStream ps = new PrintStream(response.getOutputStream()); 291 ps.println("<HTML><H1>Internal error:</H1>" + formattedMessage + "</HTML>"); 292 ps.flush(); 293 response.getOutputStream().close(); 294 } finally { 295 setHttpResponse(null); 296 } 297 } 298 299 300 /*** 301 * Replace all low ASCII chars (<32) in the message with <BR />. 302 * 303 * @param inMessage TODO: Describe the Parameter 304 * @return TODO: Describe the Return Value 305 */ 306 protected String formatMessageToHTML(String inMessage) { 307 StringBuffer formattedMessage = new StringBuffer(); 308 boolean addBreak = false; 309 for (int i = 0; i < inMessage.length(); ++i) { 310 char c = inMessage.charAt(i); 311 if (c < 32) { 312 addBreak = true; 313 } else { 314 if (addBreak) { 315 formattedMessage.append("<BR />"); 316 addBreak = false; 317 } 318 formattedMessage.append(c); 319 } 320 } 321 return formattedMessage.toString(); 322 } 323 }

This page was automatically generated by Maven