1 20 package org.jahia.admin.audit; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.http.HttpSession ; 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 54 public class ManageLogs 55 { 56 private static final String CLASS_NAME = JahiaAdministration.CLASS_NAME; 57 private static final String JSP_PATH = JahiaAdministration.JSP_PATH; 58 private static final String LOGGING_EVENT_LISTENER = "org.jahia.services.audit.LoggingEventListener"; 59 60 private static JahiaAuditLogManagerService lMgr; 61 private static JahiaListenersRegistry lReg; 62 63 64 65 73 public ManageLogs( HttpServletRequest request, 74 HttpServletResponse response, 75 HttpSession session ) 76 77 throws Throwable 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 } 87 88 89 98 private void userRequestDispatcher( HttpServletRequest req, 99 HttpServletResponse res, 100 HttpSession sess ) 101 throws IOException , ServletException 102 { 103 104 String 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 } 116 117 118 126 private void displaySettings( HttpServletRequest req, 127 HttpServletResponse res, 128 HttpSession sess ) 129 throws IOException , ServletException 130 { 131 132 String jahiaDisplayMessage = (String ) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage"); 133 boolean logEnabled = true; 134 String maxLogs = ""; 135 136 if(lReg != null) { 137 138 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 req.setAttribute("logEnabled", new Boolean (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 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 } 180 181 182 190 private void displayView( HttpServletRequest req, 191 HttpServletResponse res, 192 HttpSession sess ) 193 throws IOException , ServletException 194 { 195 String jahiaDisplayMessage = (String ) 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 ArrayList logData = lMgr.getLog(jParams); 207 208 if(jahiaDisplayMessage == null) jahiaDisplayMessage = 210 JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright", 211 jParams, jParams.getLocale()); 212 213 Boolean keeprecentlogs = (Boolean ) sess.getAttribute("keeprecentlogs"); 215 if( keeprecentlogs == null ) sess.setAttribute("keeprecentlogs", new Boolean (true)); 216 217 Integer maxlogsdays = (Integer ) sess.getAttribute("maxlogsdays"); 218 if( maxlogsdays == null ) sess.setAttribute("maxlogsdays", new Integer (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 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 } 235 236 237 245 private void processEnable( HttpServletRequest req, 246 HttpServletResponse res, 247 HttpSession sess ) 248 throws IOException , ServletException 249 { 250 String jahiaDisplayMessage = (String ) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage"); 251 252 253 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 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 sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage); 284 displaySettings( req, res, sess ); 285 286 } 288 289 290 298 private void processDisable( HttpServletRequest req, 299 HttpServletResponse res, 300 HttpSession sess ) 301 throws IOException , ServletException 302 { 303 String jahiaDisplayMessage = (String ) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage"); 304 305 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 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 sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage); 337 displaySettings( req, res, sess ); 338 339 } 341 342 343 351 private void processSettings( HttpServletRequest req, 352 HttpServletResponse res, 353 HttpSession sess ) 354 throws IOException , ServletException 355 { 356 String jahiaDisplayMessage = (String ) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage"); 357 358 String maxLogs = req.getParameter("maxlogs"); 360 if (maxLogs == null) maxLogs = "500"; 361 362 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 PropertiesManager propertiesManager = new PropertiesManager( Jahia.getJahiaPropertiesFileName() ); 374 propertiesManager.setProperty( "jahiaMaxLogs", Integer.toString(maxLogsInt) ); 375 propertiesManager.storeProperties(); 376 377 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 ); } 393 } 394 catch(NumberFormatException nfe) { 395 jahiaDisplayMessage = "Invalid value entered for maximum log size"; 396 } 397 398 sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage); 400 displaySettings( req, res, sess ); 401 402 } 404 405 406 414 private void displayFlush( HttpServletRequest req, 415 HttpServletResponse res, 416 HttpSession sess ) 417 throws IOException , ServletException 418 { 419 420 String jahiaDisplayMessage = (String ) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage"); 421 String keeprecent = (String ) req.getParameter("keeprecent"); 422 StringBuffer buf = new StringBuffer (); 423 String jsp_template = JSP_PATH + "logs_flushconfirm.jsp"; 424 425 Boolean keeprecentlogs = new Boolean (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 maxLogsDaysStr = (String ) req.getParameter("maxlogsdays"); 440 Integer maxlogsdays = new Integer (maxLogsDaysStr.trim()); 441 442 if(maxlogsdays.intValue() < 1) { 443 String 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 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 cce) { 463 String 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 nfe) { 469 String 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 } 477 478 479 487 private void processFlush( HttpServletRequest req, 488 HttpServletResponse res, 489 HttpSession sess ) 490 throws IOException , ServletException 491 { 492 493 String jahiaDisplayMessage = (String ) sess.getAttribute(CLASS_NAME + "jahiaDisplayMessage"); 494 Boolean keeprecentlogs = (Boolean ) sess.getAttribute("keeprecentlogs"); 495 Integer maxlogsdays = (keeprecentlogs.booleanValue()) ? (Integer ) sess.getAttribute("maxlogsdays") : null ; 496 497 if (lMgr !=null) { 498 499 lMgr.flushLogs( ((JahiaUser) sess.getAttribute(ParamBean.SESSION_USER)), maxlogsdays ); 501 } 502 503 sess.setAttribute(CLASS_NAME + "jahiaDisplayMessage", jahiaDisplayMessage); 505 displayView( req, res, sess ); 506 507 } 509 510 } | Popular Tags |