Project

General

Profile

« Previous | Next » 

Revision 2312

Added in servlet action 'buildindex' for building the XML_index table entries
for either a set of documents (if one or more docid params are provided) or
for the whole set of documents in the xml_documents table. The buildindex
action is restricted to only be accessible by users who are listed in the
metacat.properties file as 'administrators'. So this is a new category of
users that we can use to control administrative actions. In addition, I
restricted access to the 'getlog' action of metacat to the administrative
users.

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
43 43
import java.text.SimpleDateFormat;
44 44
import java.util.Enumeration;
45 45
import java.util.Hashtable;
46
import java.util.Iterator;
46 47
import java.util.PropertyResourceBundle;
47 48
import java.util.Vector;
49
import java.util.regex.PatternSyntaxException;
48 50
import java.util.zip.ZipEntry;
49 51
import java.util.zip.ZipOutputStream;
50 52

  
......
131 133

  
132 134
    private String htmlpath = null;
133 135

  
136
    private String[] administrators = null;
137

  
134 138
    private PropertyResourceBundle options = null;
135 139

  
136 140
    private MetaCatUtil util = null;
......
197 201
            dataDirectory = new File(datafilepath);
198 202
            servletpath = MetaCatUtil.getOption("servletpath");
199 203
            htmlpath = MetaCatUtil.getOption("htmlpath");
204
            String adminList = MetaCatUtil.getOption("administrators");
205
            try {
206
                administrators = adminList.split(":");
207
            } catch (PatternSyntaxException pse) {
208
                administrators = null;
209
                MetaCatUtil.debugMessage("Error in MetacatServlet.init: "
210
                    + pse.getMessage(), 20);
211
            }
200 212

  
201 213
            System.out.println("Metacat (" + Version.getVersion()
202 214
                    + ") initialized.");
......
497 509
                out.println(Version.getVersionAsXml());
498 510
                out.close();
499 511
            } else if (action.equals("getlog")) {
500
                handleGetLogAction(params, request, response);
512
                handleGetLogAction(params, request, response, username);
513
            } else if (action.equals("buildindex")) {
514
                handleBuildIndexAction(params, request, response, username);
501 515
            } else if (action.equals("login") || action.equals("logout")) {
516
                /*
502 517
            } else if (action.equals("protocoltest")) {
503 518
                String testURL = "metacat://dev.nceas.ucsb.edu/NCEAS.897766.9";
504 519
                try {
......
544 559
                    mue.printStackTrace(out);
545 560
                    out.close();
546 561
                }
562
                */
547 563
            } else {
548 564
                PrintWriter out = response.getWriter();
549 565
                out.println("<?xml version=\"1.0\"?>");
......
2091 2107
     * Print a report from the event log based on filter parameters passed in
2092 2108
     * from the web.
2093 2109
     *
2094
     * TODO: make sure urlencoding of timestamp params is working
2095
     *
2096 2110
     * @param params the parameters from the web request
2097 2111
     * @param request the http request object for getting request details
2098 2112
     * @param response the http response object for writing output
2099 2113
     */
2100 2114
    private void handleGetLogAction(Hashtable params, HttpServletRequest request,
2101
            HttpServletResponse response)
2115
            HttpServletResponse response, String username)
