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;
48 import mircoders.module.ModuleUsers;
49 import mircoders.global.MirGlobal;
50 import mircoders.servlet.ServletHelper;
51 import mircoders.entity.EntityUsers;
53 public class Mir extends AbstractServlet {
54 private static ModuleUsers usersModule = null;
55 private final static Map servletModules = new HashMap();
56 private static Locale fallbackLocale = null;
58 private static List loginLanguages = null;
60 private List getLoginLanguages() throws Failure {
61 synchronized (Mir.class) {
63 if (loginLanguages == null) {
65 StringRoutines.splitString(MirGlobal.config().getString("Mir.Login.Languages", "en"), ";");
67 loginLanguages = new ArrayList();
69 Iterator i = languages.iterator();
72 String code = (String) i.next();
75 MirGlobal.getBundleFactory().getBundle("etc/bundles/adminlocal", new String[] { code });
76 Bundle defaultBundle =
77 MirGlobal.getBundleFactory().getBundle("bundles/admin", new String[] { code });
79 String name = bundle.getValue("languagename", Collections.EMPTY_LIST);
82 name = defaultBundle.getValue("languagename", Collections.EMPTY_LIST);
89 Map record = new HashMap();
90 record.put("name", name);
91 record.put("code", code);
92 loginLanguages.add(record);
96 return loginLanguages;
99 throw new Failure("Error while retrieving the available login languages", t);
104 public void init(ServletConfig config) throws ServletException {
107 usersModule = new ModuleUsers();
110 protected String getDefaultLanguage(HttpServletRequest aRequest) {
111 String defaultlanguage =
112 MirGlobal.config().getString("Mir.Login.DefaultLanguage", "");
114 if (defaultlanguage.length() == 0) {
115 Locale locale = aRequest.getLocale();
116 defaultlanguage = locale.getLanguage();
119 return defaultlanguage;
122 protected synchronized Locale getFallbackLocale() throws ServletException {
124 if (fallbackLocale == null) {
125 fallbackLocale = new Locale(MirPropertiesConfiguration.instance().getString("Mir.Admin.FallbackLanguage", "en"), "");
128 catch (Throwable t) {
129 throw new ServletException(t.getMessage());
132 return fallbackLocale;
135 public EntityUsers checkCredentials(HttpServletRequest aRequest) throws ServletException {
137 EntityUsers user = ServletHelper.getUser(aRequest);
139 String username = aRequest.getParameter("login");
140 String password = aRequest.getParameter("password");
142 if (username != null && password != null) {
143 user = usersModule.getUserForLogin(username, password);
146 ServletHelper.setUser(aRequest, user);
147 usersModule.recordLogin(user);
148 aRequest.getSession().setAttribute("sessiontracker", new SessionTracker(username, user.getId()));
154 catch (Throwable t) {
155 throw new ServletException(t.getMessage());
159 public void process(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException, UnavailableException {
161 long startTime = System.currentTimeMillis();
162 long sessionConnectTime = 0;
164 HttpSession session = aRequest.getSession(true);
165 setNoCaching(aResponse);
166 aResponse.setContentType("text/html; charset=" +
168 getString("Mir.DefaultHTMLCharset", "UTF-8"));
170 EntityUsers userEntity = checkCredentials(aRequest);
172 if (userEntity == null) {
173 String queryString = aRequest.getQueryString();
175 if ( (queryString != null) && (queryString.length() != 0) && session.getAttribute("login.target") == null &&
176 (aRequest.getParameter("module")==null ||
177 (!aRequest.getParameter("module").equals("login") && !aRequest.getParameter("module").equals("logout")))) {
178 session.setAttribute("login.target", queryString);
181 _sendLoginPage(aResponse, aRequest);
184 String moduleName = aRequest.getParameter("module");
185 checkLanguage(session, aRequest);
187 if ( ( (moduleName == null) || moduleName.equals(""))) {
191 if (moduleName.equals("login")) {
192 String target = (String) session.getAttribute("login.target");
194 if (target != null) {
195 ServletHelper.redirect(aResponse, target);
198 ServletHelper.redirect(aResponse, "");
201 else if (moduleName.equals("logout")) {
202 logger.info(userEntity.getFieldValue("login") + " has logged out");
203 session.invalidate();
204 _sendLoginPage(aResponse, aRequest);
209 AdminServletModule servletModule = getServletModuleForName(moduleName);
210 servletModule.handleRequest(aRequest, aResponse);
212 sessionConnectTime = System.currentTimeMillis() - startTime;
213 logger.info("EXECTIME (" + moduleName + "): " + sessionConnectTime + " ms");
215 catch (Throwable e) {
216 Throwable cause = ExceptionRoutines.traceCauseException(e);
218 if (cause instanceof ServletModuleUserExc)
219 handleUserError(aRequest, aResponse, (ServletModuleUserExc) cause);
221 handleError(aRequest, aResponse, cause);
224 if (aRequest.getParameter("killsession")!=null)
225 aRequest.getSession().invalidate();
229 catch (Throwable t) {
230 throw new ServletException(t.toString());
235 * caching routine to get a module for a module name
237 * @param aModuleName the module name
238 * @return the requested module
239 * @throws ServletModuleExc
241 private static AdminServletModule getServletModuleForName(String aModuleName) throws ServletModuleExc {
242 return ServletHelper.getServletModule(aModuleName);
245 private void handleUserError(HttpServletRequest aRequest, HttpServletResponse aResponse, ServletModuleUserExc anException) {
247 logger.info("user error: " + anException.getMessage());
249 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
252 MirGlobal.getBundleFactory().getBundle("etc/bundles/adminlocal", new
253 String[] { getLocale(aRequest).getLanguage() });
254 Bundle defaultBundle =
255 MirGlobal.getBundleFactory().getBundle("bundles/admin", new
256 String[] { getLocale(aRequest).getLanguage() });
258 bundle.getValue(anException.getMessage(), Arrays.asList(anException.getParameters()));
262 defaultBundle.getValue(anException.getMessage(), Arrays.asList(anException.getParameters()));
265 responseData.put("errorstring", message);
266 responseData.put("date", new GregorianCalendar().getTime());
268 ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.UserErrorTemplate"));
270 catch (Throwable e) {
271 logger.error("Error handling user error" + e.toString());
275 private void handleError(HttpServletRequest aRequest, HttpServletResponse aResponse, Throwable anException) {
277 logger.error("error: " + anException);
279 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
281 responseData.put("errorstring", anException.toString());
282 StringWriter writer = new StringWriter();
283 anException.printStackTrace(new PrintWriter(writer));
284 responseData.put("stacktrace", writer.toString());
285 responseData.put("date", new GregorianCalendar().getTime());
287 ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.ErrorTemplate"));
289 catch (Throwable e) {
290 logger.error("Error handling error: " + e.toString());
293 Throwable rootException = ExceptionRoutines.traceCauseException(anException);
295 PrintWriter writer = aResponse.getWriter();
296 writer.println("<html><head><title>FATAL Error</title><body>");
297 writer.println("<h1>" + rootException.toString()+"</h1>");
298 writer.println("<code>");
299 rootException.printStackTrace(writer);
300 writer.println("</code>");
301 writer.println("</body></html>");
304 catch (Throwable t) {
311 private void _sendLoginPage(HttpServletResponse aResponse, HttpServletRequest aRequest) {
312 String loginTemplate = configuration.getString("Mir.LoginTemplate");
315 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
317 responseData.put("defaultlanguage", getDefaultLanguage(aRequest));
318 responseData.put("languages", getLoginLanguages());
320 ServletHelper.generateResponse(aResponse.getWriter(), responseData, loginTemplate);
322 catch (Throwable e) {
323 handleError(aRequest, aResponse, e);
327 public String getServletInfo() {
328 return "Mir " + configuration.getString("Mir.Version");
331 private class SessionTracker implements HttpSessionBindingListener {
335 public SessionTracker(String aUserName, String anId) {
340 public void valueBound(HttpSessionBindingEvent anEvent) {
341 MirGlobal.registerLogin(name, id);
344 public void valueUnbound(HttpSessionBindingEvent anEvent) {
345 MirGlobal.registerLogout(name, id);