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: jones $'
10
 *     '$Date: 2006-11-10 10:25:38 -0800 (Fri, 10 Nov 2006) $'
11
 * '$Revision: 3077 $'
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

    
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
  private Logger logMetacat = Logger.getLogger(AccessControlForSingleFile.class);
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
      logMetacat.info("docid in AccessControlForSingleFiel: " +
113
                                docId);
114
      logMetacat.info("principal in AccessControlForSingleFiel: " +
115
                                principal);
116
      logMetacat.info("permission in AccessControlForSingleFiel: " +
117
                                permission);
118
      logMetacat.info("permType in AccessControlForSingleFiel: " +
119
                                permType);
120
      logMetacat.info("permOrder in AccessControlForSingleFiel: " +
121
                                permOrder);
122
    }
123
    catch (Exception e)
124
    {
125
      logMetacat.error("Error in construct of AccessControlForSingle" +
126
                               "File: " + e.getMessage());
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
              ") 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
      logMetacat.error("Error in AccessControlForSingleFile.insert" +
169
                               "Permissions: " + e.getMessage());
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
}
(1-1/66)