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;
33 import mir.log.LoggerWrapper;
35 import mir.util.StringRoutines;
36 import mir.util.ExceptionRoutines;
37 import multex.Failure;
39 import javax.servlet.ServletConfig;
40 import javax.servlet.ServletException;
41 import javax.servlet.UnavailableException;
42 import javax.servlet.http.*;
43 import java.io.IOException;
44 import java.io.PrintWriter;
45 import java.io.StringWriter;
46 import java.lang.reflect.Method;
49 import mircoders.module.ModuleUsers;
50 import mircoders.global.MirGlobal;
51 import mircoders.servlet.ServletHelper;
52 import mircoders.entity.EntityUsers;
54 public class Mir extends AbstractServlet {
55 private static ModuleUsers usersModule = null;
56 private final static Map servletModuleInstanceHash = new HashMap();
57 private static Locale fallbackLocale = null;
59 private static List loginLanguages = null;
61 private List getLoginLanguages() throws Failure {
62 synchronized (Mir.class) {
64 if (loginLanguages == null) {
66 StringRoutines.splitString(MirGlobal.config().getString("Mir.Login.Languages", "en"), ";");
68 loginLanguages = new ArrayList();
70 Iterator i = languages.iterator();
73 String code = (String) i.next();
76 MirGlobal.getBundleFactory().getBundle("etc/bundles/adminlocal", new String[] { code });
77 Bundle defaultBundle =
78 MirGlobal.getBundleFactory().getBundle("bundles/admin", new String[] { code });
80 String name = bundle.getValue("languagename", Collections.EMPTY_LIST);
83 name = defaultBundle.getValue("languagename", Collections.EMPTY_LIST);
90 Map record = new HashMap();
91 record.put("name", name);
92 record.put("code", code);
93 loginLanguages.add(record);
97 return loginLanguages;
100 throw new Failure("Error while retrieving the available login languages", t);
105 public void init(ServletConfig config) throws ServletException {
108 usersModule = new ModuleUsers();
111 protected String getDefaultLanguage(HttpServletRequest aRequest) {
112 String defaultlanguage =
113 MirGlobal.config().getString("Mir.Login.DefaultLanguage", "");
115 if (defaultlanguage.length() == 0) {
116 Locale locale = aRequest.getLocale();
117 defaultlanguage = locale.getLanguage();
120 return defaultlanguage;
123 protected synchronized Locale getFallbackLocale() throws ServletException {
125 if (fallbackLocale == null) {
126 fallbackLocale = new Locale(MirPropertiesConfiguration.instance().getString("Mir.Admin.FallbackLanguage", "en"), "");
129 catch (Throwable t) {
130 throw new ServletException(t.getMessage());
133 return fallbackLocale;
136 public EntityUsers checkCredentials(HttpServletRequest aRequest) throws ServletException {
138 EntityUsers user = ServletHelper.getUser(aRequest);
140 String username = aRequest.getParameter("login");
141 String password = aRequest.getParameter("password");
143 if (username != null && password != null) {
144 user = usersModule.getUserForLogin(username, password);
147 ServletHelper.setUser(aRequest, user);
148 usersModule.recordLogin(user);
149 aRequest.getSession().setAttribute("sessiontracker", new SessionTracker(username, user.getId()));
155 catch (Throwable t) {
158 throw new ServletException(t.toString());
162 public void process(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException, UnavailableException {
164 long startTime = System.currentTimeMillis();
165 long sessionConnectTime = 0;
167 HttpSession session = aRequest.getSession(true);
168 setNoCaching(aResponse);
169 aResponse.setContentType("text/html; charset=" +
171 getString("Mir.DefaultHTMLCharset", "UTF-8"));
173 EntityUsers userEntity = checkCredentials(aRequest);
175 if (userEntity == null) {
176 String queryString = aRequest.getQueryString();
178 if ( (queryString != null) && (queryString.length() != 0) && session.getAttribute("login.target") == null &&
179 (aRequest.getParameter("module")==null ||
180 (!aRequest.getParameter("module").equals("login") && !aRequest.getParameter("module").equals("logout")))) {
181 session.setAttribute("login.target", queryString);
184 _sendLoginPage(aResponse, aRequest);
187 String moduleName = aRequest.getParameter("module");
188 checkLanguage(session, aRequest);
190 if ( ( (moduleName == null) || moduleName.equals(""))) {
195 if (moduleName.equals("login")) {
196 String target = (String) session.getAttribute("login.target");
198 if (target != null) {
199 ServletHelper.redirect(aResponse, target);
202 ServletHelper.redirect(aResponse, "");
205 else if (moduleName.equals("logout")) {
206 logger.info(userEntity.getFieldValue("login") + " has logged out");
207 session.invalidate();
208 _sendLoginPage(aResponse, aRequest);
213 ServletModule servletModule = getServletModuleForName(moduleName);
214 ServletModuleDispatch.dispatch(servletModule, aRequest, aResponse);
216 sessionConnectTime = System.currentTimeMillis() - startTime;
217 logger.info("EXECTIME (" + moduleName + "): " + sessionConnectTime + " ms");
219 catch (Throwable e) {
220 Throwable cause = ExceptionRoutines.traceCauseException(e);
222 if (cause instanceof ServletModuleUserExc)
223 handleUserError(aRequest, aResponse, (ServletModuleUserExc) cause);
225 handleError(aRequest, aResponse, cause);
228 if (aRequest.getParameter("killsession")!=null)
229 aRequest.getSession().invalidate();
233 catch (Throwable t) {
234 t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
236 throw new ServletException(t.toString());
241 * caching routine to get a module for a module name
243 * @param moduleName the module name
244 * @return the requested module
245 * @throws ServletModuleExc
248 private static ServletModule getServletModuleForName(String moduleName) throws ServletModuleExc {
250 if (!servletModuleInstanceHash.containsKey(moduleName)) {
251 // was not found in hash...
253 Class theServletModuleClass = null;
256 // first we try to get ServletModule from stern.che3.servlet
257 theServletModuleClass =
258 Class.forName("mircoders.servlet.ServletModule" + moduleName);
260 catch (ClassNotFoundException e) {
261 // on failure, we try to get it from lib-layer
262 theServletModuleClass =
263 Class.forName("mir.servlet.ServletModule" + moduleName);
266 Method m = theServletModuleClass.getMethod("getInstance", null);
267 ServletModule smod = (ServletModule) m.invoke(null, null);
269 // we put it into map for further reference
270 servletModuleInstanceHash.put(moduleName, smod);
274 catch (Exception e) {
275 throw new ServletModuleExc("*** error resolving classname for " + moduleName + " -- " + e.getMessage());
278 return (ServletModule) servletModuleInstanceHash.get(moduleName);
281 private void handleUserError(HttpServletRequest aRequest, HttpServletResponse aResponse, ServletModuleUserExc anException) {
283 logger.info("user error: " + anException.getMessage());
285 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
288 MirGlobal.getBundleFactory().getBundle("etc/bundles/adminlocal", new
289 String[] { getLocale(aRequest).getLanguage() });
290 Bundle defaultBundle =
291 MirGlobal.getBundleFactory().getBundle("bundles/admin", new
292 String[] { getLocale(aRequest).getLanguage() });
294 bundle.getValue(anException.getMessage(), Arrays.asList(anException.getParameters()));
298 defaultBundle.getValue(anException.getMessage(), Arrays.asList(anException.getParameters()));
301 responseData.put("errorstring", message);
302 responseData.put("date", new GregorianCalendar().getTime());
304 ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.UserErrorTemplate"));
306 catch (Throwable e) {
307 logger.error("Error handling user error" + e.toString());
311 private void handleError(HttpServletRequest aRequest, HttpServletResponse aResponse, Throwable anException) {
313 logger.error("error: " + anException);
315 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
317 responseData.put("errorstring", anException.toString());
318 StringWriter writer = new StringWriter();
319 anException.printStackTrace(new PrintWriter(writer));
320 responseData.put("stacktrace", writer.toString());
321 responseData.put("date", new GregorianCalendar().getTime());
323 ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.ErrorTemplate"));
325 catch (Throwable e) {
326 logger.error("Error handling error: " + e.toString());
329 Throwable rootException = ExceptionRoutines.traceCauseException(anException);
331 PrintWriter writer = aResponse.getWriter();
332 writer.println("<html><head><title>FATAL Error</title><body>");
333 writer.println("<h1>" + rootException.toString()+"</h1>");
334 writer.println("<code>");
335 rootException.printStackTrace(writer);
336 writer.println("</code>");
337 writer.println("</body></html>");
340 catch (Throwable t) {
347 private void _sendLoginPage(HttpServletResponse aResponse, HttpServletRequest aRequest) {
348 String loginTemplate = configuration.getString("Mir.LoginTemplate");
351 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
353 responseData.put("defaultlanguage", getDefaultLanguage(aRequest));
354 responseData.put("languages", getLoginLanguages());
356 ServletHelper.generateResponse(aResponse.getWriter(), responseData, loginTemplate);
358 catch (Throwable e) {
359 handleError(aRequest, aResponse, e);
363 public String getServletInfo() {
364 return "Mir " + configuration.getString("Mir.Version");
367 private class SessionTracker implements HttpSessionBindingListener {
371 public SessionTracker(String aUserName, String anId) {
376 public void valueBound(HttpSessionBindingEvent anEvent) {
377 MirGlobal.registerLogin(name, id);
380 public void valueUnbound(HttpSessionBindingEvent anEvent) {
381 MirGlobal.registerLogout(name, id);