Project

General

Profile

1 1365 tao
/**
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$'
10
 *     '$Date$'
11
 * '$Revision$'
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 2663 sgarg
import org.apache.log4j.Logger;
38 1365 tao
39 4080 daigle
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
40 1365 tao
41 4080 daigle
42 1365 tao
/**
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 2663 sgarg
  private Logger logMetacat = Logger.getLogger(AccessControlForSingleFile.class);
58 1365 tao
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 3512 barteau
108
      // If permission order is not allowFirst or denyFirst, default to denyFirst.
109
      if (permOrder == null || !permOrder.equals(AccessControlInterface.DENYFIRST)) {
110 1365 tao
        permOrder = AccessControlInterface.ALLOWFIRST;
111
      }
112 3512 barteau
113 1365 tao
      //debugMessage
114 2663 sgarg
      logMetacat.info("docid in AccessControlForSingleFiel: " +
115 2588 sgarg
                                docId);
116 2663 sgarg
      logMetacat.info("principal in AccessControlForSingleFiel: " +
117 2588 sgarg
                                principal);
118 2663 sgarg
      logMetacat.info("permission in AccessControlForSingleFiel: " +
119 2588 sgarg
                                permission);
120 2663 sgarg
      logMetacat.info("permType in AccessControlForSingleFiel: " +
121 2588 sgarg
                                permType);
122 2663 sgarg
      logMetacat.info("permOrder in AccessControlForSingleFiel: " +
123 2588 sgarg
                                permOrder);
124 1365 tao
    }
125
    catch (Exception e)
126
    {
127 2663 sgarg
      logMetacat.error("Error in construct of AccessControlForSingle" +
128 2588 sgarg
                               "File: " + e.getMessage());
129 1365 tao
      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 1370 tao
              "(docid, principal_name, permission, perm_type, perm_order " +
157
              ") VALUES (?,?,?,?,?)");
158 1365 tao
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 1370 tao
      //pstmt.setString(6, AccessControlInterface.ACLID);
166 1365 tao
      pstmt.execute();
167
    }//try
168
    catch (SQLException e)
169
    {
170 2663 sgarg
      logMetacat.error("Error in AccessControlForSingleFile.insert" +
171 2588 sgarg
                               "Permissions: " + e.getMessage());
172 1365 tao
      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 4419 leinfelder
188 4450 leinfelder
  /**
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 4419 leinfelder
  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 4447 leinfelder
		  sb.append(AccessControlList.txtValue(this.permission));
261 4419 leinfelder
		  sb.append("</permission>");
262
263
		  sb.append("<principal>");
264
		  sb.append(this.principal);
265
		  sb.append("</principal>");
266 1365 tao
267 4419 leinfelder
	  sb.append("</access>");
268
269
	  return sb.toString();
270
271
  }
272 1365 tao
273 4419 leinfelder
274 1365 tao
}