2102 2116
    {
2103
        // Get all of the parameters in the correct formats
2104
        String[] ipAddress = (String[])params.get("ipaddress");
2105
        String[] principal = (String[])params.get("principal");
2106
        String[] docid = (String[])params.get("docid");
2107
        String[] event = (String[])params.get("event");
2108
        String[] startArray = (String[]) params.get("start");
2109
        String[] endArray = (String[]) params.get("end");
2110
        String start = null;
2111
        String end = null;
2112
        if (startArray != null) {
2113
            start = startArray[0];
2114
        }
2115
        if (endArray != null) {
2116
            end = endArray[0];
2117
        }
2118
        Timestamp startDate = null;
2119
        Timestamp endDate = null;
2120
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
2121 2117
        try {
2122
            if (start != null) {
2123
                startDate = new Timestamp((format.parse(start)).getTime());
2118
            response.setContentType("text/xml");
2119
            PrintWriter out = response.getWriter();
2120
            
2121
            // Check that the user is authenticated as an administrator account
2122
            boolean adminIsAuthenticated = false;
2123
            for (int i = 0; i < administrators.length; i++) {
2124
                if (username.equals(administrators[i])) {
2125
                        adminIsAuthenticated = true;
2126
                }
2124 2127
            }
2125
            if (end != null) {
2126
                endDate = new Timestamp((format.parse(end)).getTime());
2128
            if (!adminIsAuthenticated) {
2129
                out.print("<error>");
2130
                out.print("The user \"" + username + 
2131
                        "\" is not authorized for this action.");
2132
                out.print("</error>");
2133
                return;
2127 2134
            }
2128
        } catch (ParseException e) {
2129
            System.out.println("Failed to created Timestamp from input.");
2135
    
2136
            // Get all of the parameters in the correct formats
2137
            String[] ipAddress = (String[])params.get("ipaddress");
2138
            String[] principal = (String[])params.get("principal");
2139
            String[] docid = (String[])params.get("docid");
2140
            String[] event = (String[])params.get("event");
2141
            String[] startArray = (String[]) params.get("start");
2142
            String[] endArray = (String[]) params.get("end");
2143
            String start = null;
2144
            String end = null;
2145
            if (startArray != null) {
2146
                start = startArray[0];
2147
            }
2148
            if (endArray != null) {
2149
                end = endArray[0];
2150
            }
2151
            Timestamp startDate = null;
2152
            Timestamp endDate = null;
2153
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
2154
            try {
2155
                if (start != null) {
2156
                    startDate = new Timestamp((format.parse(start)).getTime());
2157
                }
2158
                if (end != null) {
2159
                    endDate = new Timestamp((format.parse(end)).getTime());
2160
                }
2161
            } catch (ParseException e) {
2162
                System.out.println("Failed to created Timestamp from input.");
2163
            }
2164
    
2165
            // Request the report by passing the filter parameters
2166
            out.println(EventLog.getInstance().getReport(ipAddress, principal,
2167
                    docid, event, startDate, endDate));
2168
            out.close();
2169
        } catch (IOException e) {
2170
            MetaCatUtil.debugMessage(
2171
                    "Could not open http response for writing: " + e.getMessage(), 5);
2130 2172
        }
2173
    }
2131 2174

  
2132
        // Request the report by passing the filter parameters
2175
    /**
2176
     * Rebuild the index for one or more documents. If the docid parameter is
2177
     * provided, rebuild for just that one document or list of documents. If
2178
     * not, then rebuild the index for all documents in the xml_documents
2179
     * table.
2180
     *
2181
     * @param params the parameters from the web request
2182
     * @param request the http request object for getting request details
2183
     * @param response the http response object for writing output
2184
     * @param username the username of the authenticated user
2185
     */
2186
    private void handleBuildIndexAction(Hashtable params, 
2187
            HttpServletRequest request, HttpServletResponse response,
2188
            String username)
2189
    {
2190
        // Get all of the parameters in the correct formats
2191
        String[] docid = (String[])params.get("docid");
2192

  
2193
        // Rebuild the indices for appropriate documents
2133 2194
        try {
2134 2195
            response.setContentType("text/xml");
2135 2196
            PrintWriter out = response.getWriter();
2136
            out.println(EventLog.getInstance().getReport(ipAddress, principal,
2137
                    docid, event, startDate, endDate));
2197
            
2198
            // Check that the user is authenticated as an administrator account
2199
            boolean adminIsAuthenticated = false;
2200
            for (int i = 0; i < administrators.length; i++) {
2201
                if (username.equals(administrators[i])) {
2202
                        adminIsAuthenticated = true;
2203
                }
2204
            }
2205
            if (!adminIsAuthenticated) {
2206
                out.print("<error>");
2207
                out.print("The user \"" + username + 
2208
                        "\" is not authorized for this action.");
2209
                out.print("</error>");
2210
                return;
2211
            }
2212

  
2213
            // Process the documents
2214
            out.println("<success>");
2215
            if (docid == null || docid.length == 0) {
2216
                // Process all of the documents
2217
                try {
2218
                    Vector documents = getDocumentList();
2219
                    Iterator it = documents.iterator();
2220
                    while (it.hasNext()) {
2221
                        String id = (String) it.next();
2222
                        buildDocumentIndex(id, out);
2223
                    }
2224
                } catch (SQLException se) {
2225
                    out.print("<error>");
2226
                    out.print(se.getMessage());
2227
                    out.println("</error>");
2228
                }
2229
            } else {
2230
                // Only process the requested documents
2231
                for (int i = 0; i < docid.length; i++) {
2232
                    buildDocumentIndex(docid[i], out);
2233
                }
2234
            }
2235
            out.println("</success>");
2138 2236
            out.close();
2139 2237
        } catch (IOException e) {
2140 2238
            MetaCatUtil.debugMessage(
......
2144 2242
    }
2145 2243

  
2146 2244
    /**
2245
     * Build the index for one document by reading the document and 
2246
     * calling its buildIndex() method.
2247
     *
2248
     * @param docid the document (with revision) to rebuild
2249
     * @param out the PrintWriter to which output is printed
2250
     */
2251
    private void buildDocumentIndex(String docid, PrintWriter out)
2252
    {
2253
        try {
2254
            DocumentImpl doc = new DocumentImpl(docid, false);
2255
            doc.buildIndex();
2256
            out.print("<docid>" + docid);
2257
            out.println("</docid>");
2258
        } catch (McdbException me) {
2259
            out.print("<error>");
2260
            out.print(me.getMessage());
2261
            out.println("</error>");
2262
        }
2263
    }
2264

  
2265
    /**
2147 2266
     * Handle documents passed to metacat that are encoded using the
2148 2267
     * "multipart/form-data" mime type. This is typically used for uploading
2149 2268
     * data files which may be binary and large.
......
2554 2673
    }
2555 2674

  
2556 2675
    /*
2676
     * Get the list of documents from the database and return the list in an
2677
     * Vector of identifiers.
2678
     *
2679
     * @ returns the array of identifiers
2680
     */
2681
    private Vector getDocumentList() throws SQLException
2682
    {
2683
        Vector docList = new Vector();
2684
        PreparedStatement pstmt = null;
2685
        ResultSet rs = null;
2686
        DBConnection conn = null;
2687
        int serialNumber = -1;
2688

  
2689
        try {
2690
            //check out DBConnection
2691
            conn = DBConnectionPool
2692
                    .getDBConnection("MetaCatServlet.getDocumentList");
2693
            serialNumber = conn.getCheckOutSerialNumber();
2694
            pstmt = conn.prepareStatement("SELECT docid, rev"
2695
                    + " FROM xml_documents ");
2696
            pstmt.execute();
2697
            rs = pstmt.getResultSet();
2698
            while (rs.next()) {
2699
                String docid = rs.getString(1);
2700
                String rev = rs.getString(2);
2701
                docList.add(docid + "." + rev);
2702
            }
2703
        } catch (SQLException e) {
2704
            MetaCatUtil.debugMessage(
2705
                    "Exception in MetacatServlet.getDocumentList: "
2706
                            + e.getMessage(), 30);
2707
            throw e;
2708
        } finally {
2709
            try {
2710
                rs.close();
2711
                pstmt.close();
2712

  
2713
            } catch (SQLException se) {
2714
                MetaCatUtil.debugMessage(
2715
                    "Exception in MetacatServlet.getDocumentList: "
2716
                            + se.getMessage(), 30);
2717
                throw se;
2718
            } finally {
2719
                DBConnectionPool.returnDBConnection(conn, serialNumber);
2720
            }
2721
        }
2722
        return docList;
2723
    }
2724

  
2725
    /*
2557 2726
     * A method to output setAccess action result
2558 2727
     */
2559 2728
    private void outputResponse(Vector successList, Vector errorList,

Also available in: Unified diff