KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > admin > loginterface > LogInterfaceBean


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13  
14 package org.ejbca.ui.web.admin.loginterface;
15
16 import java.security.cert.X509Certificate JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.ejbca.core.ejb.ServiceLocator;
26 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocal;
27 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocalHome;
28 import org.ejbca.core.ejb.log.ILogSessionLocal;
29 import org.ejbca.core.ejb.log.ILogSessionLocalHome;
30 import org.ejbca.core.model.ca.caadmin.CADoesntExistsException;
31 import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceNotActiveException;
32 import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceRequestException;
33 import org.ejbca.core.model.ca.caadmin.extendedcaservices.IllegalExtendedCAServiceRequestException;
34 import org.ejbca.core.model.log.Admin;
35 import org.ejbca.core.model.log.ILogExporter;
36 import org.ejbca.core.model.log.LogConfiguration;
37 import org.ejbca.core.model.log.LogConstants;
38 import org.ejbca.core.model.log.LogEntry;
39 import org.ejbca.ui.web.admin.configuration.EjbcaWebBean;
40 import org.ejbca.ui.web.admin.configuration.InformationMemory;
41 import org.ejbca.util.HTMLTools;
42 import org.ejbca.util.StringTools;
43 import org.ejbca.util.query.BasicMatch;
44 import org.ejbca.util.query.IllegalQueryException;
45 import org.ejbca.util.query.LogMatch;
46 import org.ejbca.util.query.Query;
47
48 /**
49  * A java bean handling the interface between EJBCA log module and JSP pages.
50  *
51  * @author Philip Vendil
52  * @version $Id: LogInterfaceBean.java,v 1.8 2007/01/17 17:26:40 anatom Exp $
53  */

54 public class LogInterfaceBean implements java.io.Serializable JavaDoc {
55
56     /** Creates new LogInterfaceBean */
57     public LogInterfaceBean(){
58     }
59     // Public methods.
60
/**
61      * Method that initialized the bean.
62      *
63      * @param request is a reference to the http request.
64      */

65     public void initialize(HttpServletRequest JavaDoc request, EjbcaWebBean ejbcawebbean) throws Exception JavaDoc{
66
67       if(!initialized){
68         admin = new Admin(((X509Certificate JavaDoc[]) request.getAttribute( "javax.servlet.request.X509Certificate" ))[0]);
69         
70         final ServiceLocator locator = ServiceLocator.getInstance();
71         ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) locator.getLocalHome(ILogSessionLocalHome.COMP_NAME);
72         logsession = logsessionhome.create();
73         
74         ICertificateStoreSessionLocalHome certificatesessionhome = (ICertificateStoreSessionLocalHome) locator.getLocalHome(ICertificateStoreSessionLocalHome.COMP_NAME);
75         certificatesession = certificatesessionhome.create();
76         
77         this.informationmemory = ejbcawebbean.getInformationMemory();
78         
79         initializeEventNameTables(ejbcawebbean);
80                 
81         dnproxy = new SubjectDNProxy(admin, certificatesession);
82         
83         HashMap JavaDoc caidtonamemap = ejbcawebbean.getInformationMemory().getCAIdToNameMap();
84         
85         // Add Internal CA Name if it doesn't exists
86
if(caidtonamemap.get(new Integer JavaDoc(LogConstants.INTERNALCAID)) == null){
87             caidtonamemap.put(new Integer JavaDoc(LogConstants.INTERNALCAID),ejbcawebbean.getText("INTERNALCA"));
88         }
89               
90         logentriesview = new LogEntriesView(dnproxy, localinfoeventnamesunsorted, localerroreventnamesunsorted, localmodulenamesunsorted, caidtonamemap);
91         initialized =true;
92       }
93     }
94
95     /**
96      * Method that searches the log database for all events occurred related to the given query.
97      *
98      * @param query the query to use.
99      * @param index point's where in result to begin returning data.
100      * @param size the number of elements to return.
101      */

