Source code: com/aendvari/satyr/servlet/ServletModelUtil.java
1 /*
2 * ServletModelUtil.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.satyr.servlet;
11
12 import javax.servlet.*;
13 import javax.servlet.http.*;
14 import javax.servlet.jsp.*;
15 import javax.servlet.jsp.tagext.*;
16
17 import com.aendvari.common.model.*;
18
19 import com.aendvari.common.util.ServletUtil;
20
21 /**
22 * <p>Utility class for accessing {@link ModelTree} data within a servlet.</p>
23 *
24 * @author Scott Milne
25 *
26 */
27
28 public class ServletModelUtil
29 {
30 /* Constants */
31
32
33 /** Key for model tree instance. */
34 public final static String ModelTreeKey = "satyr/model";
35
36
37 /* Exceptions */
38
39
40 /**
41 * Thrown when the model tree could not be retrieved.
42 *
43 * This does not occur under normal circumstances, and does not
44 * have to be explicitly caught.
45 *
46 */
47
48 public static class NoModelException extends RuntimeException
49 {
50 public NoModelException(String message)
51 {
52 super(message);
53 }
54 }
55
56
57 /* Utilities */
58
59
60 /**
61 * Returns the model tree that is stored in the request or session.
62 *
63 * @param request The servlet request we are processing.
64 *
65 * @return A {@link ModelTree} instance, null it not available.
66 *
67 */
68
69 public static ModelTree getModelTree(HttpServletRequest request)
70 {
71 // get the model tree
72 ModelTree modelTree = (ModelTree)ServletUtil.getAttribute( request, ServletModelUtil.ModelTreeKey );
73
74 // check for model
75 if (modelTree == null)
76 {
77 throw new NoModelException("ServletModelUtil.getModelTree: No model instance could be found.");
78 }
79
80 return modelTree;
81 }
82
83 /**
84 * Returns the model tree that is stored in the session.
85 *
86 * @param session The servlet session being used.
87 *
88 * @return A {@link ModelTree} instance, null if not available.
89 *
90 */
91
92 public static ModelTree getModelTree(HttpSession session)
93 {
94 // get the model tree
95 ModelTree modelTree = (ModelTree)session.getAttribute(ServletModelUtil.ModelTreeKey);
96
97 // check for model
98 if (modelTree == null)
99 {
100 throw new NoModelException("ServletModelUtil.getModelTree: No model instance could be found.");
101 }
102
103 return modelTree;
104 }
105
106 /**
107 * Returns the model tree that is stored in the servlet context.
108 *
109 * @param request The servlet context we are processing with.
110 *
111 * @return A {@link ModelTree} instance, null it not available.
112 *
113 */
114
115 public static ModelTree getModelTree(ServletContext context)
116 {
117 // get the model tree
118 ModelTree modelTree = (ModelTree)context.getAttribute(ServletModelUtil.ModelTreeKey);
119
120 // check for model
121 if (modelTree == null)
122 {
123 throw new NoModelException("ServletModelUtil.getModelTree: No model instance could be found.");
124 }
125
126 return modelTree;
127 }
128
129 /**
130 * <p>
131 * Uses <code>scope</code> parameter for where to find the model tree instance.
132 * If not found, an attempt to find the scope manually will occur.
133 * A JSP exception will be thrown if nothing is found
134 * </p>
135 *
136 * @param pageContext The <code>pageContext</code> of a JSP.
137 * @param scope The scope to search in.
138 *
139 * @return A {@link ModelTree} instance.
140 *
141 */
142
143 public static ModelTree getModelTree(PageContext pageContext, String scope)
144 {
145 // retrieve object from scope
146 ModelTree modelTree = (ModelTree)ServletUtil.getAttribute(
147 pageContext, scope, ServletModelUtil.ModelTreeKey);
148
149 // check for model
150 if (modelTree == null)
151 {
152 throw new NoModelException("ServletModelUtil.getModelTree: No model instance could be found.");
153 }
154
155 return modelTree;
156 }
157
158 /**
159 * Sets the model tree in the request.
160 *
161 * @param request The servlet request to store the message context map into.
162 * @param modelTree The {@link ModelTree} to store.
163 *
164 */
165
166 public static void setModelTree(HttpServletRequest request, ModelTree modelTree)
167 {
168 request.setAttribute(ServletModelUtil.ModelTreeKey, modelTree);
169 }
170
171 /**
172 * Sets the model tree in the session.
173 *
174 * @param session The servlet session to store the message context map into.
175 * @param modelTree The {@link ModelTree} to store.
176 *
177 */
178
179 public static void setModelTree(HttpSession session, ModelTree modelTree)
180 {
181 session.setAttribute(ServletModelUtil.ModelTreeKey, modelTree);
182 }
183
184 /**
185 * Sets the model tree in the servlet context.
186 *
187 * @param context The servlet context to store the message context map into.
188 * @param modelTree The {@link ModelTree} to store.
189 *
190 */
191
192 public static void setModelTree(ServletContext context, ModelTree modelTree)
193 {
194 context.setAttribute(ServletModelUtil.ModelTreeKey, modelTree);
195 }
196
197 /**
198 * <p>
199 * Places the supplied object in the <code>scope</code> specified.
200 * </p>
201 *
202 * @param pageContext The <code>pageContext</code> of the current JSP.
203 * @param scope The scope to search.
204 * @param modelTree The {@link ModelTree} to store.
205 *
206 */
207
208 public static void setModelTree(PageContext pageContext, String scope, ModelTree modelTree)
209 {
210 ServletUtil.setAttribute(pageContext, scope, ServletModelUtil.ModelTreeKey, modelTree);
211 }
212 }
213