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: 2009-08-14 14:22:13 -0700 (Fri, 14 Aug 2009) $'
11
 * '$Revision: 5025 $'
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.database.DBConnection;
40
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
41
import edu.ucsb.nceas.metacat.util.DocumentUtil;
42

    
43

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

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

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

    
136
  
137

    
138

    
139
 
140

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

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

    
275

    
276
}
(1-1/63)