102
103     public LogEntryView[] filterByQuery(Query query, int index, int size) throws Exception JavaDoc {
104       Collection JavaDoc logentries = logsession.query(query, informationmemory.getViewLogQueryString(), informationmemory.getViewLogCAIdString());
105       logentriesview.setEntries(logentries);
106       lastquery = query;
107
108       return logentriesview.getEntries(index,size);
109     }
110
111     /**
112      * Method that searches the log database for all events occurred related to the given username.
113      * Used in the view user history page.
114      *
115      * @param username the username to search for
116      * @param index point's where in result to begin returning data.
117      * @param size the number of elements to return.
118      */

119     public LogEntriesView filterByUsername(String JavaDoc username, HashMap JavaDoc caidtonamemap) throws Exception JavaDoc {
120       LogEntriesView returnval = new LogEntriesView(dnproxy, localinfoeventnamesunsorted, localerroreventnamesunsorted, localmodulenamesunsorted, caidtonamemap);
121       String JavaDoc user = StringTools.strip(username);
122       Query query = new Query(Query.TYPE_LOGQUERY);
123       query.add(LogMatch.MATCH_WITH_USERNAME, BasicMatch.MATCH_TYPE_EQUALS, user);
124         
125       Collection JavaDoc logentries = logsession.query(query,informationmemory.getViewLogQueryString(), informationmemory.getViewLogCAIdString());
126       returnval.setEntries(logentries);
127       lastquery = query;
128
129       return returnval;
130     }
131     
132     /**
133      * Method that searches the log database for all events occurred within the last given minutes.
134      * Used in the view user history page.
135      *
136      * @param time the time in minutes to look for.
137      * @param index point's where in result to begin returning data.
138      * @param size the number of elements to return.
139      */

140     public LogEntryView[] filterByTime(int time, int index, int size) throws Exception JavaDoc {
141       Query query = new Query(Query.TYPE_LOGQUERY);
142       Date JavaDoc starttime = new Date JavaDoc( (new Date JavaDoc()).getTime() - (time * 60000));
143       
144       query.add(starttime, new Date JavaDoc());
145         
146       Collection JavaDoc logentries = logsession.query(query,informationmemory.getViewLogQueryString(), informationmemory.getViewLogCAIdString());
147       logentriesview.setEntries(logentries);
148       lastquery = query;
149
150       return logentriesview.getEntries(index,size);
151     }
152
153     /* Method that returns the size of a query search */
154     public int getResultSize(){
155      return logentriesview.size();
156     }
157     
158     /* Method to resort filtered user data. */
159     public void sortUserData(int sortby, int sortorder){
160       logentriesview.sortBy(sortby,sortorder);
161     }
162
163     /* Method to return the logentries between index and size, if logentries is smaller than size, a smaller array is returned. */
164     public LogEntryView[] getEntries(int index, int size){
165       return logentriesview.getEntries(index, size);
166     }
167
168     public boolean nextButton(int index, int size){
169       return index + size < logentriesview.size();
170     }
171     public boolean previousButton(int index){
172       return index > 0 ;
173     }
174      
175     /**
176      * Loads the log configuration from the database.
177      *
178      * @return the logconfiguration
179      */

180     public LogConfiguration loadLogConfiguration(int caid) {
181       return logsession.loadLogConfiguration(caid);
182     }
183         
184     /**
185      * Saves the log configuration to the database.
186      *
187      * @param logconfiguration the logconfiguration to save.
188      */

189     public void saveLogConfiguration(int caid, LogConfiguration logconfiguration) {
190       logsession.saveLogConfiguration(admin, caid, logconfiguration);
191     }
192     
193    
194  
195     /**
196      * Help methods that sets up id mappings between event ids and event name hashes.
197      *
198      * @return a hasmap with error info eventname hash to id mappings.
199      */

