Project

General

Profile

« Previous | Next » 

Revision 341

Added by berkley about 24 years ago

broke up handleQueryAction into handleQuery, handleSQuery, runQuery and transformDocument

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
21 21
import java.io.FileInputStream;
22 22
import java.util.Enumeration;
23 23
import java.util.Hashtable;
24
import java.util.ResourceBundle;
24
import java.util.ResourceBundle; 
25 25
import java.util.PropertyResourceBundle;
26 26
import java.net.URL;
27 27
import java.net.MalformedURLException;
......
226 226
    } else if (action.equals("Logout")) {
227 227
      HttpSession sess = request.getSession(false);
228 228
      if (sess != null) { sess.invalidate();  }    
229
      response.sendRedirect("/xmltodb/login.html"); 
229
      response.sendRedirect("/xmltodb/lib/login.html"); 
230 230
    // aware of session expiration on every request  
231 231
    } else {    
232 232
      HttpSession sess = request.getSession(true);
233 233
      if (sess.isNew()) { 
234 234
        // session expired or has not been stored b/w user requests
235 235
        // redirect to default page for query only access
236
        response.sendRedirect("/xmltodb/sexpire.html"); 
236
        response.sendRedirect("/xmltodb/lib/sexpire.html"); 
237 237
      } 
238 238
    }    
239 239
// End of Jivka added
......
299 299
                    return;
300 300
                } else {
301 301
                    response.sendRedirect(
302
                    response.encodeRedirectUrl("/xmltodb/index.html"));
302
                    response.encodeRedirectUrl("/xmltodb/lib/index.html"));
303 303
                }    
304 304
            } catch ( java.io.IOException ioe) {
305 305
                sess.disconnect();            
......
323 323
                    out.println("</success>");
324 324
                } else {
325 325
                    response.sendRedirect(
326
                    response.encodeRedirectUrl("/xmltodb/metacat.html"));
326
                    response.encodeRedirectUrl("/xmltodb/lib/metacat.html"));
327 327
                }    
328 328
            } catch ( java.io.IOException ioe) {
329 329
                sess.disconnect();            
......
350 350
            out.println("<?xml version=\"1.0\"?>");
351 351
            out.println("<error>");
352 352
            out.println("SRB Connection failed. " + re.getMessage());
353
            out.println("</error>");
353
            out.println("</error>"); 
354 354
    }        
355 355
  }
356 356

  
357
  /**
358
    *Create the squery xml and return it as a String.
359
    * @param params is the Hashtable of parameters that should be included
360
    * in the squery.
361
    */
362
  private String handleSQuery(Hashtable params)
363
  {
364
    //create the squery and return it to be processed
365
    String doctype = null;
366
    String[] doctypeArr = null;
367
    doctypeArr = (String[])params.get("doctype");
368
    doctype = null;
369
    if (doctypeArr != null) 
370
    {
371
      doctype = ((String[])params.get("doctype"))[0]; 
372
    }
373
    else
374
    {
375
      doctype="ANY"; 
376
    }
377
    return DBQuery.createSQuery(params, doctype);
378
  }
379
  
380
   /**
381
    *Create the query xml and return it as a String.
382
    * @param query is the free text query parameter returned through the CGI.
383
    * @param doctype is the doctype parameter returned through the CGI. 
384
    * If no doctype filter is required, set it to null or "".
385
    */
386
  private String handleQuery(Hashtable params)
387
  {
388
    String doctype=null;
389
    String[] doctypeArr=null;
390
    String query = ((String[])params.get("query"))[0]; 
391
    doctypeArr = (String[])params.get("doctype");
392
    doctype = null;
393
    if (doctypeArr != null) 
394
    {
395
      doctype = ((String[])params.get("doctype"))[0]; 
396
    }
397
    else
398
    {
399
      doctype="ANY"; 
400
    }
401
    return DBQuery.createQuery(query,doctype);
402
  }
403
  
404
  /**
405
    * Run the query and return a hashtable of results.
406
    */
407
  private Hashtable runQuery(Reader xmlquery)
408
  {
409
    Hashtable doclist=null;
410
    Connection conn = null;
411
    try
412
    {
413
        conn = util.getConnection();
414
        DBQuery queryobj = new DBQuery(conn, saxparser);
415
        doclist = queryobj.findDocuments(xmlquery);
416
        util.returnConnection(conn);
417
        return doclist;
418
    } 
419
    catch (Exception e) 
420
    {
421
      if (conn != null) 
422
      {
423
        util.returnConnection(conn); 
424
      }
425
      util.debugMessage("Error in runQuery: " + e.getMessage());
426
      doclist = null;
427
      return doclist;
428
    }    
429
  }
430
  
431
  private void transformDocument(Hashtable doclist, String qformat, 
432
                                 String xmlquery, PrintWriter out,
433
                                 HttpServletResponse response)
