Project

General

Profile

« Previous | Next » 

Revision 1511

Added by Jing Tao almost 22 years ago

Add code to handle checking if user updated a subtree which he doesn't have permission.

View differences:

src/edu/ucsb/nceas/metacat/EmlSAXHandler.java
72 72
   private Stack subTreeInfoStack = new Stack();
73 73
   private Vector subTreeList = new Vector();// store the final subtree
74 74
   private int inLineDataIndex = 1;
75
   private Hashtable unChangableSubtree;
75
   private Hashtable unChangableSubTreeHash;
76 76
   private Stack currentUnChangedableSubtreeNodeStack;
77 77
   private boolean startCriticalSubTree = false;
78
   private boolean firstElementForCriticalSubTree = false;
78
   private boolean firstElementForCriticalSubTree = true;
79 79
   private String firstElementNameForCriticalSubTree;
80 80
 
81 81
 
......
112 112
     try
113 113
     {
114 114
       PermissionController control = new PermissionController(docid);
115
       unChangableSubtree = getUnchangableSubTree(control, user, groups);
115
       unChangableSubTreeHash = getUnchangableSubTree(control, user, groups);
116 116
     }
117 117
     catch (Exception e)
118 118
     {
......
188 188
     // This will happend if the element is mixted
189 189
     if (hitTextNode && parentNode != null)
190 190
     {
191
       //compare text node data
192
       if (startCriticalSubTree && !firstElementForCriticalSubTree)
193
       {
194
            NodeRecord node = null;
195
            try
196
            { 
197
              node = (NodeRecord)currentUnChangedableSubtreeNodeStack.pop();
198
            }
199
            catch (EmptyStackException ee)
200
            {
201
              MetaCatUtil.debugMessage("Node stack is empty for text data", 35);
202
              throw new SAXException(PERMISSIONERROR);
203
            }
204
                            
205
            if (!node.getNodeType().equals("TEXT") || 
206
               !(textBuffer.toString()).equals(node.getNodeData())) 
207
            { 
208
              MetaCatUtil.debugMessage("current node type from xml is TEXT in start element", 40);
209
              MetaCatUtil.debugMessage("node type from stack: " + 
210
                                        node.getNodeType(), 40);
211
              MetaCatUtil.debugMessage("current node data from xml is: " + 
212
                                        textBuffer.toString(), 40);
213
              MetaCatUtil.debugMessage("node data from stack: " +
214
                                        node.getNodeData(), 40);
215
              MetaCatUtil.debugMessage("node name from stack: " +
216
                                        node.getNodeName(), 40);
217
              MetaCatUtil.debugMessage("node is: "+ node.getNodeId(), 40);
218
              throw new SAXException(PERMISSIONERROR);
219
            }//if
220
        }//if
191 221
       // write the textbuffer into db for parent node.
192 222
        endNodeId = writeTextForDBSAXNode(textBuffer, parentNode);
193 223
        // rest hitTextNode
......
300 330
       nsuri = (String)namespaces.get(prefix);
301 331
       endNodeId = currentNode.setNamespace(prefix, nsuri, docid);
302 332
     }
303
     namespaces = null;
304
     namespaces = new Hashtable();
333
   
305 334

  
306 335
     // Add all of the attributes
307 336
     for (int i=0; i<atts.getLength(); i++) 
......
324 353
       }
325 354
       else if (attributeName !=null && attributeName.equals(ID))
