1
|
|
package com.tirsen.angkor;
|
2
|
|
|
3
|
|
import com.tirsen.angkor.event.EventQueue;
|
4
|
|
import com.tirsen.angkor.process.ExecuteContext;
|
5
|
|
import com.tirsen.angkor.process.Pipeline;
|
6
|
|
import org.apache.log4j.Category;
|
7
|
|
|
8
|
|
import javax.servlet.ServletException;
|
9
|
|
import javax.servlet.http.HttpServletRequest;
|
10
|
|
import javax.servlet.http.HttpServletResponse;
|
11
|
|
import javax.servlet.http.HttpSession;
|
12
|
|
import javax.servlet.jsp.PageContext;
|
13
|
|
import java.io.IOException;
|
14
|
|
import java.io.PrintWriter;
|
15
|
|
import java.util.Iterator;
|
16
|
|
|
17
|
|
/**
|
18
|
|
* The RenderContext encapsulates the current processing state of the request, the partitioning of responsibilities
|
19
|
|
* between Application, RenderContext and ExecuteContext is not yet fully resolved and this needs some refactoring.
|
20
|
|
*
|
21
|
|
* This is the main engine of Angkor responsible for invoking and mainting the rest of the framework,
|
22
|
|
* it's name may be a little inaccurate by now since it ended up being the main engine while
|
23
|
|
* <a href="http://www.martinfowler.com">refactoring</a>.
|
24
|
|
*
|
25
|
|
* It will become instantiated now and then to do some work. It can actually only keep cached state
|
26
|
|
* since the same instance will not be used at all times (not even within one request). To get
|
27
|
|
* around this save its state in the request (for state only maintained within one request) or
|
28
|
|
* within the session (for state with a lifetime spanning several requests).
|
29
|
|
* To avoid name-clashes (several applications may be running within the same servlet-context) it prefixes
|
30
|
|
* each name with the path to the application. TODO well, it actually doesn't do this yet, but it's a good idea!
|
31
|
|
* It relies on the ApplicationFilter to instantiate an application accessible via the session and for call backs
|
32
|
|
* for the parse phase. For this reason the ApplicationFilter need to filter all pages using angkor.
|
33
|
|
*
|
34
|
|
* TODO refactoring: This class has too many responsibities. Move many of the processing attributes that doesn't
|
35
|
|
* directly have to do with rendering and put them as attributes into ExecuteContext, implement much of the
|
36
|
|
* functionality in Valves instead.
|
37
|
|
*
|
38
|
|
* <!-- $Id: RenderContext.java,v 1.5 2002/10/09 21:37:37 tirsen Exp $ -->
|
39
|
|
*
|
40
|
|
* @author $Author: tirsen $
|
41
|
|
* @version $Revision: 1.5 $
|
42
|
|
*/
|
43
|
|
public class RenderContext
|
44
|
|
{
|
45
|
|
private static final Category logger = Debug.getCategory();
|
46
|
|
|
47
|
|
private HttpServletRequest request;
|
48
|
|
private HttpServletResponse response;
|
49
|
|
private EventQueue queue = new EventQueue();
|
50
|
|
protected int indent;
|
51
|
|
|
52
|
20
|
public RenderContext(HttpServletRequest request, HttpServletResponse response)
|
53
|
|
{
|
54
|
20
|
this.request = request;
|
55
|
20
|
this.response = response;
|
56
|
|
}
|
57
|
|
|
58
|
0
|
public RenderContext(PageContext pageContext)
|
59
|
|
{
|
60
|
0
|
this((HttpServletRequest) pageContext.getRequest(), (HttpServletResponse) pageContext.getResponse());
|
61
|
|
}
|
62
|
|
|
63
|
78
|
public EventQueue getEventQueue()
|
64
|
|
{
|
65
|
78
|
return queue;
|
66
|
|
}
|
67
|
|
|
68
|
6
|
public Application getApplication()
|
69
|
|
{
|
70
|
6
|
Application application = null;
|
71
|
6
|
try
|
72
|
|
{
|
73
|
6
|
application = (Application) getSessionAttribute(getApplicationSessionAttributeName());
|
74
|
|
}
|
75
|
|
catch (Exception e)
|
76
|
|
{
|
77
|
0
|
throw new RuntimeException("Failed to access application:" + e.getMessage());
|
78
|
|
}
|
79
|
6
|
if (application == null)
|
80
|
|
{
|
81
|
0
|
throw new IllegalStateException("Application has not been initialized for this context: " + getRequestPath() +
|
82
|
|
", you need to deploy the ApplicationFilter correctly.");
|
83
|
|
}
|
84
|
6
|
return application;
|
85
|
|
}
|
86
|
|
|
87
|
13
|
private String getApplicationSessionAttributeName()
|
88
|
|
{
|
89
|
|
//return getApplicationPath() + "/application";
|
90
|
|
// TODO
|
91
|
13
|
return "application";
|
92
|
|
}
|
93
|
|
|
94
|
4
|
public void ensureApplicationInit(Class applicationClass) throws IOException, ServletException
|
95
|
|
{
|
96
|
4
|
Application application = (Application) getSessionAttribute(getApplicationSessionAttributeName());
|
97
|
4
|
if (application == null)
|
98
|
|
{
|
99
|
3
|
try
|
100
|
|
{
|
101
|
3
|
application = (Application) applicationClass.newInstance();
|
102
|
|
}
|
103
|
|
catch (Exception e)
|
104
|
|
{
|
105
|
0
|
logger.error("could not instantiate application");
|
106
|
0
|
throw new ServletException("could not instantiate application: " + e.getMessage());
|
107
|
|
}
|
108
|
3
|
setSessionAttribute(getApplicationSessionAttributeName(), application);
|
109
|
|
}
|
110
|
|
}
|
111
|
|
|
112
|
0
|
protected void setRequestAttribute(String name, Object value)
|
113
|
|
{
|
114
|
0
|
getRequest().setAttribute(name, value);
|
115
|
|
}
|
116
|
|
|
117
|
0
|
protected Object getRequestAttribute(String name)
|
118
|
|
{
|
119
|
0
|
return getRequest().getAttribute(name);
|
120
|
|
}
|
121
|
|
|
122
|
11
|
public HttpServletRequest getRequest()
|
123
|
|
{
|
124
|
0
|
if (request == null) throw new IllegalStateException("Tried to access non existent ServletRequest.");
|
125
|
11
|
return request;
|
126
|
|
}
|
127
|
|
|
128
|
7
|
public HttpServletResponse getResponse()
|
129
|
|
{
|
130
|
0
|
if (response == null) throw new IllegalStateException("Tried to access non existent ServletResponse.");
|
131
|
7
|
return response;
|
132
|
|
}
|
133
|
|
|
134
|
24
|
private void increaseIndent()
|
135
|
|
{
|
136
|
24
|
indent++;
|
137
|
|
}
|
138
|
|
|
139
|
24
|
private void decreaseIndent()
|
140
|
|
{
|
141
|
24
|
indent--;
|
142
|
|
}
|
143
|
|
|
144
|
11
|
protected void indent() throws IOException
|
145
|
|
{
|
146
|
11
|
for (int j = 0; j < indent; j++)
|
147
|
9
|
print("\t");
|
148
|
|
}
|
149
|
|
|
150
|
9
|
private void print(String s) throws IOException
|
151
|
|
{
|
152
|
9
|
getWriter().write(s);
|
153
|
|
}
|
154
|
|
|
155
|
20
|
public PrintWriter getWriter() throws IOException
|
156
|
|
{
|
157
|
20
|
return response.getWriter();
|
158
|
|
}
|
159
|
|
|
160
|
20
|
public void emptyTag(String tag) throws IOException
|
161
|
|
{
|
162
|
20
|
println("<" + tag + ">");
|
163
|
|
}
|
164
|
|
|
165
|
24
|
public void startTag(String tag) throws IOException
|
166
|
|
{
|
167
|
24
|
println("<" + tag + ">");
|
168
|
24
|
increaseIndent();
|
169
|
|
}
|
170
|
|
|
171
|
24
|
public void endTag(String tag) throws IOException
|
172
|
|
{
|
173
|
24
|
decreaseIndent();
|
174
|
24
|
println("</" + tag + ">");
|
175
|
|
}
|
176
|
|
|
177
|
11
|
public void println(String line) throws IOException
|
178
|
|
{
|
179
|
11
|
indent();
|
180
|
11
|
getWriter().println(line);
|
181
|
|
}
|
182
|
|
|
183
|
|
/**
|
184
|
|
* Returns an absolute uri useful for sending requests which should end up in an action.
|
185
|
|
* The angkor:header-tag renders a HTML base-tag to make all urls in angkor is relative to the application.
|
186
|
|
* For that reason links that should end up to be processed correctly by the ApplicationFilter also need to be
|
187
|
|
* relative to the context-root.
|
188
|
|
*/
|
189
|
0
|
public String getRequestPath()
|
190
|
|
{
|
191
|
0
|
return getRequest().getRequestURI();
|
192
|
|
}
|
193
|
|
|
194
|
|
/**
|
195
|
|
* Constructs an url used to send requests back to the originating component.
|
196
|
|
*/
|
197
|
2
|
public String requestURL(String url)
|
198
|
|
{
|
199
|
2
|
return encodeURL(getRequestPath() + "?" + url);
|
200
|
|
}
|
201
|
|
|
202
|
0
|
public String encodeURL(String url)
|
203
|
|
{
|
204
|
0
|
return getResponse().encodeURL(url);
|
205
|
|
}
|
206
|
|
|
207
|
0
|
public void resetParsingComponents()
|
208
|
|
{
|
209
|
0
|
getApplication().resetParsingComponents();
|
210
|
|
}
|
211
|
|
|
212
|
|
/**
|
213
|
|
* Returns a tread-safe iterator of the parsing components. Parsing components can be registered
|
214
|
|
* while using the returned iterator.
|
215
|
|
*/
|
216
|
0
|
public Iterator iterateParsingComponents()
|
217
|
|
{
|
218
|
0
|
return getApplication().iterateParsingComponents();
|
219
|
|
}
|
220
|
|
|
221
|
10
|
public void registerParsingComponent(View view)
|
222
|
|
{
|
223
|
10
|
getApplication().registerParsingComponent(view);
|
224
|
|
}
|
225
|
|
|
226
|
0
|
protected void setSessionAttribute(String attribute, Object value)
|
227
|
|
{
|
228
|
0
|
HttpSession session = getRequest().getSession();
|
229
|
0
|
session.setAttribute(attribute, value);
|
230
|
|
}
|
231
|
|
|
232
|
7
|
protected Object getSessionAttribute(String attribute)
|
233
|
|
{
|
234
|
7
|
try
|
235
|
|
{
|
236
|
7
|
HttpSession session = getRequest().getSession();
|
237
|
7
|
return session.getAttribute(attribute);
|
238
|
|
}
|
239
|
|
catch (IllegalStateException e)
|
240
|
|
{
|
241
|
0
|
return null;
|
242
|
|
}
|
243
|
|
}
|
244
|
|
|
245
|
0
|
public String nextUniqueID()
|
246
|
|
{
|
247
|
0
|
return getApplication().nextUniqueID();
|
248
|
|
}
|
249
|
|
|
250
|
0
|
public String getRequestParameter(String name)
|
251
|
|
{
|
252
|
0
|
return getRequest().getParameter(name);
|
253
|
|
}
|
254
|
|
|
255
|
1
|
public void beforeRequest() throws IOException, ServletException
|
256
|
|
{
|
257
|
1
|
getApplication().beforeRequest(this);
|
258
|
|
}
|
259
|
|
|
260
|
8
|
public void afterRequest() throws IOException, ServletException
|
261
|
|
{
|
262
|
8
|
getApplication().afterRequest(this);
|
263
|
8
|
getEventQueue().reset();
|
264
|
|
}
|
265
|
|
|
266
|
|
/**
|
267
|
|
* Gets the render context of the currently running pipeline.
|
268
|
|
*/
|
269
|
31
|
public static RenderContext getRenderContext()
|
270
|
|
{
|
271
|
31
|
return (RenderContext) Pipeline.getCurrentExecuteContext().getAttribute(ExecuteContext.RenderContextAttribute);
|
272
|
|
}
|
273
|
|
}
|
274
|
|
|