2 * Copyright (C) 2001-2006 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 * and distribute linked combinations including the two. You must obey the
23 * GNU General Public License in all respects for all of the code used other than
24 * the above mentioned libraries. If you modify this file, you may extend this
25 * exception to your version of the file, but you are not obligated to do so.
26 * If you do not wish to do so, delete this exception statement from your version.
29 import mir.bundle.Bundle;
30 import mir.config.MirPropertiesConfiguration;
31 import mir.servlet.AbstractServlet;
32 import mir.servlet.AdminServletModule;
33 import mir.servlet.ServletModuleExc;
34 import mir.servlet.ServletModuleUserExc;
35 import mir.util.ExceptionRoutines;
36 import mir.util.StringRoutines;
37 import mircoders.entity.EntityUsers;
38 import mircoders.global.MirGlobal;
39 import mircoders.module.ModuleUsers;
40 import mircoders.servlet.ServletHelper;
41 import multex.Failure;
43 import javax.servlet.ServletConfig;
44 import javax.servlet.ServletException;
45 import javax.servlet.UnavailableException;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import javax.servlet.http.HttpSession;
49 import javax.servlet.http.HttpSessionBindingEvent;
50 import javax.servlet.http.HttpSessionBindingListener;
51 import java.io.IOException;
52 import java.io.PrintWriter;
53 import java.io.StringWriter;
54 import java.util.ArrayList;
55 import java.util.Arrays;
56 import java.util.Collections;
57 import java.util.GregorianCalendar;
58 import java.util.HashMap;
59 import java.util.Iterator;
60 import java.util.List;
61 import java.util.Locale;
64 public class Mir extends AbstractServlet {
65 private static ModuleUsers usersModule = null;
66 private static Locale fallbackLocale = null;
68 private static List loginLanguages = null;
70 private List getLoginLanguages() throws Failure {
71 synchronized (Mir.class) {
73 if (loginLanguages == null) {
75 StringRoutines.splitString(MirGlobal.config().getString("Mir.Login.Languages", "en"), ";");
77 loginLanguages = new ArrayList();
79 Iterator i = languages.iterator();
82 String code = (String) i.next();
85 MirGlobal.getBundleFactory().getBundle("etc/bundles/adminlocal", new String[] { code });
86 Bundle defaultBundle =
87 MirGlobal.getBundleFactory().getBundle("bundles/admin", new String[] { code });
89 String name = bundle.getValue("languagename", Collections.EMPTY_LIST);
92 name = defaultBundle.getValue("languagename", Collections.EMPTY_LIST);
99 Map record = new HashMap();
100 record.put("name", name);
101 record.put("code", code);
102 loginLanguages.add(record);
106 return loginLanguages;
108 catch (Throwable t) {
109 throw new Failure("Error while retrieving the available login languages", t);
114 public void init(ServletConfig config) throws ServletException {
117 usersModule = new ModuleUsers();
120 protected String getDefaultLanguage(HttpServletRequest aRequest) {
121 String defaultlanguage =
122 MirGlobal.config().getString("Mir.Login.DefaultLanguage", "");
124 if (defaultlanguage.length() == 0) {
125 Locale locale = aRequest.getLocale();
126 defaultlanguage = locale.getLanguage();
129 return defaultlanguage;
132 protected synchronized Locale getFallbackLocale() throws ServletException {
134 if (fallbackLocale == null) {
135 fallbackLocale = new Locale(MirPropertiesConfiguration.instance().getString("Mir.Admin.FallbackLanguage", "en"), "");
138 catch (Throwable t) {
139 throw new ServletException(t.getMessage());
142 return fallbackLocale;
145 public EntityUsers checkCredentials(HttpServletRequest aRequest) throws ServletException {
147 EntityUsers user = ServletHelper.getUser(aRequest);
149 String username = aRequest.getParameter("login");
150 String password = aRequest.getParameter("password");
152 if (username != null && password != null) {
153 user = usersModule.getUserForLogin(username, password);
156 ServletHelper.setUser(aRequest, user);
157 usersModule.recordLogin(user);
158 aRequest.getSession().setAttribute("sessiontracker", new SessionTracker(username, user.getId()));
164 catch (Throwable t) {
165 throw new ServletException(t.getMessage());
169 public void process(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException, UnavailableException {
171 long startTime = System.currentTimeMillis();
172 long sessionConnectTime = 0;
174 HttpSession session = aRequest.getSession(true);
175 setNoCaching(aResponse);
176 aResponse.setContentType("text/html; charset=" +
178 getString("Mir.DefaultHTMLCharset", "UTF-8"));
180 EntityUsers userEntity = checkCredentials(aRequest);
182 if (userEntity == null) {
183 String queryString = aRequest.getQueryString();
185 if ( (queryString != null) && (queryString.length() != 0) && session.getAttribute("login.target") == null &&
186 (aRequest.getParameter("module")==null ||
187 (!aRequest.getParameter("module").equals("login") && !aRequest.getParameter("module").equals("logout")))) {
188 session.setAttribute("login.target", queryString);
191 _sendLoginPage(aResponse, aRequest);
194 String moduleName = aRequest.getParameter("module");
195 checkLanguage(session, aRequest);
197 if ( ( (moduleName == null) || moduleName.equals(""))) {
201 if (moduleName.equals("login")) {
202 String target = (String) session.getAttribute("login.target");
204 if (target != null) {
205 ServletHelper.redirect(aResponse, target);
208 ServletHelper.redirect(aResponse, "");
211 else if (moduleName.equals("logout")) {
212 logger.info(userEntity.getFieldValue("login") + " has logged out");
213 session.invalidate();
214 _sendLoginPage(aResponse, aRequest);
219 AdminServletModule servletModule = getServletModuleForName(moduleName);
220 servletModule.handleRequest(aRequest, aResponse);
222 sessionConnectTime = System.currentTimeMillis() - startTime;
223 logger.info("EXECTIME (" + moduleName + "): " + sessionConnectTime + " ms");
225 catch (Throwable e) {
226 Throwable cause = ExceptionRoutines.traceCauseException(e);
228 if (cause instanceof ServletModuleUserExc)
229 handleUserError(aRequest, aResponse, (ServletModuleUserExc) cause);
231 handleError(aRequest, aResponse, cause);
234 if (aRequest.getParameter("killsession")!=null)
235 aRequest.getSession().invalidate();
239 catch (Throwable t) {
240 throw new ServletException(t.toString());
245 * caching routine to get a module for a module name
247 * @param aModuleName the module name
248 * @return the requested module
249 * @throws ServletModuleExc
251 private static AdminServletModule getServletModuleForName(String aModuleName) throws ServletModuleExc {
252 return ServletHelper.getServletModule(aModuleName);
255 private void handleUserError(HttpServletRequest aRequest, HttpServletResponse aResponse, ServletModuleUserExc anException) {
257 logger.info("user error: " + anException.getMessage());
259 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
262 MirGlobal.getBundleFactory().getBundle("etc/bundles/adminlocal", new
263 String[] { getLocale(aRequest).getLanguage() });
264 Bundle defaultBundle =
265 MirGlobal.getBundleFactory().getBundle("bundles/admin", new
266 String[] { getLocale(aRequest).getLanguage() });
268 bundle.getValue(anException.getMessage(), Arrays.asList(anException.getParameters()));
272 defaultBundle.getValue(anException.getMessage(), Arrays.asList(anException.getParameters()));
275 responseData.put("errorstring", message);
276 responseData.put("date", new GregorianCalendar().getTime());
278 ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.UserErrorTemplate"));
280 catch (Throwable e) {
281 logger.error("Error handling user error" + e.toString());
285 private void handleError(HttpServletRequest aRequest, HttpServletResponse aResponse, Throwable anException) {
287 logger.error("error: " + anException);
289 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
291 responseData.put("errorstring", anException.toString());
292 StringWriter writer = new StringWriter();
293 anException.printStackTrace(new PrintWriter(writer));
294 responseData.put("stacktrace", writer.toString());
295 responseData.put("date", new GregorianCalendar().getTime());
297 ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.ErrorTemplate"));
299 catch (Throwable e) {
300 logger.error("Error handling error: " + e.toString());
303 Throwable rootException = ExceptionRoutines.traceCauseException(anException);
305 PrintWriter writer = aResponse.getWriter();
306 writer.println("<html><head><title>FATAL Error</title><body>");
307 writer.println("<h1>" + rootException.toString()+"</h1>");
308 writer.println("<code>");
309 rootException.printStackTrace(writer);
310 writer.println("</code>");
311 writer.println("</body></html>");
314 catch (Throwable t) {
321 private void _sendLoginPage(HttpServletResponse aResponse, HttpServletRequest aRequest) {
322 String loginTemplate = configuration.getString("Mir.LoginTemplate");
325 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
327 responseData.put("defaultlanguage", getDefaultLanguage(aRequest));
328 responseData.put("languages", getLoginLanguages());
330 ServletHelper.generateResponse(aResponse.getWriter(), responseData, loginTemplate);
332 catch (Throwable e) {
333 handleError(aRequest, aResponse, e);
337 public String getServletInfo() {
338 return "Mir " + configuration.getString("Mir.Version");
341 private class SessionTracker implements HttpSessionBindingListener {
345 public SessionTracker(String aUserName, String anId) {
350 public void valueBound(HttpSessionBindingEvent anEvent) {
351 MirGlobal.registerLogin(name, id);
354 public void valueUnbound(HttpSessionBindingEvent anEvent) {
355 MirGlobal.registerLogout(name, id);