001: /*************************************************************************
002: * *
003: * EJBCA: The OpenSource Certificate Authority *
004: * *
005: * This software is free software; you can redistribute it and/or *
006: * modify it under the terms of the GNU Lesser General Public *
007: * License as published by the Free Software Foundation; either *
008: * version 2.1 of the License, or any later version. *
009: * *
010: * See terms of license at gnu.org. *
011: * *
012: *************************************************************************/package org.ejbca.ui.web.admin.loginterface;
013:
014: import java.security.cert.X509Certificate;
015: import java.util.Arrays;
016: import java.util.Collection;
017: import java.util.Date;
018: import java.util.HashMap;
019: import java.util.Iterator;
020:
021: import javax.servlet.http.HttpServletRequest;
022:
023: import org.ejbca.core.ejb.ServiceLocator;
024: import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocal;
025: import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocalHome;
026: import org.ejbca.core.ejb.log.ILogSessionLocal;
027: import org.ejbca.core.ejb.log.ILogSessionLocalHome;
028: import org.ejbca.core.model.ca.caadmin.CADoesntExistsException;
029: import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceNotActiveException;
030: import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceRequestException;
031: import org.ejbca.core.model.ca.caadmin.extendedcaservices.IllegalExtendedCAServiceRequestException;
032: import org.ejbca.core.model.log.Admin;
033: import org.ejbca.core.model.log.ILogExporter;
034: import org.ejbca.core.model.log.LogConfiguration;
035: import org.ejbca.core.model.log.LogConstants;
036: import org.ejbca.ui.web.admin.configuration.EjbcaWebBean;
037: import org.ejbca.ui.web.admin.configuration.InformationMemory;
038: import org.ejbca.util.HTMLTools;
039: import org.ejbca.util.StringTools;
040: import org.ejbca.util.query.BasicMatch;
041: import org.ejbca.util.query.IllegalQueryException;
042: import org.ejbca.util.query.LogMatch;
043: import org.ejbca.util.query.Query;
044:
045: /**
046: * A java bean handling the interface between EJBCA log module and JSP pages.
047: *
048: * @author Philip Vendil
049: * @version $Id: LogInterfaceBean.java,v 1.10 2007/12/04 18:43:36 jeklund Exp $
050: */
051: public class LogInterfaceBean implements java.io.Serializable {
052:
053: /** Creates new LogInterfaceBean */
054: public LogInterfaceBean() {
055: }
056:
057: // Public methods.
058: /**
059: * Method that initialized the bean.
060: *
061: * @param request is a reference to the http request.
062: */
063: public void initialize(HttpServletRequest request,
064: EjbcaWebBean ejbcawebbean) throws Exception {
065:
066: if (!initialized) {
067: admin = new Admin(
068: ((X509Certificate[]) request
069: .getAttribute("javax.servlet.request.X509Certificate"))[0]);
070:
071: final ServiceLocator locator = ServiceLocator.getInstance();
072: ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) locator
073: .getLocalHome(ILogSessionLocalHome.COMP_NAME);
074: logsession = logsessionhome.create();
075:
076: ICertificateStoreSessionLocalHome certificatesessionhome = (ICertificateStoreSessionLocalHome) locator
077: .getLocalHome(ICertificateStoreSessionLocalHome.COMP_NAME);
078: certificatesession = certificatesessionhome.create();
079:
080: this .informationmemory = ejbcawebbean
081: .getInformationMemory();
082:
083: initializeEventNameTables(ejbcawebbean);
084:
085: dnproxy = new SubjectDNProxy(admin, certificatesession);
086:
087: HashMap caidtonamemap = ejbcawebbean.getInformationMemory()
088: .getCAIdToNameMap();
089:
090: // Add Internal CA Name if it doesn't exists
091: if (caidtonamemap
092: .get(new Integer(LogConstants.INTERNALCAID)) == null) {
093: caidtonamemap.put(
094: new Integer(LogConstants.INTERNALCAID),
095: ejbcawebbean.getText("INTERNALCA"));
096: }
097:
098: logentriesview = new LogEntriesView(dnproxy,
099: localinfoeventnamesunsorted,
100: localerroreventnamesunsorted,
101: localsystemeventnamesunsorted,
102: localmodulenamesunsorted, caidtonamemap);
103: initialized = true;
104: }
105: }
106:
107: /**
108: * Method that searches the log database for all events occurred related to the given query.
109: *
110: * @param query the query to use.
111: * @param index point's where in result to begin returning data.
112: * @param size the number of elements to return.
113: */
114:
115: public LogEntryView[] filterByQuery(String deviceName, Query query,
116: int index, int size) throws Exception {
117: Collection logentries = logsession.query(deviceName, query,
118: informationmemory.getViewLogQueryString(),
119: informationmemory.getViewLogCAIdString());
120: logentriesview.setEntries(logentries);
121: lastquery = query;
122:
123: return logentriesview.getEntries(index, size);
124: }
125:
126: /**
127: * Method that searches the log database for all events occurred related to the given username.
128: * Used in the view user history page.
129: *
130: * @param username the username to search for
131: * @param index point's where in result to begin returning data.
132: * @param size the number of elements to return.
133: */
134: public LogEntriesView filterByUsername(String deviceName,
135: String username, HashMap caidtonamemap) throws Exception {
136: LogEntriesView returnval = new LogEntriesView(dnproxy,
137: localinfoeventnamesunsorted,
138: localerroreventnamesunsorted,
139: localsystemeventnamesunsorted,
140: localmodulenamesunsorted, caidtonamemap);
141: String user = StringTools.strip(username);
142: Query query = new Query(Query.TYPE_LOGQUERY);
143: query.add(LogMatch.MATCH_WITH_USERNAME,
144: BasicMatch.MATCH_TYPE_EQUALS, user);
145:
146: Collection logentries = logsession.query(deviceName, query,
147: informationmemory.getViewLogQueryString(),
148: informationmemory.getViewLogCAIdString());
149: returnval.setEntries(logentries);
150: lastquery = query;
151:
152: return returnval;
153: }
154:
155: /**
156: * Method that searches the log database for all events occurred within the last given minutes.
157: * Used in the view user history page.
158: *
159: * @param time the time in minutes to look for.
160: * @param index point's where in result to begin returning data.
161: * @param size the number of elements to return.
162: */
163: public LogEntryView[] filterByTime(String deviceName, int time,
164: int index, int size) throws Exception {
165: Query query = new Query(Query.TYPE_LOGQUERY);
166: Date starttime = new Date((new Date()).getTime()
167: - (time * 60000));
168:
169: query.add(starttime, new Date());
170:
171: Collection logentries = logsession.query(deviceName, query,
172: informationmemory.getViewLogQueryString(),
173: informationmemory.getViewLogCAIdString());
174: logentriesview.setEntries(logentries);
175: lastquery = query;
176:
177: return logentriesview.getEntries(index, size);
178: }
179:
180: /* Method that returns the size of a query search */
181: public int getResultSize() {
182: return logentriesview.size();
183: }
184:
185: /* Method to resort filtered user data. */
186: public void sortUserData(int sortby, int sortorder) {
187: logentriesview.sortBy(sortby, sortorder);
188: }
189:
190: /* Method to return the logentries between index and size, if logentries is smaller than size, a smaller array is returned. */
191: public LogEntryView[] getEntries(int index, int size) {
192: return logentriesview.getEntries(index, size);
193: }
194:
195: public boolean nextButton(int index, int size) {
196: return index + size < logentriesview.size();
197: }
198:
199: public boolean previousButton(int index) {
200: return index > 0;
201: }
202:
203: /**
204: * Loads the log configuration from the database.
205: *
206: * @return the logconfiguration
207: */
208: public LogConfiguration loadLogConfiguration(int caid) {
209: return logsession.loadLogConfiguration(caid);
210: }
211:
212: /**
213: * Saves the log configuration to the database.
214: *
215: * @param logconfiguration the logconfiguration to save.
216: */
217: public void saveLogConfiguration(int caid,
218: LogConfiguration logconfiguration) {
219: logsession.saveLogConfiguration(admin, caid, logconfiguration);
220: }
221:
222: /**
223: * Help methods that sets up id mappings between event ids and event name hashes.
224: *
225: * @return a hasmap with error info eventname hash to id mappings.
226: */
227: public HashMap getEventNameHashToIdMap() {
228: return localeventnamehashtoid;
229: }
230:
231: /**
232: * Help methods that sets up id mappings between event ids and event names in local languange.
233: *
234: * @return a hasmap with error info eventname (translated and html-unescaped) to id mappings.
235: */
236: public HashMap getTranslatedEventNameToIdMap() {
237: return localtranslatedeventnamestoid;
238: }
239:
240: /**
241: * Help methods that sets up id mappings between module ids and module names in local languange.
242: *
243: * @return a hasmap with error info eventname to id mappings.
244: */
245: public HashMap getModuleNameToIdMap() {
246: return localmodulenamestoid;
247: }
248:
249: /**
250: * Help methods that translates info event names to the local languange.
251: *
252: * @return an array with local info eventnames.
253: */
254: public String[] getLocalInfoEventNames() {
255: return localinfoeventnames;
256: }
257:
258: /**
259: * Help methods that translates error event names to the local languange.
260: *
261: * @return an array with local info eventnames.
262: */
263: public String[] getLocalErrorEventNames() {
264: return localerroreventnames;
265: }
266:
267: /**
268: * Help methods that returns an array with all translated event names.
269: *
270: * @return an array of all translated eventnames.
271: */
272: public String[] getAllLocalEventNames() {
273: return alllocaleventnames;
274: }
275:
276: /**
277: * Help methods that returns an array with all translated module names.
278: *
279: * @return an array of all translated eventnames.
280: */
281: public String[] getLocalModuleNames(EjbcaWebBean ejbcawebbean) {
282: Collection authorizedmodules = this .informationmemory
283: .getAuthorizedModules();
284: String[] returnval = new String[authorizedmodules.size()];
285: Iterator iter = authorizedmodules.iterator();
286: int i = 0;
287: while (iter.hasNext()) {
288: returnval[i] = ejbcawebbean
289: .getText(LogConstants.MODULETEXTS[((Integer) iter
290: .next()).intValue()]);
291: i++;
292: }
293:
294: return returnval;
295: }
296:
297: /**
298: * Method that exports log entries according to an exporter passed as argument.
299: * @param exporter the export implementation to use, implements the ILogExporter interface
300: * @return byte[] byte data or null if no of exported entries are 0.
301: * @throws IllegalQueryException
302: * @throws ExtendedCAServiceNotActiveException
303: * @throws IllegalExtendedCAServiceRequestException
304: * @throws ExtendedCAServiceRequestException
305: * @throws CADoesntExistsException
306: * @see org.ejbca.core.model.log.ILogExporter
307: */
308: public byte[] exportLastQuery(String deviceName,
309: ILogExporter exporter) throws IllegalQueryException,
310: CADoesntExistsException, ExtendedCAServiceRequestException,
311: IllegalExtendedCAServiceRequestException,
312: ExtendedCAServiceNotActiveException {
313: byte[] ret = logsession.export(deviceName, admin, lastquery,
314: informationmemory.getViewLogQueryString(),
315: informationmemory.getViewLogCAIdString(), exporter);
316: return ret;
317: }
318:
319: public Collection getAvailableLogDevices() {
320: return logsession.getAvailableLogDevices();
321: }
322:
323: // Private methods.
324: private void initializeEventNameTables(EjbcaWebBean ejbcawebbean) {
325: int alleventsize = LogConstants.EVENTNAMES_INFO.length
326: + LogConstants.EVENTNAMES_ERROR.length
327: + LogConstants.EVENTNAMES_SYSTEM.length;
328: alllocaleventnames = new String[alleventsize];
329: localinfoeventnames = new String[LogConstants.EVENTNAMES_INFO.length];
330: localinfoeventnamesunsorted = new String[LogConstants.EVENTNAMES_INFO.length];
331: localeventnamehashtoid = new HashMap();
332: localtranslatedeventnamestoid = new HashMap();
333: for (int i = 0; i < localinfoeventnames.length; i++) {
334: // If the translation contains html characters (é etc) we must turn it into regular chars, just like the browser does
335: String s = ejbcawebbean
336: .getText(LogConstants.EVENTNAMES_INFO[i]);
337: localinfoeventnames[i] = s;
338: localinfoeventnamesunsorted[i] = s;
339: String translateds = HTMLTools.htmlunescape(s);
340: alllocaleventnames[i] = translateds;
341: // We must make this independent of language encoding, utf, html escaped etc
342: Integer hashcode = new Integer(localinfoeventnames[i]
343: .hashCode());
344: String hash = hashcode.toString();
345: localeventnamehashtoid.put(hash, new Integer(i));
346: localtranslatedeventnamestoid.put(translateds, new Integer(
347: i));
348: }
349: Arrays.sort(localinfoeventnames);
350:
351: localerroreventnamesunsorted = new String[LogConstants.EVENTNAMES_ERROR.length];
352: localerroreventnames = new String[LogConstants.EVENTNAMES_ERROR.length];
353: for (int i = 0; i < localerroreventnames.length; i++) {
354: // If the translation contains html characters (é etc) we must turn it into regular chars, just like the browser does
355: String s = ejbcawebbean
356: .getText(LogConstants.EVENTNAMES_ERROR[i]);
357: localerroreventnames[i] = s;
358: localerroreventnamesunsorted[i] = s;
359: String translateds = HTMLTools.htmlunescape(s);
360: alllocaleventnames[LogConstants.EVENTNAMES_INFO.length + i] = translateds;
361: // We must make this independent of language encoding, utf, html escaped etc
362: Integer hashcode = new Integer(s.hashCode());
363: String hash = hashcode.toString();
364: localeventnamehashtoid.put(hash, new Integer(i
365: + LogConstants.EVENT_ERROR_BOUNDRARY));
366: localtranslatedeventnamestoid.put(translateds, new Integer(
367: i + LogConstants.EVENT_ERROR_BOUNDRARY));
368: }
369: Arrays.sort(localerroreventnames);
370:
371: localsystemeventnamesunsorted = new String[LogConstants.EVENTNAMES_SYSTEM.length];
372: localsystemeventnames = new String[LogConstants.EVENTNAMES_SYSTEM.length];
373: for (int i = 0; i < localsystemeventnames.length; i++) {
374: // If the translation contains html characters (é etc) we must turn it into regular chars, just like the browser does
375: String s = ejbcawebbean
376: .getText(LogConstants.EVENTNAMES_SYSTEM[i]);
377: localsystemeventnames[i] = s;
378: localsystemeventnamesunsorted[i] = s;
379: String translateds = HTMLTools.htmlunescape(s);
380: alllocaleventnames[LogConstants.EVENTNAMES_INFO.length
381: + LogConstants.EVENTNAMES_ERROR.length + i] = translateds;
382: // We must make this independent of language encoding, utf, html escaped etc
383: Integer hashcode = new Integer(s.hashCode());
384: String hash = hashcode.toString();
385: localeventnamehashtoid.put(hash, new Integer(i
386: + LogConstants.EVENT_SYSTEM_BOUNDRARY));
387: localtranslatedeventnamestoid.put(translateds, new Integer(
388: i + LogConstants.EVENT_SYSTEM_BOUNDRARY));
389: }
390: Arrays.sort(localsystemeventnames);
391: Arrays.sort(alllocaleventnames);
392:
393: localmodulenames = new String[LogConstants.MODULETEXTS.length];
394: localmodulenamesunsorted = new String[LogConstants.MODULETEXTS.length];
395: localmodulenamestoid = new HashMap(9);
396: for (int i = 0; i < localmodulenames.length; i++) {
397: localmodulenames[i] = ejbcawebbean
398: .getText(LogConstants.MODULETEXTS[i]);
399: localmodulenamesunsorted[i] = localmodulenames[i];
400: localmodulenamestoid.put(localmodulenames[i],
401: new Integer(i));
402: }
403: Arrays.sort(localmodulenames);
404: }
405:
406: // Private fields.
407: private ICertificateStoreSessionLocal certificatesession;
408: private ILogSessionLocal logsession;
409: private LogEntriesView logentriesview;
410: private Admin admin;
411: private SubjectDNProxy dnproxy;
412: private boolean initialized = false;
413: private InformationMemory informationmemory;
414:
415: private HashMap localeventnamehashtoid;
416: private HashMap localtranslatedeventnamestoid;
417: private HashMap localmodulenamestoid;
418: private String[] localinfoeventnames;
419: private String[] localerroreventnames;
420: private String[] localsystemeventnames;
421: private String[] localinfoeventnamesunsorted;
422: private String[] localerroreventnamesunsorted;
423: private String[] localsystemeventnamesunsorted;
424: private String[] alllocaleventnames;
425: private String[] localmodulenames;
426: private String[] localmodulenamesunsorted;
427: private Query lastquery;
428:
429: }
|