KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > webpage > WebPageServlet


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.services.webpage;
18
19 // java.io
20
import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.BufferedReader JavaDoc;
23 import java.io.FileReader JavaDoc;
24
25 // java.util
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 JavaDoc WPS_KILLSESSION = "kill";
40     public static final String JavaDoc 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 JavaDoc
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; // request was handled by the WPS server
64
}
65         catch(Exception JavaDoc e)
66         {
67             //e.printStackTrace();
68
log.error(e);
69             displayErrorPage(response, e.getMessage() );
70             error = true;
71         }
72
73         // request wasn't handled by the WPS server (no parameters)
74
// lets just display the info page
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 JavaDoc
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; // request was handled by the WebPageManager server
100
}
101         catch(Exception JavaDoc e)
102         {
103             //e.printStackTrace();
104
log.error(e);
105             displayErrorPage(response, e.getMessage());
106             error = true;
107         }
108
109         // request wasn't handled by the WebPageManager server (no parameters)
110
// lets just display the info page
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 JavaDoc
127     {
128         Configuration config = Configuration.getInstance();
129
130         String JavaDoc nesid = request.getParameter(config.getSID());
131         String JavaDoc pxurl = request.getParameter(config.getURL());
132
133         if (nesid == null && null == pxurl)
134             return false; // no interception specified
135

136         // request is to the WebPageManager server...
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; // did not find a WebPageManager action
147
}
148
149     /**
150      * Display WebPageManager default info page.
151      */

152     private void displayInfoPage(HttpServletResponse response)
153     {
154         try
155         {
156             PrintWriter JavaDoc pw = response.getWriter ();
157             String JavaDoc infoFile = Configuration.getInstance()
158                           .getProperty(Configuration.KEY_CONTENT_INFO);
159             String JavaDoc indexFile = getServletContext().getRealPath(infoFile);
160             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(indexFile));
161             
162             String JavaDoc 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 JavaDoc 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 JavaDoc msg)
188     {
189         try
190         {
191             PrintWriter JavaDoc pw = response.getWriter ();
192     
193             String JavaDoc fileName = Configuration.getInstance()
194                           .getProperty(Configuration.KEY_CONTENT_ERROR);
195             String JavaDoc template = getServletContext().getRealPath(fileName);
196     
197             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(template));
198
199             String JavaDoc line;
200
201             while ((line = br.readLine()) != null)
202             {
203                 int index = line.indexOf("$msg");
204                 if (index > -1) {
205                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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 JavaDoc 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 JavaDoc 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      * Used as a failsafe in case the default info page can't be found.
275      *
276      */

277     private void displayInfo(HttpServletResponse response)
278     {
279         try
280         {
281             PrintWriter JavaDoc 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 JavaDoc ex)
296         {
297             log.error("Failed to get a PrintWriter on response.");
298         }
299     }
300
301     /*
302      * Used as a failsafe in case the default error page can't be found.
303      *
304      */

305     private void displayError(HttpServletResponse response, String JavaDoc msg)
306     {
307         try
308         {
309             PrintWriter JavaDoc 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 JavaDoc ex)
324         {
325             log.error("Failed to get a PrintWriter on response.");
326         }
327     }
328
329 }
330
331
Popular Tags