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
 *    Release: @release@
9
 *
10
 *   '$Author: sgarg $'
11
 *     '$Date: 2005-10-10 11:06:55 -0700 (Mon, 10 Oct 2005) $'
12
 * '$Revision: 2663 $'
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
import org.apache.log4j.Logger;
39

    
40

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

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

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

    
133
  
134

    
135

    
136
 
137

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

    
187

    
188
}
(1-1/65)