Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that loads eml-access.xml file containing ACL 
4
 *             for a metadata document into relational DB
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Jivka Bojilova
8
 *
9
 *   '$Author: leinfelder $'
10
 *     '$Date: 2011-11-04 14:45:59 -0700 (Fri, 04 Nov 2011) $'
11
 * '$Revision: 6606 $'
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.accesscontrol;
29

    
30
import java.io.IOException;
31
import java.io.StringReader;
32
import java.sql.PreparedStatement;
33
import java.sql.ResultSet;
34
import java.sql.SQLException;
35
import java.util.Stack;
36
import java.util.Vector;
37

    
38
import org.apache.log4j.Logger;
39
import org.xml.sax.Attributes;
40
import org.xml.sax.ContentHandler;
41
import org.xml.sax.EntityResolver;
42
import org.xml.sax.ErrorHandler;
43
import org.xml.sax.InputSource;
44
import org.xml.sax.SAXException;
45
import org.xml.sax.XMLReader;
46
import org.xml.sax.helpers.DefaultHandler;
47
import org.xml.sax.helpers.XMLReaderFactory;
48

    
49
import edu.ucsb.nceas.metacat.BasicNode;
50
import edu.ucsb.nceas.metacat.DBEntityResolver;
51
import edu.ucsb.nceas.metacat.DocumentImpl;
52
import edu.ucsb.nceas.metacat.McdbException;
53
import edu.ucsb.nceas.metacat.database.DBConnection;
54
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
55
import edu.ucsb.nceas.metacat.properties.PropertyService;
56
import edu.ucsb.nceas.metacat.util.SystemUtil;
57
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
58

    
59
/** 
60
 * A Class that loads eml-access.xml file containing ACL for a metadata
61
 * document into relational DB. It extends DefaultHandler class to handle
62
 * SAX parsing events when processing the XML stream.
63
 */
64
public class AccessControlList extends DefaultHandler 
65
                               implements AccessControlInterface 
