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: daigle $'
10
 *     '$Date: 2008-12-26 13:07:40 -0800 (Fri, 26 Dec 2008) $'
11
 * '$Revision: 4698 $'
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;
29

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

    
39
import edu.ucsb.nceas.metacat.util.MetacatUtil;
40

    
41

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

    
50
 
51
 
52
  private String docId;
53
  private String principal;
54
  private int    permission;
55
  private String permType;
56
  private String permOrder;
57
  private Logger logMetacat = Logger.getLogger(AccessControlForSingleFile.class);
58

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

    
134
  
135

    
136

    
137
 
138

    
139
  /**
140
   * Method to insert records into xml_access table
141
   */
142
  
143
  public void insertPermissions() throws SQLException 
144
  {
145
    PreparedStatement pstmt = null;
146
    DBConnection conn = null;
147
    int serialNumber = -1;
148
    try
149
    {
150
      //check out DBConnection
151
      conn=DBConnectionPool.getDBConnection
152
                               ("AccessControlForSingleFiel.insertPermissions");
153
      serialNumber=conn.getCheckOutSerialNumber();
154
      pstmt = conn.prepareStatement(
155
              "INSERT INTO xml_access " + 
156
              "(docid, principal_name, permission, perm_type, perm_order " +
157
              ") VALUES (?,?,?,?,?)");
158
     
159
      // Bind the values to the query
160
      pstmt.setString(1, docId);
161
      pstmt.setString(2, principal);
162
      pstmt.setInt(3, permission);
163
      pstmt.setString(4, permType);
164
      pstmt.setString(5, permOrder);
165
      //pstmt.setString(6, AccessControlInterface.ACLID);
166
      pstmt.execute();
167
    }//try
168
    catch (SQLException e)
169
    {
170
      logMetacat.error("Error in AccessControlForSingleFile.insert" +
171
                               "Permissions: " + e.getMessage());
172
      throw e;
173
    }
174
    finally
175
    {
176
      try
177
      {
178
        pstmt.close();
179
      }
180
      finally
181
      {
182
        DBConnectionPool.returnDBConnection(conn, serialNumber);
183
      }
184
    }
185
   
186
  }
187
  
188
  /**
189
   * 
190
   * @return true if the Access Control for this file already exists in the DB
191
   * @throws SQLException
192
   */
193
  public boolean accessControlExists() throws SQLException 
194
  {
195
	  boolean exists = false;
196
    PreparedStatement pstmt = null;
197
    DBConnection conn = null;
198
    int serialNumber = -1;
199
    try
200
    {
201
      //check out DBConnection
202
      conn=DBConnectionPool.getDBConnection
203
                               ("AccessControlForSingleFiel.accessControlExists");
204
      serialNumber=conn.getCheckOutSerialNumber();
205
      pstmt = conn.prepareStatement(
206
              "SELECT * FROM xml_access " + 
207
              "WHERE docid = ? " +
208
              "AND principal_name = ? " +
209
              "AND permission = ? " +
210
              "AND perm_type = ? " +
211
              "AND perm_order =? ");
212
     
213
      // Bind the values to the query
214
      pstmt.setString(1, docId);
215
      pstmt.setString(2, principal);
216
      pstmt.setInt(3, permission);
217
      pstmt.setString(4, permType);
218
      pstmt.setString(5, permOrder);
219
      
220
      pstmt.execute();
221
      ResultSet rs = pstmt.getResultSet();
222
      exists = rs.next();
223
      
224
    }//try
225
    catch (SQLException e)
226
    {
227
      logMetacat.error("Error in AccessControlForSingleFile.accessControlExists: "
228
                               + e.getMessage());
229
      throw e;
230
    }
231
    finally
232
    {
233
      try
234
      {
235
        pstmt.close();
236
      }
237
      finally
238
      {
239
        DBConnectionPool.returnDBConnection(conn, serialNumber);
240
      }
241
    }
242
    
243
    return exists;
244
   
245
  }
246
  
247
  public String getAccessString() {
248
	  StringBuffer sb = new StringBuffer();
249
	  sb.append("<access>");
250
	  
251
		  sb.append("<permOrder>");
252
		  sb.append(this.permOrder);
253
		  sb.append("</permOrder>");
254
		  
255
		  sb.append("<permType>");
256
		  sb.append(this.permType);
257
		  sb.append("</permType>");
258
		  
259
		  sb.append("<permission>");
260
		  sb.append(AccessControlList.txtValue(this.permission));
261
		  sb.append("</permission>");
262
		  
263
		  sb.append("<principal>");
264
		  sb.append(this.principal);
265
		  sb.append("</principal>");
266

    
267
	  sb.append("</access>");
268
	  
269
	  return sb.toString();
270
	  
271
  }
272

    
273

    
274
}
(1-1/69)