Project

General

Profile

Revision 2113

Added by Matt Jones over 16 years ago

Added the EventLog functionality to the servlet as action=getlog. Valid parameters are ipAddress, principal, docid, event, start, end. All parameters can be repeated except start and end.

View differences:

test/edu/ucsb/nceas/metacattest/EventLogTest.java
29 29
import java.sql.Timestamp;
30 30
import java.text.ParseException;
31 31
import java.text.SimpleDateFormat;
32
import java.util.Date;
33 32

  
34 33
import edu.ucsb.nceas.metacat.DBConnectionPool;
35 34
import edu.ucsb.nceas.metacat.EventLog;
......
93 92
        Timestamp startDate = null;
94 93
        Timestamp endDate = null;
95 94
        try {
96
            startDate = new Timestamp((format.parse("2004-04-07 09:32:00")).getTime());
97
            endDate = new Timestamp((format.parse("2004-04-07 09:50:00")).getTime());
95
            startDate = new Timestamp((format.parse("2004-04-08 02:32:00")).getTime());
96
            endDate = new Timestamp((format.parse("2004-04-08 11:20:00")).getTime());
98 97
        } catch (ParseException e) {
99 98
            System.out.println("Failed to created endDate from format.");
100 99
        }
src/edu/ucsb/nceas/metacat/EventLog.java
171 171
        int endIndex = 0;
172 172
        
173 173
        if (ipAddress != null) {
174
            query.append(formatSqlClause(clauseAdded, "ip_address", ipAddress));
174
            query.append(generateSqlClause(clauseAdded, "ip_address", ipAddress));
175 175
            clauseAdded = true;
176 176
        }
177 177
        if (principal != null) {
178
            query.append(formatSqlClause(clauseAdded, "principal", principal));
178
            query.append(generateSqlClause(clauseAdded, "principal", principal));
179 179
            clauseAdded = true;
180 180
        }
181 181
        if (docid != null) {
182
            query.append(formatSqlClause(clauseAdded, "docid", docid));
182
            query.append(generateSqlClause(clauseAdded, "docid", docid));
183 183
            clauseAdded = true;
184 184
        }
185 185
        if (event != null) {
186
            query.append(formatSqlClause(clauseAdded, "event", event));
186
            query.append(generateSqlClause(clauseAdded, "event", event));
187 187
            clauseAdded = true;
188 188
        }
