This project has retired. For details please refer to its Attic page.
InMemoryAce 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.Collections;
22  import java.util.List;
23  
24  import org.apache.chemistry.opencmis.commons.data.Ace;
25  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AccessControlEntryImpl;
26  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AccessControlPrincipalDataImpl;
27  
28  
29  public class InMemoryAce {
30  
31      private final String principalId;    
32      private Permission permission;
33      private static InMemoryAce DEFAULT_ACE = new InMemoryAce(InMemoryAce.getAnyoneUser(), Permission.ALL);
34      
35      public static final String getAnyoneUser() {
36          return "anyone";
37      }
38  
39      public static final String getAnonymousUser() {
40          return "anonymous";
41      }
42      
43      public static final InMemoryAce getDefaultAce() {
44          return DEFAULT_ACE;
45      }
46      
47      public InMemoryAce(Ace commonsAce) {
48          if (null == commonsAce || null == commonsAce.getPrincipalId() || null == commonsAce.getPermissions())
49              throw new IllegalArgumentException("Cannot create InMemoryAce with null value");
50          List<String> perms = commonsAce.getPermissions();
51          if (perms.size() != 1)
52              throw new IllegalArgumentException("InMemory only supports ACEs with a single permission.");
53          String perm = perms.get(0);
54          this.principalId = commonsAce.getPrincipalId();
55          this.permission = Permission.fromCmisString(perm);        
56      }
57  
58      public InMemoryAce(String prinicpalId, Permission permission) {
59          if (null == prinicpalId || null == permission)
60              throw new IllegalArgumentException("Cannot create InMemoryAce with null value");
61          
62          this.principalId = prinicpalId;
63          this.permission = permission;        
64      }
65      
66      public String getPrincipalId() {
67          return principalId;
68      }
69      
70      public Permission getPermission() {
71          return permission;
72      }
73      
74      public void setPermission(Permission newPermission) {
75          permission = newPermission;
76      }
77  
78      @Override
79      public int hashCode() {
80          final int prime = 31;
81          int result = 1;
82          result = prime * result + ((permission == null) ? 0 : permission.hashCode());
83          result = prime * result + ((principalId == null) ? 0 : principalId.hashCode());
84          return result;
85      }
86  
87      @Override
88      public boolean equals(Object obj) {
89          if (this == obj)
90              return true;
91          if (obj == null)
92              return false;
93          if (getClass() != obj.getClass())
94              return false;
95          InMemoryAce other = (InMemoryAce) obj;
96          if (permission != other.permission)
97              return false;
98          if (principalId == null) {
99              if (other.principalId != null)
100                 return false;
101         } else if (!principalId.equals(other.principalId))
102             return false;
103         return true;
104     }
105 
106     public boolean hasPermission(Permission permission2) {
107         return this.permission.compareTo(permission2) >= 0;
108     }
109 
110     @Override
111     public String toString() {
112         return "InMemoryAce [principalId=" + principalId + ", permission=" + permission + "]";
113     }
114 
115     public Ace toCommonsAce() {
116         return new AccessControlEntryImpl(new AccessControlPrincipalDataImpl(principalId), 
117                 Collections.singletonList(permission.toCmisString())); 
118     }
119     
120 }