66
{
67

    
68
 
69
//  private static String sysdate = DatabaseService.getDBAdapter().getDateTimeFunction();
70
//  private static String isnull = DatabaseService.getDBAdapter().getIsNULLFunction();
71
  
72
  private DBConnection connection;
73
  private String parserName;
74
  private Stack elementStack;
75
  private String server;
76
  private String sep;
77
 
78
  private boolean	processingDTD;
79
  private String  user;
80
  private String[] groups;
81
  private String  aclid;
82
  private int     rev;
83
  private String 	docname;
84
  private String 	doctype;
85
  private String 	systemid;
86

    
87
  private String docurl;
88
  private Vector resourceURL;
89
  private Vector resourceID;
90
  private Vector principal;
91
  private int    permission;
92
  private String permType;
93
  private String permOrder;
94
  private String beginTime;
95
  private String endTime;
96
  private int    ticketCount;
97
  private int    serverCode = 1;
98

    
99
  private Vector aclObjects = new Vector();
100
  private boolean instarttag = true;
101
  private String tagName = "";
102
  
103
  private static Logger logMetacat = Logger.getLogger(AccessControlList.class);
104
  /**
105
   * Construct an instance of the AccessControlList class.
106
   * It is used by the permission check up from DBQuery or DocumentImpl
107
   * and from "getaccesscontrol" action
108
   *
109
   * @param conn the JDBC connection where acl info is get
110
   */
111
  public AccessControlList(DBConnection conn) throws SQLException
112
  {
113
    this.connection = conn;
114
  }
115
  
116

    
117
  
118

    
119
  /**
120
   * Construct an instance of the AccessControlList class.
121
   * It parse acl file and loads acl data into db connection.
122
   *
123
   * @param conn the JDBC connection where acl data are loaded
124
   * @param aclid the Accession# of the document with the acl data
125
   * @param acl the acl file containing acl data
126
   * @param user the user connected to MetaCat servlet and owns the document
127
   * @param groups the groups to which user belongs
128
   * @param serverCode the serverid from xml_replication on which this document
129
   *        resides.
130
   */
131
  public AccessControlList(DBConnection conn, String aclid, int rev,
132
                           String user, String[] groups, int serverCode)
133
                  throws SAXException, IOException, McdbException, PropertyNotFoundException
134
  {
135
		String parserName = PropertyService.getProperty("xml.saxparser");
136
		this.server = SystemUtil.getSecureServerURL();
137
		this.sep = PropertyService.getProperty("document.accNumSeparator");
138

    
139
		this.connection = conn;
140
		this.parserName = parserName;
141
		this.processingDTD = false;
142
		this.elementStack = new Stack();
143

    
144
		this.user = user;
145
		this.groups = groups;
146
		this.aclid = aclid;
147
		this.resourceURL = new Vector();
148
		this.resourceID = new Vector();
149
		this.principal = new Vector();
150
		this.permission = 0;
151
		this.ticketCount = 0;
152
		// this.publicAcc = null;
153
		this.serverCode = serverCode;
154

    
155
		// read the access file from db connection
156
		DocumentImpl acldoc = new DocumentImpl(aclid + sep + rev);
157
		String acl = acldoc.toString();
158
		this.rev = acldoc.getRev();
159

    
160
		// Initialize the parse
161
		XMLReader parser = initializeParser();
162
		// parse the access file and write the info to xml_access
163
		if (acl != null) {
164
			parser.parse(new InputSource(new StringReader(acl)));
165
		} else {
166
			throw new McdbException("Could not retrieve access control list for:  " + aclid + sep + rev);
167
		}
168
	}
169

    
170
// NOT USED
171
// /**
172
// * Construct an instance of the AccessControlList class.
173
//   * It parses eml-access file and loads acl data into db connection.
174
//   * It is used from command line execution.
175
//   *
176
//   * @param conn the JDBC connection where acl data are loaded
177
//   * @param docid the Accession# of the document with the acl data
178
//   * @param aclfilename the name of acl file containing acl data
179
//   * @param user the user connected to MetaCat servlet and owns the document
180
//   * @param groups the groups to which user belongs
181
//   */
182
//  public AccessControlList( Connection conn, String aclid, String aclfilename,
183
//                           String user, String[] groups )
184
//                  throws SAXException, IOException, McdbException
185
//  {
186
//    this(conn, aclid, new FileReader(new File(aclfilename).toString()), 
187
//         user, groups, 1);
188
//  }
189
  
190
  /* Set up the SAX parser for reading the XML serialized ACL */
191
  private XMLReader initializeParser() throws SAXException 
192
  {
193
    XMLReader parser = null;
194

    
195
    // Get an instance of the parser
196
    parser = XMLReaderFactory.createXMLReader(parserName);
197

    
198
    // Turn off validation
199
    parser.setFeature("http://xml.org/sax/features/validation", true);
200
      
201
    // Set Handlers in the parser
202
    // Set the ContentHandler to this instance
203
    parser.setContentHandler((ContentHandler)this);
204

    
205
    // make a DBEntityResolver instance
206
    // Set the EntityReslover to DBEntityResolver instance
207
    EntityResolver eresolver = new DBEntityResolver(connection,this,null);
208
    parser.setEntityResolver((EntityResolver)eresolver);
209

    
210
    // Set the ErrorHandler to this instance
211
    parser.setErrorHandler((ErrorHandler)this);
212

    
213
    return parser; 
214
  }
215
  
216
  /**
217
   * Callback method used by the SAX Parser when beginning of the document
218
   */
219
  public void startDocument() throws SAXException 
220
  {
221
    //delete all previously submitted permissions @ relations
222
    //this happens only on UPDATE of the access file
223
    try {
224
      this.aclObjects = getACLObjects(aclid);
225

    
226
      //delete all permissions for resources related to @aclid if any
227
      if ( aclid != null ) {
228
        deletePermissionsForRelatedResources(aclid);
229
      }
230
    } catch (SQLException sqle) {
231
      throw new SAXException(sqle);
232
    }
233
  }
234
  
235
  /**
236
   * Callback method used by the SAX Parser when the start tag of an 
237
   * element is detected. Used in this context to parse and store
238
   * the acl information in class variables.
239
   */
240
  public void startElement (String uri, String localName, 
241
                            String qName, Attributes atts) 
242
         throws SAXException 
243
  {
244
    instarttag = true;
245
    if(localName.equals("allow"))
246
    {
247
      tagName = "allow";
248
    }
249
    else if(localName.equals("deny"))
250
    {
251
      tagName = "deny";
252
    }
253
    BasicNode currentNode = new BasicNode(localName);
254
    if (atts != null) {
255
      int len = atts.getLength();
256
      for (int i = 0; i < len; i++) {
257
        currentNode.setAttribute(atts.getLocalName(i), atts.getValue(i));
258
      }
259
    }
260
    if ( currentNode.getTagName().equals("acl") ) {
261
      permOrder = currentNode.getAttribute("order");
262
    //  publicAcc = currentNode.getAttribute("public");
263
    }
264
    elementStack.push(currentNode); 
265
  }
266

    
267
  /**
268
   * Callback method used by the SAX Parser when the text sequences of an 
269
   * xml stream are detected. Used in this context to parse and store
270
   * the acl information in class variables.
271
   */ 
272
  public void characters(char ch[], int start, int length)
273
         throws SAXException 
274
  {
275
    if(!instarttag)
276
    {
277
      return;
278
    }
279
    String inputString = new String(ch, start, length);
280
    inputString = inputString.trim(); 
281
    //System.out.println("==============inputString: " + inputString);
282
    BasicNode currentNode = (BasicNode)elementStack.peek(); 
283
    String currentTag = currentNode.getTagName();
284

    
285
      if (currentTag.equals("principal")) {
286

    
287
        principal.addElement(inputString);
288

    
289
      } else if (currentTag.equals("permission")) {
290

    
291
        if ( inputString.trim().toUpperCase().equals("READ") ) {
292
          permission = permission | READ;
293
        } else if ( inputString.trim().toUpperCase().equals("WRITE") ) {
294
          permission = permission | WRITE;
295
        } else if ( inputString.trim().toUpperCase().equals("CHANGEPERMISSION")) 
296
        {
297
          permission = permission | CHMOD;
298
        } else if ( inputString.trim().toUpperCase().equals("ALL") ) {
299
          permission = permission | ALL;
300
        }/*else{
301
          throw new SAXException("Unknown permission type: " + inputString);
302
        }*/
303

    
304
      } else if ( currentTag.equals("startDate") && beginTime == null ) {
305
        beginTime = inputString.trim();
306

    
307
      } else if ( currentTag.equals("stopDate") && endTime == null) {
308
        endTime = inputString.trim();
309

    
310
      } else if (currentTag.equals("ticketCount") && ticketCount == 0 ) {
311
        try {
312
          ticketCount = (new Integer(inputString.trim())).intValue();
313
        } catch (NumberFormatException nfe) {
314
          throw new SAXException("Wrong integer format for:" + inputString);
315
        }
316
      }
317
  }
318

    
319
  /**
320
   * Callback method used by the SAX Parser when the end tag of an 
321
   * element is detected. Used in this context to parse and store
322
   * the acl information in class variables.
323
   */
324
  public void endElement (String uri, String localName, String qName)
325
         throws SAXException 
326
  {
327
    instarttag = false;
328
    BasicNode leaving = (BasicNode)elementStack.pop();
329
    String leavingTagName = leaving.getTagName();
330

    
331
    if ( leavingTagName.equals("allow") ||
332
         leavingTagName.equals("deny")    ) {
333
      
334
      if ( permission > 0 ) {
335

    
336
        // insert into db calculated permission for the list of principals
337
        try {
338
          // go through the objects in xml_relation about this acl doc
339
          for (int i=0; i < aclObjects.size(); i++) {
340
            // docid of the current object
341
            String docid = (String)aclObjects.elementAt(i); 
342
            //DocumentIdentifier docID = new DocumentIdentifier(docid);
343
            //docid = docID.getIdentifier();
344
            // the docid get here has revision number, so we need to 
345
            // remove it.
346
            //docid = MetacatUtil.getDocIdFromAccessionNumber(docid);
347
            //System.out.println("The docid insert is!!!!!!!!!! "+ docid);
348
            insertPermissions(docid,leavingTagName);
349
          }
350
          
351
          // if acl is not in object list
352
          //should insert permission for aclid itself into database
353
          /*if (!aclObjects.contains(aclid))
354
          {
355
            DocumentIdentifier aclIdItself = new DocumentIdentifier(aclid);
356
            String aclIdString = aclIdItself.getIdentifier();
357
            insertPermissions(aclIdString,leavingTagName);
358
          }*/
359
          
360

    
361
        } catch (SQLException sqle) {
362
          throw new SAXException(sqle);
363
        } catch (Exception e) {
364
          throw new SAXException(e);
365
        }
366
      }
367

    
368
      // reset the allow/deny permission
369
      principal = new Vector();
370
      permission = 0;
371
      beginTime = null;
372
      endTime = null;
373
      ticketCount = 0;
374
    
375
    }
376

    
377
  }
378

    
379
  /** 
380
    * SAX Handler that receives notification of DOCTYPE. Sets the DTD.
381
    * @param name name of the DTD
382
    * @param publicId Public Identifier of the DTD
383
    * @param systemId System Identifier of the DTD
384
    */
385
  public void startDTD(String name, String publicId, String systemId) 
386
              throws SAXException {
387
    docname = name;
388
    doctype = publicId;
389
    systemid = systemId;
390
  }
391

    
392
  /** 
393
   * SAX Handler that receives notification of the start of entities.
394
   * @param name name of the entity
395
   */
396
  public void startEntity(String name) throws SAXException {
397
    if (name.equals("[dtd]")) {
398
      processingDTD = true;
399
    }
400
  }
401

    
402
  /** 
403
   * SAX Handler that receives notification of the end of entities.
404
   * @param name name of the entity
405
   */
406
  public void endEntity(String name) throws SAXException {
407
    if (name.equals("[dtd]")) {
408
      processingDTD = false;
409
    }
410
  }
411

    
412
  /**
413
   * Get the document name.
414
   */
415
  public String getDocname() {
416
    return docname;
417
  }
418

    
419
  /**
420
   * Get the document processing state.
421
   */
422
  public boolean processingDTD() {
423
    return processingDTD;
424
  }
425
  
426
  /* Get all objects associated with @aclid from db.*/
427
  private Vector getACLObjects(String aclid) 
428
          throws SQLException 
429
  {
430
    Vector aclObjects = new Vector();
431
    DBConnection conn = null;
432
    int serialNumber = -1;
433
    PreparedStatement pstmt = null;
434
    try
435
    {
436
      //get connection from DBConnectionPool
437
      conn=DBConnectionPool.getDBConnection("AccessControlList.getACLObject");
438
      serialNumber=conn.getCheckOutSerialNumber();
439
      
440
      // delete all acl records for resources related to @aclid if any
441
      pstmt = conn.prepareStatement(
442
                             "SELECT object FROM xml_relation " +
443
                             "WHERE subject = ? ");
444
      pstmt.setString(1,aclid);
445
      pstmt.execute();
446
      ResultSet rs = pstmt.getResultSet();
447
      boolean hasRows = rs.next();
448
      while (hasRows) {
449
        String object = rs.getString(1);
450
        //System.out.println("add acl object into vector !!!!!!!!!!!!!!!!!"+object);
451
        aclObjects.addElement(object);
452
        hasRows = rs.next();
453
      }//whil
454
    }
455
    catch (SQLException e)
456
    {
457
      throw e;
458
    }
459
    finally
460
    {
461
      try
462
      {
463
        pstmt.close();
464
      }
465
      finally
466
      {
467
        //retrun DBConnection
468
        DBConnectionPool.returnDBConnection(conn,serialNumber);
469
      }
470
    }
471
    
472
    return aclObjects;
473
  }
474

    
475
  /* Delete from db all permission for resources related to @aclid if any.*/
476
  private void deletePermissionsForRelatedResources(String aclid) 
477
          throws SQLException 
478
  {
479
    //DBConnection conn = null;
480
    //int serialNumber = -1;
481
    PreparedStatement pstmt = null;
482
    try
483
    {
484
      //check out DBConenction
485
      //conn=DBConnectionPool.getDBConnection("AccessControlList.deltePerm");
486
      //serialNumber=conn.getCheckOutSerialNumber();
487
    	String sql = "DELETE FROM xml_access WHERE accessfileid = ?";
488
      // delete all acl records for resources related to @aclid if any
489
      pstmt = connection.prepareStatement(sql);
490
      pstmt.setString(1, aclid);
491
      // Increase DBConnection usage count
492
      connection.increaseUsageCount(1);
493
      logMetacat.debug("running sql: " + pstmt.toString());
494
      pstmt.execute();
495
      //increase usageCount!!!!!!
496
      //conn.increaseUsageCount(1);
497
    }
498
    catch (SQLException e)
499
    {
500
      throw e;
501
    }
502
    finally
503
    {
504
      pstmt.close();
505
      //retrun DBConnection
506
      //DBConnectionPool.returnDBConnection(conn,serialNumber);
507
    }
508
  }
509

    
510
  /* Insert into db calculated permission for the list of principals 
511
   * The DBConnection it is use is class field. Because we want to keep rollback
512
   * features and it need use same connection
513
  */
514
  
515
  private void insertPermissions(String docid, String permType ) 
516
                                            throws SQLException 
517
  {
518
    PreparedStatement pstmt = null;
519
    //DBConnection conn = null;
520
    //int serialNumber = -1;
521
    try {
522
      //Check out DBConnection
523
      //conn=DBConnectionPool.getDBConnection("AccessControlList.insertPerm");
524
      //serialNumber=conn.getCheckOutSerialNumber();
525
      
526
      pstmt = connection.prepareStatement(
527
              "INSERT INTO xml_access " + 
528
              "(docid, principal_name, permission, perm_type, perm_order," +
529
              "ticket_count, accessfileid) VALUES " +
530
              "(?,?,?,?,?,?,?)");
531
      // Increase DBConnection usage count
532
      connection.increaseUsageCount(1);
533
      // Bind the values to the query
534
      pstmt.setString(1, docid);
535
      pstmt.setInt(3, permission);
536
      pstmt.setString(4, permType);
537
      pstmt.setString(5, permOrder);
538
      pstmt.setString(7, aclid);
539
      if ( ticketCount > 0 ) {
540
        pstmt.setInt(6, ticketCount);
541
      } else {
542
        pstmt.setInt(6, 0);
543
      }
544
      
545
      //incrase usagecount for DBConnection
546
      //conn.increaseUsageCount(1);
547
      String prName;
548
      for ( int j = 0; j < principal.size(); j++ ) {
549
        prName = (String)principal.elementAt(j);
550
        pstmt.setString(2, prName);
551
        logMetacat.debug("running sql: " + pstmt.toString());
552
        pstmt.execute();
553
      /*    
554
        // check if there are conflict with permission's order
555
        String permOrderOpos = permOrder;
556
        int perm = getPermissions(permission, prName, docid, permOrder);
557
        if (  perm != 0 ) {
558
          if ( permOrder.equals("allowFirst") ) {
559
            permOrderOpos = "denyFirst";
560
          } else if ( permOrder.equals("denyFirst") ) {
561
            permOrderOpos = "allowFirst";
562
          }
563
          throw new SQLException("Permission(s) " + txtValue(perm) + 
564
                    " for \"" + prName + "\" on document #" + docid +
565
                    " has/have been used with \"" + permOrderOpos + "\"");
566
        }
567
      */
568
      }
569
      pstmt.close();
570

    
571
    } catch (SQLException e) {
572
      throw new 
573
      SQLException("AccessControlList.insertPermissions(): " + e.getMessage());
574
    }
575
    finally
576
    {
577
      pstmt.close();
578
      //return the DBConnection
579
      //DBConnectionPool.returnDBConnection(conn, serialNumber);
580
    }
581
  }
582

    
583
  /* Get permissions with permission order different than @permOrder. */
584
  private int getPermissions(int permission, String principal,
585
                             String docid, String permOrder)
586
          throws SQLException 
587
  {
588
    PreparedStatement pstmt = null;
589
    DBConnection conn = null;
590
    int serialNumber = -1;
591
    try
592
    {
593
      //check out DBConnection
594
      conn=DBConnectionPool.getDBConnection("AccessControlList.getPermissions");
595
      serialNumber=conn.getCheckOutSerialNumber();
596
      pstmt = conn.prepareStatement(
597
            "SELECT permission FROM xml_access " +
598
            "WHERE docid = ? " +
599
            "AND principal_name = ? " +
600
            "AND perm_order NOT = ?");
601
      pstmt.setString(1, docid);
602
      pstmt.setString(2, principal);
603
      pstmt.setString(3, permOrder);
604
      logMetacat.debug("running sql: " + pstmt.toString());
605
      pstmt.execute();
606
      ResultSet rs = pstmt.getResultSet();
607
      boolean hasRow = rs.next();
608
      int perm = 0;
609
      while ( hasRow ) {
610
        perm = rs.getInt(1);
611
        perm = permission & perm;
612
        if ( perm != 0 ) {
613
          pstmt.close();
614
          return perm;
615
        }
616
        hasRow = rs.next();
617
      }
618
    }//try
619
    catch (SQLException e)
620
    {
621
      throw e;
622
    }
623
    finally
624
    {
625
      try
626
      {
627
        pstmt.close();
628
      }
629
      finally
630
      {
631
        DBConnectionPool.returnDBConnection(conn, serialNumber);
632
      }
633
    }
634
    return 0;
635
  }
636

    
637
    /* Get the int value of READ, WRITE, CHMOD or ALL. */
638
	public static int intValue(String permission) {
639
	    
640
		int thisPermission = 0;
641
		try
642
		{
643
		    thisPermission = new Integer(permission).intValue();
644
		    if(thisPermission >= 0 && thisPermission <= 7)
645
		    {
646
		        return thisPermission;
647
		    }
648
		    else
649
		    {
650
		        thisPermission = -1;
651
		    }
652
		}
653
		catch(Exception e)
654
		{ //do nothing.  this must be a word
655
		}
656
		
657
		if (permission.toUpperCase().contains(CHMODSTRING)) {
658
			thisPermission |= CHMOD;
659
		} 
660
		if (permission.toUpperCase().contains(READSTRING)) {
661
			thisPermission |= READ;
662
		} 
663
		if (permission.toUpperCase().contains(WRITESTRING)) {
664
			thisPermission |= WRITE;
665
		} 
666
		if (permission.toUpperCase().contains(ALLSTRING)) {
667
			thisPermission |= ALL;
668
		}
669

    
670
		return thisPermission;
671
	}
672
  
673
  /* Get the text value of READ, WRITE, CHMOD or ALL. */
674
	public static String txtValue(int permission) {
675
		StringBuffer txtPerm = new StringBuffer();
676
		
677
		if ((permission & ALL) == ALL) {
678
			return ALLSTRING;
679
		}		
680
		if ((permission & CHMOD) == CHMOD) {
681
			txtPerm.append(CHMODSTRING);
682
		}
683
		if ((permission & READ) == READ) {
684
			if (txtPerm.length() > 0)
685
				txtPerm.append(",");
686
			txtPerm.append(READSTRING);
687
		}
688
		if ((permission & WRITE) == WRITE) {
689
			if (txtPerm.length() > 0)
690
				txtPerm.append(",");
691
			txtPerm.append(WRITESTRING);
692
		}
693

    
694
		return txtPerm.toString();
695
	}
696

    
697
  /* Get the flag for public "read" access for @docid from db conn. */
698
  private String getPublicAccess(String docid) throws SQLException {
699
    
700
    int publicAcc = 0;
701
    PreparedStatement pstmt = null;
702
    DBConnection conn = null;
703
    int serialNumber = -1;
704
    try
705
    {
706
      //check out DBConnection
707
      conn=DBConnectionPool.getDBConnection("AccessControlList.getPublicAcces");
708
      serialNumber=conn.getCheckOutSerialNumber();
709
      pstmt = conn.prepareStatement("SELECT public_access FROM xml_documents " +
710
                                  "WHERE docid = ?");
711
      pstmt.setString(1, docid);
712
      pstmt.execute();
713
      ResultSet rs = pstmt.getResultSet();
714
      boolean hasRow = rs.next();
715
      if ( hasRow ) {
716
        publicAcc = rs.getInt(1);
717
      }
718
    
719
      return (publicAcc == 1) ? "yes" : "no";
720
    }
721
    finally
722
    {
723
      try
724
      {
725
        pstmt.close();
726
      }
727
      finally
728
      {
729
        DBConnectionPool.returnDBConnection(conn, serialNumber);
730
      }
731
    }
732
  }
733

    
734
  /* Get SystemID for @publicID from Metacat DB Catalog. */
735
  private String getSystemID(String publicID) throws SQLException, PropertyNotFoundException {
736

    
737
		String systemID = "";
738
		PreparedStatement pstmt = null;
739
		DBConnection conn = null;
740
		int serialNumber = -1;
741

    
742
		try {
743
			//check out DBConnection
744
			conn = DBConnectionPool.getDBConnection("AccessControlList.getSystemID");
745
			serialNumber = conn.getCheckOutSerialNumber();
746

    
747
			pstmt = conn.prepareStatement("SELECT system_id FROM xml_catalog "
748
					+ "WHERE entry_type = 'DTD' " + "AND public_id = ?");
749
			pstmt.setString(1, publicID);
750
			pstmt.execute();
751
			ResultSet rs = pstmt.getResultSet();
752
			boolean hasRow = rs.next();
753
			if (hasRow) {
754
				systemID = rs.getString(1);
755
				// system id may not have server url on front.  Add it if not.
756
				if (!systemID.startsWith("http://")) {
757
					systemID = SystemUtil.getContextURL() + systemID;
758
				}
759
			}
760

    
761
			return systemID;
762
		}//try
763
		finally {
764
			try {
765
				pstmt.close();
766
			} finally {
767
				DBConnectionPool.returnDBConnection(conn, serialNumber);
768
			}
769
		}//finally
770
	}
771
  
772
  public static void main(String[] args) {
773
	  System.out.println("text value for CHMOD (" + CHMOD + "): " + txtValue(CHMOD));
774
	  System.out.println("text value for READ: (" + READ + "): " + txtValue(READ));
775
	  System.out.println("text value for WRITE: (" + WRITE + "): " + txtValue(WRITE));
776
	  System.out.println("text value for ALL: (" + ALL + "): " + txtValue(ALL));
777
	  int chmod_read = CHMOD|READ;
778
	  System.out.println("text value for CHMOD|READ: (" + chmod_read + "): " + txtValue(CHMOD|READ));
779
	  int chmod_write = CHMOD|WRITE;
780
	  System.out.println("text value for CHMOD|WRITE: (" + chmod_write + "): " + txtValue(CHMOD|WRITE));
781
	  int chmod_all = CHMOD|ALL;
782
	  System.out.println("text value for CHMOD|ALL: (" + chmod_all + "): " + txtValue(CHMOD|ALL));
783
	  int read_write = READ|WRITE;
784
	  System.out.println("text value for READ|WRITE: (" + read_write + "): " + txtValue(READ|WRITE));
785
	  int read_all = READ|ALL;
786
	  System.out.println("text value for READ|ALL: (" + read_all + "): " + txtValue(READ|ALL));
787
	  int write_all = WRITE|ALL;
788
	  System.out.println("text value for WRITE|ALL: (" + write_all + "): " + txtValue(WRITE|ALL));
789
	  int chmod_read_write = CHMOD|READ|WRITE;
790
	  System.out.println("text value for CHMOD|READ|WRITE: (" + chmod_read_write + "): " + txtValue(CHMOD|READ|WRITE));
791
	  int chmod_read_all = CHMOD|READ|ALL;
792
	  System.out.println("text value for CHMOD|READ|ALL: (" + chmod_read_all + "): " + txtValue(CHMOD|READ|ALL));
793
	  int chmod_write_all = CHMOD|WRITE|ALL;
794
	  System.out.println("text value for CHMOD|WRITE|ALL: (" + chmod_write_all + "): " + txtValue(CHMOD|WRITE|ALL));
795
	  int read_write_all = READ|WRITE|ALL;
796
	  System.out.println("text value for READ|WRITE|ALL: (" + read_write_all + "): " + txtValue(READ|WRITE|ALL));
797
	  int chmod_read_write_all = CHMOD|READ|WRITE|ALL;
798
	  System.out.println("text value for CHMOD|READ|WRITE|ALL: (" + chmod_read_write_all + "): " + txtValue(CHMOD|READ|WRITE|ALL));
799
	  System.out.println();
800
	  System.out.println("int value for GOOBER: " + intValue("GOOBER"));
801
	  System.out.println("int value for CHANGEPERMISSION: " + intValue("CHANGEPERMISSION"));
802
	  System.out.println("int value for READ: " + intValue("READ"));
803
	  System.out.println("int value for WRITE: " + intValue("WRITE"));
804
	  System.out.println("int value for ALL: " + intValue("ALL"));
805
	  System.out.println("int value for CHANGEPERMISSION,READ: " + intValue("CHANGEPERMISSION,READ"));
806
	  System.out.println("int value for CHANGEPERMISSION,WRITE: " + intValue("CHANGEPERMISSION,WRITE"));
807
	  System.out.println("int value for CHANGEPERMISSION,ALL: " + intValue("CHANGEPERMISSION,ALL"));
808
	  System.out.println("int value for READ,WRITE: " + intValue("READ,WRITE"));
809
	  System.out.println("int value for READ,ALL: " + intValue("READ,ALL"));
810
	  System.out.println("int value for WRITE,ALL: " + intValue("WRITE,ALL"));
811
	  System.out.println("int value for CHANGEPERMISSION,READ,WRITE: " + intValue("CHANGEPERMISSION,READ,WRITE"));
812
	  System.out.println("int value for CHANGEPERMISSION,READ,ALL: " + intValue("CHANGEPERMISSION,READ,ALL"));
813
	  System.out.println("int value for CHANGEPERMISSION,READ,ALL: " + intValue("CHANGEPERMISSION,WRITE,ALL"));
814
	  System.out.println("int value for READ,WRITE,ALL: " + intValue("READ,WRITE,ALL"));
815
	  System.out.println("int value for CHANGEPERMISSION,READ,WRITE,ALL: " + intValue("CHANGEPERMISSION,READ,WRITE,ALL"));
816
  }
817

    
818
}
(4-4/9)