189 189
        if (startDate != null) {
......
209 209
            dbConn = DBConnectionPool.getDBConnection("EventLog.getReport");
210 210
            serialNumber = dbConn.getCheckOutSerialNumber();
211 211

  
212
            // Execute the insert statement
213
            System.out.println(query.toString());
212
            // Execute the query statement
214 213
            PreparedStatement stmt = dbConn.prepareStatement(query.toString());
215 214
            if (startIndex > 0) {
216 215
                stmt.setTimestamp(startIndex, startDate); 
......
220 219
            }
221 220
            stmt.execute();
222 221
            ResultSet rs = stmt.getResultSet();
223
            //process the result
222
            //process the result and return it as an XML document
224 223
            resultDoc.append("<?xml version=\"1.0\"?>\n");
225 224
            resultDoc.append("<log>\n");
226 225
            while (rs.next()) {
227
                resultDoc.append(formatXmlRecord(rs.getString(1), rs.getString(2),
226
                resultDoc.append(generateXmlRecord(rs.getString(1), rs.getString(2),
228 227
                                rs.getString(3), rs.getString(4), 
229 228
                                rs.getString(5), rs.getTimestamp(6)));
230 229
            }
......
250 249
     * @param values the values to match in the SQL query
251 250
     * @return a String representation of the SQL query clause
252 251
     */
253
    private String formatSqlClause(boolean addOperator, String column, String[] values)
252
    private String generateSqlClause(boolean addOperator, String column, 
253
            String[] values)
254 254
    {
255 255
        StringBuffer clause = new StringBuffer();
256 256
        if (addOperator) {
......
281 281
     * @param dateLogged the date on which the event occurred
282 282
     * @return String containing the formatted XML
283 283
     */
284
    private String formatXmlRecord(String entryId, String ipAddress, String principal, 
285
                    String docid, String event, Timestamp dateLogged)
284
    private String generateXmlRecord(String entryId, String ipAddress, 
285
            String principal, String docid, String event, Timestamp dateLogged)
286 286
    {
287 287
        StringBuffer rec = new StringBuffer();
288 288
        rec.append("<logEntry>");
289
        rec.append(formatXmlElement("entryid", entryId));
290
        rec.append(formatXmlElement("ipAddress", ipAddress));
291
        rec.append(formatXmlElement("principal", principal));
292
        rec.append(formatXmlElement("docid", docid));
293
        rec.append(formatXmlElement("event", event));
294
        rec.append(formatXmlElement("dateLogged", dateLogged.toString()));
289
        rec.append(generateXmlElement("entryid", entryId));
290
        rec.append(generateXmlElement("ipAddress", ipAddress));
291
        rec.append(generateXmlElement("principal", principal));
292
        rec.append(generateXmlElement("docid", docid));
293
        rec.append(generateXmlElement("event", event));
294
        rec.append(generateXmlElement("dateLogged", dateLogged.toString()));
295 295
        rec.append("</logEntry>\n");
296 296

  
297 297
        return rec.toString();
......
304 304
     * @param value the content of the xml element
305 305
     * @return the formatted XML element as a String
306 306
     */
307
    private String formatXmlElement(String name, String value)
307
    private String generateXmlElement(String name, String value)
308 308
    {
309 309
        return "<" + name + ">" + value + "</" + name + ">";
310 310
    }
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
38 38
import java.sql.PreparedStatement;
39 39
import java.sql.ResultSet;
40 40
import java.sql.SQLException;
41
import java.sql.Timestamp;
42
import java.text.ParseException;
43
import java.text.SimpleDateFormat;
41 44
import java.util.Enumeration;
42 45
import java.util.Hashtable;
43 46
import java.util.PropertyResourceBundle;
......
95 98
 * action=getdataguide -- retrieve a Data Guide <br>
96 99
 * action=getprincipals -- retrieve a list of principals in XML <br>
97 100
 * datadoc -- data document name (id) <br>
101
 * action=getlog -- get a report of events that have occurred in the system<br>
102
 * ipAddress --  filter on one or more IP addresses<br>
103
 * principal -- filter on one or more principals (LDAP DN syntax)<br>
104
 * docid -- filter on one or more document identifiers (with revision)<br>
105
 * event -- filter on event type (e.g., read, insert, update, delete)<br>
106
 * start -- filter out events before the start date-time<br>
107
 * end -- filter out events before the end date-time<br>
98 108
 * <p>
99 109
 * The particular combination of parameters that are valid for each particular
100 110
 * action value is quite specific. This documentation will be reorganized to
......
205 215
    {
206 216
        // Close all db connection
207 217
        System.out.println("Destroying MetacatServlet");
208
        connPool.release();
218
        DBConnectionPool.release();
209 219
    }
210 220

  
211 221
    /** Handle "GET" method requests from HTTP clients */
......
467 477
                PrintWriter out = response.getWriter();
468 478
                out.println(Version.getVersionAsXml());
469 479
                out.close();
480
            } else if (action.equals("getlog")) {
481
                handleGetLogAction(params, request, response);
470 482
            } else if (action.equals("login") || action.equals("logout")) {
471 483
            } else if (action.equals("protocoltest")) {
472 484
                String testURL = "metacat://dev.nceas.ucsb.edu/NCEAS.897766.9";
......
1139 1151
            DocumentImpl doc = new DocumentImpl(docid);
1140 1152

  
1141 1153
            //check the permission for read
1142
            if (!doc.hasReadPermission(user, groups, docid)) {
1154
            if (!DocumentImpl.hasReadPermission(user, groups, docid)) {
1143 1155
                Exception e = new Exception("User " + user
1144 1156
                        + " does not have permission"
1145 1157
                        + " to read the document with the docid " + docid);
......
1312 1324
                DocumentImpl doc = new DocumentImpl(docid);
1313 1325

  
1314 1326
                //check the permission for read
1315
                if (!doc.hasReadPermission(user, groups, docid)) {
1327
                if (!DocumentImpl.hasReadPermission(user, groups, docid)) {
1316 1328
                    Exception e = new Exception("User " + user
1317 1329
                            + " does not have "
1318 1330
                            + "permission to read the document with the docid "
......
1980 1992
            out.println(e.getMessage());
1981 1993
            out.println("</error>");
1982 1994
        }
1983

  
1984 1995
    }
1985 1996

  
1986 1997
    /**
1998
     * Print a report from the event log based on filter parameters passed in
1999
     * from the web.
2000
     * 
2001
     * TODO: make sure urlencoding of timestamp params is working
2002
     * 
2003
     * @param params the parameters from the web request
2004
     * @param request the http request object for getting request details
2005
     * @param response the http response object for writing output
2006
     */
2007
    private void handleGetLogAction(Hashtable params, HttpServletRequest request,
2008
            HttpServletResponse response)
2009
    {
2010
        // Get all of the parameters in the correct formats
2011
        String[] ipAddress = (String[])params.get("ipaddress");
2012
        String[] principal = (String[])params.get("principal");
2013
        String[] docid = (String[])params.get("docid");
2014
        String[] event = (String[])params.get("event");
2015
        String[] startArray = (String[]) params.get("start");
2016
        String[] endArray = (String[]) params.get("end");
2017
        String start = null;
2018
        String end = null;
2019
        if (startArray != null) {
2020
            start = startArray[0];
2021
        }
2022
        if (endArray != null) {
2023
            end = endArray[0];
2024
        }
2025
        Timestamp startDate = null;
2026
        Timestamp endDate = null;
2027
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
2028
        try {
2029
            if (start != null) {
2030
                startDate = new Timestamp((format.parse(start)).getTime());
2031
            }
2032
            if (end != null) {
2033
                endDate = new Timestamp((format.parse(end)).getTime());
2034
            }
2035
        } catch (ParseException e) {
2036
            System.out.println("Failed to created Timestamp from input.");
2037
        }
2038
        
2039
        // Request the report by passing the filter parameters
2040
        try {
2041
            response.setContentType("text/xml");
2042
            PrintWriter out = response.getWriter();
2043
            out.println(EventLog.getInstance().getReport(ipAddress, principal, 
2044
                    docid, event, startDate, endDate));
2045
            out.close();
2046
        } catch (IOException e) {
2047
            MetaCatUtil.debugMessage(
2048
                    "Could not open http response for writing: " 
2049
                    + e.getMessage(), 5);
2050
        }
2051
    }
2052
    
2053
    /**
1987 2054
     * Handle documents passed to metacat that are encoded using the
1988 2055
     * "multipart/form-data" mime type. This is typically used for uploading
1989 2056
     * data files which may be binary and large.

Also available in: Unified diff