KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > admin > audit > ManageLogs


1 // $Id: ManageLogs.java 9366 2005-06-06 14:41:58Z pvollenweider $
2
//
3
// ____.
4
// __/\ ______| |__/\. _______
5
// __ .____| | \ | +----+ \
6
// _______| /--| | | - \ _ | : - \_________
7
// \\______: :---| : : | : | \________>
8
// |__\---\_____________:______: :____|____:_____\
9
// /_____|
10
//
11
// . . . i n j a h i a w e t r u s t . . .
12
//
13
//
14
// ManageLogs
15
//
16
// 01.04.2001 MJ added in jahia.
17
//
18
//
19

20 package org.jahia.admin.audit;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.servlet.http.HttpSession JavaDoc;
29
30 import org.jahia.bin.Jahia;
31 import org.jahia.bin.JahiaAdministration;
32 import org.jahia.data.JahiaData;
33 import org.jahia.data.events.JahiaEventListenerInterface;
34 import org.jahia.params.ParamBean;
35 import org.jahia.registries.JahiaListenersRegistry;
36 import org.jahia.registries.ServicesRegistry;
37 import org.jahia.resourcebundle.JahiaResourceBundle;
38 import org.jahia.services.audit.JahiaAuditLogManagerService;
39 import org.jahia.services.usermanager.JahiaUser;
40 import org.jahia.settings.SettingsBean;
41 import org.jahia.utils.properties.PropertiesManager;
42
43
44 /**
45  * desc: This class provides the business methods for audit
46  * log management, from the JahiaAdministration servlet.
47  *
48  * Copyright: Copyright (c) 2002
49  * Company: Jahia Ltd
50  *
51  * @author Mikhael Janson
52  * @version 1.0
53  */

54 public class ManageLogs
55 {
56     private static final String JavaDoc CLASS_NAME = JahiaAdministration.CLASS_NAME;
57     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
58     private static final String JavaDoc LOGGING_EVENT_LISTENER = "org.jahia.services.audit.LoggingEventListener";
59
60     private static JahiaAuditLogManagerService lMgr;
61     private static JahiaListenersRegistry lReg;
62
63
64
65     /**
66     * Default constructor.
67     * @author Alexandre Kraft
68     *
69     * @param request Servlet request.
70     * @param response Servlet response.
71     * @param session Servlet session for the current user.
72     */

73     public ManageLogs( HttpServletRequest JavaDoc request,
74                        HttpServletResponse JavaDoc response,
75                        HttpSession JavaDoc session )
76
77     throws Throwable JavaDoc
78     {
79         lReg = JahiaListenersRegistry.getInstance();
80
81         ServicesRegistry sReg = ServicesRegistry.getInstance();
82         if (sReg != null) lMgr = sReg.getJahiaAuditLogManagerService();
83
84         userRequestDispatcher( request, response, session );
85     } // end constructor
86

87
88
89     /**
90      * This method serves as a dispatcher for user requests.
91      * @author Alexandre Kraft
92      * @author Mikhael Janson
93      *
94      * @param req Servlet request.
95      * @param res Servlet response.
96      * @param sess Servlet session for the current user.
97      */

98     private void userRequestDispatcher( HttpServletRequest JavaDoc req,
99                                         HttpServletResponse JavaDoc res,
100                                         HttpSession JavaDoc sess )
101     throws IOException JavaDoc, ServletException JavaDoc
102     {
103
104         String JavaDoc op = req.getParameter("sub");
105
106         if(op.equals("settings")) displaySettings( req, res, sess );
107         else if(op.equals("view")) displayView( req, res, sess );
108         else if(op.equals("flushconfirm")) displayFlush( req, res, sess );
109         else if(op.equals("manage")) processSettings( req, res, sess );
110         else if(op.equals("flush")) processFlush( req, res, sess );
111         else if(op.equals("enable")) processEnable( req, res, sess );
112         else if(op.equals("disable")) processDisable( req, res, sess );
113
114     } // userRequestDispatcher
115

116
117
118     /**
119      * Display a UI for audit log management, using doRedirect().
120      *
121      * @author Mikhael Janson
122      * @param req the HttpServletRequest object
123      * @param res the HttpServletResponse object
124      * @param sess the HttpSession object
125      */

126     private void displaySettings( HttpServletRequest JavaDoc req,
127                                   HttpServletResponse JavaDoc res,
128                                   HttpSession JavaDoc sess )
129     throws IOException JavaDoc, ServletException JavaDoc
130     {
131
132         String JavaDoc jahiaDisplayMessage = (String JavaDoc) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage");
133         boolean logEnabled = true;
134         String JavaDoc maxLogs = "";
135
136         if(lReg != null) {
137
138             // get log config and set default values...
139
logEnabled = (lReg.getListener(LOGGING_EVENT_LISTENER) == null) ? false : true ;
140
141             if(logEnabled) {
142                 maxLogs = SettingsBean.readJahiaPropertiesFile().getProperty("jahiaMaxLogs");
143
144                 if(maxLogs == null) maxLogs = "0";
145             }
146
147             if(jahiaDisplayMessage == null) {
148                   JahiaData jData = (JahiaData) req.getAttribute("org.jahia.data.JahiaData");
149                   ParamBean jParams = null;
150                   if (jData != null) {
151                       jParams = jData.params();
152                   }
153                 jahiaDisplayMessage =
154                     JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
155                         jParams, jParams.getLocale());
156             }
157
158             // set request attributes...
159
req.setAttribute("logEnabled", new Boolean JavaDoc(logEnabled));
160             req.setAttribute("maxLogs", maxLogs);
161             req.setAttribute("jahiaDisplayMessage", jahiaDisplayMessage);
162
163             JahiaAdministration.doRedirect( req, res, sess, JSP_PATH + "manage_logs.jsp" );
164
165         } else {
166             // back to menu in case of problems with the Managers...
167
JahiaData jData = (JahiaData) req.getAttribute("org.jahia.data.JahiaData");
168           ParamBean jParams = null;
169           if (jData != null) {
170               jParams = jData.params();
171           }
172             jahiaDisplayMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.noListenersRegistryInstance.label",
173                                                jParams, jParams.getLocale());
174             sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage);
175             JahiaAdministration.displayMenu( req, res, sess );
176         }
177
178     } // end displaySettings
179