326 355
       {
356
        
327 357
         //check unchangedable subtree hash if contains this subtree id
328
         if (unChangableSubtree.contains(attributeValue))
358
         if (unChangableSubTreeHash.containsKey(attributeValue))
329 359
         {
330
           // this subtree couldn't be changed by the user
360
           // this subtree couldn't be changed by the user and move it from hash
331 361
           SubTree currentUnChangedableSubtree = (SubTree)
332
                                        unChangableSubtree.get(attributeValue);
362
                                  unChangableSubTreeHash.remove(attributeValue);
333 363
           currentUnChangedableSubtreeNodeStack = currentUnChangedableSubtree.
334 364
                                                         getSubTreeNodeStack();
335 365
           startCriticalSubTree = true;
......
405 435
    {
406 436
      //store the element name
407 437
      firstElementNameForCriticalSubTree = qName;
438
      firstElementForCriticalSubTree = false;
408 439
    }//for first element
409 440
    else if (startCriticalSubTree)
410 441
    {
411 442
      //Get element subtree node stack (element node)
412
      NodeRecord node = (NodeRecord) currentUnChangedableSubtreeNodeStack.pop();
443
      NodeRecord elementNode = null;
444
      try
445
      {
446
        elementNode= (NodeRecord) currentUnChangedableSubtreeNodeStack.pop();
447
      }
448
      catch (EmptyStackException ee)
449
      {
450
        MetaCatUtil.debugMessage("Node stack is empty for element data", 35);
451
        throw new SAXException(PERMISSIONERROR);
452
      }
453
      
413 454
      // if this node is not element or local name not equal or name space not
414 455
      // equals, throw an exception
415
      if (!node.getNodeType().equals("ELEMENT") || 
416
          !localName.equals(node.getNodeName()) || 
417
          (uri != null && !uri.equals(node.getNodePrefix())))
456
      if (!elementNode.getNodeType().equals("ELEMENT") || 
457
          !localName.equals(elementNode.getNodeName()) || 
458
          (uri != null && !uri.equals(elementNode.getNodePrefix())))
418 459
      {
460
        MetaCatUtil.debugMessage("current node type from xml is ELEMENT", 40);
461
        MetaCatUtil.debugMessage("node type from stack: " + 
462
                                  elementNode.getNodeType(), 40);
463
        MetaCatUtil.debugMessage("node name from xml document: " + 
464
                                  localName, 40);
465
        MetaCatUtil.debugMessage("node name from stack: " +
466
                                  elementNode.getNodeName(), 40);
467
        MetaCatUtil.debugMessage("node data from stack: " +
468
                                  elementNode.getNodeData(), 40);
469
        MetaCatUtil.debugMessage("node is: "+ elementNode.getNodeId(), 40);
419 470
        throw new SAXException(PERMISSIONERROR);
420 471
      }
472
      
473
      //compare namespace
474
     Enumeration nameEn = namespaces.keys();
475
     while ( nameEn.hasMoreElements() ) 
476
     {
477
       //Get namespacke node stack (element node)
478
       NodeRecord nameNode = null;
479
       try
480
       { 
481
         nameNode = (NodeRecord) currentUnChangedableSubtreeNodeStack.pop();
482
       }
483
       catch (EmptyStackException ee)
484
       {
485
         MetaCatUtil.debugMessage("Node stack is empty for namespace data", 35);
486
         throw new SAXException(PERMISSIONERROR);
487
       }
488
       
489
       String prefixName = (String)nameEn.nextElement(); 
490
       String nameSpaceUri = (String)namespaces.get(prefixName);
491
       if (!nameNode.getNodeType().equals("NAMESPACE") || 
492
           !prefixName.equals(nameNode.getNodeName()) || 
493
           !nameSpaceUri.equals(nameNode.getNodeData()))
494
       { 
495
         MetaCatUtil.debugMessage("current node type from xml is NAMESPACE", 40);
496
         MetaCatUtil.debugMessage("node type from stack: " + 
497
                                  nameNode.getNodeType(), 40);
498
         MetaCatUtil.debugMessage("current node name from xml is: " + 
499
                                   prefixName, 40);
500
         MetaCatUtil.debugMessage("node name from stack: " +
501
                                  nameNode.getNodeName(), 40);
502
         MetaCatUtil.debugMessage("current node data from xml is: " + 
503
                                   nameSpaceUri, 40);
504
         MetaCatUtil.debugMessage("node data from stack: " +
505
                                  nameNode.getNodeData(), 40);
506
         MetaCatUtil.debugMessage("node is: "+ nameNode.getNodeId(), 40);
507
         throw new SAXException(PERMISSIONERROR);
508
       }
509
      
510
     }
511
      
421 512
      //compare attributes
422 513
      for (int i=0; i<atts.getLength(); i++)
423 514
      {
424
        NodeRecord attriNode = (NodeRecord) 
425
                                    currentUnChangedableSubtreeNodeStack.pop();
515
        NodeRecord attriNode = null;
516
        try
517
        {
518
          attriNode = (NodeRecord)currentUnChangedableSubtreeNodeStack.pop();
519
        }
520
        catch (EmptyStackException ee)
521
        {
522
          MetaCatUtil.debugMessage("Node stack is empty for attribute data", 35);
523
          throw new SAXException(PERMISSIONERROR);
524
        }
426 525
        String attributeName = atts.getQName(i);
427 526
        String attributeValue = atts.getValue(i);
428 527
        if (!attriNode.getNodeType().equals("ATTRIBUTE") || 
429 528
            !attributeName.equals(attriNode.getNodeName()) ||
430 529
            !attributeValue.equals(attriNode.getNodeData()))
431 530
        {
531
          MetaCatUtil.debugMessage("current node type from xml is ATTRIBUTE ", 40);
532
          MetaCatUtil.debugMessage("node type from stack: " + 
533
                                   attriNode.getNodeType(), 40);
534
          MetaCatUtil.debugMessage("current node name from xml is: " + 
535
                                    attributeName, 40);
536
          MetaCatUtil.debugMessage("node name from stack: " +
537
                                    attriNode.getNodeName(), 40);
538
          MetaCatUtil.debugMessage("current node data from xml is: " + 
539
                                    attributeValue, 40);
540
          MetaCatUtil.debugMessage("node data from stack: " +
541
                                    attriNode.getNodeData(), 40);
542
          MetaCatUtil.debugMessage("node is: "+ attriNode.getNodeId(), 40);
432 543
          throw new SAXException(PERMISSIONERROR);
433 544
        }
434
      }
545
      }//for
435 546
      
436
    }//  
437

  
547
    }
