KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.jsdkapi.GenericSession;
69 import com.jcorporate.expresso.core.logging.LogException;
70 import com.jcorporate.expresso.core.misc.ConfigManager;
71 import com.jcorporate.expresso.core.misc.StringUtil;
72 import com.jcorporate.expresso.services.html.HtmlException;
73 import org.apache.log4j.Logger;
74
75 import javax.servlet.ServletConfig JavaDoc;
76 import javax.servlet.ServletException JavaDoc;
77 import javax.servlet.http.HttpServletRequest JavaDoc;
78 import javax.servlet.http.HttpServletResponse JavaDoc;
79 import java.io.IOException JavaDoc;
80
81
82 /**
83  * A Servlet set up to have access to a relational database from the
84  * server side.
85  * This is a base class for all database maintenance and reporting servlets
86  *
87  * @author Michael Nash
88  */

89 public abstract class DBServlet
90         extends StdServlet {
91     protected static final String JavaDoc thisClass = DBServlet.class.getName();
92     private boolean skipLogin = false;
93     private static Logger log = null;
94
95     /**
96      * Standard Servlet init() function
97      *
98      * @param sc The servlet Configuration passed by the web.xml file
99      */

100     public void init(ServletConfig JavaDoc sc)
101             throws ServletException JavaDoc {
102         super.init(sc);
103         setupLog();
104     } /* init(ServletConfig) */
105
106
107     /**
108      * Implemented for when the context is unloaded.
109      */

110     public void destroy() {
111         super.destroy();
112     }
113
114     /**
115      * Sets up the Logging system.
116      */

117     private static void setupLog() {
118         if (log == null) {
119             log = Logger.getLogger(DBServlet.class);
120         }
121     } /* setupLog() */
122
123     /**
124      * Ancestor calls this function (with super.doGet) to handle
125      * making database connection, getting user login info
126      * and other common tasks
127      *
128      * @param request Standard request object
129      * @param response Standard response object
130      * @throws ServletException If an uncaught exception occurs
131      * @throws IOException If an I/O error communicating with the client occurs
132      */

133     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
134             throws ServletException JavaDoc, IOException JavaDoc {
135         setupChannels(request);
136
137         try {
138             super.doGet(request, response);
139
140             if (!skipLogin) {
141                 CheckLogin.getInstance().checkLogin(request);
142             }
143         } catch (Exception JavaDoc se) {
144             showError(se, request, response);
145         }
146     } /* doGet(HttpServletRequest, HttpServletResponse) */
147
148
149     /**
150      * Ancestor calls this method via super.doPost to handle standard operations
151      *
152      * @param req Standard request object
153      * @param res Standard response object
154      * @throws ServletException If an uncaught exception occurs
155      * @throws IOException If an I/O error occurs communicating with the client
156      */

157     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
158             throws ServletException JavaDoc, IOException JavaDoc {
159         ConfigManager.getInstance();
160         setupChannels(req);
161
162         try {
163             super.doPost(req, res);
164
165             if (!skipLogin) {
166                 CheckLogin.getInstance().checkLogin(req);
167             }
168         } catch (Exception JavaDoc de) {
169             showError(de, req, res);
170         }
171     } /* doPost(HttpServletRequest, HttpServletResponse) */
172
173
174     /**
175      * Return the "alternate" database name, if one is set. Else return an empty string
176      *
177      * @param request The servlet request
178      * @return The name of the specified alternate database
179      */

180     protected String JavaDoc getDBName(HttpServletRequest JavaDoc request) {
181         String JavaDoc dbName = null;
182
183         try {
184             dbName = GenericSession.getAttributeString(request, "db");
185         } catch (ServletException JavaDoc se) {
186             log.error(se);
187             dbName = "";
188         }
189         if (dbName.equals("")) {
190             return "default";
191         }
192
193         return dbName;
194     } /* getDBName(HttpServletRequest) */
195
196     /**
197      * Verify that the current user is logged in.
198      * If not, try to log in via the stored cookie.
199      * If that's not possible, log in as Anonymous
200      *
201      * @param req Standard request object
202      * @param res Standard response object
203      * @param nextURL to proceed with when login completed successfully
204      * @return true if login is required
205      * @throws IOException If an I/O error occurs communicating with the client
206      * @throws ServletException If an uncaught exception occurs
207      * @throws DBException If a problem occurs accessing login
208      * information in the database
209      * @deprecated does nothing but return false;
210      */

211     public boolean requireLogin(HttpServletRequest JavaDoc req,
212                                 HttpServletResponse JavaDoc res, String JavaDoc nextURL)
213             throws IOException JavaDoc, ServletException JavaDoc, DBException,
214             LogException, HtmlException {
215         return false;
216     } /* requireLogin(HttpServletRequest, HttpServletReponse, String) */
217
218
219     /**
220      * Set the "alternate" database name
221      *
222      * @param newDBName The new data context to set the session for
223      * @param req the servlet request object
224      */

225     protected void setDBName(String JavaDoc newDBName, HttpServletRequest JavaDoc req)
226             throws ServletException JavaDoc {
227         if (StringUtil.notNull(newDBName).equals("")) {
228             newDBName = "default";
229         }
230
231         GenericSession.setAttribute(req, "db", newDBName);
232     } /* setDBName(String, HttpServletRequest) */
233
234
235     /**
236      * @param newSkip set to true if you want login processing skipped
237      */

238     public void setSkipLogin(boolean newSkip) {
239         skipLogin = newSkip;
240     } /* setSkipLogin(boolean) */
241
242     /**
243      * Set up the logging channels for all DBServlets
244      *
245      * @param req The servlet request object
246      */

247     public void setupChannels(HttpServletRequest JavaDoc req)
248             throws ServletException JavaDoc {
249         log = Logger.getLogger("expresso.core.servlet");
250     } /* setupChannels(HttpServletRequest) */
251
252
253 } /* DBServlet */
254
255
Popular Tags