|
From: <tr...@us...> - 2003-06-27 02:09:24
|
Update of /cvsroot/babeldoc/babeldoc/modules/web/src/com/babeldoc/web/main In directory sc8-pr-cvs1:/tmp/cvs-serv32148/src/com/babeldoc/web/main Modified Files: ConsoleServlet.java Log Message: Reformatted and added new license header Index: ConsoleServlet.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/web/src/com/babeldoc/web/main/ConsoleServlet.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** ConsoleServlet.java 27 May 2003 03:04:57 -0000 1.7 --- ConsoleServlet.java 27 Jun 2003 02:09:21 -0000 1.8 *************** *** 1,29 **** ! /* ! * $Header$ ! * $DateTime: 2002/07/24 01:14:28 $ * * ! * babeldoc: universal document processor * ! * This program is free software; you can redistribute it and/or ! * modify it under the terms of the GNU General Public License ! * as published by the Free Software Foundation; either version 2 ! * of the License, or (at your option) any later version. * ! * This program is distributed in the hope that it will be useful, ! * but WITHOUT ANY WARRANTY; without even the implied warranty of ! * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ! * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - package com.babeldoc.web.main; import com.babeldoc.core.I18n; import com.babeldoc.core.user.*; import com.babeldoc.web.context.BabelContext; import org.apache.velocity.Template; import org.apache.velocity.app.Velocity; --- 1,74 ---- ! /* ==================================================================== ! * The Apache Software License, Version 1.1 * + * Copyright (c) 2000 The Apache Software Foundation. All rights + * reserved. * ! * Redistribution and use in source and binary forms, with or without ! * modification, are permitted provided that the following conditions ! * are met: * ! * 1. Redistributions of source code must retain the above copyright ! * notice, this list of conditions and the following disclaimer. * ! * 2. Redistributions in binary form must reproduce the above copyright ! * notice, this list of conditions and the following disclaimer in ! * the documentation and/or other materials provided with the ! * distribution. ! * ! * 3. The end-user documentation included with the redistribution, ! * if any, must include the following acknowledgment: ! * "This product includes software developed by the ! * Apache Software Foundation (http://www.apache.org/)." ! * Alternately, this acknowledgment may appear in the software itself, ! * if and wherever such third-party acknowledgments normally appear. ! * ! * 4. The names "Apache" and "Apache Software Foundation" must ! * not be used to endorse or promote products derived from this ! * software without prior written permission. For written ! * permission, please contact ap...@ap.... ! * ! * 5. Products derived from this software may not be called "Apache", ! * nor may "Apache" appear in their name, without prior written ! * permission of the Apache Software Foundation. ! * ! * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ! * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ! * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ! * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ! * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ! * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ! * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF ! * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ! * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, ! * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ! * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ! * SUCH DAMAGE. ! * ==================================================================== ! * ! * This software consists of voluntary contributions made by many ! * individuals on behalf of the Apache Software Foundation. For more ! * information on the Apache Software Foundation, please see ! * <http://www.apache.org/>. ! * ! * Portions of this software are based upon public domain software ! * originally written at the National Center for Supercomputing Applications, ! * University of Illinois, Urbana-Champaign. ! * ==================================================================== ! * ! * Babeldoc: The Universal Document Processor ! * ! * $Header$ ! * $DateTime$ ! * $Author$ * */ package com.babeldoc.web.main; import com.babeldoc.core.I18n; import com.babeldoc.core.user.*; + import com.babeldoc.web.context.BabelContext; + import org.apache.velocity.Template; import org.apache.velocity.app.Velocity; *************** *** 33,124 **** import org.apache.velocity.servlet.VelocityServlet; import javax.servlet.ServletConfig; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; ! import java.io.FileNotFoundException; ! import java.io.IOException; ! import java.util.Properties; /** * <strong>VelocityServlet</strong> ! * ! * <p>provides a means to display velocity templates. This is a centerpiece of the babeldoc web. ! * This does what is needed to get the user context based on loging information.</p> * * @author Bmcdonald * @version 1.0 */ ! public class ConsoleServlet ! extends VelocityServlet { ! /** constant: velocity standard templates */ ! final static String ERR_PARSE = "/error/parse.vm"; /** constant: velocity standard templates */ ! final static String ERR_NOTFOUND = "/error/notfound.vm"; /** constant: velocity standard templates */ ! final static String ERR_AUTHENTICATE = "/error/authentication.vm"; /** constant: velocity standard templates */ ! final static String LOGON_PAGE = "/logon.vm"; /** constant: onfiguration name */ ! final static String CONFIG_NAME = "web/mapping"; /** constant: Context user information */ ! final static String USER_CONTEXT = "userContext"; /** constant: Context user information */ ! final static String LOGON_BOUNCE = "logonBounce"; /** constant: parameters */ ! final static String PARM_NAME = "name"; /** constant: parameters */ ! final static String PARM_PASSWORD = "password"; /** ! * A fancier version of loadConfiguration(), this will ! * set the log file to be off of the webapp root, and ! * will do the same with the file loader paths * ! * @param config the servlet configuration passed by the container ! * @return the converted properties */ ! protected Properties loadConfiguration(ServletConfig config) ! throws IOException, FileNotFoundException { ! Properties p = new Properties(); ! String path = config.getServletContext().getRealPath("/"); ! if (path == null) { ! System.out.println(" SampleServlet.loadConfiguration() : unable to " ! + "get the current webapp root. Using '/'. Please fix."); ! path = "/"; } - p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path); - p.setProperty("runtime.log", path + "velocity.log"); ! com.babeldoc.core.EnvironmentLoader.loadEnvironment(); ! System.out.println(p.toString()); ! return p; } /** ! * Process the security: authorization and authentication issues ! * with this request. Return the template to return * * @param request * @return string to the template. ! * @throws com.babeldoc.core.user.UserException */ protected String handleSecurity(HttpServletRequest request) throws UserException { - HttpSession session = request.getSession(true); String path = request.getServletPath(); System.out.println("Context path: " + request.getContextPath()); System.out.println("Servet path: " + path); UserResource resource = UserResourceFactory.getInstance().getUserResource(path); IUserContext userContext = (IUserContext) session.getAttribute(USER_CONTEXT); ! com.babeldoc.core.LogService.getInstance().logDebug(com.babeldoc.core.I18n.get("web.002") + userContext); ! if (userContext == null && UserResourceFactory.getInstance().mustAuthenticate(resource)) { ! com.babeldoc.core.LogService.getInstance().logDebug(com.babeldoc.core.I18n.get("web.003")); // If we are in process of logging in. if (request.getSession(true).getAttribute(LOGON_BOUNCE) != null) { --- 78,213 ---- import org.apache.velocity.servlet.VelocityServlet; + import java.io.FileNotFoundException; + import java.io.IOException; + + import java.util.Properties; + import javax.servlet.ServletConfig; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; ! /** * <strong>VelocityServlet</strong> ! * ! * <p> ! * provides a means to display velocity templates. This is a centerpiece of ! * the babeldoc web. This does what is needed to get the user context based on ! * loging information. ! * </p> * * @author Bmcdonald * @version 1.0 */ ! public class ConsoleServlet extends VelocityServlet { /** constant: velocity standard templates */ ! static final String ERR_PARSE = "/error/parse.vm"; ! /** constant: velocity standard templates */ ! static final String ERR_NOTFOUND = "/error/notfound.vm"; ! /** constant: velocity standard templates */ ! static final String ERR_AUTHENTICATE = "/error/authentication.vm"; ! /** constant: velocity standard templates */ ! static final String LOGON_PAGE = "/logon.vm"; ! /** constant: onfiguration name */ ! static final String CONFIG_NAME = "web/mapping"; ! /** constant: Context user information */ ! static final String USER_CONTEXT = "userContext"; ! /** constant: Context user information */ ! static final String LOGON_BOUNCE = "logonBounce"; /** constant: parameters */ ! static final String PARM_NAME = "name"; ! /** constant: parameters */ ! static final String PARM_PASSWORD = "password"; /** ! * main routine to handle a request. Called by VelocityServlet, your ! * responsibility as programmer is to simply return a valid Template * ! * @param request ! * @param response ! * @param ctx Velocity context to use ! * ! * @return template to be used for the response. */ + public Template handleRequest(HttpServletRequest request, + HttpServletResponse response, Context ctx) { + Template outty = null; ! try { ! HttpSession session = request.getSession(true); ! String path = handleSecurity(request); ! ctx.put("babel", new BabelContext(request, response, ctx)); ! ctx.put("util", new com.babeldoc.core.VelocityUtilityContext()); ! ctx.put("session", session); ! ctx.put("user", session.getAttribute(USER_CONTEXT)); ! if (path.startsWith("/")) { ! path = path.substring(1); ! } ! System.out.println("Trying to get: " + path); ! ! // outty = getTemplate(request.getContextPath()+path); ! outty = getTemplate(path); ! } catch (ParseErrorException pee) { ! com.babeldoc.core.LogService.getInstance().logError(I18n.get("web.005"), ! pee); ! ! try { ! outty = getTemplate(ERR_PARSE); ! } catch (Exception e) { ! } ! } catch (ResourceNotFoundException rnfe) { ! com.babeldoc.core.LogService.getInstance().logError(com.babeldoc.core.I18n.get( ! "web.006"), rnfe); ! ! try { ! outty = getTemplate(ERR_NOTFOUND); ! } catch (Exception e) { ! } ! } catch (Exception e) { ! com.babeldoc.core.LogService.getInstance().logError(com.babeldoc.core.I18n.get( ! "000001"), e); } ! return outty; } /** ! * Process the security: authorization and authentication issues with this ! * request. Return the template to return * * @param request + * * @return string to the template. ! * ! * @throws UserException */ protected String handleSecurity(HttpServletRequest request) throws UserException { HttpSession session = request.getSession(true); String path = request.getServletPath(); System.out.println("Context path: " + request.getContextPath()); System.out.println("Servet path: " + path); + UserResource resource = UserResourceFactory.getInstance().getUserResource(path); IUserContext userContext = (IUserContext) session.getAttribute(USER_CONTEXT); ! com.babeldoc.core.LogService.getInstance().logDebug(com.babeldoc.core.I18n.get( ! "web.002") + userContext); ! ! if ((userContext == null) && ! UserResourceFactory.getInstance().mustAuthenticate(resource)) { ! com.babeldoc.core.LogService.getInstance().logDebug(com.babeldoc.core.I18n.get( ! "web.003")); ! // If we are in process of logging in. if (request.getSession(true).getAttribute(LOGON_BOUNCE) != null) { *************** *** 126,138 **** String password = request.getParameter("password"); ! if (name != null && password != null) { userContext = UserFactory.getInstance().getUserContext(name); if (UserFactory.getInstance().authenticate(userContext, ! new PasswordUserCredential(password))) { ! com.babeldoc.core.LogService.getInstance().logDebug(com.babeldoc.core.I18n.get("web.004")); request.getSession().setAttribute(USER_CONTEXT, userContext); ! com.babeldoc.core.LogService.getInstance().logDebug(com.babeldoc.core.I18n.get("web.004")); path = (String) request.getSession().getAttribute(LOGON_BOUNCE); } else { --- 215,229 ---- String password = request.getParameter("password"); ! if ((name != null) && (password != null)) { userContext = UserFactory.getInstance().getUserContext(name); if (UserFactory.getInstance().authenticate(userContext, ! new PasswordUserCredential(password))) { ! com.babeldoc.core.LogService.getInstance().logDebug(com.babeldoc.core.I18n.get( ! "web.004")); request.getSession().setAttribute(USER_CONTEXT, userContext); ! com.babeldoc.core.LogService.getInstance().logDebug(com.babeldoc.core.I18n.get( ! "web.004")); path = (String) request.getSession().getAttribute(LOGON_BOUNCE); } else { *************** *** 142,198 **** path = ERR_AUTHENTICATE; } session.removeAttribute(LOGON_BOUNCE); } else { session.setAttribute(LOGON_BOUNCE, path); ! com.babeldoc.core.LogService.getInstance().logDebug("LOGON_BOUNCE: " + path); path = LOGON_PAGE; } } return path; } /** ! * main routine to handle a request. Called by ! * VelocityServlet, your responsibility as programmer ! * is to simply return a valid Template * ! * @param request ! * @param response ! * @param ctx Velocity context to use ! * @return template to be used for the response. */ ! public Template handleRequest(HttpServletRequest request, ! HttpServletResponse response, Context ctx) { ! Template outty = null; ! try { ! HttpSession session = request.getSession(true); ! String path = handleSecurity(request); ! ctx.put("babel", new BabelContext(request, response, ctx)); ! ctx.put("util", new com.babeldoc.core.VelocityUtilityContext()); ! ctx.put("session", session); ! ctx.put("user", session.getAttribute(USER_CONTEXT)); ! if (path.startsWith("/")) { ! path = path.substring(1); ! } ! System.out.println("Trying to get: " + path); ! // outty = getTemplate(request.getContextPath()+path); ! outty = getTemplate(path); ! } catch (ParseErrorException pee) { ! com.babeldoc.core.LogService.getInstance().logError(I18n.get("web.005"), pee); ! try { ! outty = getTemplate(ERR_PARSE); ! } catch (Exception e) { ! } ! } catch (ResourceNotFoundException rnfe) { ! com.babeldoc.core.LogService.getInstance().logError(com.babeldoc.core.I18n.get("web.006"), rnfe); ! try { ! outty = getTemplate(ERR_NOTFOUND); ! } catch (Exception e) { ! } ! } catch (Exception e) { ! com.babeldoc.core.LogService.getInstance().logError(com.babeldoc.core.I18n.get("000001"), e); } ! return outty; } } --- 233,278 ---- path = ERR_AUTHENTICATE; } + session.removeAttribute(LOGON_BOUNCE); } else { session.setAttribute(LOGON_BOUNCE, path); ! com.babeldoc.core.LogService.getInstance().logDebug("LOGON_BOUNCE: " + ! path); path = LOGON_PAGE; } } + return path; } /** ! * A fancier version of loadConfiguration(), this will set the log file to be ! * off of the webapp root, and will do the same with the file loader paths * ! * @param config the servlet configuration passed by the container ! * ! * @return the converted properties ! * ! * @throws IOException DOCUMENT ME! ! * @throws FileNotFoundException DOCUMENT ME! */ ! protected Properties loadConfiguration(ServletConfig config) ! throws IOException, FileNotFoundException { ! Properties p = new Properties(); ! String path = config.getServletContext().getRealPath("/"); ! if (path == null) { ! System.out.println(" SampleServlet.loadConfiguration() : unable to " + ! "get the current webapp root. Using '/'. Please fix."); ! path = "/"; } ! ! p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path); ! p.setProperty("runtime.log", path + "velocity.log"); ! ! com.babeldoc.core.EnvironmentLoader.loadEnvironment(); ! System.out.println(p.toString()); ! ! return p; } } |