200     public HashMap JavaDoc getEventNameHashToIdMap(){
201       return localeventnamehashtoid;
202     }
203     
204     /**
205      * Help methods that sets up id mappings between event ids and event names in local languange.
206      *
207      * @return a hasmap with error info eventname (translated and html-unescaped) to id mappings.
208      */

209     public HashMap JavaDoc getTranslatedEventNameToIdMap(){
210       return localtranslatedeventnamestoid;
211     }
212     
213     /**
214      * Help methods that sets up id mappings between module ids and module names in local languange.
215      *
216      * @return a hasmap with error info eventname to id mappings.
217      */

218     public HashMap JavaDoc getModuleNameToIdMap(){
219       return localmodulenamestoid;
220     }
221
222     /**
223      * Help methods that translates info event names to the local languange.
224      *
225      * @return an array with local info eventnames.
226      */

227     public String JavaDoc[] getLocalInfoEventNames(){
228       return localinfoeventnames;
229     }
230     
231     /**
232      * Help methods that translates error event names to the local languange.
233      *
234      * @return an array with local info eventnames.
235      */

236     public String JavaDoc[] getLocalErrorEventNames(){
237       return localerroreventnames;
238     }
239     
240     /**
241      * Help methods that returns an array with all translated event names.
242      *
243      * @return an array of all translated eventnames.
244      */

245     public String JavaDoc[] getAllLocalEventNames(){
246       return alllocaleventnames;
247     }
248     
249     /**
250      * Help methods that returns an array with all translated module names.
251      *
252      * @return an array of all translated eventnames.
253      */

254     public String JavaDoc[] getLocalModuleNames(EjbcaWebBean ejbcawebbean){
255       Collection JavaDoc authorizedmodules = this.informationmemory.getAuthorizedModules();
256       String JavaDoc[] returnval = new String JavaDoc[authorizedmodules.size()];
257       Iterator JavaDoc iter = authorizedmodules.iterator();
258       int i = 0;
259       while(iter.hasNext()){
260           returnval[i] = ejbcawebbean.getText(LogEntry.MODULETEXTS[((Integer JavaDoc) iter.next()).intValue()]);
261           i++;
262       }
263       
264       return returnval;
265     }
266     
267     /**
268      * Method that exports log entries according to an exporter passed as argument.
269      * @param exporter the export implementation to use, implements the ILogExporter interface
270      * @return byte[] byte data or null if no of exported entries are 0.
271      * @throws IllegalQueryException
272      * @throws ExtendedCAServiceNotActiveException
273      * @throws IllegalExtendedCAServiceRequestException
274      * @throws ExtendedCAServiceRequestException
275      * @throws CADoesntExistsException
276      * @see org.ejbca.core.model.log.ILogExporter
277      */

