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: 2002-12-19 10:03:45 -0800 (Thu, 19 Dec 2002) $'
11
 * '$Revision: 1342 $'
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.net.URL;
47
import java.net.MalformedURLException;
48
import java.sql.PreparedStatement;
49
import java.sql.ResultSet;
50
import java.sql.Connection;
51
import java.sql.SQLException;
52
import java.lang.reflect.*;
53
import java.net.*;
54
import java.util.zip.*;
55

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

    
67
import org.xml.sax.SAXException;
68

    
69
/**
70
 * A metadata catalog server implemented as a Java Servlet
71
 *
72
 * <p>Valid parameters are:<br>
73
 * action=query -- query the values of all elements and attributes
74
 *                     and return a result set of nodes<br>
75
 * action=squery -- structured query (see pathquery.dtd)<br> 
76
 * action= -- export a zip format for data packadge<br>
77
 * action=read -- read any metadata/data file from Metacat and from Internet<br>
78
 * action=insert -- insert an XML document into the database store<br>
79
 * action=update -- update an XML document that is in the database store<br>
80
 * action=delete --  delete an XML document from the database store<br>
81
 * action=validate -- vallidate the xml contained in valtext<br>
82
 * doctype -- document type list returned by the query (publicID)<br>
83
 * qformat=xml -- display resultset from query in XML<br>
84
 * qformat=html -- display resultset from query in HTML<br>
85
 * qformat=zip -- zip resultset from query<br>
86
 * docid=34 -- display the document with the document ID number 34<br>
87
 * doctext -- XML text of the document to load into the database<br>
88
 * acltext -- XML access text for a document to load into the database<br>
89
 * dtdtext -- XML DTD text for a new DTD to load into Metacat XML Catalog<br>
90
 * query -- actual query text (to go with 'action=query' or 'action=squery')<br>
91
 * valtext -- XML text to be validated<br>
92
 * abstractpath -- XPath in metadata document to read from<br>
93
 * action=getaccesscontrol -- retrieve acl info for Metacat document<br>
94
 * action=getdoctypes -- retrieve all doctypes (publicID)<br>
95
 * action=getdtdschema -- retrieve a DTD or Schema file<br>
96
 * action=getdataguide -- retrieve a Data Guide<br>
97
 * action=getprincipals -- retrieve a list of principals in XML<br>
98
 * datadoc -- data document name (id)<br>
99
 * <p>
100
 * The particular combination of parameters that are valid for each 
101
 * particular action value is quite specific.  This documentation
102
 * will be reorganized to reflect this information.
103
 */
