Project

General

Profile

« Previous | Next » 

Revision 1365

Added by Jing Tao almost 22 years ago

A class to handle assign a access rule to a single file.

View differences:

src/edu/ucsb/nceas/metacat/AccessControlForSingleFile.java
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
 *    Release: @release@
9
 *
10
 *   '$Author$'
11
 *     '$Date$'
12
 * '$Revision$'
13
 *
14
 * This program is free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 2 of the License, or
17
 * (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
 */
28

  
29
package edu.ucsb.nceas.metacat;
30

  
31
import java.io.*;
32
import java.sql.*;
33
import java.util.Stack;
34
import java.util.Vector;
35
import java.util.Hashtable;
36
import java.net.URL;
37
import java.net.MalformedURLException;
38

  
39

  
40
/** 
41
 * A Class that loads eml-access.xml file containing ACL for a metadata
42
 * document into relational DB. It extends DefaultHandler class to handle
43
 * SAX parsing events when processing the XML stream.
44
 */
45
public class AccessControlForSingleFile implements AccessControlInterface 
46
{
47

  
48
 
49
 
50
  private String docId;
51
  private String principal;
52
  private int    permission;
53
  private String permType;
54
  private String permOrder;
55

  
56

  
57
 
58
  /**
59
   * Construct an instance of the AccessControlForSingleFile class.
60
   * @param myAccessNumber  the docid or docid with dev will be controlled
61
   * @param myprincipal  the principal will have permission
62
   * @param myPermission the permission will be given
63
   * @param myPermType  the permsission type, allow or deny
64
   * @param myPermOrder  the permission order, allowFirst or denyFirst
65
   */
66
  public AccessControlForSingleFile(String myAccessionNumber,
67
                                    String myPrincipalName,
68
                                    String myPermission,
69
                                    String myPermType,
70
                                    String myPermOrder) 
71
                                   throws Exception
72
  {
73
    try
74
    {
75
      //Get rid of dev if myaccessNumber has one;
76
      docId = MetaCatUtil.getDocIdFromString(myAccessionNumber);
77
      if (docId == null || docId.equals(""))
78
      {
79
        throw new Exception("Docid couldn't be null");
80
      }
81
      // Check principal
82
      principal = myPrincipalName;
83
      if (principal == null || principal.equals(""))
84
      {
85
        throw new Exception("principal couldn't be null");
86
      }
87
      // get permission value
88
      permission = AccessControlList.intValue(myPermission);
89
      if (permission == -1)
90
      {
91
        throw new Exception("permission "+ myPermission + " is not valid");
92
      }
93
      // check permission type
94
      permType = myPermType;
95
      // if permtype is not allow or deny, throw a exception
96
      if (permType == null
97
          || (!permType.equals(AccessControlInterface.ALLOW)
98
          && !permType.equals(AccessControlInterface.DENY)))
99
      {
100
        throw new Exception("permtype should be "+AccessControlInterface.ALLOW+
101
                            " or " +AccessControlInterface.DENY);
102
      }
103
      // check permission order
104
      permOrder = myPermOrder;
105
      //if permission order is not allowFirst or denyFirst, assing it dneyFirst
106
      if (permOrder == null
107
          || !permOrder.equals("AccessControlInterface.DENYFIRST"))
108
      {
109
        permOrder = AccessControlInterface.ALLOWFIRST;
110
      }
111
      //debugMessage
112
      MetaCatUtil.debugMessage("docid in AccessControlForSingleFiel: " +
113
                                docId, 30);
114
      MetaCatUtil.debugMessage("principal in AccessControlForSingleFiel: " +
115
                                principal, 30);
116
      MetaCatUtil.debugMessage("permission in AccessControlForSingleFiel: " +
117
                                permission, 30);
118
      MetaCatUtil.debugMessage("permType in AccessControlForSingleFiel: " +
119
                                permType, 30);
120
      MetaCatUtil.debugMessage("permOrder in AccessControlForSingleFiel: " +
121
                                permOrder, 30);
122
    }
123
    catch (Exception e)
124
    {
125
      MetaCatUtil.debugMessage("Error in construct of AccessControlForSingle" +
126
                               "File: " + e.getMessage(), 30);
127
      throw e;
128
    }
129
  }
130
  
131

  
132
  
133

  
134

  
135
 
136

  
137
  /**
138
   * Method to insert records into xml_access table
139
   */
140
  
141
  public void insertPermissions() throws SQLException 
142
  {
143
    PreparedStatement pstmt = null;
144
    DBConnection conn = null;
145
    int serialNumber = -1;
146
    try
147
    {
148
      //check out DBConnection
149
      conn=DBConnectionPool.getDBConnection
150
                               ("AccessControlForSingleFiel.insertPermissions");
151
      serialNumber=conn.getCheckOutSerialNumber();
152
      pstmt = conn.prepareStatement(
153
              "INSERT INTO xml_access " + 
154
              "(docid, principal_name, permission, perm_type, perm_order, " +
155
              "accessfileid) VALUES (?,?,?,?,?,?)");
156
     
157
      // Bind the values to the query
158
      pstmt.setString(1, docId);
159
      pstmt.setString(2, principal);
160
      pstmt.setInt(3, permission);
161
      pstmt.setString(4, permType);
162
      pstmt.setString(5, permOrder);
163
      pstmt.setString(6, AccessControlInterface.ACLID);
164
      pstmt.execute();
165
    }//try
166
    catch (SQLException e)
167
    {
168
      MetaCatUtil.debugMessage("Error in AccessControlForSingleFile.insert" +
169
                               "Permissions: " + e.getMessage(), 30);
170
      throw e;
171
    }
172
    finally
173
    {
174
      try
175
      {
176
        pstmt.close();
177
      }
178
      finally
179
      {
180
        DBConnectionPool.returnDBConnection(conn, serialNumber);
181
      }
182
    }
183
   
184
  }
185

  
186

  
187
}
0 188

  

Also available in: Unified diff