180
181
182     /**
183      * Display full audit log, using doRedirect().
184      *
185      * @author Mikhael Janson
186      * @param req the HttpServletRequest object
187      * @param res the HttpServletResponse object
188      * @param sess the HttpSession object
189      */

190     private void displayView( HttpServletRequest JavaDoc req,
191                               HttpServletResponse JavaDoc res,
192                               HttpSession JavaDoc sess )
193     throws IOException JavaDoc, ServletException JavaDoc
194     {
195         String JavaDoc jahiaDisplayMessage = (String JavaDoc) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage");
196
197         JahiaData jData = (JahiaData) req.getAttribute("org.jahia.data.JahiaData");
198         ParamBean jParams = null;
199         if (jData != null) {
200             jParams = jData.params();
201         }
202
203         if (lMgr !=null) {
204
205             // get logs
206
ArrayList JavaDoc logData = lMgr.getLog(jParams);
207
208             // set default values...
209
if(jahiaDisplayMessage == null) jahiaDisplayMessage =
210                 JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
211                     jParams, jParams.getLocale());
212
213             // set session and request attributes...
214
Boolean JavaDoc keeprecentlogs = (Boolean JavaDoc) sess.getAttribute("keeprecentlogs");
215             if( keeprecentlogs == null ) sess.setAttribute("keeprecentlogs", new Boolean JavaDoc(true));
216
217             Integer JavaDoc maxlogsdays = (Integer JavaDoc) sess.getAttribute("maxlogsdays");
218             if( maxlogsdays == null ) sess.setAttribute("maxlogsdays", new Integer JavaDoc(60));
219
220             req.setAttribute("logData", logData);
221             sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage);
222             JahiaAdministration.doRedirect( req, res, sess, JSP_PATH + "logs.jsp" );
223
224         } else {
225
226             // back to menu in case of problems with the Managers...
227
jahiaDisplayMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.noAuditLogManagerInstance.label",
228                                                jParams, jParams.getLocale());
229             sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage);
230             JahiaAdministration.displayMenu( req, res, sess );
231         }
232
233     } // end displayView
234

235
236
237     /**
238      * Enable Audit Event logging
239      *
240      * @author Mikhael Janson
241      * @param req the HttpServletRequest object
242      * @param res the HttpServletResponse object
243      * @param sess the HttpSession object
244      */

