Project

General

Profile

1 51 jones
/**
2 203 jones
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements a metadata catalog as a java Servlet
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6 361 berkley
 *    Authors: Matt Jones, Dan Higgins, Jivka Bojilova, Chad Berkley
7 348 jones
 *    Release: @release@
8 154 jones
 *
9 203 jones
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12 669 jones
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 51 jones
 */
27
28
package edu.ucsb.nceas.metacat;
29
30 2098 jones
import java.io.BufferedInputStream;
31 733 bojilova
import java.io.File;
32 2098 jones
import java.io.FileInputStream;
33
import java.io.IOException;
34 46 jones
import java.io.PrintWriter;
35 50 jones
import java.io.StringReader;
36 2098 jones
import java.net.MalformedURLException;
37
import java.net.URL;
38
import java.sql.PreparedStatement;
39
import java.sql.ResultSet;
40
import java.sql.SQLException;
41 2113 jones
import java.sql.Timestamp;
42
import java.text.ParseException;
43
import java.text.SimpleDateFormat;
44 46 jones
import java.util.Enumeration;
45
import java.util.Hashtable;
46 2312 jones
import java.util.Iterator;
47 82 jones
import java.util.PropertyResourceBundle;
48 1369 tao
import java.util.Vector;
49 2312 jones
import java.util.regex.PatternSyntaxException;
50 2098 jones
import java.util.zip.ZipEntry;
51
import java.util.zip.ZipOutputStream;
52 46 jones
53
import javax.servlet.ServletConfig;
54
import javax.servlet.ServletContext;
55
import javax.servlet.ServletException;
56 2098 jones
import javax.servlet.ServletOutputStream;
57 46 jones
import javax.servlet.http.HttpServlet;
58
import javax.servlet.http.HttpServletRequest;
59
import javax.servlet.http.HttpServletResponse;
60 210 bojilova
import javax.servlet.http.HttpSession;
61 46 jones
62 1951 jones
import edu.ucsb.nceas.utilities.Options;
63 1471 tao
64 2098 jones
import org.ecoinformatics.eml.EMLParser;
65 204 jones
66 2098 jones
import com.oreilly.servlet.multipart.FilePart;
67
import com.oreilly.servlet.multipart.MultipartParser;
68
import com.oreilly.servlet.multipart.ParamPart;
69
import com.oreilly.servlet.multipart.Part;
70
71 46 jones
/**
72
 * A metadata catalog server implemented as a Java Servlet
73 2169 sgarg
 *
74 205 jones
 * <p>
75 2098 jones
 * Valid parameters are: <br>
76
 * action=query -- query the values of all elements and attributes and return a
77
 * result set of nodes <br>
78
 * action=squery -- structured query (see pathquery.dtd) <br>
79
 * action= -- export a zip format for data packadge <br>
80
 * action=read -- read any metadata/data file from Metacat and from Internet
81
 * <br>
82
 * action=insert -- insert an XML document into the database store <br>
83
 * action=update -- update an XML document that is in the database store <br>
84
 * action=delete -- delete an XML document from the database store <br>
85
 * action=validate -- vallidate the xml contained in valtext <br>
86
 * doctype -- document type list returned by the query (publicID) <br>
87
 * qformat=xml -- display resultset from query in XML <br>
88
 * qformat=html -- display resultset from query in HTML <br>
89
 * qformat=zip -- zip resultset from query <br>
90
 * docid=34 -- display the document with the document ID number 34 <br>
91
 * doctext -- XML text of the document to load into the database <br>
92
 * acltext -- XML access text for a document to load into the database <br>
93
 * dtdtext -- XML DTD text for a new DTD to load into Metacat XML Catalog <br>
94
 * query -- actual query text (to go with 'action=query' or 'action=squery')
95
 * <br>
96
 * valtext -- XML text to be validated <br>
97
 * action=getaccesscontrol -- retrieve acl info for Metacat document <br>
98
 * action=getdoctypes -- retrieve all doctypes (publicID) <br>
99
 * action=getdtdschema -- retrieve a DTD or Schema file <br>
100
 * action=getdataguide -- retrieve a Data Guide <br>
101
 * action=getprincipals -- retrieve a list of principals in XML <br>
102
 * datadoc -- data document name (id) <br>
103 2113 jones
 * action=getlog -- get a report of events that have occurred in the system<br>
104
 * ipAddress --  filter on one or more IP addresses<br>
105
 * principal -- filter on one or more principals (LDAP DN syntax)<br>
106
 * docid -- filter on one or more document identifiers (with revision)<br>
107
 * event -- filter on event type (e.g., read, insert, update, delete)<br>
108
 * start -- filter out events before the start date-time<br>
109
 * end -- filter out events before the end date-time<br>
110 2098 jones
 * <p>
111
 * The particular combination of parameters that are valid for each particular
112
 * action value is quite specific. This documentation will be reorganized to
113
 * reflect this information.
114 46 jones
 */
115 2098 jones
public class MetaCatServlet extends HttpServlet
116
{
117 46 jones
118 2098 jones
    private ServletConfig config = null;
119 380 jones
120 2098 jones
    private ServletContext context = null;
121 82 jones
122 2098 jones
    private String resultStyleURL = null;
123 1951 jones
124 2098 jones
    private String xmlcatalogfile = null;
125 1360 tao
126 2098 jones
    private String saxparser = null;
127 184 jones
128 2098 jones
    private String datafilepath = null;
129 598 bojilova
130 2098 jones
    private File dataDirectory = null;
131 1217 tao
132 2098 jones
    private String servletpath = null;
133 46 jones
134 2098 jones
    private String htmlpath = null;
135 320 bojilova
136 2312 jones
    private String[] administrators = null;
137
138 2098 jones
    private PropertyResourceBundle options = null;
139 46 jones
140 2098 jones
    private MetaCatUtil util = null;
141 48 jones
142 2098 jones
    private DBConnectionPool connPool = null;
143 48 jones
144 2098 jones
    private Hashtable sessionHash = new Hashtable();
145 48 jones
146 2098 jones
    private static final String PROLOG = "<?xml version=\"1.0\"?>";
147 48 jones
148 2098 jones
    private static final String SUCCESS = "<success>";
149 1360 tao
150 2098 jones
    private static final String SUCCESSCLOSE = "</success>";
151 1360 tao
152 2098 jones
    private static final String ERROR = "<error>";
153 1360 tao
154 2098 jones
    private static final String ERRORCLOSE = "</error>";
155 1360 tao
156 2098 jones
    public static final String SCHEMALOCATIONKEYWORD = ":schemaLocation";
157 1360 tao
158 2098 jones
    public static final String NONAMESPACELOCATION = ":noNamespaceSchemaLocation";
159 1360 tao
160 2098 jones
    public static final String EML2KEYWORD = ":eml";
161 1360 tao
162 2098 jones
    public static final String XMLFORMAT = "xml";
163 1360 tao
164 2098 jones
    private static final String CONFIG_DIR = "WEB-INF";
165 1360 tao
166 2098 jones
    private static final String CONFIG_NAME = "metacat.properties";
167 1360 tao
168 2098 jones
    /**
169
     * Initialize the servlet by creating appropriate database connections
170
     */
171
    public void init(ServletConfig config) throws ServletException
172
    {
173
        try {
174
            super.init(config);
175
            this.config = config;
176
            this.context = config.getServletContext();
177 1360 tao
178 2098 jones
            // Initialize the properties file for our options
179
            String dirPath = context.getRealPath(CONFIG_DIR);
180
            File propertyFile = new File(dirPath, CONFIG_NAME);
181
            Options options = null;
182
            try {
183
                options = Options.initialize(propertyFile);
184
                MetaCatUtil.debugMessage("Options configured: "
185
                        + options.getOption("configured"), 20);
186
            } catch (IOException ioe) {
187
                MetaCatUtil.debugMessage("Error in loading options: "
188
                        + ioe.getMessage(), 20);
189
            }
190 1716 berkley
191 2098 jones
            util = new MetaCatUtil();
192 1360 tao
193 2098 jones
            //initial DBConnection pool
194
            connPool = DBConnectionPool.getInstance();
195 1360 tao
196 2098 jones
            // Get the configuration file information
197
            resultStyleURL = MetaCatUtil.getOption("resultStyleURL");
198
            xmlcatalogfile = MetaCatUtil.getOption("xmlcatalogfile");
199
            saxparser = MetaCatUtil.getOption("saxparser");
200
            datafilepath = MetaCatUtil.getOption("datafilepath");
201
            dataDirectory = new File(datafilepath);
202
            servletpath = MetaCatUtil.getOption("servletpath");
203
            htmlpath = MetaCatUtil.getOption("htmlpath");
204 2312 jones
            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
            }
212 1360 tao
213 2098 jones
            System.out.println("Metacat (" + Version.getVersion()
214
                    + ") initialized.");
215
        } catch (ServletException ex) {
216
            throw ex;
217
        } catch (SQLException e) {
218
            MetaCatUtil.debugMessage("Error in MetacatServlet.init: "
219
                    + e.getMessage(), 20);
220 1221 tao
        }
221 2098 jones
    }
222 1360 tao
223 2098 jones
    /**
224
     * Close all db connections from the pool
225
     */
226
    public void destroy()
227
    {
228
        // Close all db connection
229
        System.out.println("Destroying MetacatServlet");
230 2113 jones
        DBConnectionPool.release();
231 2098 jones
    }
232 1723 berkley
233 2098 jones
    /** Handle "GET" method requests from HTTP clients */
234
    public void doGet(HttpServletRequest request, HttpServletResponse response)
235
            throws ServletException, IOException
236
    {
237 2089 tao
238 2098 jones
        // Process the data and send back the response
239
        handleGetOrPost(request, response);
240 46 jones
    }
241 1360 tao
242 2098 jones
    /** Handle "POST" method requests from HTTP clients */
243
    public void doPost(HttpServletRequest request, HttpServletResponse response)
244
            throws ServletException, IOException
245
    {
246 251 bojilova
247 2098 jones
        // Process the data and send back the response
248
        handleGetOrPost(request, response);
249 297 bojilova
    }
250 2089 tao
251 2098 jones
    /**
252
     * Control servlet response depending on the action parameter specified
253
     */
254
    private void handleGetOrPost(HttpServletRequest request,
255
            HttpServletResponse response) throws ServletException, IOException
