Project

General

Profile

Revision 2089

Added by Jing Tao over 16 years ago

Revise the handle query and squery method, not finished yet.

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
127 127
  public static final String SCHEMALOCATIONKEYWORD = ":schemaLocation";
128 128
  public static final String NONAMESPACELOCATION = ":noNamespaceSchemaLocation";
129 129
  public static final String EML2KEYWORD =":eml";
130
  public static final String XMLFORMAT = "xml";
130 131
  private static final String CONFIG_DIR  = "WEB-INF";
131 132
  private static final String CONFIG_NAME = "metacat.properties";
132 133

  
......
146 147
      Options options = null;
147 148
      try {
148 149
          options = Options.initialize(propertyFile);
149
          MetaCatUtil.debugMessage("Options configured: " + 
150
          MetaCatUtil.debugMessage("Options configured: " +
150 151
            options.getOption("configured"), 20);
151 152
      } catch (IOException ioe) {
152 153
        MetaCatUtil.debugMessage("Error in loading options: "
......
320 321
        }
321 322
        else
322 323
        {
323
          MetaCatUtil.debugMessage("in session is not new or " + 
324
          MetaCatUtil.debugMessage("in session is not new or " +
324 325
                                    " has sessionid parameter", 40);
325 326
          try
326 327
          {
......
330 331
              MetaCatUtil.debugMessage("in has sessionid " + sess_id, 40);
331 332
              if(sessionHash.containsKey(sess_id))
332 333
              {
333
                MetaCatUtil.debugMessage("find the id " + sess_id + 
334
                MetaCatUtil.debugMessage("find the id " + sess_id +
334 335
                                         " in hash table", 40);
335 336
                sess = (HttpSession)sessionHash.get(sess_id);
336 337
              }
......
341 342
              /*MetaCatUtil.debugMessage("in no sessionid parameter ", 40);
342 343
              sess_id = (String)sess.getId();
343 344
              MetaCatUtil.debugMessage("storing the session id "+ sess_id +
344
                  " which has username " + sess.getAttribute("username") + 
345
                  " which has username " + sess.getAttribute("username") +
345 346
                 " into session hash in handleGetOrPost method", 35);
346 347
              sessionHash.put(sess_id, sess);*/
347 348
            }
......
359 360
          password = (String)sess.getAttribute("password");
360 361
          groupnames = (String[])sess.getAttribute("groupnames");
361 362
        }
362
      
363

  
363 364
        //make user user username should be public
364 365
        if (username == null || (username.trim().equals("")))
365 366
        {
......
524 525
      return;
525 526
    }
526 527
    boolean isValid = sess.authenticate(request, un, pw);
527
    
528

  
528 529
    //if it is authernticate is true, store the session
529 530
    if (isValid)
530 531
    {
531 532
      HttpSession session = sess.getSessions();
532 533
      String id = session.getId();
533
      MetaCatUtil.debugMessage("Store session id " + id + 
534
      MetaCatUtil.debugMessage("Store session id " + id +
534 535
               "which has username" + session.getAttribute("username")+
535 536
               " into hash in login method", 35);
536 537
      sessionHash.put(id, session);
537 538
    }
538
    
539

  
539 540
    // format and transform the output
540 541
    if (qformat.equals("xml")) {
541 542
      response.setContentType("text/xml");
......
570 571
    // close the connection
571 572
    HttpSession sess = request.getSession(false);
572 573
    MetaCatUtil.debugMessage("After get session in logout request", 40);
573
    if (sess != null) 
574
    if (sess != null)
574 575
    {
575
     MetaCatUtil.debugMessage("The session id " + sess.getId() + 
576
     MetaCatUtil.debugMessage("The session id " + sess.getId() +
576 577
                              " will be invalidate in logout action", 30);
577
     MetaCatUtil.debugMessage("The session contains user " + 
578
     MetaCatUtil.debugMessage("The session contains user " +
578 579
                               sess.getAttribute("username") +
579 580
                               " will be invalidate in logout action", 30);
580
      sess.invalidate();  
581
      sess.invalidate();
581 582
    }
582 583

  
583 584
    // produce output
......
623 624
                 HttpServletResponse response, String user, String[] groups,
624 625
                 String sessionid)
625 626
  {
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 627
    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);
628
    DBQuery queryobj = new DBQuery(saxparser);
629
    queryobj.findDocuments(response, out, params, user, groups, sessionid);
630
    double outPutTime = System.currentTimeMillis()/1000;
631
    MetaCatUtil.debugMessage("total search time: "+(outPutTime-startTime), 30);
635 632

  
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 633
  }
653 634

  
654 635
   /**
......
665 646
  {
666 647
    //create the query and run it
667 648
    String xmlquery = DBQuery.createSQuery(params);
668
    Hashtable doclist = runQuery(xmlquery, user, groups);
669
    String qformat = ((String[])params.get("qformat"))[0];
670
    String resultdoc = null;
649
    String []queryArray = new String[1];
650
    queryArray[0] = xmlquery;
651
    params.put("query", queryArray);
652
    double startTime = System.currentTimeMillis()/1000;
653
    DBQuery queryobj = new DBQuery(saxparser);
654
    queryobj.findDocuments(response, out, params, user, groups, sessionid);
655
    double outPutTime = System.currentTimeMillis()/1000;
656
    MetaCatUtil.debugMessage("total search time: "+(outPutTime-startTime), 30);
671 657

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

  
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
    }
659
    //handleSQuery(out, params, response,user, groups, sessionid);
681 660
  }
682 661

  
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 662
  // END OF SQUERY & QUERY SECTION
825 663

  
826 664
 //Exoport section
......
1537 1375
    }
1538 1376

  
1539 1377
  }
1540
  
1378

  
1541 1379
  /**
1542 1380
   * If metacat couldn't find a data file or document locally, it will read this
1543 1381
   * docid from its home server. This is for the replication feature
......
1720 1558
   * Parse XML Document to look for <!DOCTYPE ... PUBLIC/SYSTEM ... >
1721 1559
   * in order to decide whether to use validation parser
1722 1560
   */
1723
  private static boolean needDTDValidation(StringReader xmlreader) throws 
1724
                                                             IOException 
1561
  private static boolean needDTDValidation(StringReader xmlreader) throws
1562
                                                             IOException
1725 1563
  {
1726 1564

  
1727
    
1565

  
1728 1566
    StringBuffer cbuff = new StringBuffer();
1729 1567
    java.util.Stack st = new java.util.Stack();
1730 1568
    boolean validate = false;
......
1821 1659

  
1822 1660
    if (targetLine != null)
1823 1661
    {
1824
      
1662

  
1825 1663
      int startIndex = targetLine.indexOf(SCHEMALOCATIONKEYWORD);
1826 1664
      int start = 1;
1827 1665
      int end   = 1;
......
1874 1712
    char secondPreviousCharacter ='?';
1875 1713
    char previousCharacter = '?';
1876 1714
    char currentCharacter = '?';
1877
    
1715

  
1878 1716
    while ( (currentCharacter = (char) xml.read()) != -1)
1879 1717
    {
1880 1718
      //in a comment
1881
      if (currentCharacter =='-' && previousCharacter == '-'  && 
1719
      if (currentCharacter =='-' && previousCharacter == '-'  &&
1882 1720
          secondPreviousCharacter =='!' && thirdPreviousCharacter == '<')
1883 1721
      {
1884 1722
        comment = true;
1885 1723
      }
1886 1724
      //out of comment
1887
      if (comment && currentCharacter == '>' && previousCharacter == '-' && 
1725
      if (comment && currentCharacter == '>' && previousCharacter == '-' &&
1888 1726
          secondPreviousCharacter =='-')
1889 1727
      {
1890 1728
         comment = false;
1891 1729
      }
1892
      
1730

  
1893 1731
      //this is not comment
1894 1732
      if (currentCharacter !='!' && previousCharacter == '<' && !comment)
1895 1733
      {
......
1907 1745
      thirdPreviousCharacter = secondPreviousCharacter;
1908 1746
      secondPreviousCharacter = previousCharacter;
1909 1747
      previousCharacter = currentCharacter;
1910
      
1748

  
1911 1749
    }
1912 1750
    secondLine = buffer.toString();
1913 1751
    MetaCatUtil.debugMessage("the second line string is: "+secondLine, 25);

Also available in: Unified diff