1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.services.webpage;
18
19
20 import java.util.Properties;
21 import java.util.Collection;
22
23
24 import java.io.IOException;
25 import java.io.FileNotFoundException;
26 import java.io.FileInputStream;
27
28
29 import javax.servlet.http.*;
30 import javax.servlet.RequestDispatcher;
31 import javax.servlet.ServletContext;
32 import javax.servlet.ServletConfig;
33
34
35 import org.apache.velocity.Template;
36 import org.apache.velocity.context.Context;
37 import org.apache.velocity.servlet.VelocityServlet;
38 import org.apache.velocity.app.Velocity;
39 import org.apache.velocity.exception.ResourceNotFoundException;
40 import org.apache.velocity.exception.ParseErrorException;
41
42
43 /***
44 *
45 * WebPageConsoleServlet is the main servlet entry point for the WebPage console.
46 *
47 */
48
49 public class WebPageConsoleServlet extends VelocityServlet
50 {
51
52 private String servletPath = null;
53 private String proxyRoot = null;
54
55
56
57
58
59
60
61
62
63
64
65 public Template handleRequest( HttpServletRequest request,
66 HttpServletResponse response,
67 Context ctx )
68 {
69 if (servletPath == null)
70 {
71 servletPath = request.getServletPath();
72 proxyRoot = getProxyHost(request);
73 }
74
75 boolean online = WebPageManager.isInit();
76 if (online == false)
77 {
78 bootStrapProxy(request, response);
79 }
80
81 String action = request.getParameter("action");
82 String template;
83
84 if (action == null)
85 template = sessions(ctx);
86 else if (action.equals("details"))
87 template = elementSessions(ctx, request);
88 else if (action.equals("test"))
89 template = test(ctx, request);
90 else if (action.equals("kill"))
91 template = kill(ctx, request);
92 else if (action.equals("killne"))
93 template = killne(ctx);
94 else if (action.equals("clear"))
95 template = clear(ctx);
96 else
97 template = sessions(ctx);
98
99
100 Template outty = null;
101
102 try
103 {
104 outty = getTemplate(template);
105 }
106 catch( ParseErrorException pee )
107 {
108 System.out.println("WebPageConsoleServlet : parse error for template " + pee);
109 }
110 catch( ResourceNotFoundException rnfe )
111 {
112 System.out.println("WebPageConsoleServlet : template not found " + rnfe);
113 }
114 catch( Exception e )
115 {
116 System.out.println("Error " + e);
117 }
118 return outty;
119
120 }
121
122 /***
123 * Set the log file to be off of the webapp root, and
124 * will do the same with the file loader paths
125 *
126 * @param config The Servlet configuration.
127 * @return The Properties collection of Velocity properties.
128 * @throws exceptions when failed to read the properties or log files
129 */
130 protected Properties loadConfiguration(ServletConfig config)
131 throws IOException, FileNotFoundException
132 {
133
134
135
136
137 String propsFile = config.getInitParameter(INIT_PROPS_KEY);
138
139 Properties p = new Properties();
140
141 if ( propsFile != null )
142 {
143 String realPath = getServletContext().getRealPath(propsFile);
144
145 if ( realPath != null )
146 {
147 propsFile = realPath;
148 }
149
150 p.load( new FileInputStream(propsFile) );
151 }
152
153
154
155
156
157
158 String log = p.getProperty( Velocity.RUNTIME_LOG);
159
160 if (log != null )
161 {
162 log = getServletContext().getRealPath( log );
163
164 if (log != null)
165 {
166 p.setProperty( Velocity.RUNTIME_LOG, log );
167 }
168 }
169
170
171
172
173
174
175
176 String path = p.getProperty( Velocity.FILE_RESOURCE_LOADER_PATH );
177
178 if ( path != null)
179 {
180 path = getServletContext().getRealPath( path );
181
182 if ( path != null)
183 {
184 p.setProperty( Velocity.FILE_RESOURCE_LOADER_PATH, path );
185 }
186 }
187
188 return p;
189 }
190
191
192
193 private String sessions(Context ctx)
194 {
195
196
197 Collection sessions = WebPageManager.getSessions();
198 Collection targets = WebPageManager.getSites();
199 boolean online = WebPageManager.isInit();
200
201 ctx.put("sessions", sessions);
202 ctx.put("targets", targets);
203 ctx.put("online", new Boolean(online));
204 ctx.put("proxyError", WebPageManager.getErrorString());
205 ctx.put("cmd", this);
206 return "proxyConsole.vm";
207 }
208
209
210
211 private String elementSessions(Context ctx, HttpServletRequest request)
212 {
213 String sid = request.getParameter("id");
214
215 if (sid == null)
216 {
217 return sessions(ctx);
218 }
219
220 SessionMap map = WebPageManager.getSession(sid);
221 if (map == null)
222 {
223 return sessions(ctx);
224 }
225
226
227 boolean online = WebPageManager.isInit();
228
229
230
231
232 ctx.put("online", new Boolean(online));
233 ctx.put("proxyError", WebPageManager.getErrorString());
234 ctx.put("cmd", this);
235 ctx.put("ne_sessions", map);
236
237 return "neConsole.vm";
238 }
239
240
241
242 private String kill(Context ctx, HttpServletRequest request)
243 {
244
245 boolean online = WebPageManager.isInit();
246
247 Collection sessions = WebPageManager.getSessions();
248 Collection elements = WebPageManager.getSites();
249
250 ctx.put("xxx_sessions", sessions);
251 ctx.put("elements", elements);
252 ctx.put("online", new Boolean(online));
253 ctx.put("proxyError", WebPageManager.getErrorString());
254 ctx.put("cmd", this);
255
256 return "proxyConsole.vm";
257 }
258
259
260
261 private String killne(Context ctx)
262 {
263 return "proxyConsole.vm";
264 }
265
266
267
268 private String clear(Context ctx)
269 {
270 return "proxyConsole.vm";
271 }
272
273
274
275 private String test(Context ctx, HttpServletRequest request)
276 {
277 String id = request.getParameter("id");
278 if (id == null)
279 {
280 return sessions(ctx);
281 }
282 SessionMap map = WebPageManager.getSession(id);
283 if (map == null)
284 {
285 return sessions(ctx);
286 }
287 String ipa = request.getParameter("ipa");
288 if (ipa == null)
289 {
290 return sessions(ctx);
291 }
292 SiteSession session = (SiteSession)map.get(ipa);
293 if (session == null)
294 {
295 return sessions(ctx);
296 }
297
298
299 boolean online = WebPageManager.isInit();
300
301
302
303
304 ctx.put("online", new Boolean(online));
305 ctx.put("proxyError", WebPageManager.getErrorString());
306 ctx.put("cmd", this);
307 ctx.put("ne_sessions", map);
308
309 ctx.put("nes", session);
310
311 return "testConsole.vm";
312 }
313
314
315
316 public String getRefresh()
317 {
318 return servletPath + "?action=refresh";
319 }
320
321 public String getClear()
322 {
323 return servletPath + "?action=clear";
324 }
325
326 public String getDetails()
327 {
328 return servletPath + "?action=details";
329 }
330
331 public String getTest()
332 {
333 return servletPath + "?action=test";
334 }
335
336 public String getKill()
337 {
338 return servletPath + "?action=kill";
339 }
340
341 public String getKillne()
342 {
343 return servletPath + "?action=killne";
344 }
345
346 public String getLogon()
347 {
348 String proxy = proxyRoot;
349 Configuration config = Configuration.getInstance();
350 proxy = proxy.concat("?logon-test=true&" + config.getSID() + "=");
351 return proxy;
352 }
353
354 public String getProxyRoot()
355 {
356 return proxyRoot;
357 }
358
359 public String getWebapp()
360 {
361 Configuration config = Configuration.getInstance();
362 return config.getWebapp();
363
364 }
365
366
367
368 private String getProxyHost(HttpServletRequest request)
369 {
370 StringBuffer root = new StringBuffer();
371 String scheme = request.getScheme();
372 root.append(scheme);
373 root.append( "://");
374 int port = request.getServerPort();
375 root.append(request.getServerName());
376
377 if ( (port > 0) &&
378 ((scheme.equals("http") && port != 80) ||
379 (scheme.equals("https") && port != 443)
380 )
381 )
382 {
383 root.append(":");
384 root.append(port);
385 }
386 root.append( Configuration.WPS_SERVLET );
387 return root.toString();
388 }
389
390
391
392 void bootStrapProxy(HttpServletRequest request,
393 HttpServletResponse response)
394 {
395
396 ServletContext ctx = getServletContext();
397
398 try
399 {
400 RequestDispatcher dispatcher
401 = ctx.getRequestDispatcher(Configuration.WPS_SERVLET);
402
403 dispatcher.forward(request, response);
404 }
405 catch (Exception e)
406 {
407 }
408 }
409 }
410
411