256 2045 tao
    {
257 2089 tao
258 2098 jones
        if (util == null) {
259
            util = new MetaCatUtil();
260
        }
261
        /*
262
         * MetaCatUtil.debugMessage("Connection pool size: "
263
         * +connPool.getSizeOfDBConnectionPool(),10);
264
         * MetaCatUtil.debugMessage("Free DBConnection number: "
265
         */
266
        //If all DBConnection in the pool are free and DBConnection pool
267
        //size is greater than initial value, shrink the connection pool
268
        //size to initial value
269
        DBConnectionPool.shrinkDBConnectionPoolSize();
270 1360 tao
271 2098 jones
        //Debug message to print out the method which have a busy DBConnection
272
        connPool.printMethodNameHavingBusyDBConnection();
273 1360 tao
274 2098 jones
        String ctype = request.getContentType();
275
        if (ctype != null && ctype.startsWith("multipart/form-data")) {
276
            handleMultipartForm(request, response);
277
        } else {
278 1360 tao
279 2098 jones
            String name = null;
280
            String[] value = null;
281
            String[] docid = new String[3];
282
            Hashtable params = new Hashtable();
283
            Enumeration paramlist = request.getParameterNames();
284 1360 tao
285 2098 jones
            while (paramlist.hasMoreElements()) {
286 1360 tao
287 2098 jones
                name = (String) paramlist.nextElement();
288
                value = request.getParameterValues(name);
289 509 bojilova
290 2098 jones
                // Decode the docid and mouse click information
291
                if (name.endsWith(".y")) {
292
                    docid[0] = name.substring(0, name.length() - 2);
293
                    params.put("docid", docid);
294
                    name = "ypos";
295
                }
296
                if (name.endsWith(".x")) {
297
                    name = "xpos";
298
                }
299 509 bojilova
300 2098 jones
                params.put(name, value);
301
            }
302 509 bojilova
303 2098 jones
            //handle param is emptpy
304
            if (params.isEmpty() || params == null) { return; }
305 509 bojilova
306 2098 jones
            //if the user clicked on the input images, decode which image
307
            //was clicked then set the action.
308 2252 sgarg
            if(params.get("action") == null){
309
                PrintWriter out = response.getWriter();
310
                response.setContentType("text/xml");
311
                out.println("<?xml version=\"1.0\"?>");
312
                out.println("<error>");
313
                out.println("Action not specified");
314
                out.println("</error>");
315
                out.close();
316
                return;
317
            }
318
319 2098 jones
            String action = ((String[]) params.get("action"))[0];
320
            MetaCatUtil.debugMessage("Line 230: Action is: " + action, 1);
321 509 bojilova
322 2098 jones
            // This block handles session management for the servlet
323
            // by looking up the current session information for all actions
324
            // other than "login" and "logout"
325
            String username = null;
326
            String password = null;
327
            String[] groupnames = null;
328
            String sess_id = null;
329 1360 tao
330 2098 jones
            // handle login action
331
            if (action.equals("login")) {
332
                PrintWriter out = response.getWriter();
333
                handleLoginAction(out, params, request, response);
334
                out.close();
335 1360 tao
336 2098 jones
                // handle logout action
337
            } else if (action.equals("logout")) {
338
                PrintWriter out = response.getWriter();
339
                handleLogoutAction(out, params, request, response);
340
                out.close();
341 1360 tao
342 2098 jones
                // handle shrink DBConnection request
343
            } else if (action.equals("shrink")) {
344
                PrintWriter out = response.getWriter();
345
                boolean success = false;
346
                //If all DBConnection in the pool are free and DBConnection
347
                // pool
348
                //size is greater than initial value, shrink the connection
349
                // pool
350
                //size to initial value
351
                success = DBConnectionPool.shrinkConnectionPoolSize();
352
                if (success) {
353
                    //if successfully shrink the pool size to initial value
354
                    out.println("DBConnection Pool shrunk successfully.");
355
                }//if
356
                else {
357
                    out.println("DBConnection pool not shrunk successfully.");
358
                }
359
                //close out put
360
                out.close();
361 1360 tao
362 2098 jones
                // aware of session expiration on every request
363
            } else {
364
                HttpSession sess = request.getSession(true);
365
                if (sess.isNew() && !params.containsKey("sessionid")) {
366
                    // session expired or has not been stored b/w user requests
367
                    MetaCatUtil.debugMessage(
368
                            "in session is new or no sessionid", 40);
369
                    username = "public";
370
                    sess.setAttribute("username", username);
371
                } else {
372
                    MetaCatUtil.debugMessage("in session is not new or "
373
                            + " has sessionid parameter", 40);
374
                    try {
375
                        if (params.containsKey("sessionid")) {
376
                            sess_id = ((String[]) params.get("sessionid"))[0];
377
                            MetaCatUtil.debugMessage("in has sessionid "
378
                                    + sess_id, 40);
379
                            if (sessionHash.containsKey(sess_id)) {
380
                                MetaCatUtil.debugMessage("find the id "
381
                                        + sess_id + " in hash table", 40);
382
                                sess = (HttpSession) sessionHash.get(sess_id);
383
                            }
384
                        } else {
385
                            // we already store the session in login, so we
386
                            // don't need here
387
                            /*
388
                             * MetaCatUtil.debugMessage("in no sessionid
389
                             * parameter ", 40); sess_id =
390
                             * (String)sess.getId();
391
                             * MetaCatUtil.debugMessage("storing the session id "
392
                             * + sess_id + " which has username " +
393
                             * sess.getAttribute("username") + " into session
394
                             * hash in handleGetOrPost method", 35);
395
                             */
396
                        }
397
                    } catch (IllegalStateException ise) {
398
                        System.out.println(
399
                                "error in handleGetOrPost: this shouldn't "
400
                                + "happen: the session should be valid: "
401
                                + ise.getMessage());
402
                    }
403 1360 tao
404 2098 jones
                    username = (String) sess.getAttribute("username");
405
                    MetaCatUtil.debugMessage("The user name from session is: "
406
                            + username, 20);
407
                    password = (String) sess.getAttribute("password");
408
                    groupnames = (String[]) sess.getAttribute("groupnames");
409
                }
410 1360 tao
411 2098 jones
                //make user user username should be public
412
                if (username == null || (username.trim().equals(""))) {
413
                    username = "public";
414
                }
415
                MetaCatUtil.debugMessage("The user is : " + username, 5);
416
            }
417
            // Now that we know the session is valid, we can delegate the
418
            // request
419
            // to a particular action handler
420
            if (action.equals("query")) {
421
                PrintWriter out = response.getWriter();
422
                handleQuery(out, params, response, username, groupnames,
423
                        sess_id);
424
                out.close();
425
            } else if (action.equals("squery")) {
426
                PrintWriter out = response.getWriter();
427
                if (params.containsKey("query")) {
428
                    handleSQuery(out, params, response, username, groupnames,
429
                            sess_id);
430
                    out.close();
431
                } else {
432
                    out.println(
433
                            "Illegal action squery without \"query\" parameter");
434
                    out.close();
435
                }
436
            } else if (action.equals("export")) {
437 1298 tao
438 2169 sgarg
                handleExportAction(params, response, username,
439 2102 jones
                        groupnames, password);
440 2098 jones
            } else if (action.equals("read")) {
441 2102 jones
                handleReadAction(params, request, response, username, password,
442 2098 jones
                        groupnames);
443
            } else if (action.equals("readinlinedata")) {
444 2102 jones
                handleReadInlineDataAction(params, request, response, username,
445 2098 jones
                        password, groupnames);
446
            } else if (action.equals("insert") || action.equals("update")) {
447
                PrintWriter out = response.getWriter();
448
                if ((username != null) && !username.equals("public")) {
449 2102 jones
                    handleInsertOrUpdateAction(request, response,
450
                            out, params, username, groupnames);
451 2098 jones
                } else {
452 2252 sgarg
                    response.setContentType("text/xml");
453
                    out.println("<?xml version=\"1.0\"?>");
454
                    out.println("<error>");
455 2098 jones
                    out.println("Permission denied for user " + username + " "
456
                            + action);
457 2252 sgarg
                    out.println("</error>");
458 2098 jones
                }
459
                out.close();
460
            } else if (action.equals("delete")) {
461
                PrintWriter out = response.getWriter();
462
                if ((username != null) && !username.equals("public")) {
463 2102 jones
                    handleDeleteAction(out, params, request, response, username,
464 2098 jones
                            groupnames);
465
                } else {
466 2252 sgarg
                    response.setContentType("text/xml");
467
                    out.println("<?xml version=\"1.0\"?>");
468
                    out.println("<error>");
469 2098 jones
                    out.println("Permission denied for " + action);
470 2252 sgarg
                    out.println("</error>");
471 2098 jones
                }
472
                out.close();
473
            } else if (action.equals("validate")) {
474
                PrintWriter out = response.getWriter();
475
                handleValidateAction(out, params);
476
                out.close();
477
            } else if (action.equals("setaccess")) {
478
                PrintWriter out = response.getWriter();
479
                handleSetAccessAction(out, params, username);
480
                out.close();
481
            } else if (action.equals("getaccesscontrol")) {
482
                PrintWriter out = response.getWriter();
483
                handleGetAccessControlAction(out, params, response, username,
484
                        groupnames);
485
                out.close();
486
            } else if (action.equals("getprincipals")) {
487
                PrintWriter out = response.getWriter();
488
                handleGetPrincipalsAction(out, username, password);
489
                out.close();
490
            } else if (action.equals("getdoctypes")) {
491
                PrintWriter out = response.getWriter();
492
                handleGetDoctypesAction(out, params, response);
493
                out.close();
494
            } else if (action.equals("getdtdschema")) {
495
                PrintWriter out = response.getWriter();
496
                handleGetDTDSchemaAction(out, params, response);
497
                out.close();
498
            } else if (action.equals("getlastdocid")) {
499
                PrintWriter out = response.getWriter();
500
                handleGetMaxDocidAction(out, params, response);
501
                out.close();
502
            } else if (action.equals("getrevisionanddoctype")) {
503
                PrintWriter out = response.getWriter();
504
                handleGetRevisionAndDocTypeAction(out, params);
505
                out.close();
506
            } else if (action.equals("getversion")) {
507
                response.setContentType("text/xml");
508
                PrintWriter out = response.getWriter();
509
                out.println(Version.getVersionAsXml());
510
                out.close();
511 2113 jones
            } else if (action.equals("getlog")) {
512 2312 jones
                handleGetLogAction(params, request, response, username);
513
            } else if (action.equals("buildindex")) {
514
                handleBuildIndexAction(params, request, response, username);
515 2098 jones
            } else if (action.equals("login") || action.equals("logout")) {
516 2312 jones
                /*
517 2098 jones
            } else if (action.equals("protocoltest")) {
518
                String testURL = "metacat://dev.nceas.ucsb.edu/NCEAS.897766.9";
519
                try {
520
                    testURL = ((String[]) params.get("url"))[0];
521
                } catch (Throwable t) {
522
                }
523
                String phandler = System
524
                        .getProperty("java.protocol.handler.pkgs");
525
                response.setContentType("text/html");
526
                PrintWriter out = response.getWriter();
527
                out.println("<body bgcolor=\"white\">");
528
                out.println("<p>Handler property: <code>" + phandler
529
                        + "</code></p>");
530
                out.println("<p>Starting test for:<br>");
531
                out.println("    " + testURL + "</p>");
532
                try {
533
                    URL u = new URL(testURL);
534
                    out.println("<pre>");
535
                    out.println("Protocol: " + u.getProtocol());
536
                    out.println("    Host: " + u.getHost());
537
                    out.println("    Port: " + u.getPort());
538
                    out.println("    Path: " + u.getPath());
539
                    out.println("     Ref: " + u.getRef());
540
                    String pquery = u.getQuery();
541
                    out.println("   Query: " + pquery);
542
                    out.println("  Params: ");
543
                    if (pquery != null) {
544 2099 jones
                        Hashtable qparams = MetaCatUtil.parseQuery(u.getQuery());
545 2098 jones
                        for (Enumeration en = qparams.keys(); en
546
                                .hasMoreElements();) {
547
                            String pname = (String) en.nextElement();
548
                            String pvalue = (String) qparams.get(pname);
549
                            out.println("    " + pname + ": " + pvalue);
550
                        }
551
                    }
552
                    out.println("</pre>");
553
                    out.println("</body>");
554
                    out.close();
555
                } catch (MalformedURLException mue) {
556
                    System.out.println(
557
                            "bad url from MetacatServlet.handleGetOrPost");
558
                    out.println(mue.getMessage());
559
                    mue.printStackTrace(out);
560
                    out.close();
561
                }
562 2312 jones
                */
563 2098 jones
            } else {
564
                PrintWriter out = response.getWriter();
565
                out.println("<?xml version=\"1.0\"?>");
566
                out.println("<error>");
567
                out.println(
568
                     "Error: action not registered.  Please report this error.");
569
                out.println("</error>");
570
                out.close();
571
            }
572 1360 tao
573 2098 jones
            //util.closeConnections();
574
            // Close the stream to the client
575
            //out.close();
576
        }
577
    }
578 425 bojilova
579 2098 jones
    // LOGIN & LOGOUT SECTION
580
    /**
581
     * Handle the login request. Create a new session object. Do user
582
     * authentication through the session.
583
     */
584
    private void handleLoginAction(PrintWriter out, Hashtable params,
585
            HttpServletRequest request, HttpServletResponse response)
586 943 tao
    {
587 1360 tao
588 2098 jones
        AuthSession sess = null;
589 2252 sgarg
590
        if(params.get("username") == null){
591
            response.setContentType("text/xml");
592
            out.println("<?xml version=\"1.0\"?>");
593
            out.println("<error>");
594
            out.println("Username not specified");
595
            out.println("</error>");
596
            return;
597
        }
598
599
        if(params.get("password") == null){
600
            response.setContentType("text/xml");
601
            out.println("<?xml version=\"1.0\"?>");
602
            out.println("<error>");
603
            out.println("Password not specified");
604
            out.println("</error>");
605
            return;
606
        }
607
608 2098 jones
        String un = ((String[]) params.get("username"))[0];
609
        MetaCatUtil.debugMessage("user " + un + " try to login", 20);
610
        String pw = ((String[]) params.get("password"))[0];
611 943 tao
612 2252 sgarg
        String qformat = "xml";
613
        if(params.get("qformat") != null){
614
            qformat = ((String[]) params.get("qformat"))[0];
615
        }
616
617 2098 jones
        try {
618
            sess = new AuthSession();
619
        } catch (Exception e) {
620
            System.out.println("error in MetacatServlet.handleLoginAction: "
621
                    + e.getMessage());
622
            out.println(e.getMessage());
623
            return;
624
        }
625
        boolean isValid = sess.authenticate(request, un, pw);
626 1360 tao
627 2098 jones
        //if it is authernticate is true, store the session
628
        if (isValid) {
629
            HttpSession session = sess.getSessions();
630
            String id = session.getId();
631
            MetaCatUtil.debugMessage("Store session id " + id
632
                    + "which has username" + session.getAttribute("username")
633
                    + " into hash in login method", 35);
634
            sessionHash.put(id, session);
635
        }
636 1360 tao
637 2098 jones
        // format and transform the output
638
        if (qformat.equals("xml")) {
639
            response.setContentType("text/xml");
640
            out.println(sess.getMessage());
641
        } else {
642
            try {
643
                DBTransform trans = new DBTransform();
644
                response.setContentType("text/html");
645
                trans.transformXMLDocument(sess.getMessage(),
646
                        "-//NCEAS//login//EN", "-//W3C//HTML//EN", qformat,
647
                        out, null);
648
            } catch (Exception e) {
649 1360 tao
650 2098 jones
                MetaCatUtil.debugMessage(
651
                        "Error in MetaCatServlet.handleLoginAction: "
652
                                + e.getMessage(), 30);
653
            }
654
        }
655
    }
656 1716 berkley
657 2098 jones
    /**
658
     * Handle the logout request. Close the connection.
659
     */
660
    private void handleLogoutAction(PrintWriter out, Hashtable params,
661
            HttpServletRequest request, HttpServletResponse response)
662 1483 tao
    {
663 1716 berkley
664 2252 sgarg
        String qformat = "xml";
665
        if(params.get("qformat") != null){
666
            qformat = ((String[]) params.get("qformat"))[0];
667
        }
668 1716 berkley
669 2098 jones
        // close the connection
670
        HttpSession sess = request.getSession(false);
671
        MetaCatUtil.debugMessage("After get session in logout request", 40);
672
        if (sess != null) {
673
            MetaCatUtil.debugMessage("The session id " + sess.getId()
674
                    + " will be invalidate in logout action", 30);
675
            MetaCatUtil.debugMessage("The session contains user "
676
                    + sess.getAttribute("username")
677
                    + " will be invalidate in logout action", 30);
678
            sess.invalidate();
679
        }
680 1716 berkley
681 2098 jones
        // produce output
682
        StringBuffer output = new StringBuffer();
683
        output.append("<?xml version=\"1.0\"?>");
684
        output.append("<logout>");
685
        output.append("User logged out");
686
        output.append("</logout>");
687 1483 tao
688 2098 jones
        //format and transform the output
689
        if (qformat.equals("xml")) {
690
            response.setContentType("text/xml");
691
            out.println(output.toString());
692
        } else {
693
            try {
694
                DBTransform trans = new DBTransform();
695
                response.setContentType("text/html");
696
                trans.transformXMLDocument(output.toString(),
697
                        "-//NCEAS//login//EN", "-//W3C//HTML//EN", qformat,
698
                        out, null);
699
            } catch (Exception e) {
700
                MetaCatUtil.debugMessage(
701
                        "Error in MetaCatServlet.handleLogoutAction"
702
                                + e.getMessage(), 30);
703
            }
704 1716 berkley
        }
705 2098 jones
    }
