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: JSPContext.java,v 1.8 2002/09/05 15:41:48 ludovicc Exp $
37 */
38 package org.scopemvc.controller.servlet.jsp;
39
40
41 import java.io.IOException;
42 import java.util.HashMap;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47 import org.scopemvc.core.View;
48 import org.scopemvc.view.servlet.Page;
49 import org.scopemvc.view.servlet.ServletView;
50 import org.scopemvc.view.servlet.jsp.JSPPage;
51 import org.scopemvc.controller.servlet.ScopeServlet;
52 import org.scopemvc.controller.servlet.ServletContext;
53
54 /***
55 * <P>
56 *
57 * A {@link org.scopemvc.controller.servlet.ServletContext ServletContext} that
58 * handles showView() for JSPView implementations by redirecting to a JSP. </P>
59 *
60 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A>
61 * @created 05 September 2002
62 * @version $Revision: 1.8 $ $Date: 2002/09/05 15:41:48 $
63 * @see org.scopemvc.controller.servlet.ServletContext
64 */
65 public class JSPContext extends ServletContext {
66
67 /***
68 * Key that the JSP's bound model is stored under in the HTTPRequest for
69 * access by the JSP.
70 */
71 public final static String BOUND_MODEL = "org.scopemvc.model";
72
73 private final static Log LOG = LogFactory.getLog(JSPContext.class);
74
75
76 /***
77 * Create with an HttpServletResponse to use on a showView during
78 * initialisation, and a HttpServletRequest accessible to application code.
79 *
80 * @param inServlet TODO: Describe the Parameter
81 * @param inRequest TODO: Describe the Parameter
82 * @param inResponse TODO: Describe the Parameter
83 * @param inFormParameters TODO: Describe the Parameter
84 */
85 public JSPContext(ScopeServlet inServlet,
86 HttpServletRequest inRequest,
87 HttpServletResponse inResponse,
88 HashMap inFormParameters) {
89 super(inServlet, inRequest, inResponse, inFormParameters);
90 }
91
92
93 /***
94 * Show the visible JSPView from the passed ServletView, after putting its
95 * bound model into the request under the {@link #BOUND_MODEL} key.
96 *
97 * @param inView TODO: Describe the Parameter
98 */
99 public void showView(View inView) {
100 if (LOG.isDebugEnabled()) {
101 LOG.debug("showView: " + inView);
102 }
103 if (!(inView instanceof ServletView)) {
104 throw new IllegalArgumentException("JSPContext can only show Views that are instanceof ServletView, not: " + inView);
105 }
106 if (response == null) {
107 throw new UnsupportedOperationException("Can't show the view because don't have a HTTPServletResponse.\nHas a view already been shown during this servlet request?");
108 }
109
110 try {
111 Page visiblePage = ((ServletView) inView).getVisible();
112 if (!(visiblePage instanceof JSPPage)) {
113 throw new IllegalArgumentException("JSPContext can only show Pages that are instanceof JSPPage, not: " + visiblePage);
114 }
115
116 // Put the View's bound model into the request for the JSP to access
117 request.setAttribute(BOUND_MODEL, visiblePage.getBoundModel());
118
119 if (LOG.isDebugEnabled()) {
120 LOG.debug("showView: " + visiblePage);
121 }
122 request.getRequestDispatcher(((JSPPage) visiblePage).getPath()).forward(request, response);
123
124 } catch (Exception e) {
125 // Could be client broke the connection by pressing "Stop" or something...
126 if (LOG.isDebugEnabled()) {
127 LOG.debug("doShowView", e);
128 }
129 try {
130 handleInternalError(e);
131 } catch (IOException e1) {
132 if (LOG.isDebugEnabled()) {
133 LOG.debug("doShowView", e1);
134 }
135 }
136 } finally {
137 response = null;
138 }
139 }
140 }
This page was automatically generated by Maven