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.io.IOException;
21 import java.io.PrintWriter;
22 import java.io.BufferedReader;
23 import java.io.FileReader;
24
25
26 import javax.servlet.*;
27 import javax.servlet.http.*;
28
29 import org.apache.log4j.Logger;
30
31 /***
32 *
33 * WebPageServlet is the main servlet entry point for the WebPage Service server.
34 *
35 */
36
37 public class WebPageServlet extends HttpServlet
38 {
39 public static final String WPS_KILLSESSION = "kill";
40 public static final String WPS_KILLPARAM = "9";
41
42 static Logger log = Logger.getLogger(WebPageServlet.class);
43
44 /***
45 *
46 * handles an HTTP GET request.
47 *
48 */
49 public void doGet (HttpServletRequest request, HttpServletResponse response)
50 throws ServletException, IOException
51 {
52 if (!WebPageManager.isInit())
53 {
54 displayInfoPage(response);
55 log.error(WebPageManager.getErrorString());
56 return;
57 }
58
59 boolean error = false;
60 try
61 {
62 if (dispatch(request, response, true))
63 return;
64 }
65 catch(Exception e)
66 {
67
68 log.error(e);
69 displayErrorPage(response, e.getMessage() );
70 error = true;
71 }
72
73
74
75
76 if (false == error)
77 displayInfoPage(response);
78
79 }
80
81 /***
82 *
83 * handles an HTTP POST request.
84 *
85 */
86 public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
87 {
88 if (!WebPageManager.isInit())
89 {
90 displayInfoPage(response);
91 log.error(WebPageManager.getErrorString());
92 return;
93 }
94
95 boolean error = false;
96 try
97 {
98 if (dispatch(request, response, false))
99 return;
100 }
101 catch(Exception e)
102 {
103
104 log.error(e);
105 displayErrorPage(response, e.getMessage());
106 error = true;
107 }
108
109
110
111
112 if (false == error)
113 displayInfoPage(response);
114 }
115
116 /***
117 *
118 * Dispatches the HTTP GET or POST action to the WebPageManagerService.
119 * Requests to the WebPageManager server are indicated in the query parameters.
120 *
121 * @return true if the request was intercepted by the WebPageManager server, otherwise false
122 */
123 private boolean dispatch(HttpServletRequest request,
124 HttpServletResponse response,
125 boolean isGet)
126 throws ServletException, IOException
127 {
128 Configuration config = Configuration.getInstance();
129
130 String nesid = request.getParameter(config.getSID());
131 String pxurl = request.getParameter(config.getURL());
132
133 if (nesid == null && null == pxurl)
134 return false;
135
136
137
138 if (isGet) {
139 WebPageManager.get(this, request, response);
140 }
141 else {
142 WebPageManager.post(this, request, response);
143 }
144
145
146 return true;
147 }
148
149 /***
150 * Display WebPageManager default info page.
151 */
152 private void displayInfoPage(HttpServletResponse response)
153 {
154 try
155 {
156 PrintWriter pw = response.getWriter ();
157 String infoFile = Configuration.getInstance()
158 .getProperty(Configuration.KEY_CONTENT_INFO);
159 String indexFile = getServletContext().getRealPath(infoFile);
160 BufferedReader br = new BufferedReader(new FileReader(indexFile));
161
162 String line;
163 while ((line = br.readLine()) != null)
164 {
165 pw.println(line);
166 }
167
168 if (WebPageManager.isInit())
169 pw.println("<br><font color='green'>:: Status :: Online ::</font><br>" );
170 else
171 {
172 pw.println("<br><font color='red'>:: Status :: Offline ::</font><br>" );
173 pw.println(":: Reason :: " + WebPageManager.getErrorString());
174 }
175 br.close();
176 }
177 catch (Exception ex)
178 {
179 log.error("Failed to read servlet info page");
180 displayInfo(response);
181 }
182 }
183
184 /***
185 * Display WebPageManager default error page.
186 */
187 private void displayErrorPage(HttpServletResponse response, String msg)
188 {
189 try
190 {
191 PrintWriter pw = response.getWriter ();
192
193 String fileName = Configuration.getInstance()
194 .getProperty(Configuration.KEY_CONTENT_ERROR);
195 String template = getServletContext().getRealPath(fileName);
196
197 BufferedReader br = new BufferedReader(new FileReader(template));
198
199 String line;
200
201 while ((line = br.readLine()) != null)
202 {
203 int index = line.indexOf("$msg");
204 if (index > -1) {
205 StringBuffer buffer = new StringBuffer(line);
206 WebPageHelper.replaceAll(buffer, "$msg", msg);
207 pw.println(buffer.toString());
208 }
209 else
210 pw.println(line);
211 }
212 br.close();
213 }
214 catch (Exception ex)
215 {
216 log.error("Failed to read servlet info page");
217 displayError(response, msg);
218 }
219 }
220
221 /***
222 * In certain situations the init() method is called more than once,
223 * somtimes even concurrently. This causes bad things to happen,
224 * so we use this flag to prevent it.
225 */
226 private static boolean firstInit = true;
227
228 /***
229 * This init method will load the default resources from a
230 * properties file.
231 *
232 * @param config typical Servlet initialization parameter.
233 * @exception ServletException a servlet exception.
234 */
235 public final void init(ServletConfig config)
236 throws ServletException
237 {
238 super.init(config);
239
240 org.apache.log4j.PropertyConfigurator.configure("WebPageManagerLog4j.properties");
241
242 synchronized ( this.getClass() )
243 {
244 if (!firstInit)
245 {
246 return;
247 }
248 firstInit = false;
249
250 try
251 {
252 WebPageManager.init(config);
253 }
254 catch (IOException ex)
255 {
256 throw new ServletException(ex.toString());
257 }
258 }
259 }
260
261 /***
262 * Called by the servlet container to indicate to a servlet that the servlet
263 * is being taken out of service. The WebPageManager server cleans up all connections,
264 * logging out of sessions.
265 *
266 */
267 public final void destroy()
268 {
269 WebPageManager.destroy();
270 }
271
272
273
274
275
276
277 private void displayInfo(HttpServletResponse response)
278 {
279 try
280 {
281 PrintWriter pw = response.getWriter();
282 pw.println("<HTML><HEAD><title>Jetspeed Web Page Servlet</title></HEAD><BODY><H1>Jetspeed Web Page Servlet</H1>");
283
284 if (WebPageManager.isInit())
285 pw.println("<br><font color='green'>:: Status :: Online ::</font><br>" );
286 else
287 {
288 pw.println("<br><font color='red'>:: Status :: Offline ::</font><br>" );
289 pw.println(":: Reason :: " + WebPageManager.getErrorString());
290 }
291 pw.println("<br><br>Warning. The Jetspeed HTTP Web Page Servlet was not correctly installed.<br>");
292 pw.println("<br>Please contact Al Gore for support (he invented the internet).<br>");
293 pw.println("</BODY></HTML>");
294 }
295 catch (IOException ex)
296 {
297 log.error("Failed to get a PrintWriter on response.");
298 }
299 }
300
301
302
303
304
305 private void displayError(HttpServletResponse response, String msg)
306 {
307 try
308 {
309 PrintWriter pw = response.getWriter();
310 pw.println("<HTML><HEAD><title>Web Page Servlet General Exception</title></HEAD><BODY><H1>Web Page Servlet General Exception</H1>");
311
312 if (WebPageManager.isInit())
313 pw.println("<br><font color='green'>:: Status :: Online ::</font><br>" );
314 else
315 {
316 pw.println("<br><font color='red'>:: Status :: Offline ::</font><br>" );
317 pw.println(":: Reason :: " + msg);
318 }
319 pw.println("<br><br>Warning. The Jetspeed Web Page Servlet was not correctly installed.<br>");
320 pw.println("<br>Please contact Al Gore for support (he invented the internet).<br>");
321 pw.println("</BODY></HTML>");
322 }
323 catch (IOException ex)
324 {
325 log.error("Failed to get a PrintWriter on response.");
326 }
327 }
328
329 }
330