706 1483 tao
707 2098 jones
    // END OF LOGIN & LOGOUT SECTION
708 1716 berkley
709 2098 jones
    // SQUERY & QUERY SECTION
710
    /**
711
     * Retreive the squery xml, execute it and display it
712 2169 sgarg
     *
713 2098 jones
     * @param out the output stream to the client
714
     * @param params the Hashtable of parameters that should be included in the
715
     *            squery.
716
     * @param response the response object linked to the client
717
     * @param conn the database connection
718
     */
719
    protected void handleSQuery(PrintWriter out, Hashtable params,
720
            HttpServletResponse response, String user, String[] groups,
721
            String sessionid)
722
    {
723
        double startTime = System.currentTimeMillis() / 1000;
724
        DBQuery queryobj = new DBQuery(saxparser);
725
        queryobj.findDocuments(response, out, params, user, groups, sessionid);
726
        double outPutTime = System.currentTimeMillis() / 1000;
727
        MetaCatUtil.debugMessage("total search time: "
728
                + (outPutTime - startTime), 30);
729 1483 tao
730 2098 jones
    }
731 1716 berkley
732 2098 jones
    /**
733
     * Create the xml query, execute it and display the results.
734 2169 sgarg
     *
735 2098 jones
     * @param out the output stream to the client
736
     * @param params the Hashtable of parameters that should be included in the
737
     *            squery.
738
     * @param response the response object linked to the client
739
     */
740
    protected void handleQuery(PrintWriter out, Hashtable params,
741
            HttpServletResponse response, String user, String[] groups,
742
            String sessionid)
743 1490 tao
    {
744 2098 jones
        //create the query and run it
745
        String xmlquery = DBQuery.createSQuery(params);
746
        String[] queryArray = new String[1];
747
        queryArray[0] = xmlquery;
748
        params.put("query", queryArray);
749
        double startTime = System.currentTimeMillis() / 1000;
750
        DBQuery queryobj = new DBQuery(saxparser);
751
        queryobj.findDocuments(response, out, params, user, groups, sessionid);
752
        double outPutTime = System.currentTimeMillis() / 1000;
753
        MetaCatUtil.debugMessage("total search time: "
754
                + (outPutTime - startTime), 30);
755 1716 berkley
756 2098 jones
        //handleSQuery(out, params, response,user, groups, sessionid);
757 1490 tao
    }
758 1716 berkley
759 2098 jones
    // END OF SQUERY & QUERY SECTION
760 1716 berkley
761 2098 jones
    //Exoport section
762
    /**
763
     * Handle the "export" request of data package from Metacat in zip format
764 2169 sgarg
     *
765 2098 jones
     * @param params the Hashtable of HTTP request parameters
766
     * @param response the HTTP response object linked to the client
767
     * @param user the username sent the request
768
     * @param groups the user's groupnames
769
     */
770 2169 sgarg
    private void handleExportAction(Hashtable params,
771
            HttpServletResponse response,
772 2102 jones
            String user, String[] groups, String passWord)
773 2098 jones
    {
774
        // Output stream
775
        ServletOutputStream out = null;
776
        // Zip output stream
777
        ZipOutputStream zOut = null;
778
        DocumentImpl docImpls = null;
779
        DBQuery queryObj = null;
780 1716 berkley
781 2098 jones
        String[] docs = new String[10];
782
        String docId = "";
783 1360 tao
784 731 bojilova
        try {
785 2098 jones
            // read the params
786
            if (params.containsKey("docid")) {
787
                docs = (String[]) params.get("docid");
788 731 bojilova
            }
789 2098 jones
            // Create a DBuery to handle export
790
            queryObj = new DBQuery(saxparser);
791
            // Get the docid
792
            docId = docs[0];
793
            // Make sure the client specify docid
794
            if (docId == null || docId.equals("")) {
795
                response.setContentType("text/xml"); //MIME type
796
                // Get a printwriter
797
                PrintWriter pw = response.getWriter();
798
                // Send back message
799
                pw.println("<?xml version=\"1.0\"?>");
800
                pw.println("<error>");
801
                pw.println("You didn't specify requested docid");
802
                pw.println("</error>");
803
                // Close printwriter
804
                pw.close();
805
                return;
806
            }
807
            // Get output stream
808
            out = response.getOutputStream();
809
            response.setContentType("application/zip"); //MIME type
810
            zOut = new ZipOutputStream(out);
811
            zOut = queryObj
812
                    .getZippedPackage(docId, out, user, groups, passWord);
813
            zOut.finish(); //terminate the zip file
814
            zOut.close(); //close the zip stream
815 731 bojilova
816 2098 jones
        } catch (Exception e) {
817
            try {
818
                response.setContentType("text/xml"); //MIME type
819
                // Send error message back
820
                if (out != null) {
821
                    PrintWriter pw = new PrintWriter(out);
822
                    pw.println("<?xml version=\"1.0\"?>");
823
                    pw.println("<error>");
824
                    pw.println(e.getMessage());
825
                    pw.println("</error>");
826
                    // Close printwriter
827
                    pw.close();
828
                    // Close output stream
829
                    out.close();
830
                }
831
                // Close zip output stream
832
                if (zOut != null) {
833
                    zOut.close();
834
                }
835
            } catch (IOException ioe) {
836
                MetaCatUtil.debugMessage("Problem with the servlet output "
837
                        + "in MetacatServlet.handleExportAction: "
838
                        + ioe.getMessage(), 30);
839 731 bojilova
            }
840
841 2098 jones
            MetaCatUtil.debugMessage(
842
                    "Error in MetacatServlet.handleExportAction: "
843
                            + e.getMessage(), 30);
844
            e.printStackTrace(System.out);
845
846 731 bojilova
        }
847 1360 tao
848 2098 jones
    }
849 1360 tao
850 2098 jones
    /**
851
     * In eml2 document, the xml can have inline data and data was stripped off
852
     * and store in file system. This action can be used to read inline data
853
     * only
854 2169 sgarg
     *
855 2098 jones
     * @param params the Hashtable of HTTP request parameters
856
     * @param response the HTTP response object linked to the client
857
     * @param user the username sent the request
858
     * @param groups the user's groupnames
859
     */
860
    private void handleReadInlineDataAction(Hashtable params,
861 2169 sgarg
            HttpServletRequest request, HttpServletResponse response,
862 2102 jones
            String user, String passWord, String[] groups)
863 2098 jones
    {
864
        String[] docs = new String[10];
865
        String inlineDataId = null;
866
        String docId = "";
867
        ServletOutputStream out = null;
868 1360 tao
869 2098 jones
        try {
870
            // read the params
871
            if (params.containsKey("inlinedataid")) {
872
                docs = (String[]) params.get("inlinedataid");
873
            }
874
            // Get the docid
875
            inlineDataId = docs[0];
876
            // Make sure the client specify docid
877 2169 sgarg
            if (inlineDataId == null || inlineDataId.equals("")) {
878 2098 jones
                throw new Exception("You didn't specify requested inlinedataid"); }
879 1360 tao
880 2098 jones
            // check for permission
881
            docId = MetaCatUtil
882
                    .getDocIdWithoutRevFromInlineDataID(inlineDataId);
883
            PermissionController controller = new PermissionController(docId);
884
            // check top level read permission
885
            if (!controller.hasPermission(user, groups,
886 2245 sgarg
                    AccessControlInterface.READSTRING))
887
            {
888 2098 jones
                throw new Exception("User " + user
889
                        + " doesn't have permission " + " to read document "
890
                        + docId);
891
            }
892 2245 sgarg
            else
893
            {
894
              //check data access level
895
              try
896
              {
897
                Hashtable unReadableInlineDataList =
898 2292 sgarg
                    PermissionController.getUnReadableInlineDataIdList(docId,
899
                    user, groups, false);
900
                if (unReadableInlineDataList.containsValue(
901
                          MetaCatUtil.getInlineDataIdWithoutRev(inlineDataId)))
902 2245 sgarg
                {
903
                  throw new Exception("User " + user
904
                       + " doesn't have permission " + " to read inlinedata "
905
                       + inlineDataId);
906 2098 jones
907 2245 sgarg
                }//if
908
              }//try
909
              catch (Exception e)
910
              {
911
                throw e;
912
              }//catch
913
            }//else
914
915 2098 jones
            // Get output stream
916
            out = response.getOutputStream();
917
            // read the inline data from the file
918
            String inlinePath = MetaCatUtil.getOption("inlinedatafilepath");
919
            File lineData = new File(inlinePath, inlineDataId);
920
            FileInputStream input = new FileInputStream(lineData);
921
            byte[] buffer = new byte[4 * 1024];
922
            int bytes = input.read(buffer);
923
            while (bytes != -1) {
924
                out.write(buffer, 0, bytes);
925
                bytes = input.read(buffer);
926
            }
927 1292 tao
            out.close();
928 1360 tao
929 2169 sgarg
            EventLog.getInstance().log(request.getRemoteAddr(), user,
930 2102 jones
                    inlineDataId, "readinlinedata");
931 2098 jones
        } catch (Exception e) {
932
            try {
933
                PrintWriter pw = null;
934
                // Send error message back
935
                if (out != null) {
936
                    pw = new PrintWriter(out);
937
                } else {
938
                    pw = response.getWriter();
939
                }
940
                pw.println("<?xml version=\"1.0\"?>");
941
                pw.println("<error>");
942
                pw.println(e.getMessage());
943
                pw.println("</error>");
944
                // Close printwriter
945
                pw.close();
946
                // Close output stream if out is not null
947
                if (out != null) {
948
                    out.close();
949
                }
950
            } catch (IOException ioe) {
951
                MetaCatUtil.debugMessage("Problem with the servlet output "
952
                        + "in MetacatServlet.handleExportAction: "
953
                        + ioe.getMessage(), 30);
954
            }
955
            MetaCatUtil.debugMessage(
956
                    "Error in MetacatServlet.handleReadInlineDataAction: "
957
                            + e.getMessage(), 30);
958 1292 tao
        }
959 2098 jones
    }
960
961
    /*
962
     * Get the nodeid from xml_nodes for the inlinedataid
963
     */
964
    private long getInlineDataNodeId(String inLineDataId, String docId)
965
            throws SQLException
966 1292 tao
    {
967 2098 jones
        long nodeId = 0;
968
        String INLINE = "inline";
969
        boolean hasRow;
970
        PreparedStatement pStmt = null;
971
        DBConnection conn = null;
972
        int serialNumber = -1;
973
        String sql = "SELECT nodeid FROM xml_nodes WHERE docid=? AND nodedata=? "
974
                + "AND nodetype='TEXT' AND parentnodeid IN "
975
                + "(SELECT nodeid FROM xml_nodes WHERE docid=? AND "
976
                + "nodetype='ELEMENT' AND nodename='" + INLINE + "')";
977 1360 tao
978 2098 jones
        try {
979
            //check out DBConnection
980
            conn = DBConnectionPool
981
                    .getDBConnection("AccessControlList.isAllowFirst");
982
            serialNumber = conn.getCheckOutSerialNumber();
983 1360 tao
984 2098 jones
            pStmt = conn.prepareStatement(sql);
985
            //bind value
986
            pStmt.setString(1, docId);//docid
987
            pStmt.setString(2, inLineDataId);//inlinedataid
988
            pStmt.setString(3, docId);
989
            // excute query
990
            pStmt.execute();
991
            ResultSet rs = pStmt.getResultSet();
992
            hasRow = rs.next();
993
            // get result
994
            if (hasRow) {
995
                nodeId = rs.getLong(1);
996
            }//if
997
998
        } catch (SQLException e) {
999
            throw e;
1000
        } finally {
1001
            try {
1002
                pStmt.close();
1003
            } finally {
1004
                DBConnectionPool.returnDBConnection(conn, serialNumber);
1005
            }
1006 1292 tao
        }
1007 2098 jones
        MetaCatUtil.debugMessage("The nodeid for inlinedataid " + inLineDataId
1008
                + " is: " + nodeId, 35);
1009
        return nodeId;
1010
    }
1011 1360 tao
1012 2098 jones
    /**
1013
     * Handle the "read" request of metadata/data files from Metacat or any
1014
     * files from Internet; transformed metadata XML document into HTML
1015
     * presentation if requested; zip files when more than one were requested.
1016 2169 sgarg
     *
1017 2098 jones
     * @param params the Hashtable of HTTP request parameters
1018 2102 jones
     * @param request the HTTP request object linked to the client
1019 2098 jones
     * @param response the HTTP response object linked to the client
1020
     * @param user the username sent the request
1021
     * @param groups the user's groupnames
1022
     */
1023 2102 jones
    private void handleReadAction(Hashtable params, HttpServletRequest request,
1024 2098 jones
            HttpServletResponse response, String user, String passWord,
1025
            String[] groups)