245     private void processEnable( HttpServletRequest JavaDoc req,
246                                 HttpServletResponse JavaDoc res,
247                                 HttpSession JavaDoc sess )
248     throws IOException JavaDoc, ServletException JavaDoc
249     {
250         String JavaDoc jahiaDisplayMessage = (String JavaDoc) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage");
251
252
253             // set default values...
254
if(jahiaDisplayMessage == null) {
255                   JahiaData jData = (JahiaData) req.getAttribute("org.jahia.data.JahiaData");
256                   ParamBean jParams = null;
257                   if (jData != null) {
258                       jParams = jData.params();
259                   }
260                 jahiaDisplayMessage =
261                     JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
262                         jParams, jParams.getLocale());
263             }
264             if(lReg != null) {
265
266                 // get log config
267
JahiaData jData = (JahiaData) req.getAttribute("org.jahia.data.JahiaData");
268                 ParamBean jParams = null;
269                 if (jData != null) {
270                     jParams = jData.params();
271                 }
272                 JahiaEventListenerInterface theListener = (lReg.getListener(LOGGING_EVENT_LISTENER));
273                 if( lReg.addListener("LoggingEventListener", LOGGING_EVENT_LISTENER) ) {
274                   jahiaDisplayMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.auditLoggingEnabled.label",
275                                                      jParams, jParams.getLocale());
276                 } else {
277                   jahiaDisplayMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.auditLoggingNotEnabled.label",
278                                                      jParams, jParams.getLocale());
279                 }
280             }
281
282             // set request attributes...
283
sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage);
284             displaySettings( req, res, sess );
285
286     } // end processEnable
287

288
289
290     /**
291      * Disable Audit Event logging
292      *
293      * @author Mikhael Janson
294      * @param req the HttpServletRequest object
295      * @param res the HttpServletResponse object
296      * @param sess the HttpSession object
297      */

298     private void processDisable( HttpServletRequest JavaDoc req,
299                                  HttpServletResponse JavaDoc res,
300                                  HttpSession JavaDoc sess )
301     throws IOException JavaDoc, ServletException JavaDoc
302     {
303         String JavaDoc jahiaDisplayMessage = (String JavaDoc) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage");
304
305             // set default values...
306
if(jahiaDisplayMessage == null) {
307                   JahiaData jData = (JahiaData) req.getAttribute("org.jahia.data.JahiaData");
308                   ParamBean jParams = null;
309                   if (jData != null) {
310                       jParams = jData.params();
311                   }
312                 jahiaDisplayMessage =
313                     JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
314                         jParams, jParams.getLocale());
315             }
316
317             if(lReg != null) {
318
319                 // get log config
320
//JahiaEventListenerInterface theListener = (lReg.getListener(LOGGING_EVENT_LISTENER));
321
JahiaData jData = (JahiaData) req.getAttribute("org.jahia.data.JahiaData");
322                 ParamBean jParams = null;
323                 if (jData != null) {
324                     jParams = jData.params();
325                 }
326                 if( lReg.removeListener("LoggingEventListener") ) {
327                   jahiaDisplayMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.auditLoggingDisabled.label",
328                                                      jParams, jParams.getLocale());
329                 } else {
330                   jahiaDisplayMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.auditLoggingNotDisabled.label",
331                                                      jParams, jParams.getLocale());
332                 }
333             }
334
335             // set request attributes...
336
sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage);
337             displaySettings( req, res, sess );
338
339     } // end processDisable
340

341
342
343     /**
344      * Update the maxLog property...
345      *
346      * @author Mikhael Janson
347      * @param req the HttpServletRequest object
348      * @param res the HttpServletResponse object
349      * @param sess the HttpSession object
350      */

351     private void processSettings( HttpServletRequest JavaDoc req,
352                                   HttpServletResponse JavaDoc res,
353                                   HttpSession JavaDoc sess )
354     throws IOException JavaDoc, ServletException JavaDoc
355     {
356         String JavaDoc jahiaDisplayMessage = (String JavaDoc) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage");
357
358             // get request parameters...
359
String JavaDoc maxLogs = req.getParameter("maxlogs");
360             if (maxLogs == null) maxLogs = "500";
361
362             // make sure we've got a valid number
363
int maxLogsInt;
364             JahiaData jData = (JahiaData) req.getAttribute("org.jahia.data.JahiaData");
365             ParamBean jParams = null;
366             if (jData != null) {
367                 jParams = jData.params();
368             }
369             try {
370                 maxLogsInt = Integer.parseInt(maxLogs.trim());
371
372                 // update the config property...
373
PropertiesManager propertiesManager = new PropertiesManager( Jahia.getJahiaPropertiesFileName() );
374                 propertiesManager.setProperty( "jahiaMaxLogs", Integer.toString(maxLogsInt) );
375                 propertiesManager.storeProperties();
376
377                 // if needed, delete superfluous log entries
378

379                 if (lMgr !=null) {
380
381                     int result = lMgr.enforceMaxLogs(maxLogsInt);
382                     jahiaDisplayMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.logEntriesDeleted.label",
383                                                        jParams, jParams.getLocale());
384                     jahiaDisplayMessage = result + " " + jahiaDisplayMessage;
385
386                 } else {
387
388                   jahiaDisplayMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.noAuditLogManagerInstance.label",
389                                                      jParams, jParams.getLocale());
390                   sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage);
391                     JahiaAdministration.displayMenu( req, res, sess ); // back to menu in case of problems with the Managers...
392
}
393             }
394             catch(NumberFormatException JavaDoc nfe) {
395                 jahiaDisplayMessage = "Invalid value entered for maximum log size";
396             }
397
398             // set request attributes...
399
sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage);
400             displaySettings( req, res, sess );
401
402     } // end processSettings
403