104
public class MetaCatServlet extends HttpServlet {
105

    
106
  private ServletConfig config = null;
107
  private ServletContext context = null;
108
  private String resultStyleURL = null;
109
  private String xmlcatalogfile = null;
110
  private String saxparser = null;
111
  private String datafilepath = null; 
112
  private File dataDirectory = null;
113
  private String servletpath = null; 
114
  private String htmlpath = null; 
115
  private PropertyResourceBundle options = null;
116
  private MetaCatUtil util = null;
117
  private DBConnectionPool connPool = null;
118

    
119
  /**
120
   * Initialize the servlet by creating appropriate database connections
121
   */
122
  public void init( ServletConfig config ) throws ServletException {
123
    try {
124
      super.init( config );
125
      this.config = config;
126
      this.context = config.getServletContext(); 
127
      System.out.println("MetaCatServlet Initialize");
128

    
129
      util = new MetaCatUtil();
130
      
131
      //initial DBConnection pool
132
      connPool = DBConnectionPool.getInstance();
133

    
134
      // Get the configuration file information
135
      resultStyleURL = util.getOption("resultStyleURL");
136
      xmlcatalogfile = util.getOption("xmlcatalogfile");
137
      saxparser = util.getOption("saxparser");
138
      datafilepath = util.getOption("datafilepath");
139
      dataDirectory = new File(datafilepath);
140
      servletpath = util.getOption("servletpath");
141
      htmlpath = util.getOption("htmlpath");
142

    
143

    
144
    } catch ( ServletException ex ) {
145
      throw ex;
146
    } catch (SQLException e) {
147
      MetaCatUtil.debugMessage("Error in MetacatServlet.init: "
148
                                          +e.getMessage(), 20);
149
    }
150
  }
151

    
152
  /**
153
   * Close all db connections from the pool
154
   */
155
  public void destroy() {
156
      // Close all db connection  
157
      connPool.release();
158
  }
159

    
160
  /** Handle "GET" method requests from HTTP clients */
161
  public void doGet (HttpServletRequest request, HttpServletResponse response)
162
    throws ServletException, IOException {
163

    
164
    // Process the data and send back the response
165
    handleGetOrPost(request, response);
166
  }
167

    
168
  /** Handle "POST" method requests from HTTP clients */
169
  public void doPost( HttpServletRequest request, HttpServletResponse response)
170
    throws ServletException, IOException {
171

    
172
    // Process the data and send back the response
173
    handleGetOrPost(request, response);
174
  }
175

    
176
  /**
177
   * Control servlet response depending on the action parameter specified
178
   */
179
  private void handleGetOrPost(HttpServletRequest request, 
180
                               HttpServletResponse response) 
181
                               throws ServletException, IOException 
182
  {
183

    
184
    if ( util == null ) {
185
        util = new MetaCatUtil(); 
186
    }
187
    /*MetaCatUtil.debugMessage("Connection pool size: "
188
                                     +connPool.getSizeOfDBConnectionPool(),10);
189
    MetaCatUtil.debugMessage("Free DBConnection number: "
190
                                  +connPool.getFreeDBConnectionNumber(), 10);*/
191
    //If all DBConnection in the pool are free and DBConnection pool 
192
    //size is greater than initial value, shrink the connection pool 
193
    //size to initial value
194
    DBConnectionPool.shrinkDBConnectionPoolSize();
195
    
196
    //Debug message to print out the method which have a busy DBConnection
197
    connPool.printMethodNameHavingBusyDBConnection();
198
   
199
    String ctype = request.getContentType();
200
    if (ctype != null && ctype.startsWith("multipart/form-data")) {
201
      handleMultipartForm(request, response);
202
    } else {
203
    
204
  
205
      String name = null;
206
      String[] value = null;
207
      String[] docid = new String[3];
208
      Hashtable params = new Hashtable();
209
      Enumeration paramlist = request.getParameterNames();
210
     
211
    
212
      while (paramlist.hasMoreElements()) {
213
      
214
        name = (String)paramlist.nextElement();
215
        value = request.getParameterValues(name);
216
  
217
        // Decode the docid and mouse click information
218
        if (name.endsWith(".y")) {
219
          docid[0] = name.substring(0,name.length()-2);
220
          params.put("docid", docid);
221
          name = "ypos";
222
        }
223
        if (name.endsWith(".x")) {
224
          name = "xpos";
225
        } 
226
  
227
        params.put(name,value); 
228
      }  
229
      
230
    
231
      //handle param is emptpy
232
      if (params.isEmpty() || params == null)
233
      {
234
        return;
235
      }
236
      //if the user clicked on the input images, decode which image
237
      //was clicked then set the action.
238
      String action = ((String[])params.get("action"))[0];  
239
      util.debugMessage("Line 230: Action is: " + action, 1);
240
  
241
      // This block handles session management for the servlet
242
      // by looking up the current session information for all actions
243
      // other than "login" and "logout"
244
      String username = null;
245
      String password = null;
246
      String[] groupnames = null;
247
      String sess_id = null;
248
  
249
      // handle login action
250
      if (action.equals("login")) {
251
        PrintWriter out = response.getWriter();
252
        handleLoginAction(out, params, request, response);
253
        out.close();
254
  
255
      // handle logout action  
256
      } else if (action.equals("logout")) {
257
        PrintWriter out = response.getWriter();
258
        handleLogoutAction(out, params, request, response);
259
        out.close();
260
        
261
      // handle shrink DBConnection request
262
      } else if (action.equals("shrink")) {
263
        PrintWriter out = response.getWriter();
264
        boolean success = false;
265
        //If all DBConnection in the pool are free and DBConnection pool 
266
        //size is greater than initial value, shrink the connection pool 
267
        //size to initial value
268
        success = DBConnectionPool.shrinkConnectionPoolSize();
269
        if (success)
270
        {
271
          //if successfully shrink the pool size to initial value
272
          out.println("DBConnection Pool shrink successfully");
273
        }//if
274
        else
275
        {
276
          out.println("DBConnection pool couldn't shrink successfully");
277
        }
278
       //close out put
279
        out.close();
280
      
281
      // aware of session expiration on every request  
282
      } else {   
283
  
284
        HttpSession sess = request.getSession(true);
285
        if (sess.isNew()) { 
286
          // session expired or has not been stored b/w user requests
287
          username = "public";
288
          sess.setAttribute("username", username);
289
        } else {
290
          username = (String)sess.getAttribute("username");
291
          password = (String)sess.getAttribute("password");
292
          groupnames = (String[])sess.getAttribute("groupnames");
293
          try {
294
            sess_id = (String)sess.getId();
295
          } catch(IllegalStateException ise) {
296
            System.out.println("error in handleGetOrPost: this shouldn't " +
297
                               "happen: the session should be valid: " + 
298
                               ise.getMessage());
299
          }
300
        }  
301
      }    
302
  
303
       // Now that we know the session is valid, we can delegate the request
304
      // to a particular action handler
305
      if(action.equals("query")) {
306
        PrintWriter out = response.getWriter();
307
        handleQuery(out,params,response,username,groupnames);
308
        out.close();
309
      } else if(action.equals("squery")) {
310
        PrintWriter out = response.getWriter();
311
        if(params.containsKey("query")) {
312
         handleSQuery(out, params,response,username,groupnames);
313
         out.close();
314
        } else {
315
          out.println("Illegal action squery without \"query\" parameter");
316
          out.close();
317
        }
318
      } else if (action.equals("export")) {
319
        
320
        handleExportAction(params, response, username, groupnames, password);
321
      } else if (action.equals("read")) {
322
        handleReadAction(params, response, username,password, groupnames);
323
      } else if (action.equals("insert") || action.equals("update")) {
324
        PrintWriter out = response.getWriter();
325
        if ( (username != null) &&  !username.equals("public") ) {
326
          handleInsertOrUpdateAction(out,params,response,username,groupnames);
327
        } else {  
328
          out.println("Permission denied for " + action);
329
        }
330
        out.close();
331
      } else if (action.equals("delete")) {
332
        PrintWriter out = response.getWriter();
333
        if ( (username != null) &&  !username.equals("public") ) {
334
          handleDeleteAction(out, params, response, username, groupnames);
335
        } else {  
336
          out.println("Permission denied for " + action);
337
        }
338
        out.close();
339
      } else if (action.equals("validate")) {
340
        PrintWriter out = response.getWriter();
341
        handleValidateAction(out, params);
342
        out.close();
343
      } else if (action.equals("getaccesscontrol")) {
344
        PrintWriter out = response.getWriter();
345
        handleGetAccessControlAction(out,params,response,username,groupnames);
346
        out.close();
347
      } else if (action.equals("getprincipals")) {
348
        PrintWriter out = response.getWriter();
349
        handleGetPrincipalsAction(out, username, password);
350
        out.close();
351
      } else if (action.equals("getdoctypes")) {
352
        PrintWriter out = response.getWriter();
353
        handleGetDoctypesAction(out, params, response);
354
        out.close();
355
      } else if (action.equals("getdtdschema")) {
356
        PrintWriter out = response.getWriter();
357
        handleGetDTDSchemaAction(out, params, response);
358
        out.close();
359
      } else if (action.equals("getdataguide")) {
360
        PrintWriter out = response.getWriter();
361
        handleGetDataGuideAction(out, params, response);
362
        out.close();
363
      } else if (action.equals("getlastdocid")) {
364
        PrintWriter out = response.getWriter();
365
        handleGetMaxDocidAction(out, params, response);
366
        out.close();
367
      } else if (action.equals("getrevisionanddoctype")) {
368
        PrintWriter out = response.getWriter();
369
        handleGetRevisionAndDocTypeAction(out, params);
370
        out.close();
371
      } else if (action.equals("login") || action.equals("logout")) {
372
      } else if (action.equals("protocoltest")) {
373
        String testURL = "metacat://dev.nceas.ucsb.edu/NCEAS.897766.9";
374
        try {
375
          testURL = ((String[])params.get("url"))[0];
376
        } catch (Throwable t) {
377
        }
378
        String phandler = System.getProperty("java.protocol.handler.pkgs");
379
        response.setContentType("text/html");
380
        PrintWriter out = response.getWriter();
381
        out.println("<body bgcolor=\"white\">");
382
        out.println("<p>Handler property: <code>" + phandler + "</code></p>");
383
        out.println("<p>Starting test for:<br>");
384
        out.println("    " + testURL + "</p>");
385
        try {
386
          URL u = new URL(testURL);
387
          out.println("<pre>");
388
          out.println("Protocol: " + u.getProtocol());
389
          out.println("    Host: " + u.getHost());
390
          out.println("    Port: " + u.getPort());
391
          out.println("    Path: " + u.getPath());
392
          out.println("     Ref: " + u.getRef());
393
          String pquery = u.getQuery();
394
          out.println("   Query: " + pquery);
395
          out.println("  Params: ");
396
          if (pquery != null) {
397
            Hashtable qparams = util.parseQuery(u.getQuery());
398
            for (Enumeration en = qparams.keys(); en.hasMoreElements(); ) {
399
              String pname = (String)en.nextElement();
400
              String pvalue = (String)qparams.get(pname);
401
              out.println("    " + pname + ": " + pvalue);
402
            }
403
          }
404
          out.println("</pre>");
405
          out.println("</body>");
406
          out.close();
407
        } catch (MalformedURLException mue) {
408
          System.out.println("bad url from MetacatServlet.handleGetOrPost");
409
          out.println(mue.getMessage());
410
          mue.printStackTrace(out);
411
          out.close();
412
        }
413
      } else {
414
        PrintWriter out = response.getWriter();
415
        out.println("<?xml version=\"1.0\"?>");
416
        out.println("<error>");
417
        out.println("Error: action not registered.  Please report this error.");
418
        out.println("</error>");
419
        out.close();
420
      }
421
      
422
      //util.closeConnections();
423
      // Close the stream to the client
424
      //out.close();
425
    }
426
  }
427
  
428
  // LOGIN & LOGOUT SECTION
429
  /** 
430
   * Handle the login request. Create a new session object.
431
   * Do user authentication through the session.
432
   */
433
  private void handleLoginAction(PrintWriter out, Hashtable params, 
434
               HttpServletRequest request, HttpServletResponse response) {
435

    
436
    AuthSession sess = null;
437
    String un = ((String[])params.get("username"))[0];
438
    String pw = ((String[])params.get("password"))[0];
439
    String action = ((String[])params.get("action"))[0];
440
    String qformat = ((String[])params.get("qformat"))[0];
441
    
442
    try {
443
      sess = new AuthSession();
444
    } catch (Exception e) {
445
      System.out.println("error in MetacatServlet.handleLoginAction: " +
446
                          e.getMessage());
447
      out.println(e.getMessage());
448
      return;
449
    }
450
    boolean isValid = sess.authenticate(request, un, pw);
451
    // format and transform the output
452
    if (qformat.equals("xml")) {
453
      response.setContentType("text/xml");
454
      out.println(sess.getMessage()); 
455
    } else {
456
     
457
      try {
458
        
459
        DBTransform trans = new DBTransform();
460
        response.setContentType("text/html");
461
        trans.transformXMLDocument(sess.getMessage(), "-//NCEAS//login//EN",
462
                                   "-//W3C//HTML//EN", qformat, out);
463
         
464
      } catch(Exception e) {
465
        
466
        MetaCatUtil.debugMessage("Error in MetaCatServlet.handleLoginAction: "
467
                                +e.getMessage(), 30);
468
      } 
469
      
470
    // any output is returned  
471
    }
472
  }    
473

    
474
  /** 
475
   * Handle the logout request. Close the connection.
476
   */
477
  private void handleLogoutAction(PrintWriter out, Hashtable params, 
478
               HttpServletRequest request, HttpServletResponse response) {
479

    
480
    String qformat = ((String[])params.get("qformat"))[0];
481

    
482
    // close the connection
483
    HttpSession sess = request.getSession(false);
484
    if (sess != null) { sess.invalidate();  }    
485

    
486
    // produce output
487
    StringBuffer output = new StringBuffer();
488
    output.append("<?xml version=\"1.0\"?>");
489
    output.append("<logout>");
490
    output.append("User logged out");
491
    output.append("</logout>");
492

    
493
    //format and transform the output
494
    if (qformat.equals("xml")) {
495
      response.setContentType("text/xml");
496
      out.println(output.toString()); 
497
    } else {
498
      
499
      try {
500
       
501
        DBTransform trans = new DBTransform();
502
        response.setContentType("text/html");
503
        trans.transformXMLDocument(output.toString(), "-//NCEAS//login//EN", 
504
                                   "-//W3C//HTML//EN", qformat, out);
505
         
506
      } catch(Exception e) {
507
        
508
        MetaCatUtil.debugMessage("Error in MetaCatServlet.handleLogoutAction"
509
                                  +e.getMessage(), 30);
510
      } 
511
    }
512
  }
513
  // END OF LOGIN & LOGOUT SECTION
514
  
515
  // SQUERY & QUERY SECTION
516
  /**      
517
   * Retreive the squery xml, execute it and display it
518
   *
519
   * @param out the output stream to the client
520
   * @param params the Hashtable of parameters that should be included
521
   * in the squery.
522
   * @param response the response object linked to the client
523
   * @param conn the database connection 
524
   */
525
  protected void handleSQuery(PrintWriter out, Hashtable params, 
526
                 HttpServletResponse response, String user, String[] groups)
527
  { 
528
    String xmlquery = ((String[])params.get("query"))[0];
529
    String qformat = ((String[])params.get("qformat"))[0];
530
    String resultdoc = null;
531
    MetaCatUtil.debugMessage("xmlquery: "+xmlquery, 30);
532
    double startTime = System.currentTimeMillis()/1000;
533
    Hashtable doclist = runQuery(xmlquery, user, groups);
534
    double docListTime = System.currentTimeMillis()/1000; 
535
    MetaCatUtil.debugMessage("Time for getting doc list: "
536
                                            +(docListTime-startTime), 30);
537
                                            
538
    resultdoc = createResultDocument(doclist, transformQuery(xmlquery));
539
    double toStringTime = System.currentTimeMillis()/1000;
540
    MetaCatUtil.debugMessage("Time to create xml string: "
541
                              +(toStringTime-docListTime), 30);
542
    //format and transform the results
543
    double outPutTime = 0;                                          
544
    if(qformat.equals("xml")) {
545
      response.setContentType("text/xml");
546
      out.println(resultdoc);
547
      outPutTime = System.currentTimeMillis()/1000;
548
      MetaCatUtil.debugMessage("Output time: "+(outPutTime-toStringTime), 30);
549
    } else {
550
      transformResultset(resultdoc, response, out, qformat);
551
      outPutTime = System.currentTimeMillis()/1000;
552
      MetaCatUtil.debugMessage("Output time: "+(outPutTime-toStringTime), 30);
553
    }
554
  }
555

    
556
   /**
557
    * Create the xml query, execute it and display the results.
558
    *
559
    * @param out the output stream to the client
560
    * @param params the Hashtable of parameters that should be included
561
    * in the squery.
562
    * @param response the response object linked to the client
563
    */ 
564
  protected void handleQuery(PrintWriter out, Hashtable params, 
565
                 HttpServletResponse response, String user, String[] groups)
566
  {
567
    //create the query and run it
568
    String xmlquery = DBQuery.createSQuery(params);
569
    Hashtable doclist = runQuery(xmlquery, user, groups);
570
    String qformat = ((String[])params.get("qformat"))[0];
571
    String resultdoc = null;
572
    
573
    resultdoc = createResultDocument(doclist, transformQuery(params));
574

    
575
    //format and transform the results                                        
576
    if(qformat.equals("xml")) {
577
      response.setContentType("text/xml");
578
      out.println(resultdoc);
579
    } else { 
580
      transformResultset(resultdoc, response, out, qformat);
581
    }
582
  }
583
  
584
  /**
585
   * Removes the <?xml version="x"?> tag from the beginning of xmlquery
586
   * so it can properly be placed in the <query> tag of the resultset.
587
   * This method is overwritable so that other applications can customize
588
   * the structure of what is in the <query> tag.
589
   * 
590
   * @param xmlquery is the query to remove the <?xml version="x"?> tag from.
591
   */
592
  protected String transformQuery(Hashtable params)
593
  {
594
    //DBQuery.createSQuery is a re-calling of a previously called 
595
    //function but it is necessary
596
    //so that overriding methods have access to the params hashtable
597
    String xmlquery = DBQuery.createSQuery(params);
598
    //the <?xml version="1.0"?> tag is the first 22 characters of the
599
    xmlquery = xmlquery.trim();
600
    int index = xmlquery.indexOf("?>");
601
    return xmlquery.substring(index + 2, xmlquery.length());
602
  }
603
  
604
  /**
605
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
606
   * string as a param instead of a hashtable.
607
   * 
608
   * @param xmlquery a string representing a query.
609
   */
610
  protected String transformQuery(String xmlquery)
611
  {
612
    xmlquery = xmlquery.trim();
613
    int index = xmlquery.indexOf("?>");
614
    return xmlquery.substring(index + 2, xmlquery.length());
615
  }
616
  
617
  /**
618
   * Run the query and return a hashtable of results.
619
   *
620
   * @param xmlquery the query to run
621
   */
622
  private Hashtable runQuery(String xmlquery, String user, String[] groups)
623
  {
624
    Hashtable doclist=null;
625
  
626
    try
627
    {
628
    
629
      DBQuery queryobj = new DBQuery(saxparser);
630
      doclist = queryobj.findDocuments(new StringReader(xmlquery),user,groups);
631
 
632
      return doclist;
633
    } 
634
    catch (Exception e) 
635
    {
636
    
637
      MetaCatUtil.debugMessage("Error in MetacatServlet.runQuery: " 
638
                                                      + e.getMessage(), 30);
639
      doclist = null;
640
      return doclist;
641
    }    
642
  }
643
  
644
  /**
645
   * Transorms an xml resultset document to html and sends it to the browser
646
   *
647
   * @param resultdoc the string representation of the document that needs
648
   * to be transformed.
649
   * @param response the HttpServletResponse object bound to the client.
650
   * @param out the output stream to the client
651
   * @param qformat the name of the style-set to use for transformations
652
   */ 
653
  protected void transformResultset(String resultdoc, 
654
                                    HttpServletResponse response,
655
                                    PrintWriter out, String qformat)
656
  {
657
  
658
    try {
659
     
660
      DBTransform trans = new DBTransform();
661
      response.setContentType("text/html");
662
      trans.transformXMLDocument(resultdoc, "-//NCEAS//resultset//EN", 
663
                                 "-//W3C//HTML//EN", qformat, out);
664
     
665
    }
666
    catch(Exception e)
667
    {
668
      
669
      MetaCatUtil.debugMessage("Error in MetaCatServlet.transformResultset:"
670
                                +e.getMessage(), 30);
671
    } 
672
  }
673
  
674
  /**
675
   * Transforms a hashtable of documents to an xml or html result.
676
   *
677
   * @param doclist- the hashtable to transform
678
   * @param xmlquery- the query that returned the doclist result
679
   */
680
  protected String createResultDocument(Hashtable doclist, String xmlquery)
681
  {
682
    // Create a buffer to hold the xml result
683
    StringBuffer resultset = new StringBuffer();
684
 
685
    // Print the resulting root nodes 
686
    String docid = null;
687
    String document = null;
688
    resultset.append("<?xml version=\"1.0\"?>\n");
689
    resultset.append("<resultset>\n");
690
      
691
    resultset.append("  <query>" + xmlquery + "</query>");   
692

    
693
    if(doclist != null)
694
    {
695
      Enumeration doclistkeys = doclist.keys(); 
696
      while (doclistkeys.hasMoreElements()) 
697
      {
698
        docid = (String)doclistkeys.nextElement();
699
        document = (String)doclist.get(docid);
700
        resultset.append("  <document>" + document + "</document>");
701
      }
702
    }
703

    
704
    resultset.append("</resultset>");
705
    return resultset.toString();
706
  }
707
  // END OF SQUERY & QUERY SECTION
708
  
709
 //Exoport section
710
 /**
711
   * Handle the "export" request of data package from Metacat in zip format
712
   * @param params the Hashtable of HTTP request parameters
713
   * @param response the HTTP response object linked to the client
714
   * @param user the username sent the request
715
   * @param groups the user's groupnames
716
   */
717
  private void handleExportAction(Hashtable params, 
718
    HttpServletResponse response, String user, String[] groups, String passWord)
719
  {
720
    // Output stream
721
    ServletOutputStream out = null;
722
    // Zip output stream
723
    ZipOutputStream zOut = null;
724
    DocumentImpl docImpls=null;
725
    DBQuery queryObj=null;
726
  
727
    String[] docs = new String[10];
728
    String docId = "";
729

    
730
    try
731
    {
732
      // read the params
733
      if (params.containsKey("docid")) 
734
      {
735
        docs = (String[])params.get("docid");
736
      }//if
737
      // Create a DBuery to handle export
738
      queryObj = new DBQuery(saxparser);
739
      // Get the docid
740
      docId=docs[0];
741
      // Make sure the client specify docid
742
      if (docId == null || docId.equals(""))
743
      {
744
        response.setContentType("text/xml"); //MIME type
745
        // Get a printwriter
746
        PrintWriter pw = response.getWriter();
747
        // Send back message
748
        pw.println("<?xml version=\"1.0\"?>");
749
        pw.println("<error>");
750
        pw.println("You didn't specify requested docid");
751
        pw.println("</error>");
752
        // Close printwriter 
753
        pw.close();
754
        return;
755
      }//if
756
      // Get output stream
757
      out = response.getOutputStream();
758
      response.setContentType("application/zip"); //MIME type
759
      zOut = new ZipOutputStream(out);
760
      zOut =queryObj.getZippedPackage(docId, out, user, groups, passWord);
761
      zOut.finish(); //terminate the zip file
762
      zOut.close();  //close the zip stream
763
      
764
    }//try 
765
    catch (Exception e)
766
    {
767
      try
768
      {
769
        response.setContentType("text/xml"); //MIME type
770
        // Send error message back
771
        if (out != null)
772
        {
773
            PrintWriter pw = new PrintWriter(out);
774
            pw.println("<?xml version=\"1.0\"?>");
775
            pw.println("<error>");
776
            pw.println(e.getMessage());
777
            pw.println("</error>");
778
            // Close printwriter
779
            pw.close();
780
            // Close output stream
781
            out.close();
782
        }//if
783
        // Close zip output stream 
784
        if ( zOut != null )
785
        {
786
          zOut.close();
787
        }//if
788
      }//try
789
      catch (IOException ioe)
790
      {
791
        MetaCatUtil.debugMessage("Problem with the servlet output " +
792
                           "in MetacatServlet.handleExportAction: " +
793
                           ioe.getMessage(), 30);
794
      }//catch
795

    
796
      MetaCatUtil.debugMessage("Error in MetacatServlet.handleExportAction: " +
797
                         e.getMessage(), 30);
798
      e.printStackTrace(System.out);
799
      
800
    }//catch
801
 
802
  }//handleExportAction
803
  
804
  // READ SECTION
805
  /** 
806
   * Handle the "read" request of metadata/data files from Metacat
807
   * or any files from Internet;
808
   * transformed metadata XML document into HTML presentation if requested;
809
   * zip files when more than one were requested.
810
   *
811
   * @param params the Hashtable of HTTP request parameters
812
   * @param response the HTTP response object linked to the client
813
   * @param user the username sent the request
814
   * @param groups the user's groupnames
815
   */
816
  private void handleReadAction(Hashtable params, HttpServletResponse response,
817
                                String user, String passWord, String[] groups) 
818
  {
819
    ServletOutputStream out = null;
820
    ZipOutputStream zout = null;
821
    PrintWriter pw = null;
822
    boolean zip = false;
823
    
824
    try {
825
      String[] docs = new String[0];
826
      String docid = "";
827
      String qformat = "";
828
      String abstrpath = null;
829
      
830
      // read the params
831
      if (params.containsKey("docid")) {
832
        docs = (String[])params.get("docid");
833
      }
834
      if (params.containsKey("qformat")) {
835
        qformat = ((String[])params.get("qformat"))[0];
836
      }
837
      if (params.containsKey("abstractpath")) {
838
        abstrpath = ((String[])params.get("abstractpath"))[0];
839
        if ( !abstrpath.equals("") && (abstrpath != null) ) {
840
          viewAbstract(response, abstrpath, docs[0]);
841
          return;
842
        }
843
      }
844
      if ( (docs.length > 1) || qformat.equals("zip") ) {
845
        zip = true;
846
        out = response.getOutputStream();
847
        response.setContentType("application/zip"); //MIME type
848
        zout = new ZipOutputStream(out);
849
      }
850
      // go through the list of docs to read
851
      for (int i=0; i < docs.length; i++ ) {
852
        try {
853

    
854
          URL murl = new URL(docs[i]);
855
          Hashtable murlQueryStr = util.parseQuery(murl.getQuery());
856
          // case docid="http://.../?docid=aaa" 
857
          // or docid="metacat://.../?docid=bbb"
858
          if (murlQueryStr.containsKey("docid")) {
859
            // get only docid, eliminate the rest
860
            docid = (String)murlQueryStr.get("docid");
861
            if ( zip ) {
862
              addDocToZip(docid, zout, user, groups);
863
            } else {
864
              readFromMetacat(response, docid, qformat, abstrpath,
865
                              user, groups, zip, zout);
866
            }
867

    
868
          // case docid="http://.../filename"
869
          } else {
870
            docid = docs[i];
871
            if ( zip ) {
872
              addDocToZip(docid, zout, user, groups);
873
            } else {
874
              readFromURLConnection(response, docid);
875
            }
876
          }
877

    
878
        // case docid="ccc"
879
        } catch (MalformedURLException mue) {
880
          docid = docs[i];
881
          if ( zip ) {
882
            addDocToZip(docid, zout, user, groups);
883
          } else {
884
            readFromMetacat(response, docid, qformat, abstrpath,
885
                            user, groups, zip, zout);
886
          }
887
        }
888
        
889
      } /* end for */
890
      
891
      if ( zip ) {
892
        zout.finish(); //terminate the zip file
893
        zout.close();  //close the zip stream
894
      }
895
      
896
  
897
    }
898
    // To handle doc not found exception
899
    catch (McdbDocNotFoundException notFoundE)
900
    {
901
      // the docid which didn't be found
902
      String notFoundDocId = notFoundE.getUnfoundDocId();
903
      String notFoundRevision = notFoundE.getUnfoundRevision();
904
      MetaCatUtil.debugMessage("Missed id: "+ notFoundDocId, 30);
905
      MetaCatUtil.debugMessage("Missed rev: "+ notFoundRevision, 30);
906
      try
907
      {
908
        // read docid from remote server
909
        readFromRemoteMetaCat(response, notFoundDocId, notFoundRevision,
910
                                              user, passWord, out, zip, zout);
911
        // Close zout outputstream
912
        if ( zout != null)
913
        {
914
          zout.close();
915
        }
916
        // close output stream
917
        if (out != null)
918
        {
919
          out.close();
920
        }
921
        
922
      }//try
923
      catch ( Exception exc)
924
      {
925
        MetaCatUtil.debugMessage("Erorr in MetacatServlet.hanldReadAction: "+
926
                                      exc.getMessage(), 30);
927
        try
928
        {
929
          if (out != null) 
930
          {
931
            response.setContentType("text/xml");
932
            // Send back error message by printWriter
933
            pw = new PrintWriter(out);
934
            pw.println("<?xml version=\"1.0\"?>");
935
            pw.println("<error>");
936
            pw.println(notFoundE.getMessage());
937
            pw.println("</error>");
938
            pw.close();
939
            out.close();
940
            
941
          }
942
          else
943
          {
944
           response.setContentType("text/xml"); //MIME type
945
           // Send back error message if out = null
946
           if (pw == null)
947
           {
948
             // If pw is null, open the respnose
949
            pw = response.getWriter();
950
           }
951
           pw.println("<?xml version=\"1.0\"?>");
952
           pw.println("<error>");
953
           pw.println(notFoundE.getMessage());
954
           pw.println("</error>");
955
           pw.close();
956
        }
957
        // close zout
958
        if ( zout != null ) 
959
        { 
960
          zout.close(); 
961
        }
962
        }//try
963
        catch (IOException ie)
964
        {
965
          MetaCatUtil.debugMessage("Problem with the servlet output " +
966
                           "in MetacatServlet.handleReadAction: " +
967
                           ie.getMessage(), 30);
968
        }//cathch
969
      }//catch
970
    }// catch McdbDocNotFoundException
971
    catch (Exception e) 
972
    {
973
      try {
974
       
975
        if (out != null) {
976
            response.setContentType("text/xml"); //MIME type
977
            pw = new PrintWriter(out);
978
            pw.println("<?xml version=\"1.0\"?>");
979
            pw.println("<error>");
980
            pw.println(e.getMessage());
981
            pw.println("</error>");
982
            pw.close();
983
            out.close();
984
        }
985
        else
986
        {
987
           response.setContentType("text/xml"); //MIME type
988
           // Send back error message if out = null
989
           if ( pw == null)
990
           {
991
            pw = response.getWriter();
992
           }
993
           pw.println("<?xml version=\"1.0\"?>");
994
           pw.println("<error>");
995
           pw.println(e.getMessage());
996
           pw.println("</error>");
997
           pw.close();
998
          
999
        }
1000
        // Close zip output stream
1001
        if ( zout != null ) { zout.close(); }
1002
        
1003
      } catch (IOException ioe) {
1004
        MetaCatUtil.debugMessage("Problem with the servlet output " +
1005
                           "in MetacatServlet.handleReadAction: " +
1006
                           ioe.getMessage(), 30);
1007
        ioe.printStackTrace(System.out);
1008
        
1009
      }
1010

    
1011
      System.out.println("Error in MetacatServlet.handleReadAction: " +
1012
                         e.getMessage());
1013
      e.printStackTrace(System.out);
1014
    }
1015
    
1016
  }
1017
  
1018
  // read metadata or data from Metacat
1019
  private void readFromMetacat(HttpServletResponse response, String docid,
1020
                               String qformat, String abstrpath, String user,
1021
                             String[] groups, boolean zip, ZipOutputStream zout)
1022
               throws ClassNotFoundException, IOException, SQLException, 
1023
                      McdbException, Exception
1024
  {
1025
   
1026
    try {
1027
     
1028
      
1029
      DocumentImpl doc = new DocumentImpl(docid);
1030
    
1031
      //check the permission for read
1032
      if (!doc.hasReadPermission(user, groups, docid))
1033
      {
1034
        Exception e = new Exception("User " + user + " does not have permission"
1035
                       +" to read the document with the docid " + docid);
1036
    
1037
        throw e;
1038
      }
1039
     
1040
      if ( doc.getRootNodeID() == 0 ) {
1041
        // this is data file
1042
        String filepath = util.getOption("datafilepath");
1043
        if(!filepath.endsWith("/")) {
1044
          filepath += "/";
1045
        }
1046
        String filename = filepath + docid;
1047
        FileInputStream fin = null;
1048
        fin = new FileInputStream(filename);
1049
        
1050
        //MIME type
1051
        String contentType = getServletContext().getMimeType(filename);
1052
        if (contentType == null) {
1053
          if (filename.endsWith(".xml")) {
1054
            contentType="text/xml";
1055
          } else if (filename.endsWith(".css")) {
1056
            contentType="text/css";
1057
          } else if (filename.endsWith(".dtd")) {
1058
            contentType="text/plain";
1059
          } else if (filename.endsWith(".xsd")) {
1060
            contentType="text/xml";
1061
          } else if (filename.endsWith("/")) {
1062
            contentType="text/html";
1063
          } else {
1064
            File f = new File(filename);
1065
            if ( f.isDirectory() ) {
1066
              contentType="text/html";
1067
            } else {
1068
              contentType="application/octet-stream";
1069
            }
1070
          }
1071
        }
1072
        response.setContentType(contentType);
1073
        // if we decide to use "application/octet-stream" for all data returns
1074
        // response.setContentType("application/octet-stream");
1075
       
1076
        try {
1077
          
1078
          ServletOutputStream out = response.getOutputStream(); 
1079
          byte[] buf = new byte[4 * 1024]; // 4K buffer
1080
          int b = fin.read(buf);
1081
          while (b != -1) {
1082
            out.write(buf, 0, b);
1083
            b = fin.read(buf);
1084
          }
1085
        } finally {
1086
          if (fin != null) fin.close();
1087
        }
1088

    
1089
      } else {
1090
        // this is metadata doc
1091
        if ( qformat.equals("xml") ) { 
1092
          
1093
          // set content type first
1094
          response.setContentType("text/xml");   //MIME type
1095
          PrintWriter out = response.getWriter();
1096
          doc.toXml(out);
1097
        } else {
1098
          response.setContentType("text/html");  //MIME type
1099
          PrintWriter out = response.getWriter();
1100
    
1101
          // Look up the document type
1102
          String doctype = doc.getDoctype();
1103
          // Transform the document to the new doctype
1104
          DBTransform dbt = new DBTransform();
1105
          dbt.transformXMLDocument(doc.toString(),
1106
                                   doctype,"-//W3C//HTML//EN", qformat, out);
1107
        }
1108
      
1109
      }
1110
    }
1111
    catch (Exception except) 
1112
    {
1113
      throw except;
1114
    
1115
    }
1116
    
1117
  }
1118
  
1119
  // read data from URLConnection
1120
  private void readFromURLConnection(HttpServletResponse response, String docid)
1121
               throws IOException, MalformedURLException
1122
  {
1123
    ServletOutputStream out = response.getOutputStream(); 
1124
    String contentType = getServletContext().getMimeType(docid); //MIME type
1125
    if (contentType == null) {
1126
      if (docid.endsWith(".xml")) {
1127
        contentType="text/xml";
1128
      } else if (docid.endsWith(".css")) {
1129
        contentType="text/css";
1130
      } else if (docid.endsWith(".dtd")) {
1131
        contentType="text/plain";
1132
      } else if (docid.endsWith(".xsd")) {
1133
        contentType="text/xml";
1134
      } else if (docid.endsWith("/")) {
1135
        contentType="text/html";
1136
      } else {
1137
        File f = new File(docid);
1138
        if ( f.isDirectory() ) {
1139
          contentType="text/html";
1140
        } else {
1141
          contentType="application/octet-stream";
1142
        }
1143
      }
1144
    }
1145
    response.setContentType(contentType);
1146
    // if we decide to use "application/octet-stream" for all data returns
1147
    // response.setContentType("application/octet-stream");
1148

    
1149
    // this is http url
1150
    URL url = new URL(docid);
1151
    BufferedInputStream bis = null;
1152
    try {
1153
      bis = new BufferedInputStream(url.openStream());
1154
      byte[] buf = new byte[4 * 1024]; // 4K buffer
1155
      int b = bis.read(buf);
1156
      while (b != -1) {
1157
        out.write(buf, 0, b);
1158
        b = bis.read(buf);
1159
      }
1160
    } finally {
1161
      if (bis != null) bis.close();
1162
    }
1163
    
1164
  }
1165
  
1166
  // read file/doc and write to ZipOutputStream
1167
  private void addDocToZip(String docid, ZipOutputStream zout, 
1168
                              String user, String[] groups)
1169
               throws ClassNotFoundException, IOException, SQLException, 
1170
                      McdbException, Exception
1171
  {
1172
    byte[] bytestring = null;
1173
    ZipEntry zentry = null;
1174

    
1175
    try {
1176
      URL url = new URL(docid);
1177

    
1178
      // this http url; read from URLConnection; add to zip
1179
      zentry = new ZipEntry(docid);
1180
      zout.putNextEntry(zentry);
1181
      BufferedInputStream bis = null;
1182
      try {
1183
        bis = new BufferedInputStream(url.openStream());
1184
        byte[] buf = new byte[4 * 1024]; // 4K buffer
1185
        int b = bis.read(buf);
1186
        while(b != -1) {
1187
          zout.write(buf, 0, b);
1188
          b = bis.read(buf);
1189
        }
1190
      } finally {
1191
        if (bis != null) bis.close();
1192
      }
1193
      zout.closeEntry();
1194

    
1195
    } catch (MalformedURLException mue) {
1196
      
1197
      // this is metacat doc (data file or metadata doc)
1198
  
1199
      try {
1200
       
1201
        DocumentImpl doc = new DocumentImpl(docid);
1202
        
1203
        //check the permission for read
1204
        if (!doc.hasReadPermission(user, groups, docid))
1205
        {
1206
          Exception e = new Exception("User " + user + " does not have "
1207
                    +"permission to read the document with the docid " + docid);
1208
        
1209
          throw e;
1210
        } 
1211
        
1212
        if ( doc.getRootNodeID() == 0 ) {
1213
          // this is data file; add file to zip
1214
          String filepath = util.getOption("datafilepath");
1215
          if(!filepath.endsWith("/")) {
1216
            filepath += "/";
1217
          }
1218
          String filename = filepath + docid;
1219
          FileInputStream fin = null;
1220
          fin = new FileInputStream(filename);
1221
          try {
1222
            
1223
            zentry = new ZipEntry(docid);
1224
            zout.putNextEntry(zentry);
1225
            byte[] buf = new byte[4 * 1024]; // 4K buffer
1226
            int b = fin.read(buf);
1227
            while (b != -1) {
1228
              zout.write(buf, 0, b);
1229
              b = fin.read(buf);
1230
            }
1231
          } finally {
1232
            if (fin != null) fin.close();
1233
          }
1234
          zout.closeEntry();
1235

    
1236
        } else {
1237
          // this is metadata doc; add doc to zip
1238
          bytestring = doc.toString().getBytes();
1239
          zentry = new ZipEntry(docid + ".xml");
1240
          zentry.setSize(bytestring.length);
1241
          zout.putNextEntry(zentry);
1242
          zout.write(bytestring, 0, bytestring.length);
1243
          zout.closeEntry();
1244
        }
1245
      } catch (Exception except) {
1246
        throw except;
1247
       
1248
      }
1249
      
1250
    }
1251
      
1252
  }
1253
  
1254
  // view abstract within document
1255
  private void viewAbstract(HttpServletResponse response,
1256
                            String abstractpath, String docid)
1257
               throws ClassNotFoundException, IOException, SQLException,
1258
                      McdbException, Exception
1259
  {
1260
  
1261
    PrintWriter out =null;
1262
    try {
1263
   
1264
      response.setContentType("text/html");  //MIME type
1265
      out = response.getWriter();
1266
      Object[] abstracts = DBQuery.getNodeContent(abstractpath, docid);
1267
      out.println("<html><head><title>Abstract</title></head>");
1268
      out.println("<body bgcolor=\"white\"><h1>Abstract</h1>");
1269
      for (int i=0; i<abstracts.length; i++) {
1270
        out.println("<p>" + (String)abstracts[i] + "</p>");
1271
      }
1272
      out.println("</body></html>");
1273

    
1274
    } catch (Exception e) {
1275
       out.println("<?xml version=\"1.0\"?>");
1276
       out.println("<error>");
1277
       out.println(e.getMessage());
1278
       out.println("</error>");
1279
    
1280
   
1281
    }
1282
  }
1283
  /**
1284
   * If metacat couldn't find a data file or document locally, it will read this
1285
   * docid from its home server. This is for the replication feature
1286
   */
1287
  private void readFromRemoteMetaCat(HttpServletResponse response, String docid, 
1288
                     String rev, String user, String password, 
1289
                     ServletOutputStream out, boolean zip, ZipOutputStream zout)
1290
                        throws Exception
1291
 {
1292
   // Create a object of RemoteDocument, "" is for zipEntryPath
1293
   RemoteDocument remoteDoc = 
1294
                        new RemoteDocument (docid, rev,user, password, "");
1295
   String docType = remoteDoc.getDocType();
1296
   // Only read data file
1297
   if (docType.equals("BIN"))
1298
   {
1299
    // If it is zip format
1300
    if (zip)
1301
    {
1302
      remoteDoc.readDocumentFromRemoteServerByZip(zout);
1303
    }//if 
1304
    else
1305
    {
1306
      if (out == null)
1307
      {
1308
        out = response.getOutputStream();
1309
      }//if
1310
      response.setContentType("application/octet-stream");
1311
      remoteDoc.readDocumentFromRemoteServer(out);
1312
    }//else (not zip)
1313
   }//if doctype=bin 
1314
   else
1315
   {
1316
     throw new Exception("Docid: "+docid+"."+rev+" couldn't find");
1317
   }//else                 
1318
 }//readFromRemoteMetaCat
1319
  
1320
  // END OF READ SECTION
1321
    
1322
  // INSERT/UPDATE SECTION
1323
  /** 
1324
   * Handle the database putdocument request and write an XML document 
1325
   * to the database connection
1326
   */
1327
  private void handleInsertOrUpdateAction(PrintWriter out, Hashtable params, 
1328
               HttpServletResponse response, String user, String[] groups) {
1329

    
1330
    DBConnection dbConn = null;
1331
    int serialNumber = -1;
1332

    
1333
    try {
1334
      // Get the document indicated
1335
      String[] doctext = (String[])params.get("doctext");
1336

    
1337
      String pub = null;
1338
      if (params.containsKey("public")) {
1339
        pub = ((String[])params.get("public"))[0];
1340
      }
1341

    
1342
      StringReader dtd = null;
1343
      if (params.containsKey("dtdtext")) {
1344
        String[] dtdtext = (String[])params.get("dtdtext");
1345
        try {
1346
          if ( !dtdtext[0].equals("") ) {
1347
            dtd = new StringReader(dtdtext[0]);
1348
          }
1349
        } catch (NullPointerException npe) {}
1350
      }
1351
      
1352
      StringReader xml = null;
1353
      boolean validate = false;
1354
      try {
1355
        // look inside XML Document for <!DOCTYPE ... PUBLIC/SYSTEM ... > 
1356
        // in order to decide whether to use validation parser
1357
        validate = validateXML(doctext[0]);
1358
        xml = new StringReader(doctext[0]);
1359

    
1360
        String[] action = (String[])params.get("action");
1361
        String[] docid = (String[])params.get("docid");
1362
        String newdocid = null;
1363

    
1364
        String doAction = null;
1365
        if (action[0].equals("insert")) {
1366
          doAction = "INSERT";
1367
        } else if (action[0].equals("update")) {
1368
          doAction = "UPDATE";
1369
        }
1370
        
1371
        try 
1372
        {
1373
          // get a connection from the pool
1374
          dbConn=DBConnectionPool.
1375
                  getDBConnection("MetaCatServlet.handleInsertOrUpdateAction");
1376
          serialNumber=dbConn.getCheckOutSerialNumber();
1377
          
1378
          
1379
          // write the document to the database
1380
          try 
1381
          {
1382
            String accNumber = docid[0];
1383
            MetaCatUtil.debugMessage(""+ doAction + " " + accNumber +"...", 10);
1384
            if (accNumber.equals("")) 
1385
            {
1386
              accNumber = null;
1387
            }//if
1388
            newdocid = DocumentImpl.write(dbConn, xml, pub, dtd, doAction,
1389
                                          accNumber, user, groups, validate);
1390
            
1391
          }//try 
1392
          catch (NullPointerException npe) 
1393
          {
1394
            newdocid = DocumentImpl.write(dbConn, xml, pub, dtd, doAction,
1395
                                          null, user, groups, validate);
1396
          }//catch
1397
        }//try 
1398
        finally 
1399
        {
1400
          // Return db connection
1401
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1402
        }    
1403

    
1404
        // set content type and other response header fields first
1405
        response.setContentType("text/xml");
1406
        out.println("<?xml version=\"1.0\"?>");
1407
        out.println("<success>");
1408
        out.println("<docid>" + newdocid + "</docid>"); 
1409
        out.println("</success>");
1410

    
1411
      } catch (NullPointerException npe) {
1412
        response.setContentType("text/xml");
1413
        out.println("<?xml version=\"1.0\"?>");
1414
        out.println("<error>");
1415
        out.println(npe.getMessage()); 
1416
        out.println("</error>");
1417
      }
1418
    } catch (Exception e) {
1419
      response.setContentType("text/xml");
1420
      out.println("<?xml version=\"1.0\"?>");
1421
      out.println("<error>");
1422
      out.println(e.getMessage()); 
1423
      if (e instanceof SAXException) {
1424
        Exception e2 = ((SAXException)e).getException();
1425
        out.println("<error>");
1426
        try
1427
        {
1428
          out.println(e2.getMessage());
1429
        }
1430
        catch(NullPointerException npe)
1431
        {
1432
          out.println(e.getMessage());
1433
        }
1434
        out.println("</error>");
1435
      }
1436
      //e.printStackTrace(out);
1437
      out.println("</error>");
1438
    }
1439
  }
1440

    
1441
  /** 
1442
   * Parse XML Document to look for <!DOCTYPE ... PUBLIC/SYSTEM ... > 
1443
   * in order to decide whether to use validation parser
1444
   */
1445
  private static boolean validateXML(String xmltext) throws IOException {
1446
    
1447
    StringReader xmlreader = new StringReader(xmltext);
1448
    StringBuffer cbuff = new StringBuffer();
1449
    java.util.Stack st = new java.util.Stack();
1450
    boolean validate = false;
1451
    int c;
1452
    int inx;
1453
    
1454
    // read from the stream until find the keywords
1455
    while ( (st.empty() || st.size()<4) && ((c = xmlreader.read()) != -1) ) {
1456
      cbuff.append((char)c);
1457

    
1458
      // "<!DOCTYPE" keyword is found; put it in the stack
1459
      if ( (inx = cbuff.toString().indexOf("<!DOCTYPE")) != -1 ) {
1460
        cbuff = new StringBuffer();
1461
        st.push("<!DOCTYPE");
1462
      }
1463
      // "PUBLIC" keyword is found; put it in the stack
1464
      if ( (inx = cbuff.toString().indexOf("PUBLIC")) != -1 ) {
1465
        cbuff = new StringBuffer();
1466
        st.push("PUBLIC");
1467
      }
1468
      // "SYSTEM" keyword is found; put it in the stack
1469
      if ( (inx = cbuff.toString().indexOf("SYSTEM")) != -1 ) {
1470
        cbuff = new StringBuffer();
1471
        st.push("SYSTEM");
1472
      }
1473
      // ">" character is found; put it in the stack
1474
      // ">" is found twice: fisrt from <?xml ...?> 
1475
      // and second from <!DOCTYPE ... >
1476
      if ( (inx = cbuff.toString().indexOf(">")) != -1 ) {
1477
        cbuff = new StringBuffer();
1478
        st.push(">");
1479
      }
1480
    }
1481

    
1482
    // close the stream
1483
    xmlreader.close();
1484

    
1485
    // check the stack whether it contains the keywords:
1486
    // "<!DOCTYPE", "PUBLIC" or "SYSTEM", and ">" in this order
1487
    if ( st.size() == 4 ) {
1488
      if ( ((String)st.pop()).equals(">") &&
1489
           ( ((String)st.peek()).equals("PUBLIC") |
1490
             ((String)st.pop()).equals("SYSTEM") ) &&
1491
           ((String)st.pop()).equals("<!DOCTYPE") )  {
1492
        validate = true;
1493
      }
1494
    }
1495

    
1496
System.out.println("Validation is " + validate);
1497
    return validate;
1498
  }
1499
  // END OF INSERT/UPDATE SECTION
1500

    
1501
  // DELETE SECTION
1502
  /** 
1503
   * Handle the database delete request and delete an XML document 
1504
   * from the database connection
1505
   */
1506
  private void handleDeleteAction(PrintWriter out, Hashtable params, 
1507
               HttpServletResponse response, String user, String[] groups) {
1508

    
1509
    String[] docid = (String[])params.get("docid");
1510
  
1511
    // delete the document from the database
1512
    try {
1513
 
1514
                                      // NOTE -- NEED TO TEST HERE
1515
                                      // FOR EXISTENCE OF DOCID PARAM
1516
                                      // BEFORE ACCESSING ARRAY
1517
      try { 
1518
        DocumentImpl.delete(docid[0], user, groups);
1519
        response.setContentType("text/xml");
1520
        out.println("<?xml version=\"1.0\"?>");
1521
        out.println("<success>");
1522
        out.println("Document deleted."); 
1523
        out.println("</success>");
1524
      } catch (AccessionNumberException ane) {
1525
        response.setContentType("text/xml");
1526
        out.println("<?xml version=\"1.0\"?>");
1527
        out.println("<error>");
1528
        out.println("Error deleting document!!!");
1529
        out.println(ane.getMessage()); 
1530
        out.println("</error>");
1531
      }
1532
    } catch (Exception e) {
1533
      response.setContentType("text/xml");
1534
      out.println("<?xml version=\"1.0\"?>");
1535
      out.println("<error>");
1536
      out.println(e.getMessage()); 
1537
      out.println("</error>");
1538
    } 
1539
  }
1540
  // END OF DELETE SECTION
1541
  
1542
  // VALIDATE SECTION
1543
  /** 
1544
   * Handle the validation request and return the results to the requestor
1545
   */
1546
  private void handleValidateAction(PrintWriter out, Hashtable params) {
1547

    
1548
    // Get the document indicated
1549
    String valtext = null;
1550
    DBConnection dbConn = null;
1551
    int serialNumber = -1;
1552
    
1553
    try {
1554
      valtext = ((String[])params.get("valtext"))[0];
1555
    } catch (Exception nullpe) {
1556

    
1557
     
1558
      String docid = null;
1559
      try {
1560
        // Find the document id number
1561
        docid = ((String[])params.get("docid"))[0]; 
1562

    
1563
      
1564
        // Get the document indicated from the db
1565
        DocumentImpl xmldoc = new DocumentImpl(docid);
1566
        valtext = xmldoc.toString();
1567

    
1568
      } catch (NullPointerException npe) {
1569
        
1570
        out.println("<error>Error getting document ID: " + docid + "</error>");
1571
        //if ( conn != null ) { util.returnConnection(conn); }
1572
        return;
1573
      } catch (Exception e) {
1574
       
1575
        out.println(e.getMessage()); 
1576
      } 
1577
    }
1578

    
1579
  
1580
    try {
1581
      // get a connection from the pool
1582
      dbConn=DBConnectionPool.
1583
                  getDBConnection("MetaCatServlet.handleValidateAction");
1584
      serialNumber=dbConn.getCheckOutSerialNumber();
1585
      DBValidate valobj = new DBValidate(saxparser,dbConn);
1586
      boolean valid = valobj.validateString(valtext);
1587

    
1588
      // set content type and other response header fields first
1589
      
1590
      out.println(valobj.returnErrors());
1591

    
1592
    } catch (NullPointerException npe2) {
1593
      // set content type and other response header fields first
1594
      
1595
      out.println("<error>Error validating document.</error>"); 
1596
    } catch (Exception e) {
1597
     
1598
      out.println(e.getMessage()); 
1599
    } finally {
1600
      // Return db connection
1601
      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1602
    }  
1603
  }
1604
  // END OF VALIDATE SECTION
1605
 
1606
  // OTHER ACTION HANDLERS
1607
  
1608
  /**
1609
   * Handle "getrevsionanddoctype" action
1610
   * Given a docid, return it's current revision and doctype from data base
1611
   * The output is String look like "rev;doctype"
1612
   */
1613
  private void handleGetRevisionAndDocTypeAction(PrintWriter out, 
1614
                                                              Hashtable params)
1615
  {
1616
    // To store doc parameter
1617
    String [] docs = new String[10];
1618
    // Store a single doc id
1619
    String givenDocId = null;
1620
    // Get docid from parameters
1621
    if (params.containsKey("docid")) 
1622
    {
1623
      docs = (String[])params.get("docid");
1624
    }
1625
    // Get first docid form string array
1626
    givenDocId = docs[0];
1627
   
1628
    try 
1629
    {
1630
      // Make sure there is a docid
1631
      if (givenDocId == null || givenDocId.equals(""))
1632
      {
1633
        throw new Exception("User didn't specify docid!");
1634
      }//if
1635
      
1636
      // Create a DBUtil object
1637
      DBUtil dbutil = new DBUtil();
1638
      // Get a rev and doctype
1639
      String revAndDocType = 
1640
                dbutil.getCurrentRevisionAndDocTypeForGivenDocument(givenDocId);
1641
      out.println(revAndDocType);
1642

    
1643
    }//try 
1644
    catch (Exception e) 
1645
    {
1646
      // Handle exception
1647
      out.println("<?xml version=\"1.0\"?>");
1648
      out.println("<error>");
1649
      out.println(e.getMessage());
1650
      out.println("</error>");
1651
    }//catch 
1652
    
1653
  }//handleGetRevisionAndDocTypeAction
1654
  
1655
  /** 
1656
   * Handle "getaccesscontrol" action.
1657
   * Read Access Control List from db connection in XML format
1658
   */
1659
  private void handleGetAccessControlAction(PrintWriter out, Hashtable params, 
1660
                                       HttpServletResponse response, 
1661
                                       String username, String[] groupnames) {
1662

    
1663
    DBConnection dbConn = null;
1664
    int serialNumber = -1;
1665
    String docid = ((String[])params.get("docid"))[0];
1666
    
1667
    try {
1668

    
1669
        // get connection from the pool
1670
        dbConn=DBConnectionPool.
1671
                 getDBConnection("MetaCatServlet.handleGetAccessControlAction");
1672
        serialNumber=dbConn.getCheckOutSerialNumber();
1673
        AccessControlList aclobj = new AccessControlList(dbConn);
1674
        String acltext = aclobj.getACL(docid, username, groupnames);
1675
        out.println(acltext);
1676

    
1677
    } catch (Exception e) {
1678
      out.println("<?xml version=\"1.0\"?>");
1679
      out.println("<error>");
1680
      out.println(e.getMessage());
1681
      out.println("</error>");
1682
    } finally {
1683
      // Retrun db connection to pool
1684
      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1685
    }  
1686
    
1687
  }
1688

    
1689
  /** 
1690
   * Handle the "getprincipals" action.
1691
   * Read all principals from authentication scheme in XML format
1692
   */
1693
  private void handleGetPrincipalsAction(PrintWriter out, String user,
1694
                                         String password) {
1695

    
1696
   
1697
    try {
1698

    
1699
        
1700
        AuthSession auth = new AuthSession();
1701
        String principals = auth.getPrincipals(user, password);
1702
        out.println(principals);
1703

    
1704
    } catch (Exception e) {
1705
      out.println("<?xml version=\"1.0\"?>");
1706
      out.println("<error>");
1707
      out.println(e.getMessage());
1708
      out.println("</error>");
1709
    } 
1710
    
1711
  }
1712

    
1713
  /** 
1714
   * Handle "getdoctypes" action.
1715
   * Read all doctypes from db connection in XML format
1716
   */
1717
  private void handleGetDoctypesAction(PrintWriter out, Hashtable params, 
1718
                                       HttpServletResponse response) {
1719

    
1720
   
1721
    try {
1722

    
1723
     
1724
        DBUtil dbutil = new DBUtil();
1725
        String doctypes = dbutil.readDoctypes();
1726
        out.println(doctypes);
1727

    
1728
    } catch (Exception e) {
1729
      out.println("<?xml version=\"1.0\"?>");
1730
      out.println("<error>");
1731
      out.println(e.getMessage());
1732
      out.println("</error>");
1733
    } 
1734
    
1735
  }
1736

    
1737
  /** 
1738
   * Handle the "getdtdschema" action.
1739
   * Read DTD or Schema file for a given doctype from Metacat catalog system
1740
   */
1741
  private void handleGetDTDSchemaAction(PrintWriter out, Hashtable params,
1742
                                        HttpServletResponse response) {
1743

    
1744
   
1745
    String doctype = null;
1746
    String[] doctypeArr = (String[])params.get("doctype");
1747

    
1748
    // get only the first doctype specified in the list of doctypes
1749
    // it could be done for all doctypes in that list
1750
    if (doctypeArr != null) {
1751
        doctype = ((String[])params.get("doctype"))[0]; 
1752
    }
1753

    
1754
    try {
1755

    
1756
       
1757
        DBUtil dbutil = new DBUtil();
1758
        String dtdschema = dbutil.readDTDSchema(doctype);
1759
        out.println(dtdschema);
1760

    
1761
    } catch (Exception e) {
1762
      out.println("<?xml version=\"1.0\"?>");
1763
      out.println("<error>");
1764
      out.println(e.getMessage());
1765
      out.println("</error>");
1766
    } 
1767
    
1768
  }
1769

    
1770
  /** 
1771
   * Handle the "getdataguide" action.
1772
   * Read Data Guide for a given doctype from db connection in XML format
1773
   */
1774
  private void handleGetDataGuideAction(PrintWriter out, Hashtable params, 
1775
                                        HttpServletResponse response) {
1776

    
1777
  
1778
    String doctype = null;
1779
    String[] doctypeArr = (String[])params.get("doctype");
1780

    
1781
    // get only the first doctype specified in the list of doctypes
1782
    // it could be done for all doctypes in that list
1783
    if (doctypeArr != null) {
1784
        doctype = ((String[])params.get("doctype"))[0]; 
1785
    }
1786

    
1787
    try {
1788

    
1789
  
1790
        DBUtil dbutil = new DBUtil();
1791
        String dataguide = dbutil.readDataGuide(doctype);
1792
        out.println(dataguide);
1793

    
1794
    } catch (Exception e) {
1795
      out.println("<?xml version=\"1.0\"?>");
1796
      out.println("<error>");
1797
      out.println(e.getMessage());
1798
      out.println("</error>");
1799
    }
1800
    
1801
  }
1802

    
1803
  /** 
1804
   * Handle the "getlastdocid" action.
1805
   * Get the latest docid with rev number from db connection in XML format
1806
   */
1807
  private void handleGetMaxDocidAction(PrintWriter out, Hashtable params, 
1808
                                        HttpServletResponse response) {
1809

    
1810

    
1811
    String scope = ((String[])params.get("scope"))[0];
1812
    if (scope == null) {
1813
        scope = ((String[])params.get("username"))[0];
1814
    }
1815

    
1816
    try {
1817

    
1818
       
1819
        DBUtil dbutil = new DBUtil();
1820
        String lastDocid = dbutil.getMaxDocid(scope);
1821
        out.println("<?xml version=\"1.0\"?>");
1822
        out.println("<lastDocid>");
1823
        out.println("  <scope>" + scope + "</scope>");
1824
        out.println("  <docid>" + lastDocid + "</docid>");
1825
        out.println("</lastDocid>");
1826

    
1827
    } catch (Exception e) {
1828
      out.println("<?xml version=\"1.0\"?>");
1829
      out.println("<error>");
1830
      out.println(e.getMessage());
1831
      out.println("</error>");
1832
    }
1833
    
1834
  }
1835

    
1836
  /** 
1837
   * Handle documents passed to metacat that are encoded using the 
1838
   * "multipart/form-data" mime type.  This is typically used for uploading
1839
   * data files which may be binary and large.
1840
   */
1841
  private void handleMultipartForm(HttpServletRequest request,
1842
                                   HttpServletResponse response) 
1843
  {
1844
    PrintWriter out = null;
1845
    String action = null;
1846

    
1847
    // Parse the multipart form, and save the parameters in a Hashtable and
1848
    // save the FileParts in a hashtable
1849

    
1850
    Hashtable params = new Hashtable();
1851
    Hashtable fileList = new Hashtable();
1852

    
1853
    try {
1854
      // MBJ: need to put filesize limit in Metacat config (metacat.properties)
1855
      MultipartParser mp = new MultipartParser(request, 200*1024*1024); //200MB
1856
      Part part;
1857
      while ((part = mp.readNextPart()) != null) {
1858
        String name = part.getName();
1859

    
1860
        if (part.isParam()) {
1861
          // it's a parameter part
1862
          ParamPart paramPart = (ParamPart) part;
1863
          String value = paramPart.getStringValue();
1864
          params.put(name, value);
1865
          if (name.equals("action")) {
1866
            action = value;
1867
          }
1868
        } else if (part.isFile()) {
1869
          // it's a file part
1870
          FilePart filePart = (FilePart) part;
1871
          fileList.put(name, filePart);
1872

    
1873
          // Stop once the first file part is found, otherwise going onto the
1874
          // next part prevents access to the file contents.  So...for upload
1875
          // to work, the datafile must be the last part
1876
          break;
1877
        }
1878
      }
1879
    } catch (IOException ioe) {
1880
      try {
1881
        out = response.getWriter();
1882
      } catch (IOException ioe2) {
1883
        System.err.println("Fatal Error: couldn't get response output stream.");
1884
      }
1885
      out.println("<?xml version=\"1.0\"?>");
1886
      out.println("<error>");
1887
      out.println("Error: problem reading multipart data.");
1888
      out.println("</error>");
1889
    }
1890

    
1891
    // Get the session information
1892
    String username = null;
1893
    String password = null;
1894
    String[] groupnames = null;
1895
    String sess_id = null;
1896

    
1897
    // be aware of session expiration on every request  
1898
    HttpSession sess = request.getSession(true);
1899
    if (sess.isNew()) {
1900
      // session expired or has not been stored b/w user requests
1901
      username = "public";
1902
      sess.setAttribute("username", username);
1903
    } else {
1904
      username = (String)sess.getAttribute("username");
1905
      password = (String)sess.getAttribute("password");
1906
      groupnames = (String[])sess.getAttribute("groupnames");
1907
      try {
1908
        sess_id = (String)sess.getId();
1909
      } catch(IllegalStateException ise) {
1910
        System.out.println("error in  handleMultipartForm: this shouldn't " +
1911
                           "happen: the session should be valid: " + 
1912
                           ise.getMessage());
1913
      }
1914
    }  
1915
    
1916
    // Get the out stream
1917
    try {
1918
          out = response.getWriter();
1919
        } catch (IOException ioe2) {
1920
          util.debugMessage("Fatal Error: couldn't get response "+
1921
                                                              "output stream.");
1922
        }
1923
    
1924
    if ( action.equals("upload")) {
1925
      if (username != null &&  !username.equals("public")) {
1926
        handleUploadAction(request, out, params, fileList, 
1927
                           username, groupnames);
1928
      } else {
1929
       
1930
        out.println("<?xml version=\"1.0\"?>");
1931
        out.println("<error>");
1932
        out.println("Permission denied for " + action);
1933
        out.println("</error>");
1934
      }
1935
    } else {
1936
      /*try {
1937
        out = response.getWriter();
1938
      } catch (IOException ioe2) {
1939
        System.err.println("Fatal Error: couldn't get response output stream.");
1940
      }*/
1941
      out.println("<?xml version=\"1.0\"?>");
1942
      out.println("<error>");
1943
      out.println("Error: action not registered.  Please report this error.");
1944
      out.println("</error>");
1945
    }
1946
    out.close();
1947
  }
1948

    
1949
  /** 
1950
   * Handle the upload action by saving the attached file to disk and 
1951
   * registering it in the Metacat db
1952
   */
1953
  private void handleUploadAction(HttpServletRequest request,
1954
                                  PrintWriter out, 
1955
                                  Hashtable params, Hashtable fileList, 
1956
                                  String username, String[] groupnames)
1957
  {
1958
    //PrintWriter out = null;
1959
    //Connection conn = null;
1960
    String action = null;
1961
    String docid = null;
1962
  
1963
    /*response.setContentType("text/xml");
1964
    try 
1965
    {
1966
      out = response.getWriter();
1967
    } 
1968
    catch (IOException ioe2) 
1969
    {
1970
      System.err.println("Fatal Error: couldn't get response output stream.");
1971
    }*/
1972

    
1973
    if (params.containsKey("docid")) 
1974
    {
1975
      docid = (String)params.get("docid");
1976
    }
1977

    
1978
    // Make sure we have a docid and datafile
1979
    if (docid != null && fileList.containsKey("datafile")) {
1980

    
1981
      // Get a reference to the file part of the form
1982
      FilePart filePart = (FilePart)fileList.get("datafile");
1983
      String fileName = filePart.getFileName();
1984
      MetaCatUtil.debugMessage("Uploading filename: " + fileName, 10);
1985

    
1986
      // Check if the right file existed in the uploaded data
1987
      if (fileName != null) {
1988
  
1989
        try 
1990
        {
1991
           //MetaCatUtil.debugMessage("Upload datafile " + docid +"...", 10);
1992
           //If document get lock data file grant
1993
           if (DocumentImpl.getDataFileLockGrant(docid))
1994
           {
1995
              // register the file in the database (which generates an exception
1996
              //if the docid is not acceptable or other untoward things happen
1997
              DocumentImpl.registerDocument(fileName, "BIN", docid, username);
1998
    
1999
              // Save the data file to disk using "docid" as the name
2000
              dataDirectory.mkdirs();
2001
              File newFile = new File(dataDirectory, docid);
2002
              long size = filePart.writeTo(newFile);
2003
              
2004
              // Force replication this data file
2005
              // To data file, "insert" and update is same
2006
              // The fourth parameter is null. Because it is notification server 
2007
              // and this method is in MetaCatServerlet. It is original command, 
2008
              // not get force replication info from another metacat
2009
              ForceReplicationHandler frh = new ForceReplicationHandler
2010
                                                (docid, "insert", false, null);
2011
             
2012
              // set content type and other response header fields first
2013
              out.println("<?xml version=\"1.0\"?>");
2014
              out.println("<success>");
2015
              out.println("<docid>" + docid + "</docid>"); 
2016
              out.println("<size>" + size + "</size>"); 
2017
              out.println("</success>");
2018
          }//if
2019
    
2020
        } //try
2021
        catch (Exception e) 
2022
        {
2023
          out.println("<?xml version=\"1.0\"?>");
2024
          out.println("<error>");
2025
          out.println(e.getMessage()); 
2026
          out.println("</error>");
2027
        }
2028
       
2029
      }
2030
      else 
2031
      {
2032
        // the field did not contain a file
2033
        out.println("<?xml version=\"1.0\"?>");
2034
        out.println("<error>");
2035
        out.println("The uploaded data did not contain a valid file."); 
2036
        out.println("</error>");
2037
      }
2038
    } 
2039
    else 
2040
    {
2041
      // Error bcse docid missing or file missing
2042
      out.println("<?xml version=\"1.0\"?>");
2043
      out.println("<error>");
2044
      out.println("The uploaded data did not contain a valid docid " +
2045
                  "or valid file."); 
2046
      out.println("</error>");
2047
    }
2048
  }
2049
}
(34-34/48)