1026
    {
1027
        ServletOutputStream out = null;
1028
        ZipOutputStream zout = null;
1029
        PrintWriter pw = null;
1030
        boolean zip = false;
1031
        boolean withInlineData = true;
1032 1360 tao
1033 2098 jones
        try {
1034
            String[] docs = new String[0];
1035
            String docid = "";
1036
            String qformat = "";
1037
            String abstrpath = null;
1038 731 bojilova
1039 2098 jones
            // read the params
1040
            if (params.containsKey("docid")) {
1041
                docs = (String[]) params.get("docid");
1042
            }
1043
            if (params.containsKey("qformat")) {
1044
                qformat = ((String[]) params.get("qformat"))[0];
1045
            }
1046
            // the param for only metadata (eml)
1047 2245 sgarg
            // we don't support read a eml document without inline data now.
1048
            /*if (params.containsKey("inlinedata")) {
1049 1360 tao
1050 2098 jones
                String inlineData = ((String[]) params.get("inlinedata"))[0];
1051
                if (inlineData.equalsIgnoreCase("false")) {
1052
                    withInlineData = false;
1053
                }
1054 2245 sgarg
            }*/
1055 2098 jones
            if ((docs.length > 1) || qformat.equals("zip")) {
1056
                zip = true;
1057
                out = response.getOutputStream();
1058
                response.setContentType("application/zip"); //MIME type
1059
                zout = new ZipOutputStream(out);
1060
            }
1061
            // go through the list of docs to read
1062
            for (int i = 0; i < docs.length; i++) {
1063
                try {
1064 1360 tao
1065 2098 jones
                    URL murl = new URL(docs[i]);
1066 2099 jones
                    Hashtable murlQueryStr = MetaCatUtil.parseQuery(
1067
                            murl.getQuery());
1068 2098 jones
                    // case docid="http://.../?docid=aaa"
1069
                    // or docid="metacat://.../?docid=bbb"
1070
                    if (murlQueryStr.containsKey("docid")) {
1071
                        // get only docid, eliminate the rest
1072
                        docid = (String) murlQueryStr.get("docid");
1073
                        if (zip) {
1074 2102 jones
                            addDocToZip(request, docid, zout, user, groups);
1075 2098 jones
                        } else {
1076 2102 jones
                            readFromMetacat(request, response, docid, qformat,
1077 2098 jones
                                    abstrpath, user, groups, zip, zout,
1078
                                    withInlineData, params);
1079
                        }
1080 1360 tao
1081 2098 jones
                        // case docid="http://.../filename"
1082
                    } else {
1083
                        docid = docs[i];
1084
                        if (zip) {
1085 2102 jones
                            addDocToZip(request, docid, zout, user, groups);
1086 2098 jones
                        } else {
1087
                            readFromURLConnection(response, docid);
1088
                        }
1089
                    }
1090 2169 sgarg
1091 2098 jones
                } catch (MalformedURLException mue) {
1092
                    docid = docs[i];
1093
                    if (zip) {
1094 2102 jones
                        addDocToZip(request, docid, zout, user, groups);
1095 2098 jones
                    } else {
1096 2169 sgarg
                        readFromMetacat(request, response, docid, qformat,
1097
                                abstrpath, user, groups, zip, zout,
1098 2102 jones
                                withInlineData, params);
1099 2098 jones
                    }
1100
                }
1101 2099 jones
            }
1102 1360 tao
1103 2098 jones
            if (zip) {
1104
                zout.finish(); //terminate the zip file
1105
                zout.close(); //close the zip stream
1106
            }
1107 1360 tao
1108 2098 jones
        } catch (McdbDocNotFoundException notFoundE) {
1109
            // To handle doc not found exception
1110
            // the docid which didn't be found
1111
            String notFoundDocId = notFoundE.getUnfoundDocId();
1112
            String notFoundRevision = notFoundE.getUnfoundRevision();
1113
            MetaCatUtil.debugMessage("Missed id: " + notFoundDocId, 30);
1114
            MetaCatUtil.debugMessage("Missed rev: " + notFoundRevision, 30);
1115
            try {
1116
                // read docid from remote server
1117
                readFromRemoteMetaCat(response, notFoundDocId,
1118
                        notFoundRevision, user, passWord, out, zip, zout);
1119
                // Close zout outputstream
1120
                if (zout != null) {
1121
                    zout.close();
1122
                }
1123
                // close output stream
1124
                if (out != null) {
1125
                    out.close();
1126
                }
1127 1360 tao
1128 2098 jones
            } catch (Exception exc) {
1129
                MetaCatUtil.debugMessage(
1130
                        "Erorr in MetacatServlet.hanldReadAction: "
1131
                                + exc.getMessage(), 30);
1132
                try {
1133
                    if (out != null) {
1134
                        response.setContentType("text/xml");
1135
                        // Send back error message by printWriter
1136
                        pw = new PrintWriter(out);
1137
                        pw.println("<?xml version=\"1.0\"?>");
1138
                        pw.println("<error>");
1139
                        pw.println(notFoundE.getMessage());
1140
                        pw.println("</error>");
1141
                        pw.close();
1142
                        out.close();
1143 1360 tao
1144 2098 jones
                    } else {
1145
                        response.setContentType("text/xml"); //MIME type
1146
                        // Send back error message if out = null
1147
                        if (pw == null) {
1148
                            // If pw is null, open the respnose
1149
                            pw = response.getWriter();
1150
                        }
1151
                        pw.println("<?xml version=\"1.0\"?>");
1152
                        pw.println("<error>");
1153
                        pw.println(notFoundE.getMessage());
1154
                        pw.println("</error>");
1155
                        pw.close();
1156
                    }
1157
                    // close zout
1158
                    if (zout != null) {
1159
                        zout.close();
1160
                    }
1161
                } catch (IOException ie) {
1162
                    MetaCatUtil.debugMessage("Problem with the servlet output "
1163
                            + "in MetacatServlet.handleReadAction: "
1164
                            + ie.getMessage(), 30);
1165
                }
1166
            }
1167
        } catch (Exception e) {
1168
            try {
1169 1716 berkley
1170 2098 jones
                if (out != null) {
1171
                    response.setContentType("text/xml"); //MIME type
1172
                    pw = new PrintWriter(out);
1173
                    pw.println("<?xml version=\"1.0\"?>");
1174
                    pw.println("<error>");
1175
                    pw.println(e.getMessage());
1176
                    pw.println("</error>");
1177
                    pw.close();
1178
                    out.close();
1179
                } else {
1180
                    response.setContentType("text/xml"); //MIME type
1181
                    // Send back error message if out = null
1182
                    if (pw == null) {
1183
                        pw = response.getWriter();
1184
                    }
1185
                    pw.println("<?xml version=\"1.0\"?>");
1186
                    pw.println("<error>");
1187
                    pw.println(e.getMessage());
1188
                    pw.println("</error>");
1189
                    pw.close();
1190 1360 tao
1191 2098 jones
                }
1192
                // Close zip output stream
1193
                if (zout != null) {
1194
                    zout.close();
1195
                }
1196 1360 tao
1197 2098 jones
            } catch (IOException ioe) {
1198
                MetaCatUtil.debugMessage("Problem with the servlet output "
1199
                        + "in MetacatServlet.handleReadAction: "
1200
                        + ioe.getMessage(), 30);
1201
                ioe.printStackTrace(System.out);
1202 731 bojilova
1203 2098 jones
            }
1204 1360 tao
1205 2098 jones
            MetaCatUtil.debugMessage(
1206
                    "Error in MetacatServlet.handleReadAction: "
1207
                            + e.getMessage(), 30);
1208
            //e.printStackTrace(System.out);
1209 731 bojilova
        }
1210 2098 jones
    }
1211 1360 tao
1212 2169 sgarg
    /** read metadata or data from Metacat
1213 2098 jones
     */
1214 2169 sgarg
    private void readFromMetacat(HttpServletRequest request,
1215 2102 jones
            HttpServletResponse response, String docid, String qformat,
1216
            String abstrpath, String user, String[] groups, boolean zip,
1217
            ZipOutputStream zout, boolean withInlineData, Hashtable params)
1218
            throws ClassNotFoundException, IOException, SQLException,
1219
            McdbException, Exception
1220 1292 tao
    {
1221 1360 tao
1222 2098 jones
        try {
1223 1360 tao
1224 2098 jones
            DocumentImpl doc = new DocumentImpl(docid);
1225 1360 tao
1226 2098 jones
            //check the permission for read
1227 2113 jones
            if (!DocumentImpl.hasReadPermission(user, groups, docid)) {
1228 2098 jones
                Exception e = new Exception("User " + user
1229
                        + " does not have permission"
1230
                        + " to read the document with the docid " + docid);
1231 731 bojilova
1232 2098 jones
                throw e;
1233
            }
1234 1360 tao
1235 2098 jones
            if (doc.getRootNodeID() == 0) {
1236
                // this is data file
1237
                String filepath = MetaCatUtil.getOption("datafilepath");
1238
                if (!filepath.endsWith("/")) {
1239
                    filepath += "/";
1240
                }
1241
                String filename = filepath + docid;
1242
                FileInputStream fin = null;
1243
                fin = new FileInputStream(filename);
1244 1360 tao
1245 2098 jones
                //MIME type
1246
                String contentType = getServletContext().getMimeType(filename);
1247
                if (contentType == null) {
1248
                    ContentTypeProvider provider = new ContentTypeProvider(
1249
                            docid);
1250
                    contentType = provider.getContentType();
1251
                    MetaCatUtil.debugMessage("Final contenttype is: "
1252
                            + contentType, 30);
1253
                }
1254 731 bojilova
1255 2098 jones
                response.setContentType(contentType);
1256
                // if we decide to use "application/octet-stream" for all data
1257
                // returns
1258
                // response.setContentType("application/octet-stream");
1259 731 bojilova
1260 2098 jones
                try {
1261 731 bojilova
1262 2098 jones
                    ServletOutputStream out = response.getOutputStream();
1263
                    byte[] buf = new byte[4 * 1024]; // 4K buffer
1264
                    int b = fin.read(buf);
1265
                    while (b != -1) {
1266
                        out.write(buf, 0, b);
1267
                        b = fin.read(buf);
1268
                    }
1269
                } finally {
1270
                    if (fin != null) fin.close();
1271
                }
1272 2169 sgarg
1273 2098 jones
            } else {
1274
                // this is metadata doc
1275 2273 sgarg
                if (qformat.equals("xml") || qformat.equals("")) {
1276
                    // if equals "", that means no qformat is specified. hence
1277
                    // by default the document should be returned in xml format
1278 2098 jones
                    // set content type first
1279
                    response.setContentType("text/xml"); //MIME type
1280
                    PrintWriter out = response.getWriter();
1281
                    doc.toXml(out, user, groups, withInlineData);
1282
                } else {
1283
                    response.setContentType("text/html"); //MIME type
1284
                    PrintWriter out = response.getWriter();
1285 1360 tao
1286 2098 jones
                    // Look up the document type
1287
                    String doctype = doc.getDoctype();
1288
                    // Transform the document to the new doctype
1289
                    DBTransform dbt = new DBTransform();
1290
                    dbt.transformXMLDocument(doc.toString(user, groups,
1291
                            withInlineData), doctype, "-//W3C//HTML//EN",
1292
                            qformat, out, params);
1293
                }
1294 1360 tao
1295 2098 jones
            }
1296 2169 sgarg
            EventLog.getInstance().log(request.getRemoteAddr(), user,
1297 2101 jones
                    docid, "read");
1298 2098 jones
        } catch (Exception except) {
1299
            throw except;
1300
        }
1301
    }
1302 1360 tao
1303 2169 sgarg
    /**
1304
     * read data from URLConnection
1305 2098 jones
     */
1306
    private void readFromURLConnection(HttpServletResponse response,
1307
            String docid) throws IOException, MalformedURLException
1308
    {
1309
        ServletOutputStream out = response.getOutputStream();
1310
        String contentType = getServletContext().getMimeType(docid); //MIME
1311
                                                                     // type
1312
        if (contentType == null) {
1313
            if (docid.endsWith(".xml")) {
1314
                contentType = "text/xml";
1315
            } else if (docid.endsWith(".css")) {
1316
                contentType = "text/css";
1317
            } else if (docid.endsWith(".dtd")) {
1318
                contentType = "text/plain";
1319
            } else if (docid.endsWith(".xsd")) {
1320
                contentType = "text/xml";
1321
            } else if (docid.endsWith("/")) {
1322
                contentType = "text/html";
1323
            } else {
1324
                File f = new File(docid);
1325
                if (f.isDirectory()) {
1326
                    contentType = "text/html";
1327
                } else {
1328
                    contentType = "application/octet-stream";
1329
                }
1330
            }
1331 1360 tao
        }
1332 2098 jones
        response.setContentType(contentType);
1333
        // if we decide to use "application/octet-stream" for all data returns
1334
        // response.setContentType("application/octet-stream");
1335 1360 tao
1336 2098 jones
        // this is http url
1337
        URL url = new URL(docid);
1338
        BufferedInputStream bis = null;
1339
        try {
1340
            bis = new BufferedInputStream(url.openStream());
1341 731 bojilova
            byte[] buf = new byte[4 * 1024]; // 4K buffer
1342 2098 jones
            int b = bis.read(buf);
1343 731 bojilova
            while (b != -1) {
1344 2098 jones
                out.write(buf, 0, b);
1345
                b = bis.read(buf);
1346 731 bojilova
            }
1347 2098 jones
        } finally {
1348
            if (bis != null) bis.close();
1349 636 berkley
        }
1350 1360 tao
1351 636 berkley
    }
1352 1360 tao
1353 2169 sgarg
    /**
1354 2098 jones
     * read file/doc and write to ZipOutputStream
1355 2169 sgarg
     *
1356 2098 jones
     * @param docid
1357
     * @param zout
1358
     * @param user
1359
     * @param groups
1360
     * @throws ClassNotFoundException
1361
     * @throws IOException
1362
     * @throws SQLException
1363
     * @throws McdbException
1364
     * @throws Exception
1365
     */
1366 2169 sgarg
    private void addDocToZip(HttpServletRequest request, String docid,
1367 2102 jones
            ZipOutputStream zout, String user, String[] groups) throws
1368
            ClassNotFoundException, IOException, SQLException, McdbException,
1369
            Exception
