%-- - $Revision$ - $Date$ - - Copyright (C) 2004-2008 Jive Software. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --%> <%@ page import="java.io.*, org.jivesoftware.util.*, java.text.*, org.jivesoftware.util.JiveGlobals, org.jivesoftware.openfire.user.*, java.util.*" %> <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %> <%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %> <% admin.init(request, response, session, application, out ); %> <%! static final String NONE = LocaleUtils.getLocalizedString("global.none"); static final String ERROR = "error"; static final String INFO = "info"; static final String WARN = "warn"; static final String DEBUG = "debug"; static final String DEFAULT = ERROR; static final String ASCENDING = "asc"; static final String DESCENDING = "desc"; static final String[] LINES = {"50","100","250","500"}; static final String[] REFRESHES = {NONE,"10","30","60","90"}; private static HashMap parseCookie(Cookie cookie) { if (cookie == null || cookie.getValue() == null) { return new HashMap(); } StringTokenizer tokenizer = new StringTokenizer(cookie.getValue(),"&"); HashMap valueMap = new HashMap(); while (tokenizer.hasMoreTokens()) { String tok = tokenizer.nextToken(); int pos = tok.indexOf("="); if (pos > 0) { String name = tok.substring(0,pos); String value = tok.substring(pos+1,tok.length()); valueMap.put(name,value); } } return valueMap; } private static void saveCookie(HttpServletResponse response, HashMap cookie) { StringBuffer buf = new StringBuffer(); for (Iterator iter=cookie.keySet().iterator(); iter.hasNext();) { String name = (String)iter.next(); String value = (String)cookie.get(name); buf.append(name).append("=").append(value); if (iter.hasNext()) { buf.append("&"); } } Cookie newCookie = new Cookie("jiveforums.admin.logviewer",buf.toString()); newCookie.setPath("/"); newCookie.setMaxAge(60*60*24*30); // one month response.addCookie(newCookie); } private static HashMap getLogUpdate(HttpServletRequest request, HttpServletResponse response, File logDir) { // Get the cookie associated with the log files HashMap cookie = parseCookie(CookieUtils.getCookie(request,"jiveforums.admin.logviewer")); String[] logs = {"error", "info", "warn", "debug"}; HashMap newCookie = new HashMap(); HashMap updates = new HashMap(); for (String log : logs) { // Check for the value in the cookie: String key = log + ".size"; long savedSize = 0L; if (cookie.containsKey(key)) { try { savedSize = Long.parseLong((String) cookie.get(key)); } catch (NumberFormatException nfe) { } } // Update the size in the Map: File logFile = new File(logDir, log + ".log"); long currentSize = logFile.length(); newCookie.put(key, "" + currentSize); if (currentSize != savedSize) { updates.put(log, "true"); } } saveCookie(response, newCookie); return updates; } %> <% // Get parameters String log = ParamUtils.getParameter(request, "log"); String numLinesParam = ParamUtils.getParameter(request,"lines"); int numLines = ParamUtils.getIntParameter(request,"lines",50); int refresh = ParamUtils.getIntParameter(request,"refresh",10); String refreshParam = ParamUtils.getParameter(request,"refresh"); String mode = ParamUtils.getParameter(request,"mode"); boolean clearLog = ParamUtils.getBooleanParameter(request,"clearLog"); boolean markLog = ParamUtils.getBooleanParameter(request,"markLog"); boolean saveLog = ParamUtils.getBooleanParameter(request,"saveLog"); boolean emailLog = ParamUtils.getBooleanParameter(request,"emailLog"); boolean debugEnabled = ParamUtils.getBooleanParameter(request,"debugEnabled"); boolean wasDebugEnabled = ParamUtils.getBooleanParameter(request,"wasDebugEnabled"); // Enable/disable debugging if (request.getParameter("wasDebugEnabled") != null && wasDebugEnabled != debugEnabled) { Log.setDebugEnabled(debugEnabled); // Log the event admin.logEvent((debugEnabled ? "enabled" : "disabled")+" debug logging", null); response.sendRedirect("logviewer.jsp?log=debug"); return; } // Santize variables to prevent vulnerabilities if (log != null) { log = StringUtils.escapeHTMLTags(log); } debugEnabled = Log.isDebugEnabled(); User pageUser = admin.getUser(); if (clearLog && log != null) { if ("error".equals(log)) { Log.rotateErrorLogFile(); } else if ("warn".equals(log)) { Log.rotateWarnLogFile(); } else if ("info".equals(log)) { Log.rotateInfoLogFile(); } else if ("debug".equals(log)) { Log.rotateDebugLogFile(); } response.sendRedirect("logviewer.jsp?log=" + log); return; } else if (markLog && log != null) { if ("error".equals(log)) { Log.markErrorLogFile(pageUser.getUsername()); } else if ("warn".equals(log)) { Log.markWarnLogFile(pageUser.getUsername()); } else if ("info".equals(log)) { Log.markInfoLogFile(pageUser.getUsername()); } else if ("debug".equals(log)) { Log.markDebugLogFile(pageUser.getUsername()); } response.sendRedirect("logviewer.jsp?log=" + log); return; } else if (saveLog && log != null) { saveLog = false; response.sendRedirect(request.getContextPath() + "/servlet/JiveServlet/?log=" + log); return; } else if (emailLog && log != null) { response.sendRedirect("emaillog.jsp?log=" + log); return; } // Set defaults if (log == null) { log = DEFAULT; } if (mode == null) { mode = ASCENDING; } if (numLinesParam == null) { numLinesParam = "50"; } // Other vars File logDir = new File(Log.getLogDirectory()); String filename = log + ".log"; File logFile = new File(logDir, filename); // Determine if any of the log files contents have been updated: HashMap newlogs = getLogUpdate(request, response, logDir); %> <% if (refreshParam != null && !NONE.equals(refreshParam)) { %> <% } %> " width="1%"> <%= ((newlogs.containsKey("error"))?"*":"") %> " width="1%"> <%= ((newlogs.containsKey("warn"))?"*":"") %> " width="1%"> <%= ((newlogs.containsKey("info"))?"*":"") %> " width="1%"> <%= ((newlogs.containsKey("debug"))?"*":"") %> <% ByteFormat byteFormatter = new ByteFormat(); Date lastMod = new Date(logFile.lastModified()); DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); %> <%= logFile.getName() %> (<%= byteFormatter.format(logFile.length()) %>) onclick="this.form.submit();" id="rb01" > onclick="this.form.submit();" id="rb02" > <%= dateFormatter.format(lastMod) %> <% for (String aLINES : LINES) { String selected = (aLINES.equals(numLinesParam)) ? " selected" : ""; %> ><%= aLINES %> <% } %> > ')) {setLog('clearLog'); document.logViewer.submit(); return true;} else { return false; }">"> ')) {setLog('clearLog'); document.logViewer.submit(); return true;} else { return false; }" > "> : <% for (String aREFRESHES : REFRESHES) { String selected = aREFRESHES.equals(refreshParam) ? " selected" : ""; %> ><%= aREFRESHES %> <% } %> () <% if ("debug".equals(log)) { %> : id="de01"> id="de02"> Disabled "> <% } %> : <%= JiveGlobals.getHomeDirectory() %><%= File.separator %>logs " frameborder="0" height="400" width="100%" marginheight="0" marginwidth="0" scrolling="auto">