278     public byte[] exportLastQuery(ILogExporter exporter) throws IllegalQueryException, CADoesntExistsException, ExtendedCAServiceRequestException, IllegalExtendedCAServiceRequestException, ExtendedCAServiceNotActiveException {
279         byte[] ret = logsession.export(admin, lastquery, informationmemory.getViewLogQueryString(), informationmemory.getViewLogCAIdString(), exporter);
280         return ret;
281     }
282
283     // Private methods.
284
private void initializeEventNameTables(EjbcaWebBean ejbcawebbean){
285       int alleventsize = LogEntry.EVENTNAMES_INFO.length + LogEntry.EVENTNAMES_ERROR.length;
286       alllocaleventnames = new String JavaDoc[alleventsize];
287       localinfoeventnames = new String JavaDoc[LogEntry.EVENTNAMES_INFO.length];
288       localinfoeventnamesunsorted = new String JavaDoc[LogEntry.EVENTNAMES_INFO.length];
289       localeventnamehashtoid = new HashMap JavaDoc();
290       localtranslatedeventnamestoid = new HashMap JavaDoc();
291       for(int i = 0; i < localinfoeventnames.length; i++){
292           // If the translation contains html characters (&eacute; etc) we must turn it into regular chars, just like the browser does
293
String JavaDoc s = ejbcawebbean.getText(LogEntry.EVENTNAMES_INFO[i]);
294           localinfoeventnames[i] = s;
295           localinfoeventnamesunsorted[i] = s;
296           String JavaDoc translateds = HTMLTools.htmlunescape(s);
297           alllocaleventnames[i] = translateds;
298           // We must make this independent of language encoding, utf, html escaped etc
299
Integer JavaDoc hashcode = new Integer JavaDoc(localinfoeventnames[i].hashCode());
300           String JavaDoc hash = hashcode.toString();
301           localeventnamehashtoid.put(hash, new Integer JavaDoc(i));
302           localtranslatedeventnamestoid.put(translateds, new Integer JavaDoc(i));
303       }
304       Arrays.sort(localinfoeventnames);
305       
306       localerroreventnamesunsorted = new String JavaDoc[LogEntry.EVENTNAMES_ERROR.length];
307       localerroreventnames = new String JavaDoc[LogEntry.EVENTNAMES_ERROR.length];
308       for(int i = 0; i < localerroreventnames.length; i++){
309           // If the translation contains html characters (&eacute; etc) we must turn it into regular chars, just like the browser does
310
String JavaDoc s = ejbcawebbean.getText(LogEntry.EVENTNAMES_ERROR[i]);
311           localerroreventnames[i] = s;
312           localerroreventnamesunsorted[i] = s;
313           String JavaDoc translateds = HTMLTools.htmlunescape(s);
314           alllocaleventnames[LogEntry.EVENTNAMES_INFO.length + i] = translateds;
315           // We must make this independent of language encoding, utf, html escaped etc
316
Integer JavaDoc hashcode = new Integer JavaDoc(s.hashCode());
317           String JavaDoc hash = hashcode.toString();
318           localeventnamehashtoid.put(hash, new Integer JavaDoc(i + LogEntry.EVENT_ERROR_BOUNDRARY));
319           localtranslatedeventnamestoid.put(translateds, new Integer JavaDoc(i + LogEntry.EVENT_ERROR_BOUNDRARY));
320       }
321       Arrays.sort(localerroreventnames);
322       Arrays.sort(alllocaleventnames);
323       
324       localmodulenames = new String JavaDoc[LogEntry.MODULETEXTS.length];
325       localmodulenamesunsorted = new String JavaDoc[LogEntry.MODULETEXTS.length];
326       localmodulenamestoid = new HashMap JavaDoc(9);
327       for(int i = 0; i < localmodulenames.length; i++){
328         localmodulenames[i] = ejbcawebbean.getText(LogEntry.MODULETEXTS[i]);
329         localmodulenamesunsorted[i] = localmodulenames[i];
330         localmodulenamestoid.put(localmodulenames[i], new Integer JavaDoc(i));
331       }
332       Arrays.sort(localmodulenames);
333       
334     }
335     
336
337     // Private fields.
338
private ICertificateStoreSessionLocal certificatesession;
339     private ILogSessionLocal logsession;
340     private LogEntriesView logentriesview;
341     private Admin admin;
342     private SubjectDNProxy dnproxy;
343     private boolean initialized=false;
344     private InformationMemory informationmemory;
345     
346     private HashMap JavaDoc localeventnamehashtoid;
347     private HashMap JavaDoc localtranslatedeventnamestoid;
348     private HashMap JavaDoc localmodulenamestoid;
349     private String JavaDoc[] localinfoeventnames;
350     private String JavaDoc[] localerroreventnames;
351     private String JavaDoc[] localinfoeventnamesunsorted;
352     private String JavaDoc[] localerroreventnamesunsorted;
353     private String JavaDoc[] alllocaleventnames;
354     private String JavaDoc[] localmodulenames;
355     private String JavaDoc[] localmodulenamesunsorted;
356     private Query lastquery;
357     
358 }
359
Popular Tags