1370 1293 tao
    {
1371 2098 jones
        byte[] bytestring = null;
1372
        ZipEntry zentry = null;
1373 1360 tao
1374 598 bojilova
        try {
1375 2098 jones
            URL url = new URL(docid);
1376 1360 tao
1377 2098 jones
            // this http url; read from URLConnection; add to zip
1378
            zentry = new ZipEntry(docid);
1379
            zout.putNextEntry(zentry);
1380
            BufferedInputStream bis = null;
1381
            try {
1382
                bis = new BufferedInputStream(url.openStream());
1383
                byte[] buf = new byte[4 * 1024]; // 4K buffer
1384
                int b = bis.read(buf);
1385
                while (b != -1) {
1386
                    zout.write(buf, 0, b);
1387
                    b = bis.read(buf);
1388
                }
1389
            } finally {
1390
                if (bis != null) bis.close();
1391
            }
1392
            zout.closeEntry();
1393 1716 berkley
1394 2098 jones
        } catch (MalformedURLException mue) {
1395 203 jones
1396 2098 jones
            // this is metacat doc (data file or metadata doc)
1397
            try {
1398
                DocumentImpl doc = new DocumentImpl(docid);
1399 1360 tao
1400 2098 jones
                //check the permission for read
1401 2113 jones
                if (!DocumentImpl.hasReadPermission(user, groups, docid)) {
1402 2098 jones
                    Exception e = new Exception("User " + user
1403
                            + " does not have "
1404
                            + "permission to read the document with the docid "
1405
                            + docid);
1406
                    throw e;
1407
                }
1408 1360 tao
1409 2098 jones
                if (doc.getRootNodeID() == 0) {
1410
                    // this is data file; add file to zip
1411
                    String filepath = MetaCatUtil.getOption("datafilepath");
1412
                    if (!filepath.endsWith("/")) {
1413
                        filepath += "/";
1414
                    }
1415
                    String filename = filepath + docid;
1416
                    FileInputStream fin = null;
1417
                    fin = new FileInputStream(filename);
1418
                    try {
1419 1360 tao
1420 2098 jones
                        zentry = new ZipEntry(docid);
1421
                        zout.putNextEntry(zentry);
1422
                        byte[] buf = new byte[4 * 1024]; // 4K buffer
1423
                        int b = fin.read(buf);
1424
                        while (b != -1) {
1425
                            zout.write(buf, 0, b);
1426
                            b = fin.read(buf);
1427
                        }
1428
                    } finally {
1429
                        if (fin != null) fin.close();
1430
                    }
1431
                    zout.closeEntry();
1432 1716 berkley
1433 2098 jones
                } else {
1434
                    // this is metadata doc; add doc to zip
1435
                    bytestring = doc.toString().getBytes();
1436
                    zentry = new ZipEntry(docid + ".xml");
1437
                    zentry.setSize(bytestring.length);
1438
                    zout.putNextEntry(zentry);
1439
                    zout.write(bytestring, 0, bytestring.length);
1440
                    zout.closeEntry();
1441
                }
1442 2169 sgarg
                EventLog.getInstance().log(request.getRemoteAddr(), user,
1443 2101 jones
                        docid, "read");
1444 2098 jones
            } catch (Exception except) {
1445
                throw except;
1446
            }
1447 1360 tao
        }
1448 2098 jones
    }
1449 309 bojilova
1450 2098 jones
    /**
1451
     * If metacat couldn't find a data file or document locally, it will read
1452
     * this docid from its home server. This is for the replication feature
1453
     */
1454
    private void readFromRemoteMetaCat(HttpServletResponse response,
1455
            String docid, String rev, String user, String password,
1456
            ServletOutputStream out, boolean zip, ZipOutputStream zout)
1457
            throws Exception
1458 1466 tao
    {
1459 2098 jones
        // Create a object of RemoteDocument, "" is for zipEntryPath
1460
        RemoteDocument remoteDoc = new RemoteDocument(docid, rev, user,
1461
                password, "");
1462
        String docType = remoteDoc.getDocType();
1463
        // Only read data file
1464
        if (docType.equals("BIN")) {
1465
            // If it is zip format
1466
            if (zip) {
1467
                remoteDoc.readDocumentFromRemoteServerByZip(zout);
1468
            } else {
1469
                if (out == null) {
1470
                    out = response.getOutputStream();
1471
                }
1472
                response.setContentType("application/octet-stream");
1473
                remoteDoc.readDocumentFromRemoteServer(out);
1474
            }
1475
        } else {
1476
            throw new Exception("Docid: " + docid + "." + rev
1477
                    + " couldn't find");
1478
        }
1479 203 jones
    }
1480
1481 2098 jones
    /**
1482
     * Handle the database putdocument request and write an XML document to the
1483
     * database connection
1484
     */
1485 2102 jones
    private void handleInsertOrUpdateAction(HttpServletRequest request,
1486
            HttpServletResponse response, PrintWriter out, Hashtable params,
1487 2098 jones
            String user, String[] groups)
1488
    {
1489
        DBConnection dbConn = null;
1490
        int serialNumber = -1;
1491 1360 tao
1492 2252 sgarg
        if(params.get("docid") == null){
1493
            out.println("<?xml version=\"1.0\"?>");
1494
            out.println("<error>");
1495
            out.println("Docid not specified");
1496
            out.println("</error>");
1497
            return;
1498
        }
1499
1500 2098 jones
        try {
1501
            // Get the document indicated
1502
            String[] doctext = (String[]) params.get("doctext");
1503
            String pub = null;
1504
            if (params.containsKey("public")) {
1505
                pub = ((String[]) params.get("public"))[0];
1506
            }
1507 1360 tao
1508 2098 jones
            StringReader dtd = null;
1509
            if (params.containsKey("dtdtext")) {
1510
                String[] dtdtext = (String[]) params.get("dtdtext");
1511
                try {
1512
                    if (!dtdtext[0].equals("")) {
1513
                        dtd = new StringReader(dtdtext[0]);
1514
                    }
1515
                } catch (NullPointerException npe) {
1516
                }
1517
            }
1518 2347 sgarg
1519
            if(doctext == null){
1520
                out.println("<?xml version=\"1.0\"?>");
1521
                out.println("<error>");
1522
                out.println("Document text not submitted");
1523
                out.println("</error>");
1524
                return;
1525
            }
1526
1527 2098 jones
            StringReader xml = new StringReader(doctext[0]);
1528
            boolean validate = false;
1529
            DocumentImplWrapper documentWrapper = null;
1530
            try {
1531
                // look inside XML Document for <!DOCTYPE ... PUBLIC/SYSTEM ...
1532
                // >
1533
                // in order to decide whether to use validation parser
1534
                validate = needDTDValidation(xml);
1535
                if (validate) {
1536
                    // set a dtd base validation parser
1537
                    String rule = DocumentImpl.DTD;
1538
                    documentWrapper = new DocumentImplWrapper(rule, validate);
1539
                } else if (needSchemaValidation(xml)) {
1540
                    // for eml2
1541 2169 sgarg
                    String namespace = findNamespace(xml);
1542 2224 sgarg
                    if (namespace.compareTo(DocumentImpl.EML2_0_0NAMESPACE) == 0
1543
                                || namespace.compareTo(
1544
                                DocumentImpl.EML2_0_1NAMESPACE) == 0) {
1545 2098 jones
                        // set eml2 base validation parser
1546 2163 tao
                        String rule = DocumentImpl.EML200;
1547 2098 jones
                        // using emlparser to check id validation
1548
                        EMLParser parser = new EMLParser(doctext[0]);
1549
                        documentWrapper = new DocumentImplWrapper(rule, true);
1550 2169 sgarg
                    } else if (namespace.compareTo(
1551
                                DocumentImpl.EML2_1_0NAMESPACE) == 0) {
1552
                        // set eml2 base validation parser
1553
                        String rule = DocumentImpl.EML210;
1554
                        // using emlparser to check id validation
1555
                        EMLParser parser = new EMLParser(doctext[0]);
1556
                        documentWrapper = new DocumentImplWrapper(rule, true);
1557 2098 jones
                    } else {
1558
                        // set schema base validation parser
1559
                        String rule = DocumentImpl.SCHEMA;
1560
                        documentWrapper = new DocumentImplWrapper(rule, true);
1561
                    }
1562
                } else {
1563
                    documentWrapper = new DocumentImplWrapper("", false);
1564
                }
1565 695 bojilova
1566 2098 jones
                String[] action = (String[]) params.get("action");
1567
                String[] docid = (String[]) params.get("docid");
1568
                String newdocid = null;
1569 695 bojilova
1570 2098 jones
                String doAction = null;
1571
                if (action[0].equals("insert")) {
1572
                    doAction = "INSERT";
1573
                } else if (action[0].equals("update")) {
1574
                    doAction = "UPDATE";
1575
                }
1576 695 bojilova
1577 2098 jones
                try {
1578
                    // get a connection from the pool
1579
                    dbConn = DBConnectionPool
1580
                            .getDBConnection("MetaCatServlet.handleInsertOrUpdateAction");
1581
                    serialNumber = dbConn.getCheckOutSerialNumber();
1582 1716 berkley
1583 2098 jones
                    // write the document to the database
1584
                    try {
1585
                        String accNumber = docid[0];
1586
                        MetaCatUtil.debugMessage("" + doAction + " "
1587
                                + accNumber + "...", 10);
1588
                        if (accNumber.equals("")) {
1589
                            accNumber = null;
1590 2102 jones
                        }
1591 2098 jones
                        newdocid = documentWrapper.write(dbConn, xml, pub, dtd,
1592
                                doAction, accNumber, user, groups);
1593 2169 sgarg
                        EventLog.getInstance().log(request.getRemoteAddr(),
1594 2102 jones
                                user, accNumber, action[0]);
1595
                    } catch (NullPointerException npe) {
1596 2098 jones
                        newdocid = documentWrapper.write(dbConn, xml, pub, dtd,
1597
                                doAction, null, user, groups);
1598 2169 sgarg
                        EventLog.getInstance().log(request.getRemoteAddr(),
1599 2102 jones
                                user, "", action[0]);
1600
                    }
1601
                }
1602 2098 jones
                finally {
1603
                    // Return db connection
1604
                    DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1605
                }
1606 1716 berkley
1607 2098 jones
                // set content type and other response header fields first
1608
                //response.setContentType("text/xml");
1609
                out.println("<?xml version=\"1.0\"?>");
1610
                out.println("<success>");
1611
                out.println("<docid>" + newdocid + "</docid>");
1612
                out.println("</success>");
1613 1716 berkley
1614 2098 jones
            } catch (NullPointerException npe) {
1615
                //response.setContentType("text/xml");
1616
                out.println("<?xml version=\"1.0\"?>");
1617
                out.println("<error>");
1618
                out.println(npe.getMessage());
1619
                out.println("</error>");
1620
            }
1621
        } catch (Exception e) {
1622
            //response.setContentType("text/xml");
1623
            out.println("<?xml version=\"1.0\"?>");
1624
            out.println("<error>");
1625
            out.println(e.getMessage());
1626
            out.println("</error>");
1627 1760 tao
        }
1628 1409 tao
    }
1629 1716 berkley
1630 2098 jones
    /**
1631
     * Parse XML Document to look for <!DOCTYPE ... PUBLIC/SYSTEM ... > in
1632
     * order to decide whether to use validation parser
1633
     */
1634
    private static boolean needDTDValidation(StringReader xmlreader)
1635
            throws IOException
1636 1629 tao
    {
1637 2089 tao
1638 2098 jones
        StringBuffer cbuff = new StringBuffer();
1639
        java.util.Stack st = new java.util.Stack();
1640
        boolean validate = false;
1641
        int c;
1642
        int inx;
1643 2089 tao
1644 2098 jones
        // read from the stream until find the keywords
1645
        while ((st.empty() || st.size() < 4) && ((c = xmlreader.read()) != -1)) {
1646
            cbuff.append((char) c);
1647 1716 berkley
1648 2098 jones
            // "<!DOCTYPE" keyword is found; put it in the stack
1649
            if ((inx = cbuff.toString().indexOf("<!DOCTYPE")) != -1) {
1650
                cbuff = new StringBuffer();
1651
                st.push("<!DOCTYPE");
1652
            }
1653
            // "PUBLIC" keyword is found; put it in the stack
1654
            if ((inx = cbuff.toString().indexOf("PUBLIC")) != -1) {
1655
                cbuff = new StringBuffer();
1656
                st.push("PUBLIC");
1657
            }
1658
            // "SYSTEM" keyword is found; put it in the stack
1659
            if ((inx = cbuff.toString().indexOf("SYSTEM")) != -1) {
1660
                cbuff = new StringBuffer();
1661
                st.push("SYSTEM");
1662
            }
1663
            // ">" character is found; put it in the stack
1664
            // ">" is found twice: fisrt from <?xml ...?>
1665
            // and second from <!DOCTYPE ... >
1666
            if ((inx = cbuff.toString().indexOf(">")) != -1) {
1667
                cbuff = new StringBuffer();
1668
                st.push(">");
1669
            }
1670
        }
1671 203 jones
1672 2098 jones
        // close the stream
1673
        xmlreader.reset();
1674 1360 tao
1675 2098 jones
        // check the stack whether it contains the keywords:
1676
        // "<!DOCTYPE", "PUBLIC" or "SYSTEM", and ">" in this order
1677
        if (st.size() == 4) {
1678
            if (((String) st.pop()).equals(">")
1679
                    && (((String) st.peek()).equals("PUBLIC") | ((String) st
1680
                            .pop()).equals("SYSTEM"))
1681
                    && ((String) st.pop()).equals("<!DOCTYPE")) {
1682
                validate = true;
1683
            }
1684
        }
1685 1360 tao
1686 2098 jones
        MetaCatUtil.debugMessage("Validation for dtd is " + validate, 10);
1687
        return validate;
1688 1360 tao
    }
1689
1690 2098 jones
    // END OF INSERT/UPDATE SECTION
1691 68 higgins
1692 2098 jones
    /* check if the xml string contains key words to specify schema loocation */
1693
    private boolean needSchemaValidation(StringReader xml) throws IOException
1694
    {
1695
        boolean needSchemaValidate = false;
1696
        if (xml == null) {
1697
            MetaCatUtil.debugMessage("Validation for schema is "
1698
                    + needSchemaValidate, 10);
1699
            return needSchemaValidate;
1700
        }
1701
        System.out.println("before get target line");
1702
        String targetLine = getSchemaLine(xml);
1703 2268 sgarg
        System.out.println("after get target line");
1704 2098 jones
        // to see if the second line contain some keywords
1705
        if (targetLine != null
1706
                && (targetLine.indexOf(SCHEMALOCATIONKEYWORD) != -1 || targetLine
1707
                        .indexOf(NONAMESPACELOCATION) != -1)) {
1708
            // if contains schema location key word, should be validate
1709
            needSchemaValidate = true;
1710
        }
1711 1360 tao
1712 2098 jones
        MetaCatUtil.debugMessage("Validation for schema is "
1713
                + needSchemaValidate, 10);
1714
        return needSchemaValidate;
1715 68 higgins
1716 2098 jones
    }
