Project

General

Profile

1
/**
2
 *  '$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
 *    Authors: Matt Jones, Dan Higgins, Jivka Bojilova, Chad Berkley
7
 *    Release: @release@
8
 *
9
 *   '$Author: tao $'
10
 *     '$Date: 2004-03-15 14:08:39 -0800 (Mon, 15 Mar 2004) $'
11
 * '$Revision: 2045 $'
12
 *
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
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import com.oreilly.servlet.multipart.FilePart;
31
import com.oreilly.servlet.multipart.MultipartParser;
32
import com.oreilly.servlet.multipart.ParamPart;
33
import com.oreilly.servlet.multipart.Part;
34

    
35
import java.io.File;
36
import java.io.PrintWriter;
37
import java.io.IOException;
38
import java.io.StringReader;
39
import java.io.FileInputStream;
40
import java.io.BufferedInputStream;
41
import java.util.Enumeration;
42
import java.util.Hashtable;
43
import java.util.ResourceBundle;
44
import java.util.Random;
45
import java.util.PropertyResourceBundle;
46
import java.util.Vector;
47
import java.net.URL;
48
import java.net.MalformedURLException;
49
import java.sql.PreparedStatement;
50
import java.sql.ResultSet;
51
import java.sql.Connection;
52
import java.sql.SQLException;
53
import java.lang.reflect.*;
54
import java.net.*;
55
import java.util.zip.*;
56

    
57
import javax.servlet.ServletConfig;
58
import javax.servlet.ServletContext;
59
import javax.servlet.ServletException;
60
import javax.servlet.ServletInputStream;
61
import javax.servlet.http.HttpServlet;
62
import javax.servlet.http.HttpServletRequest;
63
import javax.servlet.http.HttpServletResponse;
64
import javax.servlet.http.HttpSession;
65
import javax.servlet.http.HttpUtils;
66
import javax.servlet.ServletOutputStream;
67

    
68
import org.ecoinformatics.eml.EMLParser;
69
import edu.ucsb.nceas.utilities.Options;
70

    
71
import org.xml.sax.SAXException;
72

    
73
/**
74
 * A metadata catalog server implemented as a Java Servlet
75
 *
76
 * <p>Valid parameters are:<br>
77
 * action=query -- query the values of all elements and attributes
78
 *                     and return a result set of nodes<br>
79
 * action=squery -- structured query (see pathquery.dtd)<br>
80
 * action= -- export a zip format for data packadge<br>
81
 * action=read -- read any metadata/data file from Metacat and from Internet<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')<br>
95
 * valtext -- XML text to be validated<br>
96
 * abstractpath -- XPath in metadata document to read from<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
 * <p>
104
 * The particular combination of parameters that are valid for each
105
 * particular action value is quite specific.  This documentation
106
 * will be reorganized to reflect this information.
107
 */
