001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.servlet;
066:
067: import com.jcorporate.expresso.core.db.DBException;
068: import com.jcorporate.expresso.core.jsdkapi.GenericSession;
069: import com.jcorporate.expresso.core.logging.LogException;
070: import com.jcorporate.expresso.core.misc.ConfigManager;
071: import com.jcorporate.expresso.core.misc.StringUtil;
072: import com.jcorporate.expresso.services.html.HtmlException;
073: import org.apache.log4j.Logger;
074:
075: import javax.servlet.ServletConfig;
076: import javax.servlet.ServletException;
077: import javax.servlet.http.HttpServletRequest;
078: import javax.servlet.http.HttpServletResponse;
079: import java.io.IOException;
080:
081: /**
082: * A Servlet set up to have access to a relational database from the
083: * server side.
084: * This is a base class for all database maintenance and reporting servlets
085: *
086: * @author Michael Nash
087: */
088: public abstract class DBServlet extends StdServlet {
089: protected static final String this Class = DBServlet.class.getName();
090: private boolean skipLogin = false;
091: private static Logger log = null;
092:
093: /**
094: * Standard Servlet init() function
095: *
096: * @param sc The servlet Configuration passed by the web.xml file
097: */
098: public void init(ServletConfig sc) throws ServletException {
099: super .init(sc);
100: setupLog();
101: } /* init(ServletConfig) */
102:
103: /**
104: * Implemented for when the context is unloaded.
105: */
106: public void destroy() {
107: super .destroy();
108: }
109:
110: /**
111: * Sets up the Logging system.
112: */
113: private static void setupLog() {
114: if (log == null) {
115: log = Logger.getLogger(DBServlet.class);
116: }
117: } /* setupLog() */
118:
119: /**
120: * Ancestor calls this function (with super.doGet) to handle
121: * making database connection, getting user login info
122: * and other common tasks
123: *
124: * @param request Standard request object
125: * @param response Standard response object
126: * @throws ServletException If an uncaught exception occurs
127: * @throws IOException If an I/O error communicating with the client occurs
128: */
129: public void doGet(HttpServletRequest request,
130: HttpServletResponse response) throws ServletException,
131: IOException {
132: setupChannels(request);
133:
134: try {
135: super .doGet(request, response);
136:
137: if (!skipLogin) {
138: CheckLogin.getInstance().checkLogin(request);
139: }
140: } catch (Exception se) {
141: showError(se, request, response);
142: }
143: } /* doGet(HttpServletRequest, HttpServletResponse) */
144:
145: /**
146: * Ancestor calls this method via super.doPost to handle standard operations
147: *
148: * @param req Standard request object
149: * @param res Standard response object
150: * @throws ServletException If an uncaught exception occurs
151: * @throws IOException If an I/O error occurs communicating with the client
152: */
153: public void doPost(HttpServletRequest req, HttpServletResponse res)
154: throws ServletException, IOException {
155: ConfigManager.getInstance();
156: setupChannels(req);
157:
158: try {
159: super .doPost(req, res);
160:
161: if (!skipLogin) {
162: CheckLogin.getInstance().checkLogin(req);
163: }
164: } catch (Exception de) {
165: showError(de, req, res);
166: }
167: } /* doPost(HttpServletRequest, HttpServletResponse) */
168:
169: /**
170: * Return the "alternate" database name, if one is set. Else return an empty string
171: *
172: * @param request The servlet request
173: * @return The name of the specified alternate database
174: */
175: protected String getDBName(HttpServletRequest request) {
176: String dbName = null;
177:
178: try {
179: dbName = GenericSession.getAttributeString(request, "db");
180: } catch (ServletException se) {
181: log.error(se);
182: dbName = "";
183: }
184: if (dbName.equals("")) {
185: return "default";
186: }
187:
188: return dbName;
189: } /* getDBName(HttpServletRequest) */
190:
191: /**
192: * Verify that the current user is logged in.
193: * If not, try to log in via the stored cookie.
194: * If that's not possible, log in as Anonymous
195: *
196: * @param req Standard request object
197: * @param res Standard response object
198: * @param nextURL to proceed with when login completed successfully
199: * @return true if login is required
200: * @throws IOException If an I/O error occurs communicating with the client
201: * @throws ServletException If an uncaught exception occurs
202: * @throws DBException If a problem occurs accessing login
203: * information in the database
204: * @deprecated does nothing but return false;
205: */
206: public boolean requireLogin(HttpServletRequest req,
207: HttpServletResponse res, String nextURL)
208: throws IOException, ServletException, DBException,
209: LogException, HtmlException {
210: return false;
211: } /* requireLogin(HttpServletRequest, HttpServletReponse, String) */
212:
213: /**
214: * Set the "alternate" database name
215: *
216: * @param newDBName The new data context to set the session for
217: * @param req the servlet request object
218: */
219: protected void setDBName(String newDBName, HttpServletRequest req)
220: throws ServletException {
221: if (StringUtil.notNull(newDBName).equals("")) {
222: newDBName = "default";
223: }
224:
225: GenericSession.setAttribute(req, "db", newDBName);
226: } /* setDBName(String, HttpServletRequest) */
227:
228: /**
229: * @param newSkip set to true if you want login processing skipped
230: */
231: public void setSkipLogin(boolean newSkip) {
232: skipLogin = newSkip;
233: } /* setSkipLogin(boolean) */
234:
235: /**
236: * Set up the logging channels for all DBServlets
237: *
238: * @param req The servlet request object
239: */
240: public void setupChannels(HttpServletRequest req)
241: throws ServletException {
242: log = Logger.getLogger("expresso.core.servlet");
243: } /* setupChannels(HttpServletRequest) */
244:
245: } /* DBServlet */
|