1717 1360 tao
1718 2098 jones
    /* check if the xml string contains key words to specify schema loocation */
1719 2169 sgarg
    private String findNamespace(StringReader xml) throws IOException
1720 2098 jones
    {
1721 2169 sgarg
        String namespace = null;
1722
1723
        String eml2_0_0NameSpace = DocumentImpl.EML2_0_0NAMESPACE;
1724 2224 sgarg
        String eml2_0_1NameSpace = DocumentImpl.EML2_0_1NAMESPACE;
1725 2169 sgarg
        String eml2_1_0NameSpace = DocumentImpl.EML2_1_0NAMESPACE;
1726
1727 2098 jones
        if (xml == null) {
1728
            MetaCatUtil.debugMessage("Validation for schema is "
1729 2169 sgarg
                    + namespace, 10);
1730
            return namespace;
1731 2098 jones
        }
1732
        String targetLine = getSchemaLine(xml);
1733 309 bojilova
1734 2098 jones
        if (targetLine != null) {
1735 1360 tao
1736 2098 jones
            int startIndex = targetLine.indexOf(SCHEMALOCATIONKEYWORD);
1737
            int start = 1;
1738
            int end = 1;
1739
            String schemaLocation = null;
1740
            int count = 0;
1741
            if (startIndex != -1) {
1742
                for (int i = startIndex; i < targetLine.length(); i++) {
1743
                    if (targetLine.charAt(i) == '"') {
1744
                        count++;
1745
                    }
1746
                    if (targetLine.charAt(i) == '"' && count == 1) {
1747
                        start = i;
1748
                    }
1749
                    if (targetLine.charAt(i) == '"' && count == 2) {
1750
                        end = i;
1751
                        break;
1752
                    }
1753
                }
1754
            }
1755
            schemaLocation = targetLine.substring(start + 1, end);
1756
            MetaCatUtil.debugMessage("schemaLocation in xml is: "
1757
                    + schemaLocation, 30);
1758 2169 sgarg
            if (schemaLocation.indexOf(eml2_0_0NameSpace) != -1) {
1759
                namespace = eml2_0_0NameSpace;
1760 2224 sgarg
            } else if (schemaLocation.indexOf(eml2_0_1NameSpace) != -1) {
1761
                namespace = eml2_0_1NameSpace;
1762 2169 sgarg
            } else if (schemaLocation.indexOf(eml2_1_0NameSpace) != -1) {
1763
                namespace = eml2_1_0NameSpace;
1764 2098 jones
            }
1765
        }
1766 185 jones
1767 2169 sgarg
        MetaCatUtil.debugMessage("Validation for eml is " + namespace,
1768 2098 jones
                10);
1769 2224 sgarg
1770 2169 sgarg
        return namespace;
1771 1360 tao
1772 103 jones
    }
1773 68 higgins
1774 2098 jones
    private String getSchemaLine(StringReader xml) throws IOException
1775
    {
1776
        // find the line
1777
        String secondLine = null;
1778
        int count = 0;
1779
        int endIndex = 0;
1780
        int startIndex = 0;
1781
        final int TARGETNUM = 2;
1782
        StringBuffer buffer = new StringBuffer();
1783
        boolean comment = false;
1784
        char thirdPreviousCharacter = '?';
1785
        char secondPreviousCharacter = '?';
1786
        char previousCharacter = '?';
1787
        char currentCharacter = '?';
1788 2268 sgarg
        int tmp = xml.read();
1789
        while (tmp != -1) {
1790
            currentCharacter = (char)tmp;
1791 2098 jones
            //in a comment
1792
            if (currentCharacter == '-' && previousCharacter == '-'
1793
                    && secondPreviousCharacter == '!'
1794
                    && thirdPreviousCharacter == '<') {
1795
                comment = true;
1796
            }
1797
            //out of comment
1798
            if (comment && currentCharacter == '>' && previousCharacter == '-'
1799
                    && secondPreviousCharacter == '-') {
1800
                comment = false;
1801
            }
1802 68 higgins
1803 2098 jones
            //this is not comment
1804
            if (currentCharacter != '!' && previousCharacter == '<' && !comment) {
1805
                count++;
1806
            }
1807
            // get target line
1808
            if (count == TARGETNUM && currentCharacter != '>') {
1809
                buffer.append(currentCharacter);
1810
            }
1811
            if (count == TARGETNUM && currentCharacter == '>') {
1812
                break;
1813
            }
1814
            thirdPreviousCharacter = secondPreviousCharacter;
1815
            secondPreviousCharacter = previousCharacter;
1816
            previousCharacter = currentCharacter;
1817 2268 sgarg
            tmp = xml.read();
1818 2098 jones
        }
1819
        secondLine = buffer.toString();
1820
        MetaCatUtil
1821
                .debugMessage("the second line string is: " + secondLine, 25);
1822
        xml.reset();
1823
        return secondLine;
1824
    }
1825 253 jones
1826 2098 jones
    /**
1827
     * Handle the database delete request and delete an XML document from the
1828
     * database connection
1829
     */
1830
    private void handleDeleteAction(PrintWriter out, Hashtable params,
1831 2169 sgarg
            HttpServletRequest request, HttpServletResponse response,
1832 2102 jones
            String user, String[] groups)
1833 2098 jones
    {
1834 1360 tao
1835 2098 jones
        String[] docid = (String[]) params.get("docid");
1836 1360 tao
1837 2251 sgarg
        if(docid == null){
1838
          response.setContentType("text/xml");
1839
          out.println("<?xml version=\"1.0\"?>");
1840
          out.println("<error>");
1841 2252 sgarg
          out.println("Docid not specified.");
1842 2251 sgarg
          out.println("</error>");
1843
        } else {
1844 1360 tao
1845 2251 sgarg
            // delete the document from the database
1846 2098 jones
            try {
1847 2251 sgarg
1848
                try {
1849 2298 tao
                    // null means notify server is null
1850
                    DocumentImpl.delete(docid[0], user, groups, null);
1851 2251 sgarg
                    EventLog.getInstance().log(request.getRemoteAddr(),
1852
                                               user, docid[0], "delete");
1853
                    response.setContentType("text/xml");
1854
                    out.println("<?xml version=\"1.0\"?>");
1855
                    out.println("<success>");
1856
                    out.println("Document deleted.");
1857
                    out.println("</success>");
1858
                }
1859
                catch (AccessionNumberException ane) {
1860
                    response.setContentType("text/xml");
1861
                    out.println("<?xml version=\"1.0\"?>");
1862
                    out.println("<error>");
1863 2253 sgarg
                    //out.println("Error deleting document!!!");
1864 2251 sgarg
                    out.println(ane.getMessage());
1865
                    out.println("</error>");
1866
                }
1867
            }
1868
            catch (Exception e) {
1869 2098 jones
                response.setContentType("text/xml");
1870
                out.println("<?xml version=\"1.0\"?>");
1871
                out.println("<error>");
1872 2251 sgarg
                out.println(e.getMessage());
1873 2098 jones
                out.println("</error>");
1874
            }
1875
        }
1876 1292 tao
    }
1877 1360 tao
1878 2098 jones
    /**
1879
     * Handle the validation request and return the results to the requestor
1880
     */
1881
    private void handleValidateAction(PrintWriter out, Hashtable params)
1882 1292 tao
    {
1883 1360 tao
1884 2098 jones
        // Get the document indicated
1885
        String valtext = null;
1886
        DBConnection dbConn = null;
1887
        int serialNumber = -1;
1888 1292 tao
1889 2098 jones
        try {
1890
            valtext = ((String[]) params.get("valtext"))[0];
1891
        } catch (Exception nullpe) {
1892 1360 tao
1893 2098 jones
            String docid = null;
1894
            try {
1895
                // Find the document id number
1896
                docid = ((String[]) params.get("docid"))[0];
1897 1360 tao
1898 2098 jones
                // Get the document indicated from the db
1899
                DocumentImpl xmldoc = new DocumentImpl(docid);
1900
                valtext = xmldoc.toString();
1901 688 bojilova
1902 2098 jones
            } catch (NullPointerException npe) {
1903 1360 tao
1904 2098 jones
                out.println("<error>Error getting document ID: " + docid
1905
                        + "</error>");
1906
                //if ( conn != null ) { util.returnConnection(conn); }
1907
                return;
1908
            } catch (Exception e) {
1909 688 bojilova
1910 2098 jones
                out.println(e.getMessage());
1911
            }
1912
        }
1913 688 bojilova
1914 2098 jones
        try {
1915
            // get a connection from the pool
1916
            dbConn = DBConnectionPool
1917
                    .getDBConnection("MetaCatServlet.handleValidateAction");
1918
            serialNumber = dbConn.getCheckOutSerialNumber();
1919
            DBValidate valobj = new DBValidate(saxparser, dbConn);
1920
            boolean valid = valobj.validateString(valtext);
1921 1360 tao
1922 2098 jones
            // set content type and other response header fields first
1923 688 bojilova
1924 2098 jones
            out.println(valobj.returnErrors());
1925 731 bojilova
1926 2098 jones
        } catch (NullPointerException npe2) {
1927
            // set content type and other response header fields first
1928 1360 tao
1929 2098 jones
            out.println("<error>Error validating document.</error>");
1930
        } catch (Exception e) {
1931 731 bojilova
1932 2098 jones
            out.println(e.getMessage());
1933
        } finally {
1934
            // Return db connection
1935
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1936
        }
1937
    }
1938 1360 tao
1939 2098 jones
    /**
1940
     * Handle "getrevsionanddoctype" action Given a docid, return it's current
1941
     * revision and doctype from data base The output is String look like
1942
     * "rev;doctype"
1943
     */
1944
    private void handleGetRevisionAndDocTypeAction(PrintWriter out,
1945
            Hashtable params)
1946
    {
1947
        // To store doc parameter
1948
        String[] docs = new String[10];
1949
        // Store a single doc id
1950
        String givenDocId = null;
1951
        // Get docid from parameters
1952
        if (params.containsKey("docid")) {
1953
            docs = (String[]) params.get("docid");
1954
        }
1955
        // Get first docid form string array
1956
        givenDocId = docs[0];
1957 731 bojilova
1958 2098 jones
        try {
1959
            // Make sure there is a docid
1960
            if (givenDocId == null || givenDocId.equals("")) { throw new Exception(
1961
                    "User didn't specify docid!"); }//if
1962 1360 tao
1963 2098 jones
            // Create a DBUtil object
1964
            DBUtil dbutil = new DBUtil();
1965
            // Get a rev and doctype
1966
            String revAndDocType = dbutil
1967
                    .getCurrentRevisionAndDocTypeForGivenDocument(givenDocId);
1968
            out.println(revAndDocType);
1969 731 bojilova
1970 2098 jones
        } catch (Exception e) {
1971
            // Handle exception
1972
            out.println("<?xml version=\"1.0\"?>");
1973
            out.println("<error>");
1974
            out.println(e.getMessage());
1975
            out.println("</error>");
1976
        }
1977 302 bojilova
1978 2098 jones
    }
1979 1360 tao
1980 2098 jones
    /**
1981
     * Handle "getaccesscontrol" action. Read Access Control List from db
1982
     * connection in XML format
1983
     */
1984
    private void handleGetAccessControlAction(PrintWriter out,
1985
            Hashtable params, HttpServletResponse response, String username,
1986
            String[] groupnames)
1987
    {
1988
        DBConnection dbConn = null;
1989
        int serialNumber = -1;
1990
        String docid = ((String[]) params.get("docid"))[0];
1991 302 bojilova
1992 2098 jones
        try {
1993 1360 tao
1994 2098 jones
            // get connection from the pool
1995
            dbConn = DBConnectionPool
1996
                    .getDBConnection("MetaCatServlet.handleGetAccessControlAction");
1997
            serialNumber = dbConn.getCheckOutSerialNumber();
1998
            AccessControlList aclobj = new AccessControlList(dbConn);
1999
            String acltext = aclobj.getACL(docid, username, groupnames);
2000
            out.println(acltext);
2001 302 bojilova
2002 2098 jones
        } catch (Exception e) {
2003
            out.println("<?xml version=\"1.0\"?>");
2004
            out.println("<error>");
2005
            out.println(e.getMessage());
2006
            out.println("</error>");
2007
        } finally {
2008
            // Retrun db connection to pool
2009
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2010
        }
2011 1360 tao
    }
2012
2013 2098 jones
    /**
2014
     * Handle the "getprincipals" action. Read all principals from
2015
     * authentication scheme in XML format
2016
     */
2017
    private void handleGetPrincipalsAction(PrintWriter out, String user,
2018
            String password)
2019
    {
2020
        try {
2021
            AuthSession auth = new AuthSession();
2022
            String principals = auth.getPrincipals(user, password);
2023
            out.println(principals);
2024 302 bojilova
2025 2098 jones
        } catch (Exception e) {
2026
            out.println("<?xml version=\"1.0\"?>");
2027
            out.println("<error>");
2028
            out.println(e.getMessage());
2029
            out.println("</error>");
2030
        }
2031
    }
2032 699 bojilova
2033 2098 jones
    /**
2034
     * Handle "getdoctypes" action. Read all doctypes from db connection in XML
2035
     * format
2036
     */
2037
    private void handleGetDoctypesAction(PrintWriter out, Hashtable params,
2038
            HttpServletResponse response)
2039
    {
2040
        try {
2041
            DBUtil dbutil = new DBUtil();
2042
            String doctypes = dbutil.readDoctypes();
2043
            out.println(doctypes);
2044
        } catch (Exception e) {
2045
            out.println("<?xml version=\"1.0\"?>");
2046
            out.println("<error>");
2047
            out.println(e.getMessage());
2048
            out.println("</error>");
2049
        }
2050
    }
2051 1360 tao
2052 2098 jones
    /**
2053
     * Handle the "getdtdschema" action. Read DTD or Schema file for a given
2054
     * doctype from Metacat catalog system
2055
     */
