This project has retired. For details please refer to its Attic page.
InMemoryAcl xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.inmemory.storedobj.impl;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Comparator;
24  import java.util.List;
25  
26  import org.apache.chemistry.opencmis.commons.data.Ace;
27  import org.apache.chemistry.opencmis.commons.data.Acl;
28  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AccessControlEntryImpl;
29  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AccessControlListImpl;
30  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AccessControlPrincipalDataImpl;
31  
32  public class InMemoryAcl {
33      
34      private List<InMemoryAce> acl;
35      private int id;
36      private static InMemoryAcl DEFAULT_ACL = new InMemoryAcl(new ArrayList<InMemoryAce>() {{ add(InMemoryAce.getDefaultAce()); }} );
37      private static class AceComparator<T extends InMemoryAce> implements Comparator<T> {
38  
39          public int compare(T o1, T o2) {            
40              int res = o1.getPrincipalId().compareTo(o2.getPrincipalId());
41              return res;
42          }
43          
44      };
45      
46      private static Comparator<? super InMemoryAce> COMP = new AceComparator<InMemoryAce>();
47      
48      public static InMemoryAcl createFromCommonsAcl(Acl commonsAcl) {
49          InMemoryAcl acl = new InMemoryAcl();
50          for (Ace cace : commonsAcl.getAces()) {
51              if (acl.hasPrincipal(cace.getPrincipalId())) {
52                  Permission perm = acl.getPermission(cace.getPrincipalId());
53                  Permission newPerm = Permission.fromCmisString(cace.getPermissions().get(0));
54                  if (perm.ordinal() > newPerm.ordinal())
55                      acl.setPermission(cace.getPrincipalId(), newPerm);
56              } else {
57                  acl.addAce(new InMemoryAce(cace));
58              }
59              
60          }
61          return acl;
62      }
63  
64      public static InMemoryAcl getDefaultAcl() {
65          return DEFAULT_ACL;
66      }
67      
68      public InMemoryAcl() {
69          acl = new ArrayList<InMemoryAce>(3);
70      }
71  
72      public InMemoryAcl(final List<InMemoryAce> arg ) {        
73          this.acl = new ArrayList<InMemoryAce>(arg);
74          Collections.sort(this.acl, COMP);
75          for (int i=0 ; i<acl.size(); i++) {
76              InMemoryAce ace = acl.get(i);
77              if (ace == null)
78                  throw new IllegalArgumentException("Cannot create ACLs with a null principal id or permission.");        
79          }
80          for (int i=0 ; i<acl.size()-1; i++) {
81              if (acl.get(i).equals(acl.get(i+1)))
82                  throw new IllegalArgumentException("Cannot create ACLs with same principal id in more than one ACE.");
83          }
84      }
85      
86      public void setId(int id) {
87          this.id = id;
88      }
89      
90      public int getId() {
91          return id;
92      }
93      
94      public final List<InMemoryAce> getAces() {
95          return acl;
96      }
97      
98      public boolean addAce(InMemoryAce ace) {
99          if (ace == null)
100             return false;
101         for (InMemoryAce ace2: acl) {
102             if (ace2.getPrincipalId().equals(ace.getPrincipalId()))
103                 return false;
104         }
105         acl.add(ace);
106         Collections.sort(acl, COMP);
107         return true;
108     }
109     
110     public boolean removeAce(InMemoryAce ace) {
111         return acl.remove(ace);
112     }
113     
114     public void mergeAcl(InMemoryAcl acl2) {
115         if (acl2 == null)
116             return;
117         for (InMemoryAce ace: acl2.getAces()) {
118             InMemoryAce existingAce  = getAce(ace.getPrincipalId());
119             if (existingAce == null)
120                 acl.add(ace);   
121             else if (existingAce.getPermission().ordinal() < ace.getPermission().ordinal())
122                 existingAce.setPermission(ace.getPermission());
123         }
124         Collections.sort(this.acl, COMP);
125     }
126     
127     public Permission getPermission(String principalId) {
128         InMemoryAce ace = getAce(principalId);
129         return ace== null ? Permission.NONE : ace.getPermission();
130     }
131 
132     private InMemoryAce getAce(String principalId) {
133         if (null == principalId)
134             return null;
135         
136         for (InMemoryAce ace : acl) {
137             if (ace.getPrincipalId().equals(principalId))
138                 return ace;
139         }
140         return null;
141     }
142 
143     public boolean hasPermission(String principalId, Permission permission) {
144         if (null == permission)
145             return false;
146         
147         if (null == principalId)
148             for (InMemoryAce ace : acl)
149                 if (ace.getPrincipalId().equals(InMemoryAce.getAnonymousUser()))
150                     return ace.hasPermission(permission);
151        
152         for (InMemoryAce ace : acl) {
153             if (ace.getPrincipalId().equals(principalId) || ace.getPrincipalId().equals(InMemoryAce.getAnyoneUser())
154                     || ace.getPrincipalId().equals(InMemoryAce.getAnonymousUser()))
155                 return ace.hasPermission(permission);
156         }
157         return false;
158     }
159 
160     public void setPermission(String principalId, Permission permission) {        
161         for (InMemoryAce ace : acl) {
162             if (ace.getPrincipalId().equals(principalId))
163                 ace.setPermission(permission);
164         }
165         throw new IllegalArgumentException("Unknown principalId in setPermission: " + principalId);
166     }
167    
168     public int size() {
169         return acl.size();
170     }
171      
172     @Override
173     public int hashCode() {
174         final int prime = 31;
175         int result = 1;
176         result = prime * result + ((acl == null) ? 0 : acl.hashCode());
177         return result;
178     }
179 
180     @Override
181     public boolean equals(Object obj) {
182         if (this == obj)
183             return true;
184         if (obj == null)
185             return false;
186         if (getClass() != obj.getClass())
187             return false;
188         InMemoryAcl other = (InMemoryAcl) obj;
189         if (acl == null) {
190             if (other.acl != null)
191                 return false;
192         } else if (!acl.equals(other.acl))
193             return false;
194         return true;
195     }
196 
197     @Override
198     public String toString() {
199         return "InMemoryAcl [acl=" + acl + "]";
200     }
201             
202     private boolean hasPrincipal(String principalId) {
203         for (InMemoryAce ace: acl) {
204             if (ace.getPrincipalId().equals(principalId))
205                 return true;
206         }
207         return false;
208     }
209 
210     public Acl toCommonsAcl() {
211         List<Ace> commonsAcl = new ArrayList<Ace>();
212         for (InMemoryAce memAce : acl)
213             commonsAcl.add(memAce.toCommonsAce());
214         
215         return new AccessControlListImpl(commonsAcl); 
216     }
217 
218     public InMemoryAcl clone() {
219         InMemoryAcl newAcl = new InMemoryAcl(acl);
220         return newAcl; 
221     }
222 }