1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2004 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
*
|
6
|
* '$Author: tao $'
|
7
|
* '$Date: 2015-02-06 16:46:09 -0800 (Fri, 06 Feb 2015) $'
|
8
|
* '$Revision: 9107 $'
|
9
|
*
|
10
|
* This program is free software; you can redistribute it and/or modify
|
11
|
* it under the terms of the GNU General Public License as published by
|
12
|
* the Free Software Foundation; either version 2 of the License, or
|
13
|
* (at your option) any later version.
|
14
|
*
|
15
|
* This program is distributed in the hope that it will be useful,
|
16
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
* GNU General Public License for more details.
|
19
|
*
|
20
|
* You should have received a copy of the GNU General Public License
|
21
|
* along with this program; if not, write to the Free Software
|
22
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
*/
|
24
|
package edu.ucsb.nceas.metacat;
|
25
|
|
26
|
import java.sql.PreparedStatement;
|
27
|
import java.sql.ResultSet;
|
28
|
import java.sql.SQLException;
|
29
|
import java.sql.Timestamp;
|
30
|
import java.util.ArrayList;
|
31
|
import java.util.Date;
|
32
|
import java.util.HashMap;
|
33
|
import java.util.List;
|
34
|
import java.util.Map;
|
35
|
import java.util.Vector;
|
36
|
|
37
|
import org.apache.log4j.Logger;
|
38
|
import org.dataone.service.types.v1.Identifier;
|
39
|
import org.dataone.service.types.v2.Log;
|
40
|
import org.dataone.service.types.v2.LogEntry;
|
41
|
import org.dataone.service.types.v1.Event;
|
42
|
import org.dataone.service.types.v1.NodeReference;
|
43
|
import org.dataone.service.types.v1.Subject;
|
44
|
import org.dataone.service.util.DateTimeMarshaller;
|
45
|
|
46
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
47
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
48
|
import edu.ucsb.nceas.metacat.database.DatabaseService;
|
49
|
import edu.ucsb.nceas.metacat.index.MetacatSolrIndex;
|
50
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
51
|
import edu.ucsb.nceas.metacat.util.DocumentUtil;
|
52
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
53
|
|
54
|
/**
|
55
|
* EventLog is used to intialize and store a log of events that occur in an
|
56
|
* application. The events are registered with the logger as they occur, but
|
57
|
* EventLog writes them to permenant storage when it is most convenient or
|
58
|
* efficient. EventLog is a Singleton as there should always be only one object
|
59
|
* for these logging events.
|
60
|
*
|
61
|
* TODO: Logging to the database needn't be synchronous with the event.
|
62
|
* Instead, a separate thread can be launched that periodically sleeps and only
|
63
|
* wakes periodically to see if metacat is idle. The log event can be cached
|
64
|
* and inserted later when the thread wakes and finds metacat idle.
|
65
|
*
|
66
|
* TODO: Write a function that archives a part of the log table to an
|
67
|
* external text file so that the log table doesn't get to big. This
|
68
|
* function should be able to be called manually or on a schedule.
|
69
|
*
|
70
|
* TODO: Write an access function that returns an XML report for a
|
71
|
* specific subset of events. Users should be able to query on
|
72
|
* principal, docid/rev, date, event, and possibly other fields.
|
73
|
*
|
74
|
* @author jones
|
75
|
*/
|
76
|
public class EventLog
|
77
|
{
|
78
|
public static final String DELETE = "delete";
|
79
|
/**
|
80
|
* The single instance of the event log that is always returned.
|
81
|
*/
|
82
|
private static EventLog self = null;
|
83
|
private Logger logMetacat = Logger.getLogger(EventLog.class);
|
84
|
private static final int USERAGENTLENGTH = 512;
|
85
|
|
86
|
/**
|
87
|
* A private constructor that initializes the class when getInstance() is
|
88
|
* called.
|
89
|
*/
|
90
|
private EventLog()
|
91
|
{
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Return the single instance of the event log after initializing it if it
|
96
|
* wasn't previously initialized.
|
97
|
*
|
98
|
* @return the single EventLog instance
|
99
|
*/
|
100
|
public static EventLog getInstance()
|
101
|
{
|
102
|
if (self == null) {
|
103
|
self = new EventLog();
|
104
|
}
|
105
|
return self;
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* Log an event of interest to the application. The information logged can
|
110
|
* include basic identification information about the principal or computer
|
111
|
* that initiated the event.
|
112
|
*
|
113
|
* @param ipAddress the internet protocol address for the event
|
114
|
* @param userAgent the agent making the request
|
115
|
* @param principal the principal for the event (a username, etc)
|
116
|
* @param docid the identifier of the document to which the event applies
|
117
|
* @param event the string code for the event
|
118
|
*/
|
119
|
public void log(String ipAddress, String userAgent, String principal, String docid, String event) {
|
120
|
EventLogData logData = new EventLogData(ipAddress, userAgent, principal, docid, event);
|
121
|
insertLogEntry(logData);
|
122
|
|
123
|
// update the event information in the index
|
124
|
try {
|
125
|
String localId = DocumentUtil.getSmartDocId(docid);
|
126
|
int rev = DocumentUtil.getRevisionFromAccessionNumber(docid);
|
127
|
|
128
|
String guid = IdentifierManager.getInstance().getGUID(localId, rev);
|
129
|
Identifier pid = new Identifier();
|
130
|
pid.setValue(guid);
|
131
|
|
132
|
// submit for indexing
|
133
|
MetacatSolrIndex.getInstance().submit(pid, null, this.getIndexFields(pid, event), false);
|
134
|
|
135
|
} catch (Exception e) {
|
136
|
logMetacat.error("Could not update event index information", e);
|
137
|
}
|
138
|
}
|
139
|
|
140
|
public Map<String, List<Object>> getIndexFields(Identifier pid, String event) {
|
141
|
// update the search index for the event
|
142
|
try {
|
143
|
|
144
|
if (event != null) {
|
145
|
|
146
|
String fieldName = event + "_count_i";
|
147
|
int eventCount = 0;
|
148
|
|
149
|
String docid = IdentifierManager.getInstance().getLocalId(pid.getValue());
|
150
|
Log eventLog = this.getD1Report(null, null, new String[] {docid}, event, null, null, false, 0, 0);
|
151
|
eventCount = eventLog.getTotal();
|
152
|
|
153
|
List<Object> values = new ArrayList<Object>();
|
154
|
values.add(eventCount);
|
155
|
Map<String, List<Object>> fields = new HashMap<String, List<Object>>();
|
156
|
fields.put(fieldName, values);
|
157
|
|
158
|
return fields;
|
159
|
}
|
160
|
|
161
|
} catch (Exception e) {
|
162
|
logMetacat.error("Could not update event index information on pid: " + pid.getValue() + " for event: " + event, e);
|
163
|
}
|
164
|
// default if we can't find the event information
|
165
|
return null;
|
166
|
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* Insert a single log event record to the database.
|
171
|
*
|
172
|
* @param logData the data to be logged when an event occurs
|
173
|
*/
|
174
|
private void insertLogEntry(EventLogData logData)
|
175
|
{
|
176
|
String insertString = "insert into access_log"
|
177
|
+ "(ip_address, user_agent, principal, docid, "
|
178
|
+ "event, date_logged) "
|
179
|
+ "values ( ?, ?, ?, ?, ?, ? )";
|
180
|
|
181
|
DBConnection dbConn = null;
|
182
|
int serialNumber = -1;
|
183
|
try {
|
184
|
// Get a database connection from the pool
|
185
|
dbConn = DBConnectionPool.getDBConnection("EventLog.insertLogEntry");
|
186
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
187
|
String userAgent = logData.getUserAgent();
|
188
|
if(userAgent != null && userAgent.length() > USERAGENTLENGTH) {
|
189
|
userAgent = userAgent.substring(0, USERAGENTLENGTH);
|
190
|
}
|
191
|
|
192
|
// Execute the insert statement
|
193
|
PreparedStatement stmt = dbConn.prepareStatement(insertString);
|
194
|
|
195
|
stmt.setString(1, logData.getIpAddress());
|
196
|
stmt.setString(2, userAgent);
|
197
|
stmt.setString(3, logData.getPrincipal());
|
198
|
stmt.setString(4, logData.getDocid());
|
199
|
stmt.setString(5, logData.getEvent());
|
200
|
stmt.setTimestamp(6, new Timestamp(new Date().getTime()));
|
201
|
stmt.executeUpdate();
|
202
|
stmt.close();
|
203
|
} catch (SQLException e) {
|
204
|
logMetacat.error("Error while logging event to database: "
|
205
|
+ e.getMessage());
|
206
|
} finally {
|
207
|
// Return database connection to the pool
|
208
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
209
|
}
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* Get a report of the log events that match a set of filters. The
|
214
|
* filter parameters can be null; log records are subset based on
|
215
|
* non-null filter parameters.
|
216
|
*
|
217
|
* @param ipAddress the internet protocol address for the event
|
218
|
* @param principal the principal for the event (a username, etc)
|
219
|
* @param docid the identifier of the document to which the event applies
|
220
|
* @param event the string code for the event
|
221
|
* @param startDate beginning of date range for query
|
222
|
* @param endDate end of date range for query
|
223
|
* @return an XML-formatted report of the access log entries
|
224
|
*/
|
225
|
public String getReport(String[] ipAddress, String[] principal, String[] docid,
|
226
|
String[] event, Timestamp startDate, Timestamp endDate, boolean anonymous)
|
227
|
{
|
228
|
StringBuffer resultDoc = new StringBuffer();
|
229
|
StringBuffer query = new StringBuffer();
|
230
|
query.append("select entryid, ip_address, user_agent, principal, docid, "
|
231
|
+ "event, date_logged from access_log");
|
232
|
// + ""
|
233
|
// + "event, date_logged) " + "values (" + "'"
|
234
|
// + logData.getIpAddress() + "', " + "'"
|
235
|
// + logData.getPrincipal() + "', " + "'"
|
236
|
// + logData.getDocid() + "', " + "'" + logData.getEvent()
|
237
|
// + "', " + " ? " + ")";
|
238
|
if (ipAddress != null || principal != null || docid != null
|
239
|
|| event != null || startDate != null || endDate != null) {
|
240
|
query.append(" where ");
|
241
|
}
|
242
|
boolean clauseAdded = false;
|
243
|
int startIndex = 0;
|
244
|
int endIndex = 0;
|
245
|
|
246
|
List<String> paramValues = new ArrayList<String>();
|
247
|
if (ipAddress != null) {
|
248
|
query.append("ip_address in (");
|
249
|
for (int i = 0; i < ipAddress.length; i++) {
|
250
|
if (i > 0) {
|
251
|
query.append(", ");
|
252
|
}
|
253
|
query.append("?");
|
254
|
paramValues.add(ipAddress[i]);
|
255
|
}
|
256
|
query.append(") ");
|
257
|
clauseAdded = true;
|
258
|
}
|
259
|
if (principal != null) {
|
260
|
if (clauseAdded) {
|
261
|
query.append(" and ");
|
262
|
}
|
263
|
query.append("principal in (");
|
264
|
for (int i = 0; i < principal.length; i++) {
|
265
|
if (i > 0) {
|
266
|
query.append(", ");
|
267
|
}
|
268
|
query.append("?");
|
269
|
paramValues.add(principal[i]);
|
270
|
}
|
271
|
query.append(") ");
|
272
|
clauseAdded = true;
|
273
|
}
|
274
|
if (docid != null) {
|
275
|
if (clauseAdded) {
|
276
|
query.append(" and ");
|
277
|
}
|
278
|
query.append("docid in (");
|
279
|
for (int i = 0; i < docid.length; i++) {
|
280
|
if (i > 0) {
|
281
|
query.append(", ");
|
282
|
}
|
283
|
query.append("?");
|
284
|
String fullDocid = docid[i];
|
285
|
// allow docid without revision - look up latest version
|
286
|
try {
|
287
|
fullDocid = DocumentUtil.appendRev(fullDocid);
|
288
|
} catch (Exception e) {
|
289
|
// just warn about this
|
290
|
logMetacat.warn("Could not check docid for revision: " + fullDocid, e);
|
291
|
}
|
292
|
paramValues.add(fullDocid);
|
293
|
}
|
294
|
query.append(") ");
|
295
|
clauseAdded = true;
|
296
|
}
|
297
|
if (event != null) {
|
298
|
if (clauseAdded) {
|
299
|
query.append(" and ");
|
300
|
}
|
301
|
query.append("event in (");
|
302
|
for (int i = 0; i < event.length; i++) {
|
303
|
if (i > 0) {
|
304
|
query.append(", ");
|
305
|
}
|
306
|
query.append("?");
|
307
|
paramValues.add(event[i]);
|
308
|
}
|
309
|
query.append(") ");
|
310
|
clauseAdded = true;
|
311
|
}
|
312
|
if (startDate != null) {
|
313
|
if (clauseAdded) {
|
314
|
query.append(" and ");
|
315
|
}
|
316
|
query.append("date_logged >= ?");
|
317
|
clauseAdded = true;
|
318
|
startIndex++;
|
319
|
}
|
320
|
if (endDate != null) {
|
321
|
if (clauseAdded) {
|
322
|
query.append(" and ");
|
323
|
}
|
324
|
query.append("date_logged < ?");
|
325
|
clauseAdded = true;
|
326
|
endIndex = startIndex + 1;
|
327
|
}
|
328
|
DBConnection dbConn = null;
|
329
|
int serialNumber = -1;
|
330
|
try {
|
331
|
// Get a database connection from the pool
|
332
|
dbConn = DBConnectionPool.getDBConnection("EventLog.getReport");
|
333
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
334
|
|
335
|
// Execute the query statement
|
336
|
PreparedStatement stmt = dbConn.prepareStatement(query.toString());
|
337
|
//set the param values
|
338
|
int parameterIndex = 1;
|
339
|
for (String val: paramValues) {
|
340
|
stmt.setString(parameterIndex++, val);
|
341
|
}
|
342
|
if (startDate != null) {
|
343
|
stmt.setTimestamp(parameterIndex++, startDate);
|
344
|
}
|
345
|
if (endDate != null) {
|
346
|
stmt.setTimestamp(parameterIndex++, endDate);
|
347
|
}
|
348
|
stmt.execute();
|
349
|
ResultSet rs = stmt.getResultSet();
|
350
|
//process the result and return it as an XML document
|
351
|
resultDoc.append("<?xml version=\"1.0\"?>\n");
|
352
|
resultDoc.append("<log>\n");
|
353
|
while (rs.next()) {
|
354
|
resultDoc.append(
|
355
|
generateXmlRecord(
|
356
|
rs.getString(1), //id
|
357
|
anonymous ? "" : rs.getString(2), //ip
|
358
|
rs.getString(3), //userAgent
|
359
|
anonymous ? "" : rs.getString(4), //principal
|
360
|
rs.getString(5),
|
361
|
rs.getString(6),
|
362
|
rs.getTimestamp(7)));
|
363
|
}
|
364
|
resultDoc.append("</log>");
|
365
|
stmt.close();
|
366
|
} catch (SQLException e) {
|
367
|
logMetacat.info("Error while logging event to database: "
|
368
|
+ e.getMessage());
|
369
|
} finally {
|
370
|
// Return database connection to the pool
|
371
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
372
|
}
|
373
|
return resultDoc.toString();
|
374
|
}
|
375
|
|
376
|
/**
|
377
|
* A utility method to determine if the given docid was deleted.
|
378
|
* @param docid the specified docid
|
379
|
* @return true if there is a delete event for the id; false otherwise.
|
380
|
*/
|
381
|
public boolean isDeleted(String docid) {
|
382
|
boolean deleted =false;
|
383
|
if(docid != null || !docid.trim().equals("")) {
|
384
|
String[] docids = new String[1];
|
385
|
docids[0] = docid;
|
386
|
String[] events = new String[1];
|
387
|
events[0]= DELETE;
|
388
|
String[] ipAddress = null;
|
389
|
String[] principal = null;
|
390
|
Timestamp startDate = null;
|
391
|
Timestamp endDate = null;
|
392
|
boolean anonymous = false;
|
393
|
|
394
|
String report =getReport(ipAddress, principal, docids,
|
395
|
events, startDate, endDate, anonymous);
|
396
|
//System.out.println("the report is "+report);
|
397
|
if(report != null && report.contains("<event>"+DELETE+"</event>") ){
|
398
|
deleted = true;
|
399
|
}
|
400
|
}
|
401
|
return deleted;
|
402
|
}
|
403
|
|
404
|
public Log getD1Report(String[] ipAddress, String[] principal, String[] docid,
|
405
|
String event, Timestamp startDate, Timestamp endDate, boolean anonymous, Integer start, Integer count)
|
406
|
{
|
407
|
|
408
|
Log log = new Log();
|
409
|
|
410
|
NodeReference memberNode = new NodeReference();
|
411
|
String nodeId = "localhost";
|
412
|
try {
|
413
|
nodeId = PropertyService.getProperty("dataone.nodeId");
|
414
|
} catch (PropertyNotFoundException e1) {
|
415
|
// TODO Auto-generated catch block
|
416
|
e1.printStackTrace();
|
417
|
}
|
418
|
memberNode.setValue(nodeId);
|
419
|
|
420
|
String countClause = "select count(*) ";
|
421
|
String fieldsClause = "select " +
|
422
|
"entryid, " +
|
423
|
"id.guid as identifier, " +
|
424
|
"ip_address, " +
|
425
|
"user_agent, " +
|
426
|
"principal, " +
|
427
|
"case " +
|
428
|
" when event = 'insert' then 'create' " +
|
429
|
" else event " +
|
430
|
"end as event, " +
|
431
|
"date_logged ";
|
432
|
|
433
|
StringBuffer queryWhereClause = new StringBuffer();
|
434
|
queryWhereClause.append(
|
435
|
"from access_log al, identifier id " +
|
436
|
"where al.docid = id.docid||'.'||id.rev "
|
437
|
);
|
438
|
|
439
|
boolean clauseAdded = true;
|
440
|
|
441
|
List<String> paramValues = new ArrayList<String>();
|
442
|
if (ipAddress != null) {
|
443
|
if (clauseAdded) {
|
444
|
queryWhereClause.append(" and ");
|
445
|
}
|
446
|
queryWhereClause.append("ip_address in (");
|
447
|
for (int i = 0; i < ipAddress.length; i++) {
|
448
|
if (i > 0) {
|
449
|
queryWhereClause.append(", ");
|
450
|
}
|
451
|
queryWhereClause.append("?");
|
452
|
paramValues.add(ipAddress[i]);
|
453
|
}
|
454
|
queryWhereClause.append(") ");
|
455
|
clauseAdded = true;
|
456
|
}
|
457
|
if (principal != null) {
|
458
|
if (clauseAdded) {
|
459
|
queryWhereClause.append(" and ");
|
460
|
}
|
461
|
queryWhereClause.append("principal in (");
|
462
|
for (int i = 0; i < principal.length; i++) {
|
463
|
if (i > 0) {
|
464
|
queryWhereClause.append(", ");
|
465
|
}
|
466
|
queryWhereClause.append("?");
|
467
|
paramValues.add(principal[i]);
|
468
|
}
|
469
|
queryWhereClause.append(") ");
|
470
|
clauseAdded = true;
|
471
|
}
|
472
|
if (docid != null) {
|
473
|
if (clauseAdded) {
|
474
|
queryWhereClause.append(" and ");
|
475
|
}
|
476
|
queryWhereClause.append("al.docid in (");
|
477
|
for (int i = 0; i < docid.length; i++) {
|
478
|
if (i > 0) {
|
479
|
queryWhereClause.append(", ");
|
480
|
}
|
481
|
queryWhereClause.append("?");
|
482
|
paramValues.add(docid[i]);
|
483
|
}
|
484
|
queryWhereClause.append(") ");
|
485
|
clauseAdded = true;
|
486
|
}
|
487
|
if (event != null) {
|
488
|
if (clauseAdded) {
|
489
|
queryWhereClause.append(" and ");
|
490
|
}
|
491
|
queryWhereClause.append("event in (");
|
492
|
queryWhereClause.append("?");
|
493
|
String eventString = event;
|
494
|
if (eventString.equals(Event.CREATE.xmlValue())) {
|
495
|
eventString = "insert";
|
496
|
}
|
497
|
paramValues.add(eventString);
|
498
|
queryWhereClause.append(") ");
|
499
|
clauseAdded = true;
|
500
|
}
|
501
|
|
502
|
if (startDate != null) {
|
503
|
if (clauseAdded) {
|
504
|
queryWhereClause.append(" and ");
|
505
|
}
|
506
|
queryWhereClause.append("date_logged >= ?");
|
507
|
clauseAdded = true;
|
508
|
}
|
509
|
if (endDate != null) {
|
510
|
if (clauseAdded) {
|
511
|
queryWhereClause.append(" and ");
|
512
|
}
|
513
|
queryWhereClause.append("date_logged < ?");
|
514
|
clauseAdded = true;
|
515
|
}
|
516
|
|
517
|
// order by
|
518
|
String orderByClause = " order by entryid ";
|
519
|
|
520
|
// select the count
|
521
|
String countQuery = countClause + queryWhereClause.toString();
|
522
|
|
523
|
// select the fields
|
524
|
String pagedQuery = DatabaseService.getInstance().getDBAdapter().getPagedQuery(fieldsClause + queryWhereClause.toString() + orderByClause, start, count);
|
525
|
|
526
|
DBConnection dbConn = null;
|
527
|
int serialNumber = -1;
|
528
|
try {
|
529
|
// Get a database connection from the pool
|
530
|
dbConn = DBConnectionPool.getDBConnection("EventLog.getD1Report");
|
531
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
532
|
|
533
|
// Execute the query statement
|
534
|
PreparedStatement fieldsStmt = dbConn.prepareStatement(pagedQuery);
|
535
|
PreparedStatement countStmt = dbConn.prepareStatement(countQuery);
|
536
|
|
537
|
//set the param values
|
538
|
int parameterIndex = 1;
|
539
|
for (String val: paramValues) {
|
540
|
countStmt.setString(parameterIndex, val);
|
541
|
fieldsStmt.setString(parameterIndex, val);
|
542
|
parameterIndex++;
|
543
|
}
|
544
|
if (startDate != null) {
|
545
|
countStmt.setTimestamp(parameterIndex, startDate);
|
546
|
fieldsStmt.setTimestamp(parameterIndex, startDate);
|
547
|
parameterIndex++;
|
548
|
}
|
549
|
if (endDate != null) {
|
550
|
countStmt.setTimestamp(parameterIndex, endDate);
|
551
|
fieldsStmt.setTimestamp(parameterIndex, endDate);
|
552
|
parameterIndex++;
|
553
|
}
|
554
|
|
555
|
// for the return Log list
|
556
|
List<LogEntry> logs = new Vector<LogEntry>();
|
557
|
|
558
|
// get the fields form the query
|
559
|
if (count != 0) {
|
560
|
fieldsStmt.execute();
|
561
|
ResultSet rs = fieldsStmt.getResultSet();
|
562
|
//process the result and return it
|
563
|
while (rs.next()) {
|
564
|
LogEntry logEntry = new LogEntry();
|
565
|
logEntry.setEntryId(rs.getString(1));
|
566
|
|
567
|
Identifier identifier = new Identifier();
|
568
|
identifier.setValue(rs.getString(2));
|
569
|
logEntry.setIdentifier(identifier);
|
570
|
|
571
|
logEntry.setIpAddress(anonymous ? "N/A" : rs.getString(3));
|
572
|
String userAgent = "N/A";
|
573
|
if (rs.getString(4) != null) {
|
574
|
userAgent = rs.getString(4);
|
575
|
}
|
576
|
logEntry.setUserAgent(userAgent);
|
577
|
|
578
|
Subject subject = new Subject();
|
579
|
subject.setValue(anonymous ? "N/A" : rs.getString(5));
|
580
|
logEntry.setSubject(subject);
|
581
|
|
582
|
String logEventString = rs.getString(6);
|
583
|
logEntry.setEvent(logEventString);
|
584
|
logEntry.setDateLogged(rs.getTimestamp(7));
|
585
|
|
586
|
logEntry.setNodeIdentifier(memberNode);
|
587
|
logs.add(logEntry);
|
588
|
}
|
589
|
fieldsStmt.close();
|
590
|
}
|
591
|
|
592
|
// set what we have
|
593
|
log.setLogEntryList(logs);
|
594
|
log.setStart(start);
|
595
|
log.setCount(logs.size());
|
596
|
|
597
|
// get total for out query
|
598
|
int total = 0;
|
599
|
countStmt.execute();
|
600
|
ResultSet countRs = countStmt.getResultSet();
|
601
|
if (countRs.next()) {
|
602
|
total = countRs.getInt(1);
|
603
|
}
|
604
|
countStmt.close();
|
605
|
log.setTotal(total);
|
606
|
|
607
|
} catch (SQLException e) {
|
608
|
logMetacat.error("Error while getting log events: " + e.getMessage(), e);
|
609
|
} finally {
|
610
|
// Return database connection to the pool
|
611
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
612
|
}
|
613
|
return log;
|
614
|
}
|
615
|
|
616
|
/**
|
617
|
* Format each returned log record as an XML structure.
|
618
|
*
|
619
|
* @param entryId the identifier of the log entry
|
620
|
* @param ipAddress the internet protocol address for the event
|
621
|
* @param the agent making the request
|
622
|
* @param principal the principal for the event (a username, etc)
|
623
|
* @param docid the identifier of the document to which the event applies
|
624
|
* @param event the string code for the event
|
625
|
* @param dateLogged the date on which the event occurred
|
626
|
* @return String containing the formatted XML
|
627
|
*/
|
628
|
private String generateXmlRecord(String entryId, String ipAddress, String userAgent,
|
629
|
String principal, String docid, String event, Timestamp dateLogged)
|
630
|
{
|
631
|
StringBuffer rec = new StringBuffer();
|
632
|
rec.append("<logEntry>");
|
633
|
rec.append(generateXmlElement("entryid", entryId));
|
634
|
rec.append(generateXmlElement("ipAddress", ipAddress));
|
635
|
rec.append(generateXmlElement("userAgent", userAgent));
|
636
|
rec.append(generateXmlElement("principal", principal));
|
637
|
rec.append(generateXmlElement("docid", docid));
|
638
|
rec.append(generateXmlElement("event", event));
|
639
|
rec.append(generateXmlElement("dateLogged", DateTimeMarshaller.serializeDateToUTC(dateLogged)));
|
640
|
rec.append("</logEntry>\n");
|
641
|
|
642
|
return rec.toString();
|
643
|
}
|
644
|
|
645
|
/**
|
646
|
* Return an XML formatted element for a given name/value pair.
|
647
|
*
|
648
|
* @param name the name of the xml element
|
649
|
* @param value the content of the xml element
|
650
|
* @return the formatted XML element as a String
|
651
|
*/
|
652
|
private String generateXmlElement(String name, String value)
|
653
|
{
|
654
|
return "<" + name + ">" + value + "</" + name + ">";
|
655
|
}
|
656
|
}
|