2056
    private void handleGetDTDSchemaAction(PrintWriter out, Hashtable params,
2057
            HttpServletResponse response)
2058
    {
2059 699 bojilova
2060 2098 jones
        String doctype = null;
2061
        String[] doctypeArr = (String[]) params.get("doctype");
2062 699 bojilova
2063 2098 jones
        // get only the first doctype specified in the list of doctypes
2064
        // it could be done for all doctypes in that list
2065
        if (doctypeArr != null) {
2066
            doctype = ((String[]) params.get("doctype"))[0];
2067
        }
2068 699 bojilova
2069 2098 jones
        try {
2070
            DBUtil dbutil = new DBUtil();
2071
            String dtdschema = dbutil.readDTDSchema(doctype);
2072
            out.println(dtdschema);
2073 1360 tao
2074 2098 jones
        } catch (Exception e) {
2075
            out.println("<?xml version=\"1.0\"?>");
2076
            out.println("<error>");
2077
            out.println(e.getMessage());
2078
            out.println("</error>");
2079
        }
2080 699 bojilova
2081 1360 tao
    }
2082
2083 2098 jones
    /**
2084
     * Handle the "getlastdocid" action. Get the latest docid with rev number
2085
     * from db connection in XML format
2086
     */
2087
    private void handleGetMaxDocidAction(PrintWriter out, Hashtable params,
2088
            HttpServletResponse response)
2089
    {
2090 699 bojilova
2091 2098 jones
        String scope = ((String[]) params.get("scope"))[0];
2092
        if (scope == null) {
2093
            scope = ((String[]) params.get("username"))[0];
2094
        }
2095 793 bojilova
2096 2098 jones
        try {
2097 1217 tao
2098 2098 jones
            DBUtil dbutil = new DBUtil();
2099
            String lastDocid = dbutil.getMaxDocid(scope);
2100
            out.println("<?xml version=\"1.0\"?>");
2101
            out.println("<lastDocid>");
2102
            out.println("  <scope>" + scope + "</scope>");
2103
            out.println("  <docid>" + lastDocid + "</docid>");
2104
            out.println("</lastDocid>");
2105 793 bojilova
2106 2098 jones
        } catch (Exception e) {
2107
            out.println("<?xml version=\"1.0\"?>");
2108
            out.println("<error>");
2109
            out.println(e.getMessage());
2110
            out.println("</error>");
2111
        }
2112 1217 tao
    }
2113 1360 tao
2114 2098 jones
    /**
2115 2113 jones
     * Print a report from the event log based on filter parameters passed in
2116
     * from the web.
2117 2169 sgarg
     *
2118 2113 jones
     * @param params the parameters from the web request
2119
     * @param request the http request object for getting request details
2120
     * @param response the http response object for writing output
2121
     */
2122
    private void handleGetLogAction(Hashtable params, HttpServletRequest request,
2123 2312 jones
            HttpServletResponse response, String username)
2124 2113 jones
    {
2125
        try {
2126 2312 jones
            response.setContentType("text/xml");
2127
            PrintWriter out = response.getWriter();
2128 2347 sgarg
2129 2312 jones
            // Check that the user is authenticated as an administrator account
2130
            boolean adminIsAuthenticated = false;
2131
            for (int i = 0; i < administrators.length; i++) {
2132
                if (username.equals(administrators[i])) {
2133
                        adminIsAuthenticated = true;
2134
                }
2135 2113 jones
            }
2136 2312 jones
            if (!adminIsAuthenticated) {
2137
                out.print("<error>");
2138 2347 sgarg
                out.print("The user \"" + username +
2139 2312 jones
                        "\" is not authorized for this action.");
2140
                out.print("</error>");
2141
                return;
2142 2113 jones
            }
2143 2347 sgarg
2144 2312 jones
            // Get all of the parameters in the correct formats
2145
            String[] ipAddress = (String[])params.get("ipaddress");
2146
            String[] principal = (String[])params.get("principal");
2147
            String[] docid = (String[])params.get("docid");
2148
            String[] event = (String[])params.get("event");
2149
            String[] startArray = (String[]) params.get("start");
2150
            String[] endArray = (String[]) params.get("end");
2151
            String start = null;
2152
            String end = null;
2153
            if (startArray != null) {
2154
                start = startArray[0];
2155
            }
2156
            if (endArray != null) {
2157
                end = endArray[0];
2158
            }
2159
            Timestamp startDate = null;
2160
            Timestamp endDate = null;
2161
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
2162
            try {
2163
                if (start != null) {
2164
                    startDate = new Timestamp((format.parse(start)).getTime());
2165
                }
2166
                if (end != null) {
2167
                    endDate = new Timestamp((format.parse(end)).getTime());
2168
                }
2169
            } catch (ParseException e) {
2170
                System.out.println("Failed to created Timestamp from input.");
2171
            }
2172 2347 sgarg
2173 2312 jones
            // Request the report by passing the filter parameters
2174
            out.println(EventLog.getInstance().getReport(ipAddress, principal,
2175
                    docid, event, startDate, endDate));
2176
            out.close();
2177
        } catch (IOException e) {
2178
            MetaCatUtil.debugMessage(
2179
                    "Could not open http response for writing: " + e.getMessage(), 5);
2180 2113 jones
        }
2181 2312 jones
    }
2182 2169 sgarg
2183 2312 jones
    /**
2184
     * Rebuild the index for one or more documents. If the docid parameter is
2185
     * provided, rebuild for just that one document or list of documents. If
2186
     * not, then rebuild the index for all documents in the xml_documents
2187
     * table.
2188
     *
2189
     * @param params the parameters from the web request
2190
     * @param request the http request object for getting request details
2191
     * @param response the http response object for writing output
2192
     * @param username the username of the authenticated user
2193
     */
2194 2347 sgarg
    private void handleBuildIndexAction(Hashtable params,
2195 2312 jones
            HttpServletRequest request, HttpServletResponse response,
2196
            String username)
2197
    {
2198
        // Get all of the parameters in the correct formats
2199
        String[] docid = (String[])params.get("docid");
2200
2201
        // Rebuild the indices for appropriate documents
2202 2113 jones
        try {
2203
            response.setContentType("text/xml");
2204
            PrintWriter out = response.getWriter();
2205 2347 sgarg
2206 2312 jones
            // Check that the user is authenticated as an administrator account
2207
            boolean adminIsAuthenticated = false;
2208
            for (int i = 0; i < administrators.length; i++) {
2209
                if (username.equals(administrators[i])) {
2210
                        adminIsAuthenticated = true;
2211
                }
2212
            }
2213
            if (!adminIsAuthenticated) {
2214
                out.print("<error>");
2215 2347 sgarg
                out.print("The user \"" + username +
2216 2312 jones
                        "\" is not authorized for this action.");
2217
                out.print("</error>");
2218
                return;
2219
            }
2220
2221
            // Process the documents
2222
            out.println("<success>");
2223
            if (docid == null || docid.length == 0) {
2224
                // Process all of the documents
2225
                try {
2226
                    Vector documents = getDocumentList();
2227
                    Iterator it = documents.iterator();
2228
                    while (it.hasNext()) {
2229
                        String id = (String) it.next();
2230
                        buildDocumentIndex(id, out);
2231
                    }
2232
                } catch (SQLException se) {
2233
                    out.print("<error>");
2234
                    out.print(se.getMessage());
2235
                    out.println("</error>");
2236
                }
2237
            } else {
2238
                // Only process the requested documents
2239
                for (int i = 0; i < docid.length; i++) {
2240
                    buildDocumentIndex(docid[i], out);
2241
                }
2242
            }
2243
            out.println("</success>");
2244 2113 jones
            out.close();
2245
        } catch (IOException e) {
2246
            MetaCatUtil.debugMessage(
2247 2169 sgarg
                    "Could not open http response for writing: "
2248 2113 jones
                    + e.getMessage(), 5);
2249
        }
2250
    }
2251 2169 sgarg
2252 2113 jones
    /**
2253 2347 sgarg
     * Build the index for one document by reading the document and
2254 2312 jones
     * calling its buildIndex() method.
2255
     *
2256
     * @param docid the document (with revision) to rebuild
2257
     * @param out the PrintWriter to which output is printed
2258
     */
2259
    private void buildDocumentIndex(String docid, PrintWriter out)
2260
    {
2261
        try {
2262
            DocumentImpl doc = new DocumentImpl(docid, false);
2263
            doc.buildIndex();
2264
            out.print("<docid>" + docid);
2265
            out.println("</docid>");
2266
        } catch (McdbException me) {
2267
            out.print("<error>");
2268
            out.print(me.getMessage());
2269
            out.println("</error>");
2270
        }
2271
    }
2272
2273
    /**
2274 2098 jones
     * Handle documents passed to metacat that are encoded using the
2275
     * "multipart/form-data" mime type. This is typically used for uploading
2276
     * data files which may be binary and large.
2277
     */
2278
    private void handleMultipartForm(HttpServletRequest request,
2279
            HttpServletResponse response)
2280
    {
2281
        PrintWriter out = null;
2282
        String action = null;
2283 793 bojilova
2284 2098 jones
        // Parse the multipart form, and save the parameters in a Hashtable and
2285
        // save the FileParts in a hashtable
2286 798 jones
2287 2098 jones
        Hashtable params = new Hashtable();
2288
        Hashtable fileList = new Hashtable();
2289
        int sizeLimit = (new Integer(MetaCatUtil.getOption("datafilesizelimit")))
2290
                .intValue();
2291
        MetaCatUtil.debugMessage(
2292
                "The limit size of data file is: " + sizeLimit, 50);
2293 798 jones
2294 2098 jones
        try {
2295
            // MBJ: need to put filesize limit in Metacat config
2296
            // (metacat.properties)
2297
            MultipartParser mp = new MultipartParser(request,
2298
                    sizeLimit * 1024 * 1024);
2299
            Part part;
2300
            while ((part = mp.readNextPart()) != null) {
2301
                String name = part.getName();
2302 798 jones
2303 2098 jones
                if (part.isParam()) {
2304
                    // it's a parameter part
2305
                    ParamPart paramPart = (ParamPart) part;
2306
                    String value = paramPart.getStringValue();
2307
                    params.put(name, value);
2308
                    if (name.equals("action")) {
2309
                        action = value;
2310
                    }
2311
                } else if (part.isFile()) {
2312
                    // it's a file part
2313
                    FilePart filePart = (FilePart) part;
2314
                    fileList.put(name, filePart);
2315 798 jones
2316 2098 jones
                    // Stop once the first file part is found, otherwise going
2317
                    // onto the
2318
                    // next part prevents access to the file contents. So...for
2319
                    // upload
2320
                    // to work, the datafile must be the last part
2321
                    break;
2322
                }
2323
            }
2324
        } catch (IOException ioe) {
2325
            try {
2326
                out = response.getWriter();
2327
            } catch (IOException ioe2) {
2328
                System.err
2329
                        .println("Fatal Error: couldn't get response output stream.");
2330
            }
2331
            out.println("<?xml version=\"1.0\"?>");
2332
            out.println("<error>");
2333
            out.println("Error: problem reading multipart data.");
2334
            out.println("</error>");
2335 798 jones
        }
2336
2337 2098 jones
        // Get the session information
2338
        String username = null;
2339
        String password = null;
2340
        String[] groupnames = null;
2341
        String sess_id = null;
2342 798 jones
2343 2098 jones
        // be aware of session expiration on every request
2344
        HttpSession sess = request.getSession(true);
2345
        if (sess.isNew()) {
2346
            // session expired or has not been stored b/w user requests
2347
            username = "public";
2348
            sess.setAttribute("username", username);
2349
        } else {
2350
            username = (String) sess.getAttribute("username");
2351
            password = (String) sess.getAttribute("password");
2352
            groupnames = (String[]) sess.getAttribute("groupnames");
2353
            try {
2354
                sess_id = (String) sess.getId();
2355
            } catch (IllegalStateException ise) {
2356
                System.out
2357
                        .println("error in  handleMultipartForm: this shouldn't "
2358
                                + "happen: the session should be valid: "
2359
                                + ise.getMessage());
2360
            }
2361
        }
2362 1360 tao
2363 2098 jones
        // Get the out stream
2364
        try {
2365
            out = response.getWriter();
2366 798 jones
        } catch (IOException ioe2) {
2367 2098 jones
            MetaCatUtil.debugMessage("Fatal Error: couldn't get response "
2368
                    + "output stream.", 30);
2369 798 jones
        }
2370 1360 tao
2371 2098 jones
        if (action.equals("upload")) {
2372
            if (username != null && !username.equals("public")) {
2373
                handleUploadAction(request, out, params, fileList, username,
2374
                        groupnames);
2375
            } else {
2376 1360 tao
2377 2098 jones
                out.println("<?xml version=\"1.0\"?>");
2378
                out.println("<error>");
2379
                out.println("Permission denied for " + action);
2380
                out.println("</error>");
2381
            }
2382
        } else {
2383
            /*
2384
             * try { out = response.getWriter(); } catch (IOException ioe2) {
2385
             * System.err.println("Fatal Error: couldn't get response output
2386
             * stream.");
2387
             */
2388
            out.println("<?xml version=\"1.0\"?>");
2389
            out.println("<error>");
2390
            out.println(
2391
                    "Error: action not registered.  Please report this error.");
2392
            out.println("</error>");
2393
        }
2394
        out.close();
2395 798 jones
    }
2396
2397 2098 jones
    /**
2398
     * Handle the upload action by saving the attached file to disk and
2399
     * registering it in the Metacat db
2400
     */
2401
    private void handleUploadAction(HttpServletRequest request,
2402
            PrintWriter out, Hashtable params, Hashtable fileList,
2403
            String username, String[] groupnames)
