2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
31 import java.io.IOException;
32 import java.lang.reflect.Method;
33 import java.util.GregorianCalendar;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Locale;
39 import java.util.Vector;
41 import javax.servlet.ServletConfig;
42 import javax.servlet.ServletException;
43 import javax.servlet.UnavailableException;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46 import javax.servlet.http.HttpSession;
47 import javax.servlet.http.HttpSessionBindingEvent;
48 import javax.servlet.http.HttpSessionBindingListener;
50 import mir.config.MirPropertiesConfiguration;
51 import mir.servlet.AbstractServlet;
52 import mir.servlet.ServletModule;
53 import mir.servlet.ServletModuleDispatch;
54 import mir.servlet.ServletModuleExc;
55 import mir.servlet.ServletModuleUserExc;
56 import mir.util.ExceptionFunctions;
57 import mir.util.StringRoutines;
58 import mircoders.entity.EntityUsers;
59 import mircoders.global.MirGlobal;
60 import mircoders.module.ModuleMessage;
61 import mircoders.module.ModuleUsers;
62 import mircoders.servlet.ServletHelper;
63 import mircoders.storage.DatabaseUsers;
65 import org.apache.struts.util.MessageResources;
71 * Mir.java - main servlet, that dispatches to servletmodules
73 * @author $Author: rk $
74 * @version $Id: Mir.java,v 1.49.2.8 2003/10/23 14:55:26 rk Exp $
77 public class Mir extends AbstractServlet {
78 private static ModuleUsers usersModule = null;
79 private static ModuleMessage messageModule = null;
80 private final static Map servletModuleInstanceHash = new HashMap();
81 private static Locale fallbackLocale = null;
83 private static List loginLanguages = null;
85 protected List getLoginLanguages() throws ServletException {
86 synchronized (Mir.class) {
88 if (loginLanguages == null) {
89 MessageResources messageResources =
90 MessageResources.getMessageResources("bundles.adminlocal");
91 MessageResources messageResources2 =
92 MessageResources.getMessageResources("bundles.admin");
95 StringRoutines.splitString(MirGlobal.config().getString("Mir.Login.Languages", "en"), ";");
97 loginLanguages = new Vector();
99 Iterator i = languages.iterator();
101 while (i.hasNext()) {
102 String code = (String) i.next();
103 Locale locale = new Locale(code, "");
104 String name = messageResources.getMessage(locale, "languagename");
107 name = messageResources2.getMessage(locale, "languagename");
114 Map record = new HashMap();
115 record.put("name", name);
116 record.put("code", code);
117 loginLanguages.add(record);
121 return loginLanguages;
123 catch (Throwable t) {
124 throw new ServletException(t.getMessage());
129 public void init(ServletConfig config) throws ServletException {
132 usersModule = new ModuleUsers(DatabaseUsers.getInstance());
135 protected String getDefaultLanguage(HttpServletRequest aRequest) {
136 String defaultlanguage =
137 MirGlobal.config().getString("Mir.Login.DefaultLanguage", "");
139 if (defaultlanguage.length() == 0) {
140 Locale locale = aRequest.getLocale();
141 defaultlanguage = locale.getLanguage();
144 return defaultlanguage;
147 protected synchronized Locale getFallbackLocale() throws ServletException {
149 if (fallbackLocale == null) {
150 fallbackLocale = new Locale(MirPropertiesConfiguration.instance().getString("Mir.Admin.FallbackLanguage", "en"), "");
153 catch (Throwable t) {
154 throw new ServletException(t.getMessage());
157 return fallbackLocale;
160 public EntityUsers checkCredentials(HttpServletRequest aRequest) throws ServletException {
162 EntityUsers user = ServletHelper.getUser(aRequest);
164 String username = aRequest.getParameter("login");
165 String password = aRequest.getParameter("password");
167 if (username != null && password != null) {
168 user = usersModule.getUserForLogin(username, password);
171 ServletHelper.setUser(aRequest, user);
172 aRequest.getSession().setAttribute("sessiontracker", new SessionTracker(username));
178 catch (Throwable t) {
181 throw new ServletException(t.toString());
185 public void process(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException, UnavailableException {
187 long startTime = System.currentTimeMillis();
188 long sessionConnectTime = 0;
190 HttpSession session = aRequest.getSession(true);
191 setNoCaching(aResponse);
192 Locale locale = new Locale(getDefaultLanguage(aRequest), "");
193 aResponse.setContentType("text/html; charset=" +
195 getString("Mir.DefaultHTMLCharset", "UTF-8"));
197 EntityUsers userEntity = checkCredentials(aRequest);
199 if (userEntity == null) {
200 String queryString = aRequest.getQueryString();
202 if ( (queryString != null) && (queryString.length() != 0) && session.getAttribute("login.target") == null &&
203 (aRequest.getParameter("module")==null ||
204 (!aRequest.getParameter("module").equals("login") && !aRequest.getParameter("module").equals("logout")))) {
205 session.setAttribute("login.target", queryString);
208 _sendLoginPage(aResponse, aRequest);
211 String moduleName = aRequest.getParameter("module");
212 checkLanguage(session, aRequest);
214 if ( ( (moduleName == null) || moduleName.equals(""))) {
219 if (moduleName.equals("login")) {
220 String target = (String) session.getAttribute("login.target");
222 if (target != null) {
223 ServletHelper.redirect(aResponse, target);
226 ServletHelper.redirect(aResponse, "");
229 else if (moduleName.equals("logout")) {
230 logger.info(userEntity.getValue("login") + " has logged out");
231 session.invalidate();
232 _sendLoginPage(aResponse, aRequest);
237 ServletModule servletModule = getServletModuleForName(moduleName);
238 ServletModuleDispatch.dispatch(servletModule, aRequest, aResponse);
240 sessionConnectTime = System.currentTimeMillis() - startTime;
241 logger.info("EXECTIME (" + moduleName + "): " + sessionConnectTime + " ms");
243 catch (Throwable e) {
244 Throwable cause = ExceptionFunctions.traceCauseException(e);
246 if (cause instanceof ServletModuleUserExc)
247 handleUserError(aRequest, aResponse, (ServletModuleUserExc) cause);
249 handleError(aRequest, aResponse, cause);
252 if (aRequest.getParameter("killsession")!=null)
253 aRequest.getSession().invalidate();
257 catch (Throwable t) {
260 throw new ServletException(t.toString());
265 * caching routine to get a module for a module name
267 * @param moduleName the module name
268 * @return the requested module
269 * @throws ServletModuleExc
272 private static ServletModule getServletModuleForName(String moduleName) throws ServletModuleExc {
274 if (!servletModuleInstanceHash.containsKey(moduleName)) {
275 // was not found in hash...
277 Class theServletModuleClass = null;
280 // first we try to get ServletModule from stern.che3.servlet
281 theServletModuleClass =
282 Class.forName("mircoders.servlet.ServletModule" + moduleName);
284 catch (ClassNotFoundException e) {
285 // on failure, we try to get it from lib-layer
286 theServletModuleClass =
287 Class.forName("mir.servlet.ServletModule" + moduleName);
290 Method m = theServletModuleClass.getMethod("getInstance", null);
291 ServletModule smod = (ServletModule) m.invoke(null, null);
293 // we put it into map for further reference
294 servletModuleInstanceHash.put(moduleName, smod);
298 catch (Exception e) {
299 throw new ServletModuleExc("*** error resolving classname for " + moduleName + " -- " + e.getMessage());
303 return (ServletModule) servletModuleInstanceHash.get(moduleName);
307 private void handleUserError(HttpServletRequest aRequest, HttpServletResponse aResponse, ServletModuleUserExc anException) {
309 logger.info("user error: " + anException.getMessage());
311 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
313 MessageResources messages = MessageResources.getMessageResources("bundles.admin");
314 responseData.put("errorstring", messages.getMessage(getLocale(aRequest), anException.getMessage(), anException.getParameters()));
315 responseData.put("date", new GregorianCalendar().getTime());
317 ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.UserErrorTemplate"));
319 catch (Throwable e) {
320 logger.error("Error handling user error" + e.toString());
324 private void handleError(HttpServletRequest aRequest, HttpServletResponse aResponse, Throwable anException) {
326 logger.error("error: " + anException);
328 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
330 responseData.put("errorstring", anException.toString());
331 responseData.put("date", new GregorianCalendar().getTime());
333 ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.ErrorTemplate"));
335 catch (Throwable e) {
336 logger.error("Error handling error: " + e.toString());
341 private void _sendLoginPage(HttpServletResponse aResponse, HttpServletRequest aRequest) {
342 String loginTemplate = configuration.getString("Mir.LoginTemplate");
345 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
347 responseData.put("defaultlanguage", getDefaultLanguage(aRequest));
348 responseData.put("languages", getLoginLanguages());
350 ServletHelper.generateResponse(aResponse.getWriter(), responseData, loginTemplate);
352 catch (Throwable e) {
353 handleError(aRequest, aResponse, e);
357 public String getServletInfo() {
358 return "Mir " + configuration.getString("Mir.Version");
361 private class SessionTracker implements HttpSessionBindingListener {
364 public SessionTracker(String aUserName) {
368 public void valueBound(HttpSessionBindingEvent anEvent) {
369 MirGlobal.registerLogin(name);
372 public void valueUnbound(HttpSessionBindingEvent anEvent) {
373 MirGlobal.registerLogout(name);