434
  {
435
    // Create a buffer to hold the xml result
436
    StringBuffer resultset = new StringBuffer();
437
 
438
    // Print the resulting root nodes
439
    String docid = null;
440
    String document = null;
441
    resultset.append("<?xml version=\"1.0\"?>\n");
442
    resultset.append("<resultset>\n");
443
    resultset.append("  <query>" + xmlquery + "</query>");   
444
    Enumeration doclistkeys = doclist.keys(); 
445
    while (doclistkeys.hasMoreElements()) 
446
    {
447
      docid = (String)doclistkeys.nextElement();
448
      document = (String)doclist.get(docid);
449
      resultset.append("  <document>" + document + "</document>");
450
    }
451
    resultset.append("</resultset>");
452

  
453
    if(qformat.equals("xml")) 
454
    {
455
      // set content type and other response header fields first
456
      response.setContentType("text/xml");
457
      out.println(resultset.toString());
458
    } 
459
    else if(qformat.equals("html")) 
460
    {
461
      // set content type and other response header fields first
462
      response.setContentType("text/html");
463
      XMLDocumentFragment htmldoc = null;
464
      try 
465
      {
466
        XSLStylesheet style = new XSLStylesheet(
467
                              new URL(resultStyleURL), null);
468
        htmldoc = (new XSLProcessor()).processXSL(style, 
469
                  (Reader)(new StringReader(resultset.toString())),null);
470
        htmldoc.print(out);
471
      } 
472
      catch (Exception e) 
473
      {
474
        out.println("Error transforming document:\n" + e.getMessage());
475
      }
476
    }
477
  }
478
                              
357 479
  /** 
358 480
   * Handle the database query request and return a result set, possibly
359 481
   * transformed from XML into HTML
360 482
   */
361 483
  private void handleQueryAction(PrintWriter out, Hashtable params, 
362
               HttpServletResponse response) {
484
                                 HttpServletResponse response) 
485
  {
363 486
      String action = ((String[])params.get("action"))[0];
364
      String query = ((String[])params.get("query"))[0]; 
365 487
      Hashtable doclist = null;
366 488
      String[] doctypeArr = null;
367 489
      String doctype = null;
368 490
      Reader xmlquery = null;
369 491
      Connection conn = null;
370 492

  
371
      // Run the query if it is a structured query
372
      // or, if it is a free-text, simple query,
373
      // format it first as a structured query and then run it
374
      if (action.equals("query")) {
375
        doctypeArr = (String[])params.get("doctype");
376
        doctype = null;
377
        if (doctypeArr != null) {
378
          doctype = ((String[])params.get("doctype"))[0]; 
379
        }
380

  
381
        if (doctype != null) {
382
          xmlquery = new StringReader(DBQuery.createQuery(query,doctype));
383
        } else {
384
          xmlquery = new StringReader(DBQuery.createQuery(query));
385
        }
386
      } else if (action.equals("squery")) {
387
        xmlquery = new StringReader(query);
388
      } else {
389
        System.err.println("Error handling query -- illegal action value");
493
      if(action.equals("query"))
494
      {
495
        xmlquery = new StringReader(handleQuery(params)); 
390 496
      }
391

  
392
      try {
393

  
394
        conn = util.getConnection();
395
        DBQuery queryobj = new DBQuery(conn, saxparser);
396
        doclist = queryobj.findDocuments(xmlquery);
397
        util.returnConnection(conn);
398

  
399
      } catch (Exception e) {
400
        if ( conn != null ) { util.returnConnection(conn); }
401
        response.setContentType("text/html");
402
        out.println(e.getMessage());
403
        return;
404
      }    
405

  
406
      // Create a buffer to hold the xml result
407
      StringBuffer resultset = new StringBuffer();
408
 
409
      // Print the resulting root nodes
410
      String docid;
411
      String document = null;
412
      resultset.append("<?xml version=\"1.0\"?>\n");
413
      //resultset.append("<!DOCTYPE resultset PUBLIC " +
414
      //               "\"-//NCEAS//resultset//EN\" \"resultset.dtd\">\n");
415
      resultset.append("<resultset>\n");
416
  // following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
417
  //    resultset.append("  <query>" + query + "</query>");   
418
      Enumeration doclistkeys = doclist.keys(); 
419
      while (doclistkeys.hasMoreElements()) {
420
        docid = (String)doclistkeys.nextElement();
421
        document = (String)doclist.get(docid);
422
        resultset.append("  <document>" + document + "</document>");
497
      else if(action.equals("squery"))
498
      {
499
        xmlquery = new StringReader(handleSQuery(params)); 
423 500
      }
424
      resultset.append("</resultset>");
425

  
501
      
502
      doclist = runQuery(xmlquery);
503
      
426 504
      String qformat = ((String[])params.get("qformat"))[0]; 
427
      if (qformat.equals("xml")) {
428
        // set content type and other response header fields first
429
        response.setContentType("text/xml");
430
        out.println(resultset.toString());
431
      } else if (qformat.equals("html")) {
432
        // set content type and other response header fields first
433
        response.setContentType("text/html");
434
        //out.println("Converting to HTML...");
435
        XMLDocumentFragment htmldoc = null;
436
        try {
437
          XSLStylesheet style = new XSLStylesheet(
438
                                    new URL(resultStyleURL), null);
439
          htmldoc = (new XSLProcessor()).processXSL(style, 
440
                     (Reader)(new StringReader(resultset.toString())),null);
441
          htmldoc.print(out);
442
        } catch (Exception e) {
443
          out.println("Error transforming document:\n" + e.getMessage());
444
        }
445
      }
505
      transformDocument(doclist, qformat, xmlquery.toString(), out, response);
446 506
  }
447 507

  
448 508
  /** 
......
807 867

  
808 868
/**
809 869
 * '$Log$
870
 * 'Revision 1.61  2000/08/10 18:56:48  bojilova
871
 * 'added "anonymous" user connection
872
 * '
810 873
 * 'Revision 1.60  2000/08/09 00:39:47  jones
811 874
 * '-Reorganized xmltodb module to support new install process for the new
812 875
 * 'linux server (dev.nceas.ucsb.edu).  Added "build.sh" shell script that

Also available in: Unified diff