2404 1041 tao
    {
2405 2098 jones
        //PrintWriter out = null;
2406
        //Connection conn = null;
2407
        String action = null;
2408
        String docid = null;
2409 798 jones
2410 2098 jones
        /*
2411
         * response.setContentType("text/xml"); try { out =
2412
         * response.getWriter(); } catch (IOException ioe2) {
2413
         * System.err.println("Fatal Error: couldn't get response output
2414
         * stream.");
2415
         */
2416 798 jones
2417 2098 jones
        if (params.containsKey("docid")) {
2418
            docid = (String) params.get("docid");
2419
        }
2420 798 jones
2421 2098 jones
        // Make sure we have a docid and datafile
2422
        if (docid != null && fileList.containsKey("datafile")) {
2423 798 jones
2424 2098 jones
            // Get a reference to the file part of the form
2425
            FilePart filePart = (FilePart) fileList.get("datafile");
2426
            String fileName = filePart.getFileName();
2427
            MetaCatUtil.debugMessage("Uploading filename: " + fileName, 10);
2428 1360 tao
2429 2098 jones
            // Check if the right file existed in the uploaded data
2430
            if (fileName != null) {
2431 1360 tao
2432 2098 jones
                try {
2433
                    //MetaCatUtil.debugMessage("Upload datafile " + docid
2434
                    // +"...", 10);
2435
                    //If document get lock data file grant
2436
                    if (DocumentImpl.getDataFileLockGrant(docid)) {
2437
                        // register the file in the database (which generates
2438
                        // an exception
2439
                        //if the docid is not acceptable or other untoward
2440
                        // things happen
2441
                        DocumentImpl.registerDocument(fileName, "BIN", docid,
2442 2259 sgarg
                                username, groupnames);
2443 1360 tao
2444 2098 jones
                        // Save the data file to disk using "docid" as the name
2445
                        dataDirectory.mkdirs();
2446
                        File newFile = new File(dataDirectory, docid);
2447
                        long size = filePart.writeTo(newFile);
2448 1360 tao
2449 2169 sgarg
                        EventLog.getInstance().log(request.getRemoteAddr(),
2450 2102 jones
                                username, docid, "upload");
2451 2098 jones
                        // Force replication this data file
2452
                        // To data file, "insert" and update is same
2453
                        // The fourth parameter is null. Because it is
2454
                        // notification server
2455
                        // and this method is in MetaCatServerlet. It is
2456
                        // original command,
2457
                        // not get force replication info from another metacat
2458
                        ForceReplicationHandler frh = new ForceReplicationHandler(
2459
                                docid, "insert", false, null);
2460 1360 tao
2461 2098 jones
                        // set content type and other response header fields
2462
                        // first
2463
                        out.println("<?xml version=\"1.0\"?>");
2464
                        out.println("<success>");
2465
                        out.println("<docid>" + docid + "</docid>");
2466
                        out.println("<size>" + size + "</size>");
2467
                        out.println("</success>");
2468
                    }
2469
2470
                } catch (Exception e) {
2471
                    out.println("<?xml version=\"1.0\"?>");
2472
                    out.println("<error>");
2473
                    out.println(e.getMessage());
2474
                    out.println("</error>");
2475
                }
2476
            } else {
2477
                // the field did not contain a file
2478
                out.println("<?xml version=\"1.0\"?>");
2479
                out.println("<error>");
2480
                out.println("The uploaded data did not contain a valid file.");
2481
                out.println("</error>");
2482
            }
2483
        } else {
2484
            // Error bcse docid missing or file missing
2485
            out.println("<?xml version=\"1.0\"?>");
2486
            out.println("<error>");
2487
            out.println("The uploaded data did not contain a valid docid "
2488
                    + "or valid file.");
2489
            out.println("</error>");
2490 798 jones
        }
2491 2098 jones
    }
2492 1360 tao
2493 2098 jones
    /*
2494
     * A method to handle set access action
2495
     */
2496
    private void handleSetAccessAction(PrintWriter out, Hashtable params,
2497
            String username)
2498 1041 tao
    {
2499 2098 jones
        String[] docList = null;
2500
        String[] principalList = null;
2501
        String[] permissionList = null;
2502
        String[] permTypeList = null;
2503
        String[] permOrderList = null;
2504
        String permission = null;
2505
        String permType = null;
2506
        String permOrder = null;
2507
        Vector errorList = new Vector();
2508
        String error = null;
2509
        Vector successList = new Vector();
2510
        String success = null;
2511 1716 berkley
2512 2098 jones
        // Get parameters
2513
        if (params.containsKey("docid")) {
2514
            docList = (String[]) params.get("docid");
2515
        }
2516
        if (params.containsKey("principal")) {
2517
            principalList = (String[]) params.get("principal");
2518
        }
2519
        if (params.containsKey("permission")) {
2520
            permissionList = (String[]) params.get("permission");
2521 1716 berkley
2522 2098 jones
        }
2523
        if (params.containsKey("permType")) {
2524
            permTypeList = (String[]) params.get("permType");
2525 1716 berkley
2526 2098 jones
        }
2527
        if (params.containsKey("permOrder")) {
2528
            permOrderList = (String[]) params.get("permOrder");
2529 1716 berkley
2530 2098 jones
        }
2531 1716 berkley
2532 2098 jones
        // Make sure the parameter is not null
2533
        if (docList == null || principalList == null || permTypeList == null
2534
                || permissionList == null) {
2535
            error = "Please check your parameter list, it should look like: "
2536
                    + "?action=setaccess&docid=pipeline.1.1&principal=public"
2537
                    + "&permission=read&permType=allow&permOrder=allowFirst";
2538
            errorList.addElement(error);
2539
            outputResponse(successList, errorList, out);
2540
            return;
2541
        }
2542 1716 berkley
2543 2098 jones
        // Only select first element for permission, type and order
2544
        permission = permissionList[0];
2545
        permType = permTypeList[0];
2546
        if (permOrderList != null) {
2547
            permOrder = permOrderList[0];
2548
        }
2549 1716 berkley
2550 2098 jones
        // Get package doctype set
2551
        Vector packageSet = MetaCatUtil.getOptionList(MetaCatUtil
2552
                .getOption("packagedoctypeset"));
2553
        //debug
2554
        if (packageSet != null) {
2555
            for (int i = 0; i < packageSet.size(); i++) {
2556
                MetaCatUtil.debugMessage("doctype in package set: "
2557
                        + (String) packageSet.elementAt(i), 34);
2558
            }
2559
        }
2560 1716 berkley
2561 2098 jones
        // handle every accessionNumber
2562
        for (int i = 0; i < docList.length; i++) {
2563
            String accessionNumber = docList[i];
2564
            String owner = null;
2565
            String publicId = null;
2566
            // Get document owner and public id
2567
            try {
2568
                owner = getFieldValueForDoc(accessionNumber, "user_owner");
2569
                publicId = getFieldValueForDoc(accessionNumber, "doctype");
2570
            } catch (Exception e) {
2571
                MetaCatUtil.debugMessage("Error in handleSetAccessAction: "
2572
                        + e.getMessage(), 30);
2573
                error = "Error in set access control for document - "
2574
                        + accessionNumber + e.getMessage();
2575
                errorList.addElement(error);
2576
                continue;
2577
            }
2578
            //check if user is the owner. Only owner can do owner
2579
            if (username == null || owner == null || !username.equals(owner)) {
2580
                error = "User - " + username
2581
                        + " does not have permission to set "
2582
                        + "access control for docid - " + accessionNumber;
2583
                errorList.addElement(error);
2584
                continue;
2585
            }
2586 1716 berkley
2587 2098 jones
            // If docid publicid is BIN data file or other beta4, 6 package
2588
            // document
2589
            // we could not do set access control. Because we don't want
2590
            // inconsistent
2591
            // to its access docuemnt
2592
            if (publicId != null && packageSet != null
2593
                    && packageSet.contains(publicId)) {
2594
                error = "Could not set access control to document "
2595
                        + accessionNumber
2596
                        + "because it is in a pakcage and it has a access file for it";
2597
                errorList.addElement(error);
2598
                continue;
2599
            }
2600 1716 berkley
2601 2098 jones
            // for every principle
2602
            for (int j = 0; j < principalList.length; j++) {
2603
                String principal = principalList[j];
2604
                try {
2605
                    //insert permission
2606
                    AccessControlForSingleFile accessControl = new AccessControlForSingleFile(
2607
                            accessionNumber, principal, permission, permType,
2608
                            permOrder);
2609
                    accessControl.insertPermissions();
2610
                    success = "Set access control to document "
2611
                            + accessionNumber + " successfully";
2612
                    successList.addElement(success);
2613
                } catch (Exception ee) {
2614
                    MetaCatUtil.debugMessage(
2615
                            "Erorr in handleSetAccessAction2: "
2616
                                    + ee.getMessage(), 30);
2617
                    error = "Faild to set access control for document "
2618
                            + accessionNumber + " because " + ee.getMessage();
2619
                    errorList.addElement(error);
2620
                    continue;
2621
                }
2622
            }
2623 1369 tao
        }
2624 2098 jones
        outputResponse(successList, errorList, out);
2625
    }
2626 1716 berkley
2627 2098 jones
    /*
2628
     * A method try to determin a docid's public id, if couldn't find null will
2629
     * be returned.
2630
     */
2631
    private String getFieldValueForDoc(String accessionNumber, String fieldName)
2632
            throws Exception
2633 1369 tao
    {
2634 2098 jones
        if (accessionNumber == null || accessionNumber.equals("")
2635
                || fieldName == null || fieldName.equals("")) { throw new Exception(
2636
                "Docid or field name was not specified"); }
2637 1716 berkley
2638 2098 jones
        PreparedStatement pstmt = null;
2639
        ResultSet rs = null;
2640
        String fieldValue = null;
2641
        String docId = null;
2642
        DBConnection conn = null;
2643
        int serialNumber = -1;
2644 1716 berkley
2645 2098 jones
        // get rid of revision if access number has
2646
        docId = MetaCatUtil.getDocIdFromString(accessionNumber);
2647
        try {
2648
            //check out DBConnection
2649
            conn = DBConnectionPool
2650
                    .getDBConnection("MetaCatServlet.getPublicIdForDoc");
2651
            serialNumber = conn.getCheckOutSerialNumber();
2652
            pstmt = conn.prepareStatement("SELECT " + fieldName
2653
                    + " FROM xml_documents " + "WHERE docid = ? ");
2654 1716 berkley
2655 2098 jones
            pstmt.setString(1, docId);
2656
            pstmt.execute();
2657
            rs = pstmt.getResultSet();
2658
            boolean hasRow = rs.next();
2659
            int perm = 0;
2660
            if (hasRow) {
2661
                fieldValue = rs.getString(1);
2662
            } else {
2663
                throw new Exception("Could not find document: "
2664
                        + accessionNumber);
2665
            }
2666
        } catch (Exception e) {
2667
            MetaCatUtil.debugMessage(
2668
                    "Exception in MetacatServlet.getPublicIdForDoc: "
2669
                            + e.getMessage(), 30);
2670
            throw e;
2671
        } finally {
2672
            try {
2673
                rs.close();
2674
                pstmt.close();
2675 1716 berkley
2676 2098 jones
            } finally {
2677
                DBConnectionPool.returnDBConnection(conn, serialNumber);
2678
            }
2679
        }
2680
        return fieldValue;
2681 1369 tao
    }
2682 1716 berkley
2683 2098 jones
    /*
2684 2312 jones
     * Get the list of documents from the database and return the list in an
2685
     * Vector of identifiers.
2686
     *
2687
     * @ returns the array of identifiers
2688
     */
2689
    private Vector getDocumentList() throws SQLException
2690
    {
2691
        Vector docList = new Vector();
2692
        PreparedStatement pstmt = null;
2693
        ResultSet rs = null;
2694
        DBConnection conn = null;
2695
        int serialNumber = -1;
2696
2697
        try {
2698
            //check out DBConnection
2699
            conn = DBConnectionPool
2700
                    .getDBConnection("MetaCatServlet.getDocumentList");
2701
            serialNumber = conn.getCheckOutSerialNumber();
2702
            pstmt = conn.prepareStatement("SELECT docid, rev"
2703
                    + " FROM xml_documents ");
2704
            pstmt.execute();
2705
            rs = pstmt.getResultSet();
2706
            while (rs.next()) {
2707
                String docid = rs.getString(1);
2708
                String rev = rs.getString(2);
2709
                docList.add(docid + "." + rev);
2710
            }
2711
        } catch (SQLException e) {
2712
            MetaCatUtil.debugMessage(
2713
                    "Exception in MetacatServlet.getDocumentList: "
2714
                            + e.getMessage(), 30);
2715
            throw e;
2716
        } finally {
2717
            try {
2718
                rs.close();
2719
                pstmt.close();
2720
2721
            } catch (SQLException se) {
2722
                MetaCatUtil.debugMessage(
2723
                    "Exception in MetacatServlet.getDocumentList: "
2724
                            + se.getMessage(), 30);
2725
                throw se;
2726
            } finally {
2727
                DBConnectionPool.returnDBConnection(conn, serialNumber);
2728
            }
2729
        }
2730
        return docList;
2731
    }
2732
2733
    /*
2734 2098 jones
     * A method to output setAccess action result
2735
     */
2736
    private void outputResponse(Vector successList, Vector errorList,
2737
            PrintWriter out)
2738 1369 tao
    {
2739 2098 jones
        boolean error = false;
2740
        boolean success = false;
2741
        // Output prolog
2742
        out.println(PROLOG);
2743
        // output success message
2744
        if (successList != null) {
2745
            for (int i = 0; i < successList.size(); i++) {
2746
                out.println(SUCCESS);
2747
                out.println((String) successList.elementAt(i));
2748
                out.println(SUCCESSCLOSE);
2749
                success = true;
2750
            }
2751
        }
2752
        // output error message
2753
        if (errorList != null) {
2754
            for (int i = 0; i < errorList.size(); i++) {
2755
                out.println(ERROR);
2756
                out.println((String) errorList.elementAt(i));
2757
                out.println(ERRORCLOSE);
2758
                error = true;
2759
            }
2760
        }
2761 1716 berkley
2762 2098 jones
        // if no error and no success info, send a error that nothing happened
2763
        if (!error && !success) {
2764
            out.println(ERROR);
2765
            out.println("Nothing happend for setaccess action");
2766
            out.println(ERRORCLOSE);
2767
        }
2768 1369 tao
    }
2769 46 jones
}