001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.console.logmanager;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.geronimo.console.BasePortlet;
021: import org.apache.geronimo.console.util.PortletManager;
022: import org.apache.geronimo.gbean.AbstractName;
023: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
024: import org.apache.geronimo.management.geronimo.WebAccessLog;
025: import org.apache.geronimo.management.geronimo.WebContainer;
026: import org.apache.geronimo.management.geronimo.WebManager;
027:
028: import javax.portlet.ActionRequest;
029: import javax.portlet.ActionResponse;
030: import javax.portlet.PortletConfig;
031: import javax.portlet.PortletContext;
032: import javax.portlet.PortletException;
033: import javax.portlet.PortletRequestDispatcher;
034: import javax.portlet.PortletSession;
035: import javax.portlet.RenderRequest;
036: import javax.portlet.RenderResponse;
037: import javax.portlet.WindowState;
038: import java.io.IOException;
039: import java.io.Serializable;
040: import java.text.DateFormat;
041: import java.text.ParseException;
042: import java.text.SimpleDateFormat;
043: import java.util.Calendar;
044: import java.util.Date;
045: import java.util.LinkedHashMap;
046: import java.util.Map;
047:
048: /**
049: * @version $Rev: 575775 $ $Date: 2007-09-14 11:47:08 -0700 (Fri, 14 Sep 2007) $
050: */
051: public class WebAccessLogViewerPortlet extends BasePortlet {
052: private final static String CRITERIA_KEY = "org.apache.geronimo.console.web.log.CRITERIA";
053: private final static Log log = LogFactory
054: .getLog(WebAccessLogViewerPortlet.class);
055: private static final int DEFAULT_MAX_RESULTS = 10;
056:
057: protected PortletRequestDispatcher searchView;
058:
059: protected PortletRequestDispatcher helpView;
060:
061: protected void doHelp(RenderRequest renderRequest,
062: RenderResponse renderRespose) throws PortletException,
063: IOException {
064: helpView.include(renderRequest, renderRespose);
065: }
066:
067: protected void doView(RenderRequest renderRequest,
068: RenderResponse renderRespose) throws PortletException,
069: IOException {
070: if (WindowState.MINIMIZED
071: .equals(renderRequest.getWindowState())) {
072: return;
073: }
074:
075: WebManager[] managers = PortletManager.getCurrentServer(
076: renderRequest).getWebManagers();
077:
078: Criteria criteria = (Criteria) renderRequest.getPortletSession(
079: true).getAttribute(CRITERIA_KEY,
080: PortletSession.PORTLET_SCOPE);
081:
082: //todo: new
083: Map products = new LinkedHashMap();
084: String chosen = renderRequest.getParameter("selectedContainer");
085: if (chosen != null) { // Carry on to render the results with the right selection
086: renderRequest.setAttribute("selectedContainer", chosen);
087: }
088: WebAccessLog chosenLog = null;
089: if (managers != null) {
090: for (int i = 0; i < managers.length; i++) {
091: WebManager manager = managers[i];
092: AbstractName managerName = PortletManager.getNameFor(
093: renderRequest, manager);
094: WebContainer[] containers = (WebContainer[]) manager
095: .getContainers();
096: if (containers != null) {
097: for (int j = 0; j < containers.length; j++) {
098: WebContainer container = containers[j];
099: AbstractName containerName = PortletManager
100: .getNameFor(renderRequest, container);
101: String combined = managerName + "%"
102: + containerName;
103: if (containers.length == 1) {
104: products.put(manager.getProductName(),
105: combined);
106: } else {
107: products.put(manager.getProductName()
108: + " ("
109: + containerName.getName().get(
110: NameFactory.J2EE_NAME)
111: + ")", combined);
112: }
113: if (chosenLog == null) { // will pick the correct match, or the first if no selection is specified
114: if (chosen == null
115: || chosen.equals(combined)) {
116: chosenLog = PortletManager
117: .getWebAccessLog(renderRequest,
118: managerName,
119: containerName);
120: }
121: }
122: }
123: } else {
124: log.error("No web containers found for manager "
125: + manager.getProductName());
126: }
127: }
128: } else {
129: log.error("No web managers found!");
130: }
131: renderRequest.setAttribute("webContainers", products);
132: final String[] logNames = chosenLog.getLogNames();
133: renderRequest.setAttribute("webLogs", logNames);
134: String logToSearch = renderRequest.getParameter("selectedLog");
135: if (logToSearch == null) {
136: logToSearch = logNames[0];
137: } else { //what if the log options for Jetty were showing, but the user picked Tomcat to search? todo: fix this with some AJAX to repopulate the form when container is changed
138: boolean found = false;
139: for (int i = 0; i < logNames.length; i++) {
140: String test = logNames[i];
141: if (test.equals(logToSearch)) {
142: found = true;
143: break;
144: }
145: }
146: if (!found) { // Must has been for the other container -- make it work.
147: logToSearch = logNames[0];
148: }
149: }
150:
151: String action = renderRequest.getParameter("action");
152: if (criteria == null
153: || (action != null && !"refresh".equals(action))) {
154: if (criteria == null)
155: criteria = new Criteria();
156:
157: String fromDate = renderRequest.getParameter("fromDate");
158: String toDate = renderRequest.getParameter("toDate");
159: String requestHost = renderRequest
160: .getParameter("requestHost");
161: String authUser = renderRequest.getParameter("authUser");
162: String method = renderRequest.getParameter("requestMethod");
163: String uri = renderRequest.getParameter("requestedURI");
164: String result = renderRequest.getParameter("startResult");
165: Integer max = criteria.maxResult;
166: try {
167: max = Integer.parseInt(renderRequest
168: .getParameter("maxResult"));
169: } catch (NumberFormatException e) {
170: //ignore
171: }
172: String ignoreDates = renderRequest
173: .getParameter("ignoreDates");
174:
175: criteria.fromDate = fromDate == null || fromDate.equals("") ? null
176: : fromDate;
177: criteria.toDate = toDate == null || toDate.equals("") ? null
178: : toDate;
179: criteria.requestHost = requestHost == null
180: || requestHost.equals("") ? null : requestHost;
181: criteria.authUser = authUser == null || authUser.equals("") ? null
182: : authUser;
183: criteria.requestMethod = method == null
184: || method.equals("") ? null : method;
185: criteria.requestedURI = uri == null || uri.equals("") ? null
186: : uri;
187: criteria.startResult = result == null || result.equals("") ? null
188: : result;
189: criteria.maxResult = max;
190: criteria.ignoreDates = ignoreDates != null
191: && !ignoreDates.equals("");
192: }
193: String fromDateStr = criteria.fromDate;
194: String toDateStr = criteria.toDate;
195:
196: Calendar cal1 = Calendar.getInstance(), cal2 = Calendar
197: .getInstance();
198: // If not all dates were passed and ignoreDates is not enabled, filter on the current date.
199: // Note: This happens only when the portlet is loaded for the first time. Subsequent invocation (other than
200: // using "Refresh") will have either ignoreDates enabled or both the dates non null.
201: if ((fromDateStr == null || toDateStr == null)
202: && !criteria.ignoreDates) {
203: // just keep the month date and year
204: cal1.set(Calendar.MILLISECOND, 0);
205: cal1.set(Calendar.MINUTE, 0);
206: cal1.set(Calendar.SECOND, 0);
207: cal1.set(Calendar.HOUR_OF_DAY, 0);
208:
209: cal2.set(Calendar.HOUR_OF_DAY, cal2
210: .getMaximum(Calendar.HOUR_OF_DAY));
211: cal2.set(Calendar.MINUTE, cal2.getMaximum(Calendar.MINUTE));
212: cal2.set(Calendar.SECOND, cal2.getMaximum(Calendar.SECOND));
213: cal2.set(Calendar.MILLISECOND, cal2
214: .getMaximum(Calendar.MILLISECOND));
215:
216: WebAccessLog.SearchResults matchingItems = chosenLog
217: .getMatchingItems(logToSearch, null, null, null,
218: null, cal1.getTime(), cal2.getTime(), null,
219: Integer.valueOf(criteria.maxResult
220: .intValue() - 1));
221: renderRequest.setAttribute("logs", matchingItems
222: .getResults());
223: renderRequest.setAttribute("logLength", new Integer(
224: matchingItems.getLineCount()));
225: renderRequest.setAttribute("maxResult", criteria.maxResult);
226: renderRequest.setAttribute("ignoreDates", Boolean
227: .valueOf(criteria.ignoreDates));
228:
229: } else {
230: // Get other search criteria
231: String requestHost = criteria.requestHost;
232: String authUser = criteria.authUser;
233: String requestMethod = criteria.requestMethod;
234: String requestedURI = criteria.requestedURI;
235: String startResult = criteria.startResult;
236: Integer iStartResult = null;
237: Integer iMaxResult = criteria.maxResult;
238: try {
239: iStartResult = Integer.valueOf(startResult);
240: } catch (NumberFormatException e) {
241: //ignore
242: }
243:
244: boolean ignoreDates = criteria.ignoreDates;
245: if (ignoreDates) {
246: WebAccessLog.SearchResults matchingItems = chosenLog
247: .getMatchingItems(
248: logToSearch,
249: requestHost,
250: authUser,
251: requestMethod,
252: requestedURI,
253: null,
254: null,
255: iStartResult,
256: Integer
257: .valueOf(iMaxResult.intValue() - 1));
258: renderRequest.setAttribute("logs", matchingItems
259: .getResults());
260: renderRequest.setAttribute("logLength", new Integer(
261: matchingItems.getLineCount()));
262: } else {
263: Date fromDate = null, toDate = null;
264: // Check if the from and to date format is MM/DD/YYYY
265: DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
266: try {
267: fromDate = df.parse(fromDateStr);
268: // get the requested start date (defaults to 00:00:00:000 for time)
269: cal1.setTime(fromDate);
270: String mmddyyyy = (cal1.get(Calendar.MONTH) < 9 ? "0"
271: : "")
272: + (cal1.get(Calendar.MONTH) + 1);
273: mmddyyyy += "/"
274: + (cal1.get(Calendar.DAY_OF_MONTH) < 10 ? "0"
275: : "")
276: + (cal1.get(Calendar.DAY_OF_MONTH));
277: mmddyyyy += "/" + cal1.get(Calendar.YEAR);
278: if (!mmddyyyy.equals(fromDateStr)) {
279: // This should not arise since date input has been validated using javascript.
280: // If this does occur, ignore dates in search criteria and log a WARNING
281: log
282: .warn("From date must be a date in MM/DD/YYYY format, not '"
283: + fromDateStr
284: + "'. Dates will be ignored.");
285: fromDate = null;
286: }
287: toDate = df.parse(toDateStr);
288: cal2.setTime(toDate);
289: mmddyyyy = (cal2.get(Calendar.MONTH) < 9 ? "0" : "")
290: + (cal2.get(Calendar.MONTH) + 1);
291: mmddyyyy += "/"
292: + (cal2.get(Calendar.DAY_OF_MONTH) < 10 ? "0"
293: : "")
294: + (cal2.get(Calendar.DAY_OF_MONTH));
295: mmddyyyy += "/" + cal2.get(Calendar.YEAR);
296: if (!mmddyyyy.equals(toDateStr)) {
297: // This should not arise since date input has been validated using javascript.
298: // If this does occur, ignore to date in search criteria and log a WARNING
299: log
300: .warn("To date must be a date in MM/DD/YYYY format, not "
301: + toDateStr
302: + "'. Dates will be ignored.");
303: toDate = null;
304: } else {
305: // get the requested end date - Note: must set time to end of day
306: cal2.set(Calendar.HOUR_OF_DAY, cal2
307: .getMaximum(Calendar.HOUR_OF_DAY));
308: cal2.set(Calendar.MINUTE, cal2
309: .getMaximum(Calendar.MINUTE));
310: cal2.set(Calendar.SECOND, cal2
311: .getMaximum(Calendar.SECOND));
312: cal2.set(Calendar.MILLISECOND, cal2
313: .getMaximum(Calendar.MILLISECOND));
314: toDate = cal2.getTime();
315: }
316: } catch (ParseException e) {
317: // Should not occur since date input has been validated using javascript.
318: // If this does occur, ignore from and to dates and log a WARNING
319: log
320: .warn(
321: "Error parsing input dates. Dates will be ignored.",
322: e);
323: }
324: if (fromDate == null || toDate == null) {
325: fromDate = toDate = null;
326: }
327: WebAccessLog.SearchResults matchingItems = chosenLog
328: .getMatchingItems(
329: logToSearch,
330: requestHost,
331: authUser,
332: requestMethod,
333: requestedURI,
334: fromDate,
335: toDate,
336: iStartResult,
337: Integer
338: .valueOf(iMaxResult.intValue() - 1));
339: renderRequest.setAttribute("logs", matchingItems
340: .getResults());
341: renderRequest.setAttribute("logLength", new Integer(
342: matchingItems.getLineCount()));
343: }
344: renderRequest.setAttribute("ignoreDates", new Boolean(
345: ignoreDates));
346: renderRequest.setAttribute("requestHost", requestHost);
347: renderRequest.setAttribute("authUser", authUser);
348: renderRequest.setAttribute("requestMethod", requestMethod);
349: renderRequest.setAttribute("requestedURI", requestedURI);
350: if (iStartResult != null)
351: renderRequest.setAttribute("startResult", iStartResult);
352: if (iMaxResult != null)
353: renderRequest.setAttribute("maxResult", iMaxResult);
354: }
355: renderRequest.setAttribute("toDate", toDateStr);
356: renderRequest.setAttribute("fromDate", fromDateStr);
357: renderRequest.getPortletSession(true).setAttribute(
358: CRITERIA_KEY, criteria, PortletSession.PORTLET_SCOPE);
359: searchView.include(renderRequest, renderRespose);
360: }
361:
362: public void init(PortletConfig portletConfig)
363: throws PortletException {
364: PortletContext pc = portletConfig.getPortletContext();
365: searchView = pc
366: .getRequestDispatcher("/WEB-INF/view/webaccesslogmanager/view.jsp");
367: helpView = pc
368: .getRequestDispatcher("/WEB-INF/view/webaccesslogmanager/help.jsp");
369: super .init(portletConfig);
370: }
371:
372: public void processAction(ActionRequest actionRequest,
373: ActionResponse actionResponse) throws PortletException,
374: IOException {
375: String[] paramNames = { "action", "fromDate", "toDate",
376: "ignoreDates", "requestHost", "authUser",
377: "requestMethod", "requestedURI", "startResult",
378: "maxResult" };
379: // copy all action parameters to render parameters
380: for (int i = 0; i < paramNames.length; i++) {
381: if (actionRequest.getParameter(paramNames[i]) != null) {
382: actionResponse.setRenderParameter(paramNames[i],
383: actionRequest.getParameter(paramNames[i]));
384: }
385: }
386: }
387:
388: private static class Criteria implements Serializable {
389: Integer maxResult = new Integer(DEFAULT_MAX_RESULTS);
390: String fromDate = null;
391: String toDate = null;
392: boolean ignoreDates = false;
393: String requestHost = null;
394: String authUser = null;
395: String requestMethod = "ANY";
396: String requestedURI = null;
397: String startResult = null;
398: }
399:
400: }
|