108
public class MetaCatServlet extends HttpServlet {
109

    
110
  private ServletConfig config = null;
111
  private ServletContext context = null;
112
  private String resultStyleURL = null;
113
  private String xmlcatalogfile = null;
114
  private String saxparser = null;
115
  private String datafilepath = null;
116
  private File dataDirectory = null;
117
  private String servletpath = null;
118
  private String htmlpath = null;
119
  private PropertyResourceBundle options = null;
120
  private MetaCatUtil util = null;
121
  private DBConnectionPool connPool = null;
122
  private Hashtable sessionHash = new Hashtable();
123
  private static final String PROLOG = "<?xml version=\"1.0\"?>";
124
  private static final String SUCCESS = "<success>";
125
  private static final String SUCCESSCLOSE = "</success>";
126
  private static final String ERROR = "<error>";
127
  private static final String ERRORCLOSE = "</error>";
128
  public static final String SCHEMALOCATIONKEYWORD = ":schemaLocation";
129
  public static final String NONAMESPACELOCATION = ":noNamespaceSchemaLocation";
130
  public static final String EML2KEYWORD =":eml";
131
  private static final String CONFIG_DIR  = "WEB-INF";
132
  private static final String CONFIG_NAME = "metacat.properties";
133

    
134
  /**
135
   * Initialize the servlet by creating appropriate database connections
136
   */
137
  public void init( ServletConfig config ) throws ServletException {
138
    try {
139
      super.init( config );
140
      this.config = config;
141
      this.context = config.getServletContext();
142
      System.out.println("MetaCatServlet Initialize");
143

    
144
      // Initialize the properties file for our options
145
      String dirPath = context.getRealPath(CONFIG_DIR);
146
      File propertyFile = new File(dirPath,CONFIG_NAME);
147
      Options options = null;
148
      try {
149
          options = Options.initialize(propertyFile);
150
          MetaCatUtil.debugMessage("Options configured: " + 
151
            options.getOption("configured"), 20);
152
      } catch (IOException ioe) {
153
        MetaCatUtil.debugMessage("Error in loading options: "
154
           +ioe.getMessage(), 20);
155
      }
156

    
157
      util = new MetaCatUtil();
158

    
159
      //initial DBConnection pool
160
      connPool = DBConnectionPool.getInstance();
161

    
162
      // Get the configuration file information
163
      resultStyleURL = util.getOption("resultStyleURL");
164
      xmlcatalogfile = util.getOption("xmlcatalogfile");
165
      saxparser = util.getOption("saxparser");
166
      datafilepath = util.getOption("datafilepath");
167
      dataDirectory = new File(datafilepath);
168
      servletpath = util.getOption("servletpath");
169
      htmlpath = util.getOption("htmlpath");
170

    
171

    
172
    } catch ( ServletException ex ) {
173
      throw ex;
174
    } catch (SQLException e) {
175
      MetaCatUtil.debugMessage("Error in MetacatServlet.init: "
176
                                          +e.getMessage(), 20);
177
    }
178
  }
179

    
180
  /**
181
   * Close all db connections from the pool
182
   */
183
  public void destroy() {
184
      // Close all db connection
185
      System.out.println("Destroying MetacatServlet");
186
      connPool.release();
187
  }
188

    
189
  /** Handle "GET" method requests from HTTP clients */
190
  public void doGet (HttpServletRequest request, HttpServletResponse response)
191
    throws ServletException, IOException {
192

    
193
    // Process the data and send back the response
194
    handleGetOrPost(request, response);
195
  }
196

    
197
  /** Handle "POST" method requests from HTTP clients */
198
  public void doPost( HttpServletRequest request, HttpServletResponse response)
199
    throws ServletException, IOException {
200

    
201
    // Process the data and send back the response
202
    handleGetOrPost(request, response);
203
  }
204

    
205
  /**
206
   * Control servlet response depending on the action parameter specified
207
   */
208
  private void handleGetOrPost(HttpServletRequest request,
209
                               HttpServletResponse response)
210
                               throws ServletException, IOException
211
  {
212

    
213
    if ( util == null ) {
214
        util = new MetaCatUtil();
215
    }
216
    /*MetaCatUtil.debugMessage("Connection pool size: "
217
                                     +connPool.getSizeOfDBConnectionPool(),10);
218
    MetaCatUtil.debugMessage("Free DBConnection number: "
219
                                  +connPool.getFreeDBConnectionNumber(), 10);*/
220
    //If all DBConnection in the pool are free and DBConnection pool
221
    //size is greater than initial value, shrink the connection pool
222
    //size to initial value
223
    DBConnectionPool.shrinkDBConnectionPoolSize();
224

    
225
    //Debug message to print out the method which have a busy DBConnection
226
    connPool.printMethodNameHavingBusyDBConnection();
227

    
228
    String ctype = request.getContentType();
229
    if (ctype != null && ctype.startsWith("multipart/form-data")) {
230
      handleMultipartForm(request, response);
231
    } else {
232

    
233

    
234
      String name = null;
235
      String[] value = null;
236
      String[] docid = new String[3];
237
      Hashtable params = new Hashtable();
238
      Enumeration paramlist = request.getParameterNames();
239

    
240

    
241
      while (paramlist.hasMoreElements()) {
242

    
243
        name = (String)paramlist.nextElement();
244
        value = request.getParameterValues(name);
245

    
246
        // Decode the docid and mouse click information
247
        if (name.endsWith(".y")) {
248
          docid[0] = name.substring(0,name.length()-2);
249
          params.put("docid", docid);
250
          name = "ypos";
251
        }
252
        if (name.endsWith(".x")) {
253
          name = "xpos";
254
        }
255

    
256
        params.put(name,value);
257
      }
258

    
259

    
260
      //handle param is emptpy
261
      if (params.isEmpty() || params == null)
262
      {
263
        return;
264
      }
265

    
266
      //if the user clicked on the input images, decode which image
267
      //was clicked then set the action.
268
      String action = ((String[])params.get("action"))[0];
269
      util.debugMessage("Line 230: Action is: " + action, 1);
270

    
271
      // This block handles session management for the servlet
272
      // by looking up the current session information for all actions
273
      // other than "login" and "logout"
274
      String username = null;
275
      String password = null;
276
      String[] groupnames = null;
277
      String sess_id = null;
278

    
279
      // handle login action
280
      if (action.equals("login")) {
281
        PrintWriter out = response.getWriter();
282
        handleLoginAction(out, params, request, response);
283
        out.close();
284

    
285
      // handle logout action
286
      } else if (action.equals("logout")) {
287
        PrintWriter out = response.getWriter();
288
        handleLogoutAction(out, params, request, response);
289
        out.close();
290

    
291
      // handle shrink DBConnection request
292
      } else if (action.equals("shrink")) {
293
        PrintWriter out = response.getWriter();
294
        boolean success = false;
295
        //If all DBConnection in the pool are free and DBConnection pool
296
        //size is greater than initial value, shrink the connection pool
297
        //size to initial value
298
        success = DBConnectionPool.shrinkConnectionPoolSize();
299
        if (success)
300
        {
301
          //if successfully shrink the pool size to initial value
302
          out.println("DBConnection Pool shrink successfully");
303
        }//if
304
        else
305
        {
306
          out.println("DBConnection pool couldn't shrink successfully");
307
        }
308
       //close out put
309
        out.close();
310

    
311
      // aware of session expiration on every request
312
      }
313
      else
314
      {
315
        HttpSession sess = request.getSession(true);
316
        if (sess.isNew() && !params.containsKey("sessionid")) {
317
          // session expired or has not been stored b/w user requests
318
          MetaCatUtil.debugMessage("in session is new or no sessionid", 40);
319
          username = "public";
320
          sess.setAttribute("username", username);
321
        }
322
        else
323
        {
324
          MetaCatUtil.debugMessage("in session is not new or " + 
325
                                    " has sessionid parameter", 40);
326
          try
327
          {
328
            if(params.containsKey("sessionid"))
329
            {
330
              sess_id = ((String[])params.get("sessionid"))[0];
331
              MetaCatUtil.debugMessage("in has sessionid " + sess_id, 40);
332
              if(sessionHash.containsKey(sess_id))
333
              {
334
                MetaCatUtil.debugMessage("find the id " + sess_id + 
335
                                         " in hash table", 40);
336
                sess = (HttpSession)sessionHash.get(sess_id);
337
              }
338
            }
339
            else
340
            {
341
              // we already store the session in login, so we don't need here
342
              /*MetaCatUtil.debugMessage("in no sessionid parameter ", 40);
343
              sess_id = (String)sess.getId();
344
              MetaCatUtil.debugMessage("storing the session id "+ sess_id +
345
                  " which has username " + sess.getAttribute("username") + 
346
                 " into session hash in handleGetOrPost method", 35);
347
              sessionHash.put(sess_id, sess);*/
348
            }
349
          }
350
          catch(IllegalStateException ise)
351
          {
352
            System.out.println("error in handleGetOrPost: this shouldn't " +
353
                               "happen: the session should be valid: " +
354
                               ise.getMessage());
355
          }
356

    
357
          username = (String)sess.getAttribute("username");
358
          MetaCatUtil.debugMessage("The user name from session is: "+
359
                                   username, 20);
360
          password = (String)sess.getAttribute("password");
361
          groupnames = (String[])sess.getAttribute("groupnames");
362
        }
363
      
364
        //make user user username should be public
365
        if (username == null || (username.trim().equals("")))
366
        {
367
          username = "public";
368
        }
369
        MetaCatUtil.debugMessage("The user is : "+ username, 5);
370
      }
371
       // Now that we know the session is valid, we can delegate the request
372
      // to a particular action handler
373
      if(action.equals("query")) {
374
        PrintWriter out = response.getWriter();
375
        handleQuery(out,params,response,username,groupnames,sess_id);
376
        out.close();
377
      } else if(action.equals("squery")) {
378
        PrintWriter out = response.getWriter();
379
        if(params.containsKey("query")) {
380
         handleSQuery(out, params,response,username,groupnames,sess_id);
381
         out.close();
382
        } else {
383
          out.println("Illegal action squery without \"query\" parameter");
384
          out.close();
385
        }
386
      } else if (action.equals("export")) {
387

    
388
        handleExportAction(params, response, username, groupnames, password);
389
      } else if (action.equals("read")) {
390
        handleReadAction(params, response, username,password, groupnames);
391
      } else if (action.equals("readinlinedata")) {
392
        handleReadInlineDataAction(params, response, username,
393
                                   password, groupnames);
394
      } else if (action.equals("insert") || action.equals("update")) {
395
        PrintWriter out = response.getWriter();
396
        if ( (username != null) &&  !username.equals("public") ) {
397
          handleInsertOrUpdateAction(out,params,username,groupnames);
398
        } else {
399
          out.println("Permission denied for user "+username +" " + action);
400
       }
401
        out.close();
402
      } else if (action.equals("delete")) {
403
        PrintWriter out = response.getWriter();
404
        if ( (username != null) &&  !username.equals("public") ) {
405
          handleDeleteAction(out, params, response, username, groupnames);
406
        } else {
407
          out.println("Permission denied for " + action);
408
        }
409
        out.close();
410
      } else if (action.equals("validate")) {
411
        PrintWriter out = response.getWriter();
412
        handleValidateAction(out, params);
413
        out.close();
414
      } else if (action.equals("setaccess")) {
415
         PrintWriter out = response.getWriter();
416
         handleSetAccessAction(out, params, username);
417
        out.close();
418
      } else if (action.equals("getaccesscontrol")) {
419
        PrintWriter out = response.getWriter();
420
        handleGetAccessControlAction(out,params,response,username,groupnames);
421
        out.close();
422
      } else if (action.equals("getprincipals")) {
423
        PrintWriter out = response.getWriter();
424
        handleGetPrincipalsAction(out, username, password);
425
        out.close();
426
      } else if (action.equals("getdoctypes")) {
427
        PrintWriter out = response.getWriter();
428
        handleGetDoctypesAction(out, params, response);
429
        out.close();
430
      } else if (action.equals("getdtdschema")) {
431
        PrintWriter out = response.getWriter();
432
        handleGetDTDSchemaAction(out, params, response);
433
        out.close();
434
      } else if (action.equals("getdataguide")) {
435
        PrintWriter out = response.getWriter();
436
        handleGetDataGuideAction(out, params, response);
437
        out.close();
438
      } else if (action.equals("getlastdocid")) {
439
        PrintWriter out = response.getWriter();
440
        handleGetMaxDocidAction(out, params, response);
441
        out.close();
442
      } else if (action.equals("getrevisionanddoctype")) {
443
        PrintWriter out = response.getWriter();
444
        handleGetRevisionAndDocTypeAction(out, params);
445
        out.close();
446
      } else if (action.equals("login") || action.equals("logout")) {
447
      } else if (action.equals("protocoltest")) {
448
        String testURL = "metacat://dev.nceas.ucsb.edu/NCEAS.897766.9";
449
        try {
450
          testURL = ((String[])params.get("url"))[0];
451
        } catch (Throwable t) {
452
        }
453
        String phandler = System.getProperty("java.protocol.handler.pkgs");
454
        response.setContentType("text/html");
455
        PrintWriter out = response.getWriter();
456
        out.println("<body bgcolor=\"white\">");
457
        out.println("<p>Handler property: <code>" + phandler + "</code></p>");
458
        out.println("<p>Starting test for:<br>");
459
        out.println("    " + testURL + "</p>");
460
        try {
461
          URL u = new URL(testURL);
462
          out.println("<pre>");
463
          out.println("Protocol: " + u.getProtocol());
464
          out.println("    Host: " + u.getHost());
465
          out.println("    Port: " + u.getPort());
466
          out.println("    Path: " + u.getPath());
467
          out.println("     Ref: " + u.getRef());
468
          String pquery = u.getQuery();
469
          out.println("   Query: " + pquery);
470
          out.println("  Params: ");
471
          if (pquery != null) {
472
            Hashtable qparams = util.parseQuery(u.getQuery());
473
            for (Enumeration en = qparams.keys(); en.hasMoreElements(); ) {
474
              String pname = (String)en.nextElement();
475
              String pvalue = (String)qparams.get(pname);
476
              out.println("    " + pname + ": " + pvalue);
477
            }
478
          }
479
          out.println("</pre>");
480
          out.println("</body>");
481
          out.close();
482
        } catch (MalformedURLException mue) {
483
          System.out.println("bad url from MetacatServlet.handleGetOrPost");
484
          out.println(mue.getMessage());
485
          mue.printStackTrace(out);
486
          out.close();
487
        }
488
      } else {
489
        PrintWriter out = response.getWriter();
490
        out.println("<?xml version=\"1.0\"?>");
491
        out.println("<error>");
492
        out.println("Error: action not registered.  Please report this error.");
493
        out.println("</error>");
494
        out.close();
495
      }
496

    
497
      //util.closeConnections();
498
      // Close the stream to the client
499
      //out.close();
500
    }
501
  }
502

    
503
  // LOGIN & LOGOUT SECTION
504
  /**
505
   * Handle the login request. Create a new session object.
506
   * Do user authentication through the session.
507
   */
508
  private void handleLoginAction(PrintWriter out, Hashtable params,
509
               HttpServletRequest request, HttpServletResponse response) {
510

    
511
    AuthSession sess = null;
512
    String un = ((String[])params.get("username"))[0];
513
    MetaCatUtil.debugMessage("user " + un + " try to login", 20);
514
    String pw = ((String[])params.get("password"))[0];
515
    String action = ((String[])params.get("action"))[0];
516
    String qformat = ((String[])params.get("qformat"))[0];
517

    
518
    try {
519
      sess = new AuthSession();
520
    } catch (Exception e) {
521
      System.out.println("error in MetacatServlet.handleLoginAction: " +
522
                          e.getMessage());
523
      out.println(e.getMessage());
524
      return;
525
    }
526
    boolean isValid = sess.authenticate(request, un, pw);
527
    
528
    //if it is authernticate is true, store the session
529
    if (isValid)
530
    {
531
      HttpSession session = sess.getSessions();
532
      String id = session.getId();
533
      MetaCatUtil.debugMessage("Store session id " + id + 
534
               "which has username" + session.getAttribute("username")+
535
               " into hash in login method", 35);
536
      sessionHash.put(id, session);
537
    }
538
    
539
    // format and transform the output
540
    if (qformat.equals("xml")) {
541
      response.setContentType("text/xml");
542
      out.println(sess.getMessage());
543
    } else {
544

    
545
      try {
546

    
547
        DBTransform trans = new DBTransform();
548
        response.setContentType("text/html");
549
        trans.transformXMLDocument(sess.getMessage(), "-//NCEAS//login//EN",
550
                                   "-//W3C//HTML//EN", qformat, out, null);
551

    
552
      } catch(Exception e) {
553

    
554
        MetaCatUtil.debugMessage("Error in MetaCatServlet.handleLoginAction: "
555
                                +e.getMessage(), 30);
556
      }
557

    
558
    // any output is returned
559
    }
560
  }
561

    
562
  /**
563
   * Handle the logout request. Close the connection.
564
   */
565
  private void handleLogoutAction(PrintWriter out, Hashtable params,
566
               HttpServletRequest request, HttpServletResponse response) {
567

    
568
    String qformat = ((String[])params.get("qformat"))[0];
569

    
570
    // close the connection
571
    HttpSession sess = request.getSession(false);
572
    MetaCatUtil.debugMessage("After get session in logout request", 40);
573
    if (sess != null) 
574
    {
575
     MetaCatUtil.debugMessage("The session id " + sess.getId() + 
576
                              " will be invalidate in logout action", 30);
577
     MetaCatUtil.debugMessage("The session contains user " + 
578
                               sess.getAttribute("username") +
579
                               " will be invalidate in logout action", 30);
580
      sess.invalidate();  
581
    }
582

    
583
    // produce output
584
    StringBuffer output = new StringBuffer();
585
    output.append("<?xml version=\"1.0\"?>");
586
    output.append("<logout>");
587
    output.append("User logged out");
588
    output.append("</logout>");
589

    
590
    //format and transform the output
591
    if (qformat.equals("xml")) {
592
      response.setContentType("text/xml");
593
      out.println(output.toString());
594
    } else {
595

    
596
      try {
597

    
598
        DBTransform trans = new DBTransform();
599
        response.setContentType("text/html");
600
        trans.transformXMLDocument(output.toString(), "-//NCEAS//login//EN",
601
                                   "-//W3C//HTML//EN", qformat, out, null);
602

    
603
      } catch(Exception e) {
604

    
605
        MetaCatUtil.debugMessage("Error in MetaCatServlet.handleLogoutAction"
606
                                  +e.getMessage(), 30);
607
      }
608
    }
609
  }
610
  // END OF LOGIN & LOGOUT SECTION
611

    
612
  // SQUERY & QUERY SECTION
613
  /**
614
   * Retreive the squery xml, execute it and display it
615
   *
616
   * @param out the output stream to the client
617
   * @param params the Hashtable of parameters that should be included
618
   * in the squery.
619
   * @param response the response object linked to the client
620
   * @param conn the database connection
621
   */
622
  protected void handleSQuery(PrintWriter out, Hashtable params,
623
                 HttpServletResponse response, String user, String[] groups,
624
                 String sessionid)
625
  {
626
    String xmlquery = ((String[])params.get("query"))[0];
627
    String qformat = ((String[])params.get("qformat"))[0];
628
    String resultdoc = null;
629
    MetaCatUtil.debugMessage("xmlquery: "+xmlquery, 30);
630
    double startTime = System.currentTimeMillis()/1000;
631
    Hashtable doclist = runQuery(xmlquery, user, groups);
632
    double docListTime = System.currentTimeMillis()/1000;
633
    MetaCatUtil.debugMessage("Time for getting doc list: "
634
                                            +(docListTime-startTime), 30);
635

    
636
    resultdoc = createResultDocument(doclist, transformQuery(xmlquery));
637
    double toStringTime = System.currentTimeMillis()/1000;
638
    MetaCatUtil.debugMessage("Time to create xml string: "
639
                              +(toStringTime-docListTime), 30);
640
    //format and transform the results
641
    double outPutTime = 0;
642
    if(qformat.equals("xml")) {
643
      response.setContentType("text/xml");
644
      out.println(resultdoc);
645
      outPutTime = System.currentTimeMillis()/1000;
646
      MetaCatUtil.debugMessage("Output time: "+(outPutTime-toStringTime), 30);
647
    } else {
648
      transformResultset(resultdoc, response, out, qformat, sessionid, params);
649
      outPutTime = System.currentTimeMillis()/1000;
650
      MetaCatUtil.debugMessage("Output time: "+(outPutTime-toStringTime), 30);
651
    }
652
  }
653

    
654
   /**
655
    * Create the xml query, execute it and display the results.
656
    *
657
    * @param out the output stream to the client
658
    * @param params the Hashtable of parameters that should be included
659
    * in the squery.
660
    * @param response the response object linked to the client
661
    */
662
  protected void handleQuery(PrintWriter out, Hashtable params,
663
                 HttpServletResponse response, String user, String[] groups,
664
                 String sessionid)
665
  {
666
    //create the query and run it
667
    String xmlquery = DBQuery.createSQuery(params);
668
    Hashtable doclist = runQuery(xmlquery, user, groups);
669
    String qformat = ((String[])params.get("qformat"))[0];
670
    String resultdoc = null;
671

    
672
    resultdoc = createResultDocument(doclist, transformQuery(params));
673

    
674
    //format and transform the results
675
    if(qformat.equals("xml")) {
676
      response.setContentType("text/xml");
677
      out.println(resultdoc);
678
    } else {
679
      transformResultset(resultdoc, response, out, qformat, sessionid, params);
680
    }
681
  }
682

    
683
  /**
684
   * Removes the <?xml version="x"?> tag from the beginning of xmlquery
685
   * so it can properly be placed in the <query> tag of the resultset.
686
   * This method is overwritable so that other applications can customize
687
   * the structure of what is in the <query> tag.
688
   *
689
   * @param xmlquery is the query to remove the <?xml version="x"?> tag from.
690
   */
691
  protected String transformQuery(Hashtable params)
692
  {
693
    //DBQuery.createSQuery is a re-calling of a previously called
694
    //function but it is necessary
695
    //so that overriding methods have access to the params hashtable
696
    String xmlquery = DBQuery.createSQuery(params);
697
    //the <?xml version="1.0"?> tag is the first 22 characters of the
698
    xmlquery = xmlquery.trim();
699
    int index = xmlquery.indexOf("?>");
700
    if ( index != -1 )
701
    {
702
      //have <?xml version="1.0"?>
703
      return xmlquery.substring(index + 2, xmlquery.length());
704
    }
705
    else
706
    {
707
      // don't have <?xml version="1.0"?>
708
      return xmlquery;
709
    }
710
  }
711

    
712
  /**
713
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
714
   * string as a param instead of a hashtable.
715
   *
716
   * @param xmlquery a string representing a query.
717
   */
718
  protected String transformQuery(String xmlquery)
719
  {
720
    xmlquery = xmlquery.trim();
721
    int index = xmlquery.indexOf("?>");
722
    if (index != -1)
723
    {
724
      return xmlquery.substring(index + 2, xmlquery.length());
725
    }
726
    else
727
    {
728
      return xmlquery;
729
    }
730
  }
731

    
732
  /**
733
   * Run the query and return a hashtable of results.
734
   *
735
   * @param xmlquery the query to run
736
   */
737
  private Hashtable runQuery(String xmlquery, String user, String[] groups)
738
  {
739
    Hashtable doclist=null;
740

    
741
    try
742
    {
743

    
744
      DBQuery queryobj = new DBQuery(saxparser);
745
      doclist = queryobj.findDocuments(new StringReader(xmlquery),user,groups);
746

    
747
      return doclist;
748
    }
749
    catch (Exception e)
750
    {
751

    
752
      MetaCatUtil.debugMessage("Error in MetacatServlet.runQuery: "
753
                                                      + e.getMessage(), 30);
754
      doclist = null;
755
      return doclist;
756
    }
757
  }
758

    
759
  /**
760
   * Transorms an xml resultset document to html and sends it to the browser
761
   *
762
   * @param resultdoc the string representation of the document that needs
763
   * to be transformed.
764
   * @param response the HttpServletResponse object bound to the client.
765
   * @param out the output stream to the client
766
   * @param qformat the name of the style-set to use for transformations
767
   */
768
  protected void transformResultset(String resultdoc,
769
                                    HttpServletResponse response,
770
                                    PrintWriter out, String qformat,
771
                                    String sessionid, Hashtable params)
772
  {
773

    
774
    try {
775

    
776
      DBTransform trans = new DBTransform();
777
      response.setContentType("text/html");
778
      trans.transformXMLDocument(resultdoc, "-//NCEAS//resultset//EN",
779
                                 "-//W3C//HTML//EN", qformat, out, params,
780
                                 sessionid);
781

    
782
    }
783
    catch(Exception e)
784
    {
785

    
786
      MetaCatUtil.debugMessage("Error in MetaCatServlet.transformResultset:"
787
                                +e.getMessage(), 30);
788
    }
789
  }
790

    
791
  /**
792
   * Transforms a hashtable of documents to an xml or html result.
793
   *
794
   * @param doclist- the hashtable to transform
795
   * @param xmlquery- the query that returned the doclist result
796
   */
797
  protected String createResultDocument(Hashtable doclist, String xmlquery)
798
  {
799
    // Create a buffer to hold the xml result
800
    StringBuffer resultset = new StringBuffer();
801

    
802
    // Print the resulting root nodes
803
    String docid = null;
804
    String document = null;
805
    resultset.append("<?xml version=\"1.0\"?>\n");
806
    resultset.append("<resultset>\n");
807

    
808
    resultset.append("  <query>" + xmlquery + "</query>");
809

    
810
    if(doclist != null)
811
    {
812
      Enumeration doclistkeys = doclist.keys();
813
      while (doclistkeys.hasMoreElements())
814
      {
815
        docid = (String)doclistkeys.nextElement();
816
        document = (String)doclist.get(docid);
817
        resultset.append("  <document>" + document + "</document>");
818
      }
819
    }
820

    
821
    resultset.append("</resultset>");
822
    return resultset.toString();
823
  }
824
  // END OF SQUERY & QUERY SECTION
825

    
826
 //Exoport section
827
 /**
828
   * Handle the "export" request of data package from Metacat in zip format
829
   * @param params the Hashtable of HTTP request parameters
830
   * @param response the HTTP response object linked to the client
831
   * @param user the username sent the request
832
   * @param groups the user's groupnames
833
   */
834
  private void handleExportAction(Hashtable params,
835
    HttpServletResponse response, String user, String[] groups, String passWord)
836
  {
837
    // Output stream
838
    ServletOutputStream out = null;
839
    // Zip output stream
840
    ZipOutputStream zOut = null;
841
    DocumentImpl docImpls=null;
842
    DBQuery queryObj=null;
843

    
844
    String[] docs = new String[10];
845
    String docId = "";
846

    
847
    try
848
    {
849
      // read the params
850
      if (params.containsKey("docid"))
851
      {
852
        docs = (String[])params.get("docid");
853
      }//if
854
      // Create a DBuery to handle export
855
      queryObj = new DBQuery(saxparser);
856
      // Get the docid
857
      docId=docs[0];
858
      // Make sure the client specify docid
859
      if (docId == null || docId.equals(""))
860
      {
861
        response.setContentType("text/xml"); //MIME type
862
        // Get a printwriter
863
        PrintWriter pw = response.getWriter();
864
        // Send back message
865
        pw.println("<?xml version=\"1.0\"?>");
866
        pw.println("<error>");
867
        pw.println("You didn't specify requested docid");
868
        pw.println("</error>");
869
        // Close printwriter
870
        pw.close();
871
        return;
872
      }//if
873
      // Get output stream
874
      out = response.getOutputStream();
875
      response.setContentType("application/zip"); //MIME type
876
      zOut = new ZipOutputStream(out);
877
      zOut =queryObj.getZippedPackage(docId, out, user, groups, passWord);
878
      zOut.finish(); //terminate the zip file
879
      zOut.close();  //close the zip stream
880

    
881
    }//try
882
    catch (Exception e)
883
    {
884
      try
885
      {
886
        response.setContentType("text/xml"); //MIME type
887
        // Send error message back
888
        if (out != null)
889
        {
890
            PrintWriter pw = new PrintWriter(out);
891
            pw.println("<?xml version=\"1.0\"?>");
892
            pw.println("<error>");
893
            pw.println(e.getMessage());
894
            pw.println("</error>");
895
            // Close printwriter
896
            pw.close();
897
            // Close output stream
898
            out.close();
899
        }//if
900
        // Close zip output stream
901
        if ( zOut != null )
902
        {
903
          zOut.close();
904
        }//if
905
      }//try
906
      catch (IOException ioe)
907
      {
908
        MetaCatUtil.debugMessage("Problem with the servlet output " +
909
                           "in MetacatServlet.handleExportAction: " +
910
                           ioe.getMessage(), 30);
911
      }//catch
912

    
913
      MetaCatUtil.debugMessage("Error in MetacatServlet.handleExportAction: " +
914
                         e.getMessage(), 30);
915
      e.printStackTrace(System.out);
916

    
917
    }//catch
918

    
919
  }//handleExportAction
920

    
921

    
922
   //read inline data section
923
 /**
924
   * In eml2 document, the xml can have inline data and data was stripped off
925
   * and store in file system. This action can be used to read inline data only
926
   * @param params the Hashtable of HTTP request parameters
927
   * @param response the HTTP response object linked to the client
928
   * @param user the username sent the request
929
   * @param groups the user's groupnames
930
   */
931
  private void handleReadInlineDataAction(Hashtable params,
932
                                          HttpServletResponse response,
933
                                          String user, String passWord,
934
                                          String[] groups)
935
  {
936
    String[] docs = new String[10];
937
    String inlineDataId = null;
938
    String docId = "";
939
    ServletOutputStream out = null;
940

    
941
    try
942
    {
943
      // read the params
944
      if (params.containsKey("inlinedataid"))
945
      {
946
        docs = (String[])params.get("inlinedataid");
947
      }//if
948
      // Get the docid
949
      inlineDataId=docs[0];
950
      // Make sure the client specify docid
951
      if (inlineDataId == null || inlineDataId.equals(""))
952
      {
953
        throw new Exception("You didn't specify requested inlinedataid");
954
      }//if
955

    
956
      // check for permission
957
      docId = MetaCatUtil.getDocIdWithoutRevFromInlineDataID(inlineDataId);
958
      PermissionController controller = new PermissionController(docId);
959
      // check top level read permission
960
      if (!controller.hasPermission(user, groups,
961
                                    AccessControlInterface.READSTRING))
962
      {
963
          throw new Exception("User "+ user + " doesn't have permission "+
964
                              " to read document " + docId);
965
      }//if
966
      // if the document has subtree control, we need to check subtree control
967
      else if(controller.hasSubTreeAccessControl())
968
      {
969
        // get node id for inlinedata
970
        long nodeId=getInlineDataNodeId(inlineDataId, docId);
971
        if (!controller.hasPermissionForSubTreeNode(user, groups,
972
                                     AccessControlInterface.READSTRING, nodeId))
973
        {
974
           throw new Exception("User "+ user + " doesn't have permission "+
975
                              " to read inlinedata " + inlineDataId);
976
        }//if
977

    
978
      }//else
979

    
980
      // Get output stream
981
      out = response.getOutputStream();
982
      // read the inline data from the file
983
      String inlinePath = MetaCatUtil.getOption("inlinedatafilepath");
984
      File lineData = new File(inlinePath, inlineDataId);
985
      FileInputStream input = new FileInputStream(lineData);
986
      byte [] buffer = new byte[4*1024];
987
      int bytes = input.read(buffer);
988
      while (bytes != -1)
989
      {
990
        out.write(buffer, 0, bytes);
991
        bytes = input.read(buffer);
992
      }
993
      out.close();
994

    
995
    }//try
996
    catch (Exception e)
997
    {
998
      try
999
      {
1000
        PrintWriter pw = null;
1001
        // Send error message back
1002
        if (out != null)
1003
        {
1004
            pw = new PrintWriter(out);
1005
        }//if
1006
        else
1007
        {
1008
          pw = response.getWriter();
1009
        }
1010
         pw.println("<?xml version=\"1.0\"?>");
1011
         pw.println("<error>");
1012
         pw.println(e.getMessage());
1013
         pw.println("</error>");
1014
         // Close printwriter
1015
         pw.close();
1016
         // Close output stream if out is not null
1017
         if (out != null)
1018
         {
1019
           out.close();
1020
         }
1021
     }//try
1022
     catch (IOException ioe)
1023
     {
1024
        MetaCatUtil.debugMessage("Problem with the servlet output " +
1025
                           "in MetacatServlet.handleExportAction: " +
1026
                           ioe.getMessage(), 30);
1027
     }//catch
1028

    
1029
      MetaCatUtil.debugMessage("Error in MetacatServlet.handleReadInlineDataAction: "
1030
                                + e.getMessage(), 30);
1031

    
1032
    }//catch
1033

    
1034
  }//handleReadInlineDataAction
1035

    
1036
  /*
1037
   * Get the nodeid from xml_nodes for the inlinedataid
1038
   */
1039
  private long getInlineDataNodeId(String inLineDataId, String docId)
1040
                                   throws SQLException
1041
  {
1042
    long nodeId = 0;
1043
    String INLINE = "inline";
1044
    boolean hasRow;
1045
    PreparedStatement pStmt = null;
1046
    DBConnection conn = null;
1047
    int serialNumber = -1;
1048
    String sql ="SELECT nodeid FROM xml_nodes WHERE docid=? AND nodedata=? " +
1049
                "AND nodetype='TEXT' AND parentnodeid IN " +
1050
                "(SELECT nodeid FROM xml_nodes WHERE docid=? AND " +
1051
                "nodetype='ELEMENT' AND nodename='" + INLINE + "')";
1052

    
1053
    try
1054
    {
1055
      //check out DBConnection
1056
      conn=DBConnectionPool.getDBConnection("AccessControlList.isAllowFirst");
1057
      serialNumber=conn.getCheckOutSerialNumber();
1058

    
1059
      pStmt = conn.prepareStatement(sql);
1060
      //bind value
1061
      pStmt.setString(1, docId);//docid
1062
      pStmt.setString(2, inLineDataId);//inlinedataid
1063
      pStmt.setString(3, docId);
1064
      // excute query
1065
      pStmt.execute();
1066
      ResultSet rs = pStmt.getResultSet();
1067
      hasRow=rs.next();
1068
      // get result
1069
      if (hasRow)
1070
      {
1071
        nodeId = rs.getLong(1);
1072
      }//if
1073

    
1074
    }//try
1075
    catch (SQLException e)
1076
    {
1077
      throw e;
1078
    }
1079
    finally
1080
    {
1081
      try
1082
      {
1083
        pStmt.close();
1084
      }
1085
      finally
1086
      {
1087
        DBConnectionPool.returnDBConnection(conn, serialNumber);
1088
      }
1089
    }
1090
    MetaCatUtil.debugMessage("The nodeid for inlinedataid " + inLineDataId +
1091
                             " is: "+nodeId, 35);
1092
    return nodeId;
1093
  }
1094

    
1095

    
1096

    
1097
  // READ SECTION
1098
  /**
1099
   * Handle the "read" request of metadata/data files from Metacat
1100
   * or any files from Internet;
1101
   * transformed metadata XML document into HTML presentation if requested;
1102
   * zip files when more than one were requested.
1103
   *
1104
   * @param params the Hashtable of HTTP request parameters
1105
   * @param response the HTTP response object linked to the client
1106
   * @param user the username sent the request
1107
   * @param groups the user's groupnames
1108
   */
1109
  private void handleReadAction(Hashtable params, HttpServletResponse response,
1110
                                String user, String passWord, String[] groups)
1111
  {
1112
    ServletOutputStream out = null;
1113
    ZipOutputStream zout = null;
1114
    PrintWriter pw = null;
1115
    boolean zip = false;
1116
    boolean withInlineData = true;
1117

    
1118
    try {
1119
      String[] docs = new String[0];
1120
      String docid = "";
1121
      String qformat = "";
1122
      String abstrpath = null;
1123

    
1124
      // read the params
1125
      if (params.containsKey("docid")) {
1126
        docs = (String[])params.get("docid");
1127
      }
1128
      if (params.containsKey("qformat")) {
1129
        qformat = ((String[])params.get("qformat"))[0];
1130
      }
1131
      // the param for only metadata (eml)
1132
      if (params.containsKey("inlinedata"))
1133
      {
1134

    
1135
        String inlineData = ((String[])params.get("inlinedata"))[0];
1136
        if (inlineData.equalsIgnoreCase("false"))
1137
        {
1138
          withInlineData = false;
1139
        }
1140
      }
1141
      if (params.containsKey("abstractpath")) {
1142
        abstrpath = ((String[])params.get("abstractpath"))[0];
1143
        if ( !abstrpath.equals("") && (abstrpath != null) ) {
1144
          viewAbstract(response, abstrpath, docs[0]);
1145
          return;
1146
        }
1147
      }
1148
      if ( (docs.length > 1) || qformat.equals("zip") ) {
1149
        zip = true;
1150
        out = response.getOutputStream();
1151
        response.setContentType("application/zip"); //MIME type
1152
        zout = new ZipOutputStream(out);
1153
      }
1154
      // go through the list of docs to read
1155
      for (int i=0; i < docs.length; i++ ) {
1156
        try {
1157

    
1158
          URL murl = new URL(docs[i]);
1159
          Hashtable murlQueryStr = util.parseQuery(murl.getQuery());
1160
          // case docid="http://.../?docid=aaa"
1161
          // or docid="metacat://.../?docid=bbb"
1162
          if (murlQueryStr.containsKey("docid")) {
1163
            // get only docid, eliminate the rest
1164
            docid = (String)murlQueryStr.get("docid");
1165
            if ( zip ) {
1166
              addDocToZip(docid, zout, user, groups);
1167
            } else {
1168
              readFromMetacat(response, docid, qformat, abstrpath,
1169
                              user, groups, zip, zout, withInlineData, params);
1170
            }
1171

    
1172
          // case docid="http://.../filename"
1173
          } else {
1174
            docid = docs[i];
1175
            if ( zip ) {
1176
              addDocToZip(docid, zout, user, groups);
1177
            } else {
1178
              readFromURLConnection(response, docid);
1179
            }
1180
          }
1181

    
1182
        // case docid="ccc"
1183
        } catch (MalformedURLException mue) {
1184
          docid = docs[i];
1185
          if ( zip ) {
1186
            addDocToZip(docid, zout, user, groups);
1187
          } else {
1188
            readFromMetacat(response, docid, qformat, abstrpath,
1189
                            user, groups, zip, zout, withInlineData, params);
1190
          }
1191
        }
1192

    
1193
      } /* end for */
1194

    
1195
      if ( zip ) {
1196
        zout.finish(); //terminate the zip file
1197
        zout.close();  //close the zip stream
1198
      }
1199

    
1200

    
1201
    }
1202
    // To handle doc not found exception
1203
    catch (McdbDocNotFoundException notFoundE)
1204
    {
1205
      // the docid which didn't be found
1206
      String notFoundDocId = notFoundE.getUnfoundDocId();
1207
      String notFoundRevision = notFoundE.getUnfoundRevision();
1208
      MetaCatUtil.debugMessage("Missed id: "+ notFoundDocId, 30);
1209
      MetaCatUtil.debugMessage("Missed rev: "+ notFoundRevision, 30);
1210
      try
1211
      {
1212
        // read docid from remote server
1213
        readFromRemoteMetaCat(response, notFoundDocId, notFoundRevision,
1214
                                              user, passWord, out, zip, zout);
1215
        // Close zout outputstream
1216
        if ( zout != null)
1217
        {
1218
          zout.close();
1219
        }
1220
        // close output stream
1221
        if (out != null)
1222
        {
1223
          out.close();
1224
        }
1225

    
1226
      }//try
1227
      catch ( Exception exc)
1228
      {
1229
        MetaCatUtil.debugMessage("Erorr in MetacatServlet.hanldReadAction: "+
1230
                                      exc.getMessage(), 30);
1231
        try
1232
        {
1233
          if (out != null)
1234
          {
1235
            response.setContentType("text/xml");
1236
            // Send back error message by printWriter
1237
            pw = new PrintWriter(out);
1238
            pw.println("<?xml version=\"1.0\"?>");
1239
            pw.println("<error>");
1240
            pw.println(notFoundE.getMessage());
1241
            pw.println("</error>");
1242
            pw.close();
1243
            out.close();
1244

    
1245
          }
1246
          else
1247
          {
1248
           response.setContentType("text/xml"); //MIME type
1249
           // Send back error message if out = null
1250
           if (pw == null)
1251
           {
1252
             // If pw is null, open the respnose
1253
            pw = response.getWriter();
1254
           }
1255
           pw.println("<?xml version=\"1.0\"?>");
1256
           pw.println("<error>");
1257
           pw.println(notFoundE.getMessage());
1258
           pw.println("</error>");
1259
           pw.close();
1260
        }
1261
        // close zout
1262
        if ( zout != null )
1263
        {
1264
          zout.close();
1265
        }
1266
        }//try
1267
        catch (IOException ie)
1268
        {
1269
          MetaCatUtil.debugMessage("Problem with the servlet output " +
1270
                           "in MetacatServlet.handleReadAction: " +
1271
                           ie.getMessage(), 30);
1272
        }//cathch
1273
      }//catch
1274
    }// catch McdbDocNotFoundException
1275
    catch (Exception e)
1276
    {
1277
      try {
1278

    
1279
        if (out != null) {
1280
            response.setContentType("text/xml"); //MIME type
1281
            pw = new PrintWriter(out);
1282
            pw.println("<?xml version=\"1.0\"?>");
1283
            pw.println("<error>");
1284
            pw.println(e.getMessage());
1285
            pw.println("</error>");
1286
            pw.close();
1287
            out.close();
1288
        }
1289
        else
1290
        {
1291
           response.setContentType("text/xml"); //MIME type
1292
           // Send back error message if out = null
1293
           if ( pw == null)
1294
           {
1295
            pw = response.getWriter();
1296
           }
1297
           pw.println("<?xml version=\"1.0\"?>");
1298
           pw.println("<error>");
1299
           pw.println(e.getMessage());
1300
           pw.println("</error>");
1301
           pw.close();
1302

    
1303
        }
1304
        // Close zip output stream
1305
        if ( zout != null ) { zout.close(); }
1306

    
1307
      } catch (IOException ioe) {
1308
        MetaCatUtil.debugMessage("Problem with the servlet output " +
1309
                           "in MetacatServlet.handleReadAction: " +
1310
                           ioe.getMessage(), 30);
1311
        ioe.printStackTrace(System.out);
1312

    
1313
      }
1314

    
1315
      MetaCatUtil.debugMessage("Error in MetacatServlet.handleReadAction: " +
1316
                               e.getMessage(), 30);
1317
      //e.printStackTrace(System.out);
1318
    }
1319

    
1320
  }
1321

    
1322
  // read metadata or data from Metacat
1323
  private void readFromMetacat(HttpServletResponse response, String docid,
1324
                               String qformat, String abstrpath, String user,
1325
                               String[] groups, boolean zip,
1326
                               ZipOutputStream zout, boolean withInlineData,
1327
                               Hashtable params)
1328
               throws ClassNotFoundException, IOException, SQLException,
1329
                      McdbException, Exception
1330
  {
1331

    
1332
    try {
1333

    
1334

    
1335
      DocumentImpl doc = new DocumentImpl(docid);
1336

    
1337
      //check the permission for read
1338
      if (!doc.hasReadPermission(user, groups, docid))
1339
      {
1340
        Exception e = new Exception("User " + user + " does not have permission"
1341
                       +" to read the document with the docid " + docid);
1342

    
1343
        throw e;
1344
      }
1345

    
1346
      if ( doc.getRootNodeID() == 0 ) {
1347
        // this is data file
1348
        String filepath = util.getOption("datafilepath");
1349
        if(!filepath.endsWith("/")) {
1350
          filepath += "/";
1351
        }
1352
        String filename = filepath + docid;
1353
        FileInputStream fin = null;
1354
        fin = new FileInputStream(filename);
1355

    
1356
        //MIME type
1357
        String contentType = getServletContext().getMimeType(filename);
1358
        if (contentType == null)
1359
        {
1360
          ContentTypeProvider provider = new ContentTypeProvider(docid);
1361
          contentType = provider.getContentType();
1362
          MetaCatUtil.debugMessage("Final contenttype is: "+ contentType, 30);
1363
        }
1364

    
1365
        response.setContentType(contentType);
1366
        // if we decide to use "application/octet-stream" for all data returns
1367
        // response.setContentType("application/octet-stream");
1368

    
1369
        try {
1370

    
1371
          ServletOutputStream out = response.getOutputStream();
1372
          byte[] buf = new byte[4 * 1024]; // 4K buffer
1373
          int b = fin.read(buf);
1374
          while (b != -1) {
1375
            out.write(buf, 0, b);
1376
            b = fin.read(buf);
1377
          }
1378
        } finally {
1379
          if (fin != null) fin.close();
1380
        }
1381

    
1382
      } else {
1383
        // this is metadata doc
1384
        if ( qformat.equals("xml") ) {
1385

    
1386
          // set content type first
1387
          response.setContentType("text/xml");   //MIME type
1388
          PrintWriter out = response.getWriter();
1389
          doc.toXml(out, user, groups, withInlineData);
1390
        } else {
1391
          response.setContentType("text/html");  //MIME type
1392
          PrintWriter out = response.getWriter();
1393

    
1394
          // Look up the document type
1395
          String doctype = doc.getDoctype();
1396
          // Transform the document to the new doctype
1397
          DBTransform dbt = new DBTransform();
1398
          dbt.transformXMLDocument(doc.toString(user, groups, withInlineData),
1399
                                   doctype,"-//W3C//HTML//EN",
1400
                                   qformat, out, params);
1401
        }
1402

    
1403
      }
1404
    }
1405
    catch (Exception except)
1406
    {
1407
      throw except;
1408

    
1409
    }
1410

    
1411
  }
1412

    
1413
  // read data from URLConnection
1414
  private void readFromURLConnection(HttpServletResponse response, String docid)
1415
               throws IOException, MalformedURLException
1416
  {
1417
    ServletOutputStream out = response.getOutputStream();
1418
    String contentType = getServletContext().getMimeType(docid); //MIME type
1419
    if (contentType == null) {
1420
      if (docid.endsWith(".xml")) {
1421
        contentType="text/xml";
1422
      } else if (docid.endsWith(".css")) {
1423
        contentType="text/css";
1424
      } else if (docid.endsWith(".dtd")) {
1425
        contentType="text/plain";
1426
      } else if (docid.endsWith(".xsd")) {
1427
        contentType="text/xml";
1428
      } else if (docid.endsWith("/")) {
1429
        contentType="text/html";
1430
      } else {
1431
        File f = new File(docid);
1432
        if ( f.isDirectory() ) {
1433
          contentType="text/html";
1434
        } else {
1435
          contentType="application/octet-stream";
1436
        }
1437
      }
1438
    }
1439
    response.setContentType(contentType);
1440
    // if we decide to use "application/octet-stream" for all data returns
1441
    // response.setContentType("application/octet-stream");
1442

    
1443
    // this is http url
1444
    URL url = new URL(docid);
1445
    BufferedInputStream bis = null;
1446
    try {
1447
      bis = new BufferedInputStream(url.openStream());
1448
      byte[] buf = new byte[4 * 1024]; // 4K buffer
1449
      int b = bis.read(buf);
1450
      while (b != -1) {
1451
        out.write(buf, 0, b);
1452
        b = bis.read(buf);
1453
      }
1454
    } finally {
1455
      if (bis != null) bis.close();
1456
    }
1457

    
1458
  }
1459

    
1460
  // read file/doc and write to ZipOutputStream
1461
  private void addDocToZip(String docid, ZipOutputStream zout,
1462
                              String user, String[] groups)
1463
               throws ClassNotFoundException, IOException, SQLException,
1464
                      McdbException, Exception
1465
  {
1466
    byte[] bytestring = null;
1467
    ZipEntry zentry = null;
1468

    
1469
    try {
1470
      URL url = new URL(docid);
1471

    
1472
      // this http url; read from URLConnection; add to zip
1473
      zentry = new ZipEntry(docid);
1474
      zout.putNextEntry(zentry);
1475
      BufferedInputStream bis = null;
1476
      try {
1477
        bis = new BufferedInputStream(url.openStream());
1478
        byte[] buf = new byte[4 * 1024]; // 4K buffer
1479
        int b = bis.read(buf);
1480
        while(b != -1) {
1481
          zout.write(buf, 0, b);
1482
          b = bis.read(buf);
1483
        }
1484
      } finally {
1485
        if (bis != null) bis.close();
1486
      }
1487
      zout.closeEntry();
1488

    
1489
    } catch (MalformedURLException mue) {
1490

    
1491
      // this is metacat doc (data file or metadata doc)
1492

    
1493
      try {
1494

    
1495
        DocumentImpl doc = new DocumentImpl(docid);
1496

    
1497
        //check the permission for read
1498
        if (!doc.hasReadPermission(user, groups, docid))
1499
        {
1500
          Exception e = new Exception("User " + user + " does not have "
1501
                    +"permission to read the document with the docid " + docid);
1502

    
1503
          throw e;
1504
        }
1505

    
1506
        if ( doc.getRootNodeID() == 0 ) {
1507
          // this is data file; add file to zip
1508
          String filepath = util.getOption("datafilepath");
1509
          if(!filepath.endsWith("/")) {
1510
            filepath += "/";
1511
          }
1512
          String filename = filepath + docid;
1513
          FileInputStream fin = null;
1514
          fin = new FileInputStream(filename);
1515
          try {
1516

    
1517
            zentry = new ZipEntry(docid);
1518
            zout.putNextEntry(zentry);
1519
            byte[] buf = new byte[4 * 1024]; // 4K buffer
1520
            int b = fin.read(buf);
1521
            while (b != -1) {
1522
              zout.write(buf, 0, b);
1523
              b = fin.read(buf);
1524
            }
1525
          } finally {
1526
            if (fin != null) fin.close();
1527
          }
1528
          zout.closeEntry();
1529

    
1530
        } else {
1531
          // this is metadata doc; add doc to zip
1532
          bytestring = doc.toString().getBytes();
1533
          zentry = new ZipEntry(docid + ".xml");
1534
          zentry.setSize(bytestring.length);
1535
          zout.putNextEntry(zentry);
1536
          zout.write(bytestring, 0, bytestring.length);
1537
          zout.closeEntry();
1538
        }
1539
      } catch (Exception except) {
1540
        throw except;
1541

    
1542
      }
1543

    
1544
    }
1545

    
1546
  }
1547

    
1548
  // view abstract within document
1549
  private void viewAbstract(HttpServletResponse response,
1550
                            String abstractpath, String docid)
1551
               throws ClassNotFoundException, IOException, SQLException,
1552
                      McdbException, Exception
1553
  {
1554

    
1555
    PrintWriter out =null;
1556
    try {
1557

    
1558
      response.setContentType("text/html");  //MIME type
1559
      out = response.getWriter();
1560
      Object[] abstracts = DBQuery.getNodeContent(abstractpath, docid);
1561
      out.println("<html><head><title>Abstract</title></head>");
1562
      out.println("<body bgcolor=\"white\"><h1>Abstract</h1>");
1563
      for (int i=0; i<abstracts.length; i++) {
1564
        out.println("<p>" + (String)abstracts[i] + "</p>");
1565
      }
1566
      out.println("</body></html>");
1567

    
1568
    } catch (Exception e) {
1569
       out.println("<?xml version=\"1.0\"?>");
1570
       out.println("<error>");
1571
       out.println(e.getMessage());
1572
       out.println("</error>");
1573

    
1574

    
1575
    }
1576
  }
1577
  /**
1578
   * If metacat couldn't find a data file or document locally, it will read this
1579
   * docid from its home server. This is for the replication feature
1580
   */
1581
  private void readFromRemoteMetaCat(HttpServletResponse response, String docid,
1582
                     String rev, String user, String password,
1583
                     ServletOutputStream out, boolean zip, ZipOutputStream zout)
1584
                        throws Exception
1585
 {
1586
   // Create a object of RemoteDocument, "" is for zipEntryPath
1587
   RemoteDocument remoteDoc =
1588
                        new RemoteDocument (docid, rev,user, password, "");
1589
   String docType = remoteDoc.getDocType();
1590
   // Only read data file
1591
   if (docType.equals("BIN"))
1592
   {
1593
    // If it is zip format
1594
    if (zip)
1595
    {
1596
      remoteDoc.readDocumentFromRemoteServerByZip(zout);
1597
    }//if
1598
    else
1599
    {
1600
      if (out == null)
1601
      {
1602
        out = response.getOutputStream();
1603
      }//if
1604
      response.setContentType("application/octet-stream");
1605
      remoteDoc.readDocumentFromRemoteServer(out);
1606
    }//else (not zip)
1607
   }//if doctype=bin
1608
   else
1609
   {
1610
     throw new Exception("Docid: "+docid+"."+rev+" couldn't find");
1611
   }//else
1612
 }//readFromRemoteMetaCat
1613

    
1614
  // END OF READ SECTION
1615

    
1616

    
1617

    
1618
  // INSERT/UPDATE SECTION
1619
  /**
1620
   * Handle the database putdocument request and write an XML document
1621
   * to the database connection
1622
   */
1623
  private void handleInsertOrUpdateAction(PrintWriter out, Hashtable params,
1624
               String user, String[] groups) {
1625

    
1626
    DBConnection dbConn = null;
1627
    int serialNumber = -1;
1628

    
1629
    try {
1630
      // Get the document indicated
1631
      String[] doctext = (String[])params.get("doctext");
1632

    
1633
      String pub = null;
1634
      if (params.containsKey("public")) {
1635
        pub = ((String[])params.get("public"))[0];
1636
      }
1637

    
1638
      StringReader dtd = null;
1639
      if (params.containsKey("dtdtext")) {
1640
        String[] dtdtext = (String[])params.get("dtdtext");
1641
        try {
1642
          if ( !dtdtext[0].equals("") ) {
1643
            dtd = new StringReader(dtdtext[0]);
1644
          }
1645
        } catch (NullPointerException npe) {}
1646
      }
1647

    
1648
      StringReader xml = new StringReader(doctext[0]);
1649
      boolean validate = false;
1650
      DocumentImplWrapper documentWrapper = null;
1651
      try {
1652
        // look inside XML Document for <!DOCTYPE ... PUBLIC/SYSTEM ... >
1653
        // in order to decide whether to use validation parser
1654
        validate = needDTDValidation(xml);
1655
        if (validate)
1656
        {
1657
          // set a dtd base validation parser
1658
          String rule = DocumentImpl.DTD;
1659
          documentWrapper = new DocumentImplWrapper(rule, validate);
1660
        }
1661
        else if (needSchemaValidation(xml))
1662
        {
1663
          // for eml2
1664
          if (needEml2Validation(xml))
1665
          {
1666
             // set eml2 base validation parser
1667
            String rule = DocumentImpl.EML2;
1668
            // using emlparser to check id validation
1669
            EMLParser parser = new EMLParser(doctext[0]);
1670
            documentWrapper = new DocumentImplWrapper(rule, true);
1671
          }
1672
          else
1673
          {
1674
            // set schema base validation parser
1675
            String rule = DocumentImpl.SCHEMA;
1676
            documentWrapper = new DocumentImplWrapper(rule, true);
1677
          }
1678
        }
1679
        else
1680
        {
1681
          documentWrapper = new DocumentImplWrapper("", false);
1682
        }
1683

    
1684
        String[] action = (String[])params.get("action");
1685
        String[] docid = (String[])params.get("docid");
1686
        String newdocid = null;
1687

    
1688
        String doAction = null;
1689
        if (action[0].equals("insert")) {
1690
          doAction = "INSERT";
1691
        } else if (action[0].equals("update")) {
1692
          doAction = "UPDATE";
1693
        }
1694

    
1695
        try
1696
        {
1697
          // get a connection from the pool
1698
          dbConn=DBConnectionPool.
1699
                  getDBConnection("MetaCatServlet.handleInsertOrUpdateAction");
1700
          serialNumber=dbConn.getCheckOutSerialNumber();
1701

    
1702
           // write the document to the database
1703
          try
1704
          {
1705
            String accNumber = docid[0];
1706
            MetaCatUtil.debugMessage(""+ doAction + " " + accNumber +"...", 10);
1707
            if (accNumber.equals(""))
1708
            {
1709
              accNumber = null;
1710
            }//if
1711
            newdocid = documentWrapper.write(dbConn, xml, pub, dtd, doAction,
1712
                                          accNumber, user, groups);
1713

    
1714
          }//try
1715
          catch (NullPointerException npe)
1716
          {
1717
            newdocid = documentWrapper.write(dbConn, xml, pub, dtd, doAction,
1718
                                          null, user, groups);
1719
          }//catch
1720

    
1721
        }//try
1722
        finally
1723
        {
1724
          // Return db connection
1725
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1726
        }
1727

    
1728
        // set content type and other response header fields first
1729
        //response.setContentType("text/xml");
1730
        out.println("<?xml version=\"1.0\"?>");
1731
        out.println("<success>");
1732
        out.println("<docid>" + newdocid + "</docid>");
1733
        out.println("</success>");
1734

    
1735
      }
1736
      catch (NullPointerException npe)
1737
      {
1738
        //response.setContentType("text/xml");
1739
        out.println("<?xml version=\"1.0\"?>");
1740
        out.println("<error>");
1741
        out.println(npe.getMessage());
1742
        out.println("</error>");
1743
      }
1744
    }
1745
    catch (Exception e)
1746
    {
1747
      //response.setContentType("text/xml");
1748
      out.println("<?xml version=\"1.0\"?>");
1749
      out.println("<error>");
1750
      out.println(e.getMessage());
1751
      out.println("</error>");
1752
    }
1753
  }
1754

    
1755
  /**
1756
   * Parse XML Document to look for <!DOCTYPE ... PUBLIC/SYSTEM ... >
1757
   * in order to decide whether to use validation parser
1758
   */
1759
  private static boolean needDTDValidation(StringReader xmlreader) throws 
1760
                                                             IOException 
1761
  {
1762

    
1763
    
1764
    StringBuffer cbuff = new StringBuffer();
1765
    java.util.Stack st = new java.util.Stack();
1766
    boolean validate = false;
1767
    int c;
1768
    int inx;
1769

    
1770
    // read from the stream until find the keywords
1771
    while ( (st.empty() || st.size()<4) && ((c = xmlreader.read()) != -1) ) {
1772
      cbuff.append((char)c);
1773

    
1774
      // "<!DOCTYPE" keyword is found; put it in the stack
1775
      if ( (inx = cbuff.toString().indexOf("<!DOCTYPE")) != -1 ) {
1776
        cbuff = new StringBuffer();
1777
        st.push("<!DOCTYPE");
1778
      }
1779
      // "PUBLIC" keyword is found; put it in the stack
1780
      if ( (inx = cbuff.toString().indexOf("PUBLIC")) != -1 ) {
1781
        cbuff = new StringBuffer();
1782
        st.push("PUBLIC");
1783
      }
1784
      // "SYSTEM" keyword is found; put it in the stack
1785
      if ( (inx = cbuff.toString().indexOf("SYSTEM")) != -1 ) {
1786
        cbuff = new StringBuffer();
1787
        st.push("SYSTEM");
1788
      }
1789
      // ">" character is found; put it in the stack
1790
      // ">" is found twice: fisrt from <?xml ...?>
1791
      // and second from <!DOCTYPE ... >
1792
      if ( (inx = cbuff.toString().indexOf(">")) != -1 ) {
1793
        cbuff = new StringBuffer();
1794
        st.push(">");
1795
      }
1796
    }
1797

    
1798
    // close the stream
1799
    xmlreader.reset();
1800

    
1801
    // check the stack whether it contains the keywords:
1802
    // "<!DOCTYPE", "PUBLIC" or "SYSTEM", and ">" in this order
1803
    if ( st.size() == 4 ) {
1804
      if ( ((String)st.pop()).equals(">") &&
1805
           ( ((String)st.peek()).equals("PUBLIC") |
1806
             ((String)st.pop()).equals("SYSTEM") ) &&
1807
           ((String)st.pop()).equals("<!DOCTYPE") )  {
1808
        validate = true;
1809
      }
1810
    }
1811

    
1812
    MetaCatUtil.debugMessage("Validation for dtd is " + validate, 10);
1813
    return validate;
1814
  }
1815
  // END OF INSERT/UPDATE SECTION
1816

    
1817
  /* check if the xml string contains key words to specify schema loocation*/
1818
  private boolean needSchemaValidation(StringReader xml) throws IOException
1819
  {
1820
    boolean needSchemaValidate =false;
1821
    if (xml == null)
1822
    {
1823
      MetaCatUtil.debugMessage("Validation for schema is " +
1824
                               needSchemaValidate, 10);
1825
      return needSchemaValidate;
1826
    }
1827
    System.out.println("before get target line");
1828
    String targetLine = getSchemaLine(xml);
1829
    System.out.println("before get target line");
1830
    // to see if the second line contain some keywords
1831
    if (targetLine != null && (targetLine.indexOf(SCHEMALOCATIONKEYWORD) != -1||
1832
             targetLine.indexOf(NONAMESPACELOCATION) != -1 ))
1833
    {
1834
      // if contains schema location key word, should be validate
1835
      needSchemaValidate = true;
1836
    }
1837

    
1838
    MetaCatUtil.debugMessage("Validation for schema is " +
1839
                             needSchemaValidate, 10);
1840
    return needSchemaValidate;
1841

    
1842
  }
1843

    
1844
   /* check if the xml string contains key words to specify schema loocation*/
1845
  private boolean needEml2Validation(StringReader xml) throws IOException
1846
  {
1847
    boolean needEml2Validate =false;
1848
    String emlNameSpace =DocumentImpl.EMLNAMESPACE;
1849
    String schemaLocationContent = null;
1850
    if (xml == null)
1851
    {
1852
      MetaCatUtil.debugMessage("Validation for schema is " +
1853
                               needEml2Validate, 10);
1854
      return needEml2Validate;
1855
    }
1856
    String targetLine = getSchemaLine(xml);
1857

    
1858
    if (targetLine != null)
1859
    {
1860
      
1861
      int startIndex = targetLine.indexOf(SCHEMALOCATIONKEYWORD);
1862
      int start = 1;
1863
      int end   = 1;
1864
      String schemaLocation = null;
1865
      int count = 0;
1866
      if (startIndex != -1)
1867
      {
1868
        for ( int i=startIndex; i<targetLine.length(); i++)
1869
        {
1870
          if (targetLine.charAt(i) =='"')
1871
          {
1872
            count ++;
1873
          }
1874
          if (targetLine.charAt(i) =='"' && count == 1)
1875
          {
1876
            start = i;
1877
          }
1878
          if (targetLine.charAt(i) =='"' && count == 2)
1879
          {
1880
            end = i;
1881
            break;
1882
          }
1883
        }
1884
      }
1885
      schemaLocation = targetLine.substring(start+1, end);
1886
      MetaCatUtil.debugMessage("schemaLocation in xml is: "+schemaLocation, 30);
1887
      if ( schemaLocation.indexOf(emlNameSpace) != -1)
1888
      {
1889
        needEml2Validate = true;
1890
      }
1891
    }
1892

    
1893
    MetaCatUtil.debugMessage("Validation for eml is " +
1894
                             needEml2Validate, 10);
1895
    return needEml2Validate;
1896

    
1897
  }
1898

    
1899
  private String getSchemaLine(StringReader xml) throws IOException
1900
  {
1901
    // find the line
1902
    String secondLine = null;
1903
    int count =0;
1904
    int endIndex = 0;
1905
    int startIndex = 0;
1906
    final int TARGETNUM = 2;
1907
    StringBuffer buffer = new StringBuffer();
1908
    boolean comment =false;
1909
    char thirdPreviousCharacter = '?';
1910
    char secondPreviousCharacter ='?';
1911
    char previousCharacter = '?';
1912
    char currentCharacter = '?';
1913
    
1914
    while ( (currentCharacter = (char) xml.read()) != -1)
1915
    {
1916
      //in a comment
1917
      if (currentCharacter =='-' && previousCharacter == '-'  && 
1918
          secondPreviousCharacter =='!' && thirdPreviousCharacter == '<')
1919
      {
1920
        comment = true;
1921
      }
1922
      //out of comment
1923
      if (comment && currentCharacter == '>' && previousCharacter == '-' && 
1924
          secondPreviousCharacter =='-')
1925
      {
1926
         comment = false;
1927
      }
1928
      
1929
      //this is not comment
1930
      if (currentCharacter !='!' && previousCharacter == '<' && !comment)
1931
      {
1932
        count ++;
1933
      }
1934
      // get target line
1935
      if (count == TARGETNUM && currentCharacter !='>')
1936
      {
1937
        buffer.append(currentCharacter);
1938
      }
1939
      if (count == TARGETNUM && currentCharacter == '>')
1940
      {
1941
          break;
1942
      }
1943
      thirdPreviousCharacter = secondPreviousCharacter;
1944
      secondPreviousCharacter = previousCharacter;
1945
      previousCharacter = currentCharacter;
1946
      
1947
    }
1948
    secondLine = buffer.toString();
1949
    MetaCatUtil.debugMessage("the second line string is: "+secondLine, 25);
1950
    xml.reset();
1951
    return secondLine;
1952
  }
1953

    
1954
  // DELETE SECTION
1955
  /**
1956
   * Handle the database delete request and delete an XML document
1957
   * from the database connection
1958
   */
1959
  private void handleDeleteAction(PrintWriter out, Hashtable params,
1960
               HttpServletResponse response, String user, String[] groups) {
1961

    
1962
    String[] docid = (String[])params.get("docid");
1963

    
1964
    // delete the document from the database
1965
    try {
1966

    
1967
                                      // NOTE -- NEED TO TEST HERE
1968
                                      // FOR EXISTENCE OF DOCID PARAM
1969
                                      // BEFORE ACCESSING ARRAY
1970
      try {
1971
        DocumentImpl.delete(docid[0], user, groups);
1972
        response.setContentType("text/xml");
1973
        out.println("<?xml version=\"1.0\"?>");
1974
        out.println("<success>");
1975
        out.println("Document deleted.");
1976
        out.println("</success>");
1977
      } catch (AccessionNumberException ane) {
1978
        response.setContentType("text/xml");
1979
        out.println("<?xml version=\"1.0\"?>");
1980
        out.println("<error>");
1981
        out.println("Error deleting document!!!");
1982
        out.println(ane.getMessage());
1983
        out.println("</error>");
1984
      }
1985
    } catch (Exception e) {
1986
      response.setContentType("text/xml");
1987
      out.println("<?xml version=\"1.0\"?>");
1988
      out.println("<error>");
1989
      out.println(e.getMessage());
1990
      out.println("</error>");
1991
    }
1992
  }
1993
  // END OF DELETE SECTION
1994

    
1995
  // VALIDATE SECTION
1996
  /**
1997
   * Handle the validation request and return the results to the requestor
1998
   */
1999
  private void handleValidateAction(PrintWriter out, Hashtable params) {
2000

    
2001
    // Get the document indicated
2002
    String valtext = null;
2003
    DBConnection dbConn = null;
2004
    int serialNumber = -1;
2005

    
2006
    try {
2007
      valtext = ((String[])params.get("valtext"))[0];
2008
    } catch (Exception nullpe) {
2009

    
2010

    
2011
      String docid = null;
2012
      try {
2013
        // Find the document id number
2014
        docid = ((String[])params.get("docid"))[0];
2015

    
2016

    
2017
        // Get the document indicated from the db
2018
        DocumentImpl xmldoc = new DocumentImpl(docid);
2019
        valtext = xmldoc.toString();
2020

    
2021
      } catch (NullPointerException npe) {
2022

    
2023
        out.println("<error>Error getting document ID: " + docid + "</error>");
2024
        //if ( conn != null ) { util.returnConnection(conn); }
2025
        return;
2026
      } catch (Exception e) {
2027

    
2028
        out.println(e.getMessage());
2029
      }
2030
    }
2031

    
2032

    
2033
    try {
2034
      // get a connection from the pool
2035
      dbConn=DBConnectionPool.
2036
                  getDBConnection("MetaCatServlet.handleValidateAction");
2037
      serialNumber=dbConn.getCheckOutSerialNumber();
2038
      DBValidate valobj = new DBValidate(saxparser,dbConn);
2039
      boolean valid = valobj.validateString(valtext);
2040

    
2041
      // set content type and other response header fields first
2042

    
2043
      out.println(valobj.returnErrors());
2044

    
2045
    } catch (NullPointerException npe2) {
2046
      // set content type and other response header fields first
2047

    
2048
      out.println("<error>Error validating document.</error>");
2049
    } catch (Exception e) {
2050

    
2051
      out.println(e.getMessage());
2052
    } finally {
2053
      // Return db connection
2054
      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2055
    }
2056
  }
2057
  // END OF VALIDATE SECTION
2058

    
2059
  // OTHER ACTION HANDLERS
2060

    
2061
  /**
2062
   * Handle "getrevsionanddoctype" action
2063
   * Given a docid, return it's current revision and doctype from data base
2064
   * The output is String look like "rev;doctype"
2065
   */
2066
  private void handleGetRevisionAndDocTypeAction(PrintWriter out,
2067
                                                              Hashtable params)
2068
  {
2069
    // To store doc parameter
2070
    String [] docs = new String[10];
2071
    // Store a single doc id
2072
    String givenDocId = null;
2073
    // Get docid from parameters
2074
    if (params.containsKey("docid"))
2075
    {
2076
      docs = (String[])params.get("docid");
2077
    }
2078
    // Get first docid form string array
2079
    givenDocId = docs[0];
2080

    
2081
    try
2082
    {
2083
      // Make sure there is a docid
2084
      if (givenDocId == null || givenDocId.equals(""))
2085
      {
2086
        throw new Exception("User didn't specify docid!");
2087
      }//if
2088

    
2089
      // Create a DBUtil object
2090
      DBUtil dbutil = new DBUtil();
2091
      // Get a rev and doctype
2092
      String revAndDocType =
2093
                dbutil.getCurrentRevisionAndDocTypeForGivenDocument(givenDocId);
2094
      out.println(revAndDocType);
2095

    
2096
    }//try
2097
    catch (Exception e)
2098
    {
2099
      // Handle exception
2100
      out.println("<?xml version=\"1.0\"?>");
2101
      out.println("<error>");
2102
      out.println(e.getMessage());
2103
      out.println("</error>");
2104
    }//catch
2105

    
2106
  }//handleGetRevisionAndDocTypeAction
2107

    
2108
  /**
2109
   * Handle "getaccesscontrol" action.
2110
   * Read Access Control List from db connection in XML format
2111
   */
2112
  private void handleGetAccessControlAction(PrintWriter out, Hashtable params,
2113
                                       HttpServletResponse response,
2114
                                       String username, String[] groupnames) {
2115

    
2116
    DBConnection dbConn = null;
2117
    int serialNumber = -1;
2118
    String docid = ((String[])params.get("docid"))[0];
2119

    
2120
    try {
2121

    
2122
        // get connection from the pool
2123
        dbConn=DBConnectionPool.
2124
                 getDBConnection("MetaCatServlet.handleGetAccessControlAction");
2125
        serialNumber=dbConn.getCheckOutSerialNumber();
2126
        AccessControlList aclobj = new AccessControlList(dbConn);
2127
        String acltext = aclobj.getACL(docid, username, groupnames);
2128
        out.println(acltext);
2129

    
2130
    } catch (Exception e) {
2131
      out.println("<?xml version=\"1.0\"?>");
2132
      out.println("<error>");
2133
      out.println(e.getMessage());
2134
      out.println("</error>");
2135
    } finally {
2136
      // Retrun db connection to pool
2137
      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2138
    }
2139

    
2140
  }
2141

    
2142
  /**
2143
   * Handle the "getprincipals" action.
2144
   * Read all principals from authentication scheme in XML format
2145
   */
2146
  private void handleGetPrincipalsAction(PrintWriter out, String user,
2147
                                         String password) {
2148

    
2149

    
2150
    try {
2151

    
2152

    
2153
        AuthSession auth = new AuthSession();
2154
        String principals = auth.getPrincipals(user, password);
2155
        out.println(principals);
2156

    
2157
    } catch (Exception e) {
2158
      out.println("<?xml version=\"1.0\"?>");
2159
      out.println("<error>");
2160
      out.println(e.getMessage());
2161
      out.println("</error>");
2162
    }
2163

    
2164
  }
2165

    
2166
  /**
2167
   * Handle "getdoctypes" action.
2168
   * Read all doctypes from db connection in XML format
2169
   */
2170
  private void handleGetDoctypesAction(PrintWriter out, Hashtable params,
2171
                                       HttpServletResponse response) {
2172

    
2173

    
2174
    try {
2175

    
2176

    
2177
        DBUtil dbutil = new DBUtil();
2178
        String doctypes = dbutil.readDoctypes();
2179
        out.println(doctypes);
2180

    
2181
    } catch (Exception e) {
2182
      out.println("<?xml version=\"1.0\"?>");
2183
      out.println("<error>");
2184
      out.println(e.getMessage());
2185
      out.println("</error>");
2186
    }
2187

    
2188
  }
2189

    
2190
  /**
2191
   * Handle the "getdtdschema" action.
2192
   * Read DTD or Schema file for a given doctype from Metacat catalog system
2193
   */
2194
  private void handleGetDTDSchemaAction(PrintWriter out, Hashtable params,
2195
                                        HttpServletResponse response) {
2196

    
2197

    
2198
    String doctype = null;
2199
    String[] doctypeArr = (String[])params.get("doctype");
2200

    
2201
    // get only the first doctype specified in the list of doctypes
2202
    // it could be done for all doctypes in that list
2203
    if (doctypeArr != null) {
2204
        doctype = ((String[])params.get("doctype"))[0];
2205
    }
2206

    
2207
    try {
2208

    
2209

    
2210
        DBUtil dbutil = new DBUtil();
2211
        String dtdschema = dbutil.readDTDSchema(doctype);
2212
        out.println(dtdschema);
2213

    
2214
    } catch (Exception e) {
2215
      out.println("<?xml version=\"1.0\"?>");
2216
      out.println("<error>");
2217
      out.println(e.getMessage());
2218
      out.println("</error>");
2219
    }
2220

    
2221
  }
2222

    
2223
  /**
2224
   * Handle the "getdataguide" action.
2225
   * Read Data Guide for a given doctype from db connection in XML format
2226
   */
2227
  private void handleGetDataGuideAction(PrintWriter out, Hashtable params,
2228
                                        HttpServletResponse response) {
2229

    
2230

    
2231
    String doctype = null;
2232
    String[] doctypeArr = (String[])params.get("doctype");
2233

    
2234
    // get only the first doctype specified in the list of doctypes
2235
    // it could be done for all doctypes in that list
2236
    if (doctypeArr != null) {
2237
        doctype = ((String[])params.get("doctype"))[0];
2238
    }
2239

    
2240
    try {
2241

    
2242

    
2243
        DBUtil dbutil = new DBUtil();
2244
        String dataguide = dbutil.readDataGuide(doctype);
2245
        out.println(dataguide);
2246

    
2247
    } catch (Exception e) {
2248
      out.println("<?xml version=\"1.0\"?>");
2249
      out.println("<error>");
2250
      out.println(e.getMessage());
2251
      out.println("</error>");
2252
    }
2253

    
2254
  }
2255

    
2256
  /**
2257
   * Handle the "getlastdocid" action.
2258
   * Get the latest docid with rev number from db connection in XML format
2259
   */
2260
  private void handleGetMaxDocidAction(PrintWriter out, Hashtable params,
2261
                                        HttpServletResponse response) {
2262

    
2263

    
2264
    String scope = ((String[])params.get("scope"))[0];
2265
    if (scope == null) {
2266
        scope = ((String[])params.get("username"))[0];
2267
    }
2268

    
2269
    try {
2270

    
2271

    
2272
        DBUtil dbutil = new DBUtil();
2273
        String lastDocid = dbutil.getMaxDocid(scope);
2274
        out.println("<?xml version=\"1.0\"?>");
2275
        out.println("<lastDocid>");
2276
        out.println("  <scope>" + scope + "</scope>");
2277
        out.println("  <docid>" + lastDocid + "</docid>");
2278
        out.println("</lastDocid>");
2279

    
2280
    } catch (Exception e) {
2281
      out.println("<?xml version=\"1.0\"?>");
2282
      out.println("<error>");
2283
      out.println(e.getMessage());
2284
      out.println("</error>");
2285
    }
2286

    
2287
  }
2288

    
2289
  /**
2290
   * Handle documents passed to metacat that are encoded using the
2291
   * "multipart/form-data" mime type.  This is typically used for uploading
2292
   * data files which may be binary and large.
2293
   */
2294
  private void handleMultipartForm(HttpServletRequest request,
2295
                                   HttpServletResponse response)
2296
  {
2297
    PrintWriter out = null;
2298
    String action = null;
2299

    
2300
    // Parse the multipart form, and save the parameters in a Hashtable and
2301
    // save the FileParts in a hashtable
2302

    
2303
    Hashtable params = new Hashtable();
2304
    Hashtable fileList = new Hashtable();
2305
    int sizeLimit = (new Integer(MetaCatUtil.getOption("datafilesizelimit")))
2306
                                                                   .intValue();
2307
    MetaCatUtil.debugMessage("The limit size of data file is: "+sizeLimit, 50);
2308

    
2309
    try {
2310
      // MBJ: need to put filesize limit in Metacat config (metacat.properties)
2311
      MultipartParser mp = new MultipartParser(request, sizeLimit*1024*1024);
2312
      Part part;
2313
      while ((part = mp.readNextPart()) != null) {
2314
        String name = part.getName();
2315

    
2316
        if (part.isParam()) {
2317
          // it's a parameter part
2318
          ParamPart paramPart = (ParamPart) part;
2319
          String value = paramPart.getStringValue();
2320
          params.put(name, value);
2321
          if (name.equals("action")) {
2322
            action = value;
2323
          }
2324
        } else if (part.isFile()) {
2325
          // it's a file part
2326
          FilePart filePart = (FilePart) part;
2327
          fileList.put(name, filePart);
2328

    
2329
          // Stop once the first file part is found, otherwise going onto the
2330
          // next part prevents access to the file contents.  So...for upload
2331
          // to work, the datafile must be the last part
2332
          break;
2333
        }
2334
      }
2335
    } catch (IOException ioe) {
2336
      try {
2337
        out = response.getWriter();
2338
      } catch (IOException ioe2) {
2339
        System.err.println("Fatal Error: couldn't get response output stream.");
2340
      }
2341
      out.println("<?xml version=\"1.0\"?>");
2342
      out.println("<error>");
2343
      out.println("Error: problem reading multipart data.");
2344
      out.println("</error>");
2345
    }
2346

    
2347
    // Get the session information
2348
    String username = null;
2349
    String password = null;
2350
    String[] groupnames = null;
2351
    String sess_id = null;
2352

    
2353
    // be aware of session expiration on every request
2354
    HttpSession sess = request.getSession(true);
2355
    if (sess.isNew()) {
2356
      // session expired or has not been stored b/w user requests
2357
      username = "public";
2358
      sess.setAttribute("username", username);
2359
    } else {
2360
      username = (String)sess.getAttribute("username");
2361
      password = (String)sess.getAttribute("password");
2362
      groupnames = (String[])sess.getAttribute("groupnames");
2363
      try {
2364
        sess_id = (String)sess.getId();
2365
      } catch(IllegalStateException ise) {
2366
        System.out.println("error in  handleMultipartForm: this shouldn't " +
2367
                           "happen: the session should be valid: " +
2368
                           ise.getMessage());
2369
      }
2370
    }
2371

    
2372
    // Get the out stream
2373
    try {
2374
          out = response.getWriter();
2375
        } catch (IOException ioe2) {
2376
          util.debugMessage("Fatal Error: couldn't get response "+
2377
                             "output stream.", 30);
2378
        }
2379

    
2380
    if ( action.equals("upload")) {
2381
      if (username != null &&  !username.equals("public")) {
2382
        handleUploadAction(request, out, params, fileList,
2383
                           username, groupnames);
2384
      } else {
2385

    
2386
        out.println("<?xml version=\"1.0\"?>");
2387
        out.println("<error>");
2388
        out.println("Permission denied for " + action);
2389
        out.println("</error>");
2390
      }
2391
    } else {
2392
      /*try {
2393
        out = response.getWriter();
2394
      } catch (IOException ioe2) {
2395
        System.err.println("Fatal Error: couldn't get response output stream.");
2396
      }*/
2397
      out.println("<?xml version=\"1.0\"?>");
2398
      out.println("<error>");
2399
      out.println("Error: action not registered.  Please report this error.");
2400
      out.println("</error>");
2401
    }
2402
    out.close();
2403
  }
2404

    
2405
  /**
2406
   * Handle the upload action by saving the attached file to disk and
2407
   * registering it in the Metacat db
2408
   */
2409
  private void handleUploadAction(HttpServletRequest request,
2410
                                  PrintWriter out,
2411
                                  Hashtable params, Hashtable fileList,
2412
                                  String username, String[] groupnames)
2413
  {
2414
    //PrintWriter out = null;
2415
    //Connection conn = null;
2416
    String action = null;
2417
    String docid = null;
2418

    
2419
    /*response.setContentType("text/xml");
2420
    try
2421
    {
2422
      out = response.getWriter();
2423
    }
2424
    catch (IOException ioe2)
2425
    {
2426
      System.err.println("Fatal Error: couldn't get response output stream.");
2427
    }*/
2428

    
2429
    if (params.containsKey("docid"))
2430
    {
2431
      docid = (String)params.get("docid");
2432
    }
2433

    
2434
    // Make sure we have a docid and datafile
2435
    if (docid != null && fileList.containsKey("datafile")) {
2436

    
2437
      // Get a reference to the file part of the form
2438
      FilePart filePart = (FilePart)fileList.get("datafile");
2439
      String fileName = filePart.getFileName();
2440
      MetaCatUtil.debugMessage("Uploading filename: " + fileName, 10);
2441

    
2442
      // Check if the right file existed in the uploaded data
2443
      if (fileName != null) {
2444

    
2445
        try
2446
        {
2447
           //MetaCatUtil.debugMessage("Upload datafile " + docid +"...", 10);
2448
           //If document get lock data file grant
2449
           if (DocumentImpl.getDataFileLockGrant(docid))
2450
           {
2451
              // register the file in the database (which generates an exception
2452
              //if the docid is not acceptable or other untoward things happen
2453
              DocumentImpl.registerDocument(fileName, "BIN", docid, username);
2454

    
2455
              // Save the data file to disk using "docid" as the name
2456
              dataDirectory.mkdirs();
2457
              File newFile = new File(dataDirectory, docid);
2458
              long size = filePart.writeTo(newFile);
2459

    
2460
              // Force replication this data file
2461
              // To data file, "insert" and update is same
2462
              // The fourth parameter is null. Because it is notification server
2463
              // and this method is in MetaCatServerlet. It is original command,
2464
              // not get force replication info from another metacat
2465
              ForceReplicationHandler frh = new ForceReplicationHandler
2466
                                                (docid, "insert", false, null);
2467

    
2468
              // set content type and other response header fields first
2469
              out.println("<?xml version=\"1.0\"?>");
2470
              out.println("<success>");
2471
              out.println("<docid>" + docid + "</docid>");
2472
              out.println("<size>" + size + "</size>");
2473
              out.println("</success>");
2474
          }//if
2475

    
2476
        } //try
2477
        catch (Exception e)
2478
        {
2479
          out.println("<?xml version=\"1.0\"?>");
2480
          out.println("<error>");
2481
          out.println(e.getMessage());
2482
          out.println("</error>");
2483
        }
2484

    
2485
      }
2486
      else
2487
      {
2488
        // the field did not contain a file
2489
        out.println("<?xml version=\"1.0\"?>");
2490
        out.println("<error>");
2491
        out.println("The uploaded data did not contain a valid file.");
2492
        out.println("</error>");
2493
      }
2494
    }
2495
    else
2496
    {
2497
      // Error bcse docid missing or file missing
2498
      out.println("<?xml version=\"1.0\"?>");
2499
      out.println("<error>");
2500
      out.println("The uploaded data did not contain a valid docid " +
2501
                  "or valid file.");
2502
      out.println("</error>");
2503
    }
2504
  }
2505

    
2506
  /*
2507
   * A method to handle set access action
2508
   */
2509
  private void handleSetAccessAction(PrintWriter out,
2510
                                   Hashtable params,
2511
                                   String username)
2512
  {
2513
    String [] docList        = null;
2514
    String [] principalList  = null;
2515
    String [] permissionList = null;
2516
    String [] permTypeList   = null;
2517
    String [] permOrderList  = null;
2518
    String permission = null;
2519
    String permType   = null;
2520
    String permOrder  = null;
2521
    Vector errorList  = new Vector();
2522
    String error      = null;
2523
    Vector successList = new Vector();
2524
    String success    = null;
2525

    
2526

    
2527
    // Get parameters
2528
    if (params.containsKey("docid"))
2529
    {
2530
      docList = (String[])params.get("docid");
2531
    }
2532
    if (params.containsKey("principal"))
2533
    {
2534
      principalList = (String[])params.get("principal");
2535
    }
2536
    if (params.containsKey("permission"))
2537
    {
2538
      permissionList = (String[])params.get("permission");
2539

    
2540
    }
2541
    if (params.containsKey("permType"))
2542
    {
2543
      permTypeList = (String[])params.get("permType");
2544

    
2545
    }
2546
    if (params.containsKey("permOrder"))
2547
    {
2548
      permOrderList = (String[])params.get("permOrder");
2549

    
2550
    }
2551

    
2552
    // Make sure the parameter is not null
2553
    if (docList == null || principalList == null || permTypeList == null ||
2554
        permissionList == null)
2555
    {
2556
      error = "Please check your parameter list, it should look like: "+
2557
              "?action=setaccess&docid=pipeline.1.1&principal=public" +
2558
              "&permission=read&permType=allow&permOrder=allowFirst";
2559
      errorList.addElement(error);
2560
      outputResponse(successList, errorList, out);
2561
      return;
2562
    }
2563

    
2564
    // Only select first element for permission, type and order
2565
    permission = permissionList[0];
2566
    permType = permTypeList[0];
2567
    if (permOrderList != null)
2568
    {
2569
       permOrder = permOrderList[0];
2570
    }
2571

    
2572
    // Get package doctype set
2573
    Vector packageSet =MetaCatUtil.getOptionList(
2574
                                    MetaCatUtil.getOption("packagedoctypeset"));
2575
    //debug
2576
    if (packageSet != null)
2577
    {
2578
      for (int i = 0; i<packageSet.size(); i++)
2579
      {
2580
        MetaCatUtil.debugMessage("doctype in package set: " +
2581
                              (String)packageSet.elementAt(i), 34);
2582
      }
2583
    }//if
2584

    
2585
    // handle every accessionNumber
2586
    for (int i=0; i <docList.length; i++)
2587
    {
2588
      String accessionNumber = docList[i];
2589
      String owner = null;
2590
      String publicId = null;
2591
      // Get document owner and public id
2592
      try
2593
      {
2594
        owner = getFieldValueForDoc(accessionNumber, "user_owner");
2595
        publicId = getFieldValueForDoc(accessionNumber, "doctype");
2596
      }//try
2597
      catch (Exception e)
2598
      {
2599
        MetaCatUtil.debugMessage("Error in handleSetAccessAction: " +
2600
                                  e.getMessage(), 30);
2601
        error = "Error in set access control for document - " + accessionNumber+
2602
                 e.getMessage();
2603
        errorList.addElement(error);
2604
        continue;
2605
      }
2606
      //check if user is the owner. Only owner can do owner
2607
      if (username == null || owner == null || !username.equals(owner))
2608
      {
2609
        error = "User - " + username + " does not have permission to set " +
2610
                "access control for docid - " + accessionNumber;
2611
        errorList.addElement(error);
2612
        continue;
2613
      }
2614

    
2615
      // If docid publicid is BIN data file or other beta4, 6 package document
2616
      // we could not do set access control. Because we don't want inconsistent
2617
      // to its access docuemnt
2618
      if (publicId!=null && packageSet!=null && packageSet.contains(publicId))
2619
      {
2620
        error = "Could not set access control to document "+ accessionNumber +
2621
                "because it is in a pakcage and it has a access file for it";
2622
        errorList.addElement(error);
2623
        continue;
2624
      }
2625

    
2626
      // for every principle
2627
      for (int j = 0; j<principalList.length; j++)
2628
      {
2629
        String principal = principalList[j];
2630
        try
2631
        {
2632
          //insert permission
2633
          AccessControlForSingleFile accessControl = new
2634
                           AccessControlForSingleFile(accessionNumber,
2635
                                    principal, permission, permType, permOrder);
2636
          accessControl.insertPermissions();
2637
          success = "Set access control to document "+ accessionNumber +
2638
                    " successfully";
2639
          successList.addElement(success);
2640
        }
2641
        catch (Exception ee)
2642
        {
2643
          MetaCatUtil.debugMessage("Erorr in handleSetAccessAction2: " +
2644
                                   ee.getMessage(), 30);
2645
          error = "Faild to set access control for document " +
2646
                  accessionNumber + " because " + ee.getMessage();
2647
          errorList.addElement(error);
2648
          continue;
2649
        }
2650
      }//for every principle
2651
    }//for every document
2652
    outputResponse(successList, errorList, out);
2653
  }//handleSetAccessAction
2654

    
2655

    
2656
  /*
2657
   * A method try to determin a docid's public id, if couldn't find null
2658
   * will be returned.
2659
   */
2660
  private String getFieldValueForDoc(String accessionNumber, String fieldName)
2661
                                      throws Exception
2662
  {
2663
    if (accessionNumber==null || accessionNumber.equals("") ||fieldName == null
2664
        || fieldName.equals(""))
2665
    {
2666
      throw new Exception("Docid or field name was not specified");
2667
    }
2668

    
2669
    PreparedStatement pstmt = null;
2670
    ResultSet rs = null;
2671
    String fieldValue = null;
2672
    String docId = null;
2673
    DBConnection conn = null;
2674
    int serialNumber = -1;
2675

    
2676
    // get rid of revision if access number has
2677
    docId = MetaCatUtil.getDocIdFromString(accessionNumber);
2678
    try
2679
    {
2680
      //check out DBConnection
2681
      conn=DBConnectionPool.getDBConnection("MetaCatServlet.getPublicIdForDoc");
2682
      serialNumber=conn.getCheckOutSerialNumber();
2683
      pstmt = conn.prepareStatement(
2684
            "SELECT " + fieldName + " FROM xml_documents " +
2685
            "WHERE docid = ? ");
2686

    
2687
      pstmt.setString(1, docId);
2688
      pstmt.execute();
2689
      rs = pstmt.getResultSet();
2690
      boolean hasRow = rs.next();
2691
      int perm = 0;
2692
      if ( hasRow )
2693
      {
2694
        fieldValue = rs.getString(1);
2695
      }
2696
      else
2697
      {
2698
        throw new Exception("Could not find document: "+accessionNumber);
2699
      }
2700
    }//try
2701
    catch (Exception e)
2702
    {
2703
      MetaCatUtil.debugMessage("Exception in MetacatServlet.getPublicIdForDoc: "
2704
                               + e.getMessage(), 30);
2705
      throw e;
2706
    }
2707
    finally
2708
    {
2709
      try
2710
      {
2711
        rs.close();
2712
        pstmt.close();
2713

    
2714
      }
2715
      finally
2716
      {
2717
        DBConnectionPool.returnDBConnection(conn, serialNumber);
2718
      }
2719
    }
2720
    return fieldValue;
2721
  }//getFieldValueForDoc
2722

    
2723
  /*
2724
   * A method to output setAccess action result
2725
   */
2726
  private void outputResponse(Vector successList,
2727
                              Vector errorList,
2728
                              PrintWriter out)
2729
  {
2730
    boolean error = false;
2731
    boolean success = false;
2732
    // Output prolog
2733
    out.println(PROLOG);
2734
    // output success message
2735
    if ( successList != null)
2736
    {
2737
      for (int i = 0; i<successList.size(); i++)
2738
      {
2739
        out.println(SUCCESS);
2740
        out.println((String)successList.elementAt(i));
2741
        out.println(SUCCESSCLOSE);
2742
        success = true;
2743
      }//for
2744
    }//if
2745
    // output error message
2746
    if (errorList != null)
2747
    {
2748
      for (int i = 0; i<errorList.size(); i++)
2749
      {
2750
        out.println(ERROR);
2751
        out.println((String)errorList.elementAt(i));
2752
        out.println(ERRORCLOSE);
2753
        error = true;
2754
      }//for
2755
    }//if
2756

    
2757
    // if no error and no success info, send a error that nothing happened
2758
    if( !error && !success)
2759
    {
2760
      out.println(ERROR);
2761
      out.println("Nothing happend for setaccess action");
2762
      out.println(ERRORCLOSE);
2763
    }
2764

    
2765
  }//outputResponse
2766
}
(39-39/58)