KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > servlet > StdServlet


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.servlet;
66
67 /*
68  * StdServlet.java
69  *
70  * Copyright 1999, 2000, 2001 Jcorporate Ltd.
71  */

72
73 import com.jcorporate.expresso.core.i18n.Messages;
74 import com.jcorporate.expresso.core.misc.ConfigManager;
75 import com.jcorporate.expresso.core.misc.HTTPUtil;
76 import com.jcorporate.expresso.core.misc.StringUtil;
77
78 import javax.servlet.ServletConfig JavaDoc;
79 import javax.servlet.ServletException JavaDoc;
80 import javax.servlet.http.HttpServlet JavaDoc;
81 import javax.servlet.http.HttpServletRequest JavaDoc;
82 import javax.servlet.http.HttpServletResponse JavaDoc;
83 import javax.servlet.http.HttpSession JavaDoc;
84 import java.io.IOException JavaDoc;
85
86
87 /**
88  * A Servlet base class adding some basic common functionality to the
89  * core HttpServlet class.
90  *
91  * @author Michael Nash
92  * @see com.jcorporate.expresso.core.servlet.DBServlet
93  */

94 public abstract class StdServlet
95         extends HttpServlet JavaDoc {
96     protected static final String JavaDoc thisClass = StdServlet.class.getName() + ".";
97     protected String JavaDoc mySchema = null;
98
99     /**
100      * Ancestor calls this function (with super.doGet) to handle
101      * making database connection, getting user login info
102      * and other common tasks.
103      *
104      * @param request Standard request object
105      * @param response Standard response object
106      * @throws ServletException If an uncaught exception occurs
107      * @throws IOException If an I/O error occurs while communicating with
108      * the client
109      */

110     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
111             throws ServletException JavaDoc, IOException JavaDoc {
112         HttpSession JavaDoc session = request.getSession(false);
113
114         if (session == null) {
115             session = request.getSession(true);
116         }
117         try {
118             handleParam(request, response);
119         } catch (ServletException JavaDoc se) {
120             showError(se, request, response);
121
122             return;
123         }
124     } /* doGet(HttpServletRequest, HttpServletResponse) */
125
126
127     /**
128      * Ancestor calls this method via super.doPost to handle standard
129      * operations.
130      *
131      * @param req Standard request object
132      * @param res Standard response object
133      * @throws ServletException If an uncaught exception occurs
134      * @throws IOException If an I/O error occurs while communicating with
135      * the client
136      */

137     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
138             throws ServletException JavaDoc, IOException JavaDoc {
139         try {
140             handleParam(req, res);
141         } catch (Exception JavaDoc de) {
142             showError(de, req, res);
143
144             return;
145         }
146     } /* doPost(HttpServletRequest, HttpServletResponse) */
147
148
149     /**
150      * Return the name of the current web server system
151      *
152      * @param request
153      * @return String The name of the current web server
154      */

155     protected String JavaDoc getServerName(HttpServletRequest JavaDoc request) {
156         return request.getServerName();
157     } /* getServerName(HttpServletRequest) */
158
159     /**
160      * Return the port number the servlet runtime engine is running on
161      *
162      * @param request
163      * @return int The port number for servlets
164      */

165     public int getServerPort(HttpServletRequest JavaDoc request) {
166         return request.getServerPort();
167     } /* getServerPort(HttpServletRequest) */
168
169     /**
170      * Return a string to be used as part of the URL to call this servlet.
171      * Often used by doGet methods to build the action for a form to call
172      * the doPost method of the same servlet.
173      *
174      * @param request
175      * @return String The string for building a URL
176      * @throws ServletException If the string cannot be built due to
177      * setup information not being available
178      */

179     public String JavaDoc getServletPrefix(HttpServletRequest JavaDoc request)
180             throws ServletException JavaDoc {
181         return (request.getRequestURI());
182     } /* getServletPrefix(HttpServletRequest) */
183
184
185     /**
186      * @param request
187      * @param servlet
188      * @return
189      */

190     public String JavaDoc getServletPrefix(HttpServletRequest JavaDoc request, String JavaDoc servlet)
191             throws ServletException JavaDoc {
192         String JavaDoc initPath = getServletPrefix(request);
193         int lastPos = initPath.lastIndexOf("/");
194
195         if (lastPos > 0) {
196             return initPath.substring(0, lastPos) + "/" + servlet;
197         } else {
198             return initPath + "/" + servlet;
199         }
200     } /* getServletPrefix(HttpServletRequest, String) */
201
202
203     /**
204      * Ancestors of this class override this method to return a specific title that is
205      * used by the security administration Controllers to describe the servlet
206      *
207      * @return
208      */

209     public String JavaDoc getTitle() {
210         return ("No Title - " + getClass().getName());
211     } /* getTitle() */
212
213     /**
214      * Handle the parameters to this servlet & set up standard information
215      *
216      * @param request Standard request object
217      * @param response Standard response object
218      * @throws IOException If an I/O error occurs communicating with the client
219      * @throws ServletException If an uncaught exception occurs
220      */

221     protected void handleParam(HttpServletRequest JavaDoc request,
222                                HttpServletResponse JavaDoc response)
223             throws IOException JavaDoc, ServletException JavaDoc {
224         HTTPUtil.setBackURL(request);
225         HTTPUtil.setContinueURLFromQueryString(request);
226     } /* handleParam(HttpServletRequest, HttpServletResponse) */
227
228
229     /**
230      * Display an error to the user in a nicely formatted manner
231      *
232      * @param errorMessage Error message
233      * @param request
234      * @param response
235      */

236     protected void showError(String JavaDoc errorMessage, HttpServletRequest JavaDoc request,
237                              HttpServletResponse JavaDoc response)
238             throws ServletException JavaDoc {
239         Exception JavaDoc e = new Exception JavaDoc("No specific exception available");
240         showError(e, request, response);
241     } /* showError(String, HttpServletRequest, HttpServletResponse) */
242
243
244     /**
245      * Display a throwable (like an exception) to the user in a nicely
246      * formatted manner. Also show the stack trace in an HTML comment
247      *
248      * @param t The throwable error
249      * @param request
250      * @param response
251      */

252     protected void showError(Throwable JavaDoc t, HttpServletRequest JavaDoc request,
253                              HttpServletResponse JavaDoc response)
254             throws ServletException JavaDoc {
255         throw new ServletException JavaDoc(t);
256     } /* showError(Throwable, HttpServletRequest, HttpServletResponse) */
257
258
259     /**
260      * Servlet initialization. Checks to make sure ConfigManager has been
261      * initialized and starts the configuration process if it hasn't.
262      *
263      * @param sc The ServletConfig. Should contain 'configDir' as a context
264      * parameter, and possibly 'logDir'.
265      */

266     public void init(ServletConfig JavaDoc sc)
267             throws ServletException JavaDoc {
268
269         super.init(sc);
270
271         if (!ConfigManager.isInitialized()) {
272             ConfigManager.config(sc);
273         }
274     } /* init(ServletConfig) */
275
276
277     /**
278      * Called when the web context is destroyed or reloaded
279      */

280     public void destroy() {
281     }
282
283
284     /**
285      * Tell this StdServlet object what Schema it belongs to. This is used
286      * when the Controller tries to use it's "getString(String, Object[])"
287      * method to prepare internationalized messages - it passes the call
288      * along to the appropriate schema which knows how to locate the
289      * proper message file
290      *
291      * @param schemaClass
292      */

293     protected void setSchema(String JavaDoc schemaClass) {
294         StringUtil.assertNotBlank(schemaClass,
295                 "Schema cannot be set to blank or null");
296         mySchema = schemaClass;
297     } /* setSchema(String) */
298
299     /**
300      * Pass on a call to retrieve an appropriate localized string from the
301      * correct Schema object
302      *
303      * @param req
304      * @param stringCode
305      * @param args
306      * @return
307      */

308     public String JavaDoc getString(HttpServletRequest JavaDoc req, String JavaDoc stringCode,
309                             Object JavaDoc[] args)
310             throws ServletException JavaDoc {
311         if (mySchema == null) {
312             setSchema("com.jcorporate.expresso.core.ExpressoSchema");
313         }
314
315         return Messages.getString(getSchema(), req, stringCode, args);
316     } /* getString(HttpServletRequest, String, Object[]) */
317
318
319     /**
320      * Convenience method to request a localized message with no parameters
321      *
322      * @param req
323      * @param stringCode
324      * @return
325      */

326     public String JavaDoc getString(HttpServletRequest JavaDoc req, String JavaDoc stringCode)
327             throws ServletException JavaDoc {
328         Object JavaDoc[] args = {};
329
330         return getString(req, stringCode, args);
331     } /* getString(HttpServletRequest, String) */
332
333
334     /**
335      * Return the name of the schema class that this servlet is registered
336      * within
337      *
338      * @return
339      */

340     protected String JavaDoc getSchema() {
341         return mySchema;
342     } /* getSchema() */
343
344 } /* StdServlet */
345
Popular Tags