404
405
406     /**
407      * Display a confirmation request for flushing all audit log entries, using doRedirect().
408      *
409      * @author Mikhael Janson
410      * @param req the HttpServletRequest object
411      * @param res the HttpServletResponse object
412      * @param sess the HttpSession object
413      */

414     private void displayFlush( HttpServletRequest JavaDoc req,
415                                HttpServletResponse JavaDoc res,
416                                HttpSession JavaDoc sess )
417     throws IOException JavaDoc, ServletException JavaDoc
418     {
419
420         String JavaDoc jahiaDisplayMessage = (String JavaDoc) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage");
421         String JavaDoc keeprecent = (String JavaDoc) req.getParameter("keeprecent");
422         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
423         String JavaDoc jsp_template = JSP_PATH + "logs_flushconfirm.jsp";
424
425             // set request attributes...
426
Boolean JavaDoc keeprecentlogs = new Boolean JavaDoc(keeprecent);
427             sess.setAttribute("keeprecentlogs", keeprecentlogs );
428             JahiaData jData = (JahiaData) req.getAttribute("org.jahia.data.JahiaData");
429             ParamBean jParams = null;
430             if (jData != null) {
431                 jParams = jData.params();
432             }
433             if( !keeprecentlogs.booleanValue() ) {
434                 req.setAttribute("engineMessage", "");
435                 JahiaAdministration.doRedirect( req, res, sess, jsp_template );
436
437             } else {
438                 try {
439                     String JavaDoc maxLogsDaysStr = (String JavaDoc) req.getParameter("maxlogsdays");
440                     Integer JavaDoc maxlogsdays = new Integer JavaDoc(maxLogsDaysStr.trim());
441
442                     if(maxlogsdays.intValue() < 1) {
443                       String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.numberDaysInvalid.label",
444                                                          jParams, jParams.getLocale());
445                       req.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg);
446                         sess.setAttribute("maxlogsdays", null);
447                         displayView( req, res, sess );
448                     } else {
449                         sess.setAttribute("maxlogsdays", maxlogsdays);
450                         String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.engineMessage.olderThan.label",
451                                                            jParams, jParams.getLocale());
452                         buf.append(" " + dspMsg + " ");
453                         buf.append(maxlogsdays.toString());
454                         dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.engineMessage.days.label",
455                                                            jParams, jParams.getLocale());
456                         buf.append(" " + dspMsg);
457                         sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage);
458                         req.setAttribute("engineMessage", buf.toString());
459                         JahiaAdministration.doRedirect( req, res, sess, jsp_template );
460                     }
461                 }
462                 catch (ClassCastException JavaDoc cce) {
463                   String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestNotFulfilled.label",
464                                                      jParams, jParams.getLocale());
465                   req.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg);
466                     displayView( req, res, sess );
467                 }
468                 catch (NumberFormatException JavaDoc nfe) {
469                   String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.numberDaysInvalid.label",
470                                                      jParams, jParams.getLocale());
471                   req.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg);
472                     displayView( req, res, sess );
473                 }
474             }
475     } // end displayFlush
476

477
478
479     /**
480      * Remove all audit log entries.
481      *
482      * @author Mikhael Janson
483      * @param req the HttpServletRequest object
484      * @param res the HttpServletResponse object
485      * @param sess the HttpSession object
486      */

487     private void processFlush( HttpServletRequest JavaDoc req,
488                                HttpServletResponse JavaDoc res,
489                                HttpSession JavaDoc sess )
490     throws IOException JavaDoc, ServletException JavaDoc
491     {
492
493         String JavaDoc jahiaDisplayMessage = (String JavaDoc) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage");
494         Boolean JavaDoc keeprecentlogs = (Boolean JavaDoc) sess.getAttribute("keeprecentlogs");
495         Integer JavaDoc maxlogsdays = (keeprecentlogs.booleanValue()) ? (Integer JavaDoc) sess.getAttribute("maxlogsdays") : null ;
496
497         if (lMgr !=null) {
498
499             // flush logs
500
lMgr.flushLogs( ((JahiaUser) sess.getAttribute(ParamBean.SESSION_USER)), maxlogsdays );
501         }
502
503         // set request attributes...
504
sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage);
505         displayView( req, res, sess );
506
507     } // end processFlush
508

509
510 } // end class ManageLogs
511
Popular Tags