-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMir.java
More file actions
executable file
·376 lines (308 loc) · 12.7 KB
/
Copy pathMir.java
File metadata and controls
executable file
·376 lines (308 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright (C) 2001, 2002 The Mir-coders group
*
* This file is part of Mir.
*
* Mir is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Mir is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Mir; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, The Mir-coders gives permission to link
* the code of this program with any library licensed under the Apache Software License,
* The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
* (or with modified versions of the above that use the same license as the above),
* and distribute linked combinations including the two. You must obey the
* GNU General Public License in all respects for all of the code used other than
* the above mentioned libraries. If you modify this file, you may extend this
* exception to your version of the file, but you are not obligated to do so.
* If you do not wish to do so, delete this exception statement from your version.
*/
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Vector;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import mir.config.MirPropertiesConfiguration;
import mir.servlet.AbstractServlet;
import mir.servlet.ServletModule;
import mir.servlet.ServletModuleDispatch;
import mir.servlet.ServletModuleExc;
import mir.servlet.ServletModuleUserExc;
import mir.util.ExceptionFunctions;
import mir.util.StringRoutines;
import mircoders.entity.EntityUsers;
import mircoders.global.MirGlobal;
import mircoders.module.ModuleMessage;
import mircoders.module.ModuleUsers;
import mircoders.servlet.ServletHelper;
import mircoders.storage.DatabaseUsers;
import org.apache.struts.util.MessageResources;
/**
* Mir.java - main servlet, that dispatches to servletmodules
*
* @author $Author: rk $
* @version $Id: Mir.java,v 1.49.2.8 2003/10/23 14:55:26 rk Exp $
*
*/
public class Mir extends AbstractServlet {
private static ModuleUsers usersModule = null;
private static ModuleMessage messageModule = null;
private final static Map servletModuleInstanceHash = new HashMap();
private static Locale fallbackLocale = null;
private static List loginLanguages = null;
protected List getLoginLanguages() throws ServletException {
synchronized (Mir.class) {
try {
if (loginLanguages == null) {
MessageResources messageResources =
MessageResources.getMessageResources("bundles.adminlocal");
MessageResources messageResources2 =
MessageResources.getMessageResources("bundles.admin");
List languages =
StringRoutines.splitString(MirGlobal.config().getString("Mir.Login.Languages", "en"), ";");
loginLanguages = new Vector();
Iterator i = languages.iterator();
while (i.hasNext()) {
String code = (String) i.next();
Locale locale = new Locale(code, "");
String name = messageResources.getMessage(locale, "languagename");
if (name == null) {
name = messageResources2.getMessage(locale, "languagename");
}
if (name == null) {
name = code;
}
Map record = new HashMap();
record.put("name", name);
record.put("code", code);
loginLanguages.add(record);
}
}
return loginLanguages;
}
catch (Throwable t) {
throw new ServletException(t.getMessage());
}
}
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
usersModule = new ModuleUsers(DatabaseUsers.getInstance());
}
protected String getDefaultLanguage(HttpServletRequest aRequest) {
String defaultlanguage =
MirGlobal.config().getString("Mir.Login.DefaultLanguage", "");
if (defaultlanguage.length() == 0) {
Locale locale = aRequest.getLocale();
defaultlanguage = locale.getLanguage();
}
return defaultlanguage;
}
protected synchronized Locale getFallbackLocale() throws ServletException {
try {
if (fallbackLocale == null) {
fallbackLocale = new Locale(MirPropertiesConfiguration.instance().getString("Mir.Admin.FallbackLanguage", "en"), "");
}
}
catch (Throwable t) {
throw new ServletException(t.getMessage());
}
return fallbackLocale;
}
public EntityUsers checkCredentials(HttpServletRequest aRequest) throws ServletException {
try {
EntityUsers user = ServletHelper.getUser(aRequest);
String username = aRequest.getParameter("login");
String password = aRequest.getParameter("password");
if (username != null && password != null) {
user = usersModule.getUserForLogin(username, password);
if (user!=null) {
ServletHelper.setUser(aRequest, user);
aRequest.getSession().setAttribute("sessiontracker", new SessionTracker(username));
}
}
return user;
}
catch (Throwable t) {
t.printStackTrace();
throw new ServletException(t.toString());
}
}
public void process(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException, UnavailableException {
try {
long startTime = System.currentTimeMillis();
long sessionConnectTime = 0;
HttpSession session = aRequest.getSession(true);
setNoCaching(aResponse);
Locale locale = new Locale(getDefaultLanguage(aRequest), "");
aResponse.setContentType("text/html; charset=" +
configuration.
getString("Mir.DefaultHTMLCharset", "UTF-8"));
EntityUsers userEntity = checkCredentials(aRequest);
if (userEntity == null) {
String queryString = aRequest.getQueryString();
if ( (queryString != null) && (queryString.length() != 0) && session.getAttribute("login.target") == null &&
(aRequest.getParameter("module")==null ||
(!aRequest.getParameter("module").equals("login") && !aRequest.getParameter("module").equals("logout")))) {
session.setAttribute("login.target", queryString);
}
_sendLoginPage(aResponse, aRequest);
}
else {
String moduleName = aRequest.getParameter("module");
checkLanguage(session, aRequest);
if ( ( (moduleName == null) || moduleName.equals(""))) {
moduleName="Admin";
}
if (moduleName.equals("login")) {
String target = (String) session.getAttribute("login.target");
if (target != null) {
ServletHelper.redirect(aResponse, target);
}
else {
ServletHelper.redirect(aResponse, "");
}
}
else if (moduleName.equals("logout")) {
logger.info(userEntity.getValue("login") + " has logged out");
session.invalidate();
_sendLoginPage(aResponse, aRequest);
return;
}
else {
try {
ServletModule servletModule = getServletModuleForName(moduleName);
ServletModuleDispatch.dispatch(servletModule, aRequest, aResponse);
sessionConnectTime = System.currentTimeMillis() - startTime;
logger.info("EXECTIME (" + moduleName + "): " + sessionConnectTime + " ms");
}
catch (Throwable e) {
Throwable cause = ExceptionFunctions.traceCauseException(e);
if (cause instanceof ServletModuleUserExc)
handleUserError(aRequest, aResponse, (ServletModuleUserExc) cause);
else
handleError(aRequest, aResponse, cause);
}
if (aRequest.getParameter("killsession")!=null)
aRequest.getSession().invalidate();
}
}
}
catch (Throwable t) {
t.printStackTrace();
throw new ServletException(t.toString());
}
}
/**
* caching routine to get a module for a module name
*
* @param moduleName the module name
* @return the requested module
* @throws ServletModuleExc
*/
private static ServletModule getServletModuleForName(String moduleName) throws ServletModuleExc {
// Instance in Map ?
if (!servletModuleInstanceHash.containsKey(moduleName)) {
// was not found in hash...
try {
Class theServletModuleClass = null;
try {
// first we try to get ServletModule from stern.che3.servlet
theServletModuleClass =
Class.forName("mircoders.servlet.ServletModule" + moduleName);
}
catch (ClassNotFoundException e) {
// on failure, we try to get it from lib-layer
theServletModuleClass =
Class.forName("mir.servlet.ServletModule" + moduleName);
}
Method m = theServletModuleClass.getMethod("getInstance", null);
ServletModule smod = (ServletModule) m.invoke(null, null);
// we put it into map for further reference
servletModuleInstanceHash.put(moduleName, smod);
return smod;
}
catch (Exception e) {
throw new ServletModuleExc("*** error resolving classname for " + moduleName + " -- " + e.getMessage());
}
}
else {
return (ServletModule) servletModuleInstanceHash.get(moduleName);
}
}
private void handleUserError(HttpServletRequest aRequest, HttpServletResponse aResponse, ServletModuleUserExc anException) {
try {
logger.info("user error: " + anException.getMessage());
Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
MessageResources messages = MessageResources.getMessageResources("bundles.admin");
responseData.put("errorstring", messages.getMessage(getLocale(aRequest), anException.getMessage(), anException.getParameters()));
responseData.put("date", new GregorianCalendar().getTime());
ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.UserErrorTemplate"));
}
catch (Throwable e) {
logger.error("Error handling user error" + e.toString());
}
}
private void handleError(HttpServletRequest aRequest, HttpServletResponse aResponse, Throwable anException) {
try {
logger.error("error: " + anException);
Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
responseData.put("errorstring", anException.toString());
responseData.put("date", new GregorianCalendar().getTime());
ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.ErrorTemplate"));
}
catch (Throwable e) {
logger.error("Error handling error: " + e.toString());
}
}
// Redirect-methods
private void _sendLoginPage(HttpServletResponse aResponse, HttpServletRequest aRequest) {
String loginTemplate = configuration.getString("Mir.LoginTemplate");
try {
Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
responseData.put("defaultlanguage", getDefaultLanguage(aRequest));
responseData.put("languages", getLoginLanguages());
ServletHelper.generateResponse(aResponse.getWriter(), responseData, loginTemplate);
}
catch (Throwable e) {
handleError(aRequest, aResponse, e);
}
}
public String getServletInfo() {
return "Mir " + configuration.getString("Mir.Version");
}
private class SessionTracker implements HttpSessionBindingListener {
private String name;
public SessionTracker(String aUserName) {
name = aUserName;
}
public void valueBound(HttpSessionBindingEvent anEvent) {
MirGlobal.registerLogin(name);
}
public void valueUnbound(HttpSessionBindingEvent anEvent) {
MirGlobal.registerLogout(name);
}
}
}