548
    // reset name space
549
    namespaces = null;
550
    namespaces = new Hashtable();
438 551
  }
439 552
  
440 553
   
......
448 561
     DBSAXNode currentNode = (DBSAXNode)nodeStack.pop();
449 562
     String currentTag = currentNode.getTagName();
450 563
     
564
     //end crtical subtree(user doesn't have permission to write)
565
     //When reach the first element and stack is empty
566
     if (localName.equals(firstElementNameForCriticalSubTree) && 
567
         currentUnChangedableSubtreeNodeStack.isEmpty()) 
568
     {
569
       startCriticalSubTree = false;
570
     }
571
     
572
     
451 573
    
452 574
     // If before the end element, the parser hit text nodes and store them
453 575
     // into the buffer, write the buffer to data base. The reason we put
......
506 628
       if (!localName.equals(INLINE))
507 629
       {
508 630
          MetaCatUtil.debugMessage("Write text into DB in End Element", 50);
631
          //compare whitespace if need
632
          if (startCriticalSubTree)
633
          {
634
            NodeRecord node = null;
635
            try
636
            {
637
              node = (NodeRecord)currentUnChangedableSubtreeNodeStack.pop();
638
            }
639
            catch (EmptyStackException ee)
640
            {
641
              MetaCatUtil.debugMessage("the stack is empty for text data", 32);
642
              throw new SAXException(PERMISSIONERROR);
643
            }
644
            if (!node.getNodeType().equals("TEXT") || 
645
               !(textBuffer.toString()).equals(node.getNodeData())) 
646
            { 
647
              MetaCatUtil.debugMessage("current node type from xml is TEXT in endElement", 40);
648
              MetaCatUtil.debugMessage("node type from stack: " + 
649
                                        node.getNodeType(), 40);
650
              MetaCatUtil.debugMessage("current node data from xml is: " + 
651
                                        textBuffer.toString(), 40);
652
              MetaCatUtil.debugMessage("node data from stack: " +
653
                                        node.getNodeData(), 40);
654
              MetaCatUtil.debugMessage("node name from stack: " + 
655
                                        node.getNodeName(), 40);
656
              MetaCatUtil.debugMessage("node is from stack: " + 
657
                                        node.getNodeId(), 40);
658
              throw new SAXException(PERMISSIONERROR);
659
            }//if
660
          }//if
509 661
          endNodeId = writeTextForDBSAXNode(textBuffer, currentNode);
510 662
       }
511 663
       else
......
606 758
     }
607 759
   }
608 760
   
761
   /**
762
    * SAX Handler that receives notification of comments in the DTD
763
    */
764
   public void comment(char[] ch, int start, int length) throws SAXException {
765
     MetaCatUtil.debugMessage("COMMENT", 50);
766
     if ( !processingDTD ) 
767
     {
768
       DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
769
       String str = new String(ch, start, length);
770
       
771
       //compare comment if need
772
       if (startCriticalSubTree)
773
       {
774
         NodeRecord node = null;
775
         try
776
         {
777
           node = (NodeRecord)currentUnChangedableSubtreeNodeStack.pop();
778
         }
779
         catch (EmptyStackException ee)
780
         {
781
           MetaCatUtil.debugMessage("the stack is empty for comment data", 32);
782
           throw new SAXException(PERMISSIONERROR);
783
         }
784
         if (!node.getNodeType().equals("COMMENT") || 
785
             !str.equals(node.getNodeData())) 
786
         { 
787
          MetaCatUtil.debugMessage("current node type from xml is COMMENT", 40);
788
          MetaCatUtil.debugMessage("node type from stack: " + 
789
                                    node.getNodeType(), 40);
790
          MetaCatUtil.debugMessage("current node data from xml is: " + 
791
                                    str, 40);
792
          MetaCatUtil.debugMessage("node data from stack: " +
793
                                    node.getNodeData(), 40);
794
          MetaCatUtil.debugMessage("node is from stack: " + 
795
                                        node.getNodeId(), 40);
796
          throw new SAXException(PERMISSIONERROR);
797
        }//if
798
       }//if
799
       
800
       endNodeId = currentNode.writeChildNodeToDB("COMMENT", null, str, docid);
801
     }
802
   }
803
   
804
      /**
805
    * SAX Handler that is called for each XML text node that is
806
    * Ignorable white space
807
    */
808
   public void ignorableWhitespace(char[] cbuf, int start, int len)
809
               throws SAXException 
810
  {
811
     // When validation is turned "on", white spaces are reported here
812
     // When validation is turned "off" white spaces are not reported here,
813
     // but through characters() callback
814
     MetaCatUtil.debugMessage("IGNORABLEWHITESPACE", 50);
815
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
816
     String data = null;
817
     int leftover = len;
818
     int offset = start;
819
     boolean moredata = true;
820

  
821
     // This loop deals with the case where there are more characters
822
     // than can fit in a single database text field (limit is
823
     // MAXDATACHARS).  If the text to be inserted exceeds MAXDATACHARS,
824
     // write a series of nodes that are MAXDATACHARS long, and then the
825
     // final node contains the remainder
826
     while (moredata) 
827
     {
828
       if (leftover > MAXDATACHARS) 
829
       {
830
         data = new String(cbuf, offset, MAXDATACHARS);
831
         leftover -= MAXDATACHARS;
832
         offset += MAXDATACHARS;
833
       } 
834
       else 
835
       {
836
         data = new String(cbuf, offset, leftover);
837
         moredata = false;
838
       }
839

  
840
       //compare whitespace if need
841
       if (startCriticalSubTree)
842
       {
843
         NodeRecord node = null;
844
         try
845
         {
846
           node = (NodeRecord)currentUnChangedableSubtreeNodeStack.pop();
847
         }
848
         catch (EmptyStackException ee)
849
         {
850
           MetaCatUtil.debugMessage("the stack is empty for whitespace data", 32);
851
           throw new SAXException(PERMISSIONERROR);
852
         }
853
         if (!node.getNodeType().equals("TEXT") || 
854
             !data.equals(node.getNodeData())) 
855
         { 
856
          MetaCatUtil.debugMessage("current node type from xml is WHITESPACE TEXT", 40);
857
          MetaCatUtil.debugMessage("node type from stack: " + 
858
                                    node.getNodeType(), 40);
859
          MetaCatUtil.debugMessage("current node data from xml is: " + 
860
                                    data, 40);
861
          MetaCatUtil.debugMessage("node data from stack: " +
862
                                    node.getNodeData(), 40);
863
          MetaCatUtil.debugMessage("node is from stack: " + 
864
                                        node.getNodeId(), 40);
865
          throw new SAXException(PERMISSIONERROR);
866
        }//if
867
       }//if
868
       // Write the content of the node to the database
869
       endNodeId = currentNode.writeChildNodeToDB("TEXT", null, data, docid);
870
     }
871
     
872
     
873
   
874
   }
875
   
609 876
   /** SAX Handler that receives notification of end of the document */
610 877
   public void endDocument() throws SAXException 
611 878
   {
612 879
     MetaCatUtil.debugMessage("end Document", 50);
880
     // There are some unchangable subtree didn't be compare
881
     // This maybe cause user change the subtree id
882
     if (!unChangableSubTreeHash.isEmpty())
883
     {
884
       MetaCatUtil.debugMessage("The unChangealbe subtree is not empty", 40);
885
       throw new SAXException(PERMISSIONERROR);
886
     }
887
     
613 888
     // write access rule to db
614 889
     writeAccessRuleToDB();
890
   
615 891
     // Starting new thread for writing XML Index.
616 892
     // It calls the run method of the thread.
617 893
     try 
......
625 901
       SAXException("Problem with starting thread for writing XML Index. " +
626 902
                    e.getMessage());
627 903
     }
904
   
628 905
   }
629 906
   
630 907
  /* The method to write all access rule intodb */

Also available in: Unified diff