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 mir.bundle.Bundle;
32 import mir.config.MirPropertiesConfiguration;
34 import mir.util.ExceptionFunctions;
35 import mir.util.StringRoutines;
36 import mir.log.LoggerWrapper;
37 import mircoders.entity.EntityUsers;
38 import mircoders.global.MirGlobal;
39 import mircoders.module.ModuleUsers;
40 import mircoders.servlet.ServletHelper;
42 import javax.servlet.ServletConfig;
43 import javax.servlet.ServletException;
44 import javax.servlet.UnavailableException;
45 import javax.servlet.http.*;
46 import java.io.IOException;
47 import java.io.PrintWriter;
48 import java.io.PrintStream;
49 import java.io.StringWriter;
50 import java.lang.reflect.Method;
53 import multex.Failure;
56 * Mir.java - main servlet, that dispatches to servletmodules
58 public class Mir extends AbstractServlet {
59 private static ModuleUsers usersModule = null;
60 private final static Map servletModuleInstanceHash = new HashMap();
61 private static Locale fallbackLocale = null;
63 private static List loginLanguages = null;
65 private List getLoginLanguages() throws Failure {
66 synchronized (Mir.class) {
68 if (loginLanguages == null) {
70 StringRoutines.splitString(MirGlobal.config().getString("Mir.Login.Languages", "en"), ";");
72 loginLanguages = new ArrayList();
74 Iterator i = languages.iterator();
77 String code = (String) i.next();
80 MirGlobal.getBundleFactory().getBundle("etc/bundles/adminlocal", new String[] { code });
81 Bundle defaultBundle =
82 MirGlobal.getBundleFactory().getBundle("bundles/admin", new String[] { code });
84 String name = bundle.getValue("languagename", Collections.EMPTY_LIST);
87 name = defaultBundle.getValue("languagename", Collections.EMPTY_LIST);
94 Map record = new HashMap();
95 record.put("name", name);
96 record.put("code", code);
97 loginLanguages.add(record);
101 return loginLanguages;
103 catch (Throwable t) {
104 throw new Failure("Error while retrieving the available login languages", t);
109 public void init(ServletConfig config) throws ServletException {
112 usersModule = new ModuleUsers();
115 protected String getDefaultLanguage(HttpServletRequest aRequest) {
116 String defaultlanguage =
117 MirGlobal.config().getString("Mir.Login.DefaultLanguage", "");
119 if (defaultlanguage.length() == 0) {
120 Locale locale = aRequest.getLocale();
121 defaultlanguage = locale.getLanguage();
124 return defaultlanguage;
127 protected synchronized Locale getFallbackLocale() throws ServletException {
129 if (fallbackLocale == null) {
130 fallbackLocale = new Locale(MirPropertiesConfiguration.instance().getString("Mir.Admin.FallbackLanguage", "en"), "");
133 catch (Throwable t) {
134 throw new ServletException(t.getMessage());
137 return fallbackLocale;
140 public EntityUsers checkCredentials(HttpServletRequest aRequest) throws ServletException {
142 EntityUsers user = ServletHelper.getUser(aRequest);
144 String username = aRequest.getParameter("login");
145 String password = aRequest.getParameter("password");
147 if (username != null && password != null) {
148 user = usersModule.getUserForLogin(username, password);
151 ServletHelper.setUser(aRequest, user);
152 usersModule.recordLogin(user);
153 aRequest.getSession().setAttribute("sessiontracker", new SessionTracker(username, user.getId()));
159 catch (Throwable t) {
162 throw new ServletException(t.toString());
166 public void process(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException, UnavailableException {
168 long startTime = System.currentTimeMillis();
169 long sessionConnectTime = 0;
171 HttpSession session = aRequest.getSession(true);
172 setNoCaching(aResponse);
173 aResponse.setContentType("text/html; charset=" +
175 getString("Mir.DefaultHTMLCharset", "UTF-8"));
177 EntityUsers userEntity = checkCredentials(aRequest);
179 if (userEntity == null) {
180 String queryString = aRequest.getQueryString();
182 if ( (queryString != null) && (queryString.length() != 0) && session.getAttribute("login.target") == null &&
183 (aRequest.getParameter("module")==null ||
184 (!aRequest.getParameter("module").equals("login") && !aRequest.getParameter("module").equals("logout")))) {
185 session.setAttribute("login.target", queryString);
188 _sendLoginPage(aResponse, aRequest);
191 String moduleName = aRequest.getParameter("module");
192 checkLanguage(session, aRequest);
194 if ( ( (moduleName == null) || moduleName.equals(""))) {
199 if (moduleName.equals("login")) {
200 String target = (String) session.getAttribute("login.target");
202 if (target != null) {
203 ServletHelper.redirect(aResponse, target);
206 ServletHelper.redirect(aResponse, "");
209 else if (moduleName.equals("logout")) {
210 logger.info(userEntity.getFieldValue("login") + " has logged out");
211 session.invalidate();
212 _sendLoginPage(aResponse, aRequest);
217 ServletModule servletModule = getServletModuleForName(moduleName);
218 ServletModuleDispatch.dispatch(servletModule, aRequest, aResponse);
220 sessionConnectTime = System.currentTimeMillis() - startTime;
221 logger.info("EXECTIME (" + moduleName + "): " + sessionConnectTime + " ms");
223 catch (Throwable e) {
224 Throwable cause = ExceptionFunctions.traceCauseException(e);
226 if (cause instanceof ServletModuleUserExc)
227 handleUserError(aRequest, aResponse, (ServletModuleUserExc) cause);
229 handleError(aRequest, aResponse, cause);
232 if (aRequest.getParameter("killsession")!=null)
233 aRequest.getSession().invalidate();
237 catch (Throwable t) {
238 t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
240 throw new ServletException(t.toString());
245 * caching routine to get a module for a module name
247 * @param moduleName the module name
248 * @return the requested module
249 * @throws ServletModuleExc
252 private static ServletModule getServletModuleForName(String moduleName) throws ServletModuleExc {
254 if (!servletModuleInstanceHash.containsKey(moduleName)) {
255 // was not found in hash...
257 Class theServletModuleClass = null;
260 // first we try to get ServletModule from stern.che3.servlet
261 theServletModuleClass =
262 Class.forName("mircoders.servlet.ServletModule" + moduleName);
264 catch (ClassNotFoundException e) {
265 // on failure, we try to get it from lib-layer
266 theServletModuleClass =
267 Class.forName("mir.servlet.ServletModule" + moduleName);
270 Method m = theServletModuleClass.getMethod("getInstance", null);
271 ServletModule smod = (ServletModule) m.invoke(null, null);
273 // we put it into map for further reference
274 servletModuleInstanceHash.put(moduleName, smod);
278 catch (Exception e) {
279 throw new ServletModuleExc("*** error resolving classname for " + moduleName + " -- " + e.getMessage());
283 return (ServletModule) servletModuleInstanceHash.get(moduleName);
287 private void handleUserError(HttpServletRequest aRequest, HttpServletResponse aResponse, ServletModuleUserExc anException) {
289 logger.info("user error: " + anException.getMessage());
291 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
294 MirGlobal.getBundleFactory().getBundle("etc/bundles/adminlocal", new
295 String[] { getLocale(aRequest).getLanguage() });
296 Bundle defaultBundle =
297 MirGlobal.getBundleFactory().getBundle("bundles/admin", new
298 String[] { getLocale(aRequest).getLanguage() });
300 bundle.getValue(anException.getMessage(), Arrays.asList(anException.getParameters()));
304 defaultBundle.getValue(anException.getMessage(), Arrays.asList(anException.getParameters()));
307 responseData.put("errorstring", message);
308 responseData.put("date", new GregorianCalendar().getTime());
310 ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.UserErrorTemplate"));
312 catch (Throwable e) {
313 logger.error("Error handling user error" + e.toString());
317 private void handleError(HttpServletRequest aRequest, HttpServletResponse aResponse, Throwable anException) {
319 logger.error("error: " + anException);
321 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
323 responseData.put("errorstring", anException.toString());
324 StringWriter writer = new StringWriter();
325 anException.printStackTrace(new PrintWriter(writer));
326 responseData.put("stacktrace", writer.toString());
327 responseData.put("date", new GregorianCalendar().getTime());
329 ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.ErrorTemplate"));
331 catch (Throwable e) {
332 logger.error("Error handling error: " + e.toString());
335 Throwable rootException = ExceptionFunctions.traceCauseException(anException);
337 PrintWriter writer = aResponse.getWriter();
338 writer.println("<html><head><title>FATAL Error</title><body>");
339 writer.println("<h1>" + rootException.toString()+"</h1>");
340 writer.println("<code>");
341 rootException.printStackTrace(writer);
342 writer.println("</code>");
343 writer.println("</body></html>");
346 catch (Throwable t) {
353 private void _sendLoginPage(HttpServletResponse aResponse, HttpServletRequest aRequest) {
354 String loginTemplate = configuration.getString("Mir.LoginTemplate");
357 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
359 responseData.put("defaultlanguage", getDefaultLanguage(aRequest));
360 responseData.put("languages", getLoginLanguages());
362 ServletHelper.generateResponse(aResponse.getWriter(), responseData, loginTemplate);
364 catch (Throwable e) {
365 handleError(aRequest, aResponse, e);
369 public String getServletInfo() {
370 return "Mir " + configuration.getString("Mir.Version");
373 private class SessionTracker implements HttpSessionBindingListener {
377 public SessionTracker(String aUserName, String anId) {
382 public void valueBound(HttpSessionBindingEvent anEvent) {
383 MirGlobal.registerLogin(name, id);
386 public void valueUnbound(HttpSessionBindingEvent anEvent) {
387 MirGlobal.registerLogout(name, id);