KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webservice > accesscontrol > AccessControlWebService


1 package org.alfresco.repo.webservice.accesscontrol;
2
3 import java.rmi.RemoteException JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Set JavaDoc;
7
8 import org.alfresco.repo.transaction.TransactionComponent;
9 import org.alfresco.repo.transaction.TransactionUtil;
10 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
11 import org.alfresco.repo.webservice.AbstractWebService;
12 import org.alfresco.repo.webservice.Utils;
13 import org.alfresco.repo.webservice.action.ActionFault;
14 import org.alfresco.repo.webservice.types.Predicate;
15 import org.alfresco.service.cmr.repository.NodeRef;
16 import org.alfresco.service.cmr.security.AccessPermission;
17 import org.alfresco.service.cmr.security.AccessStatus;
18 import org.alfresco.service.cmr.security.OwnableService;
19 import org.alfresco.service.cmr.security.PermissionService;
20 import org.alfresco.service.namespace.QName;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 public class AccessControlWebService extends AbstractWebService implements AccessControlServiceSoapPort
25 {
26     /** Log */
27     private static Log logger = LogFactory.getLog(AccessControlWebService.class);
28     
29     /** Transaction service */
30     private TransactionComponent transactionService = null;
31     
32     /** Permission service */
33     private PermissionService permissionService = null;
34     
35     /** Ownable service */
36     private OwnableService ownableService = null;
37     
38     /**
39      * Set the transaction service
40      *
41      * @param transactionService the transaction service
42      */

43     public void setTransactionService(TransactionComponent transactionService)
44     {
45         this.transactionService = transactionService;
46     }
47     
48     /**
49      * Set the permissions service
50      *
51      * @param permissionService the permissions service
52      */

53     public void setPermissionService(PermissionService permissionService)
54     {
55         this.permissionService = permissionService;
56     }
57     
58     /**
59      * Set the ownable service
60      *
61      * @param ownableService the ownable service
62      */

63     public void setOwnableService(OwnableService ownableService)
64     {
65         this.ownableService = ownableService;
66     }
67     
68     /**
69      * @see org.alfresco.repo.webservice.accesscontrol.AccessControlServiceSoapPort#getACLs(org.alfresco.repo.webservice.types.Predicate, org.alfresco.repo.webservice.accesscontrol.ACE)
70      */

71     public ACL[] getACLs(final Predicate predicate, final ACE filter) throws RemoteException JavaDoc, AccessControlFault
72     {
73         try
74         {
75             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<ACL[]>()
76             {
77                 public ACL[] doWork() throws Exception JavaDoc
78                 {
79                     return getACLsImpl(predicate, filter);
80                 }
81             });
82         }
83         catch (Throwable JavaDoc exception)
84         {
85             if (logger.isDebugEnabled())
86             {
87                 logger.error("Unexpected error occurred", exception);
88             }
89             
90             throw new ActionFault(0, exception.getMessage());
91         }
92     }
93
94     /**
95      * Get the ACL's for the predicate, filtered if appropraite.
96      *
97      * @param predicate the predicate
98      * @param filter the fileter (optional)
99      * @return an array of ACL's
100      */

101     private ACL[] getACLsImpl(Predicate predicate, ACE filter)
102     {
103         // Resolve the nodes
104
List JavaDoc<NodeRef> nodes = Utils.resolvePredicate(predicate, this.nodeService, this.searchService, this.namespaceService);
105         ACL[] acls = new ACL[nodes.size()];
106         
107         int index = 0;
108         for (NodeRef node : nodes)
109         {
110             // Create ACL of node
111
ACL acl = getACLFromNodeRef(node, filter);
112             
113             // Add the acl to the results
114
acls[index] = acl;
115             index++;
116         }
117         
118         return acls;
119     }
120
121     /**
122      * Given a node reference, creates the relating ACL
123      *
124      * @param node the node reference
125      * @return the ACL
126      */

127     private ACL getACLFromNodeRef(NodeRef node, ACE filter)
128     {
129         // Create the acl
130
ACL acl = new ACL();
131         acl.setReference(Utils.convertToReference(node));
132         
133         // Set the inhertied value
134
boolean inheritPermission = this.permissionService.getInheritParentPermissions(node);
135         acl.setInheritPermissions(inheritPermission);
136         
137         // Get the access permissions
138
Set JavaDoc<AccessPermission> accessPermissions = this.permissionService.getAllSetPermissions(node);
139         ACE[] aces = new ACE[accessPermissions.size()];
140         
141         // Marshal the permissions into ACE's
142
int count = 0;
143         for (AccessPermission permission : accessPermissions)
144         {
145             // TODO need to filter the results accordingly using ACE filter
146

147             // Create the ace
148
org.alfresco.repo.webservice.accesscontrol.AccessStatus accessStatus = org.alfresco.repo.webservice.accesscontrol.AccessStatus.declined;
149             if (AccessStatus.ALLOWED.equals(permission.getAccessStatus()) == true)
150             {
151                 accessStatus = org.alfresco.repo.webservice.accesscontrol.AccessStatus.acepted;
152             }
153             ACE ace = new ACE(permission.getAuthority(),permission.getPermission(), accessStatus);
154             
155             // Add ace to array
156
aces[count] = ace;
157             count ++;
158         }
159         acl.setAces(aces);
160         return acl;
161     }
162
163     /**
164      * @see org.alfresco.repo.webservice.accesscontrol.AccessControlServiceSoapPort#addACEs(org.alfresco.repo.webservice.types.Predicate, org.alfresco.repo.webservice.accesscontrol.ACE[])
165      */

166     public ACL[] addACEs(final Predicate predicate, final ACE[] aces) throws RemoteException JavaDoc, AccessControlFault
167     {
168         try
169         {
170             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<ACL[]>()
171             {
172                 public ACL[] doWork() throws Exception JavaDoc
173                 {
174                     return addACEsImpl(predicate, aces);
175                 }
176             });
177         }
178         catch (Throwable JavaDoc exception)
179         {
180             if (logger.isDebugEnabled())
181             {
182                 logger.error("Unexpected error occurred", exception);
183             }
184             
185             throw new ActionFault(0, exception.getMessage());
186         }
187     }
188
189     /**
190      * Add ACE to a collection of nodes
191      *
192      * @param predicate the predicate
193      * @param aces the ACE's to add
194      * @return the ACL's of the modified node
195      */

196     private ACL[] addACEsImpl(Predicate predicate, ACE[] aces)
197     {
198         // Resolce the predicate
199
List JavaDoc<NodeRef> nodes = Utils.resolvePredicate(predicate, this.nodeService, this.searchService, this.namespaceService);
200         ACL[] acls = new ACL[nodes.size()];
201         
202         int count = 0;
203         for (NodeRef node : nodes)
204         {
205             // Add the permissions for each ace
206
for (ACE ace : aces)
207             {
208                 // Add the permissions associated with the ace
209
boolean allow = false;
210                 if (ace.getAccessStatus().equals(org.alfresco.repo.webservice.accesscontrol.AccessStatus.acepted) == true)
211                 {
212                     allow = true;
213                 }
214                 this.permissionService.setPermission(node, ace.getAuthority(), ace.getPermission(), allow);
215             }
216             
217             // Add the ACL forthis node to the returned array
218
acls[count] = getACLFromNodeRef(node, null);
219             count++;
220         }
221         
222         return acls;
223     }
224
225     /**
226      * @see org.alfresco.repo.webservice.accesscontrol.AccessControlServiceSoapPort#removeACEs(org.alfresco.repo.webservice.types.Predicate, org.alfresco.repo.webservice.accesscontrol.ACE[])
227      */

228     public ACL[] removeACEs(final Predicate predicate, final ACE[] aces) throws RemoteException JavaDoc, AccessControlFault
229     {
230         try
231         {
232             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<ACL[]>()
233             {
234                 public ACL[] doWork() throws Exception JavaDoc
235                 {
236                     return removeACEsImpl(predicate, aces);
237                 }
238             });
239         }
240         catch (Throwable JavaDoc exception)
241         {
242             if (logger.isDebugEnabled())
243             {
244                 logger.error("Unexpected error occurred", exception);
245             }
246             
247             throw new ActionFault(0, exception.getMessage());
248         }
249     }
250
251     /**
252      * Remove specified ACE's from the nodes. Removes all permissions if no ACE's specified.
253      *
254      * @param predicate the predicate
255      * @param aces the ACE's to remove
256      * @return the modified ACL's
257      */

258     private ACL[] removeACEsImpl(Predicate predicate, ACE[] aces)
259     {
260         // Resolce the predicate
261
List JavaDoc<NodeRef> nodes = Utils.resolvePredicate(predicate, this.nodeService, this.searchService, this.namespaceService);
262         ACL[] acls = new ACL[nodes.size()];
263         
264         int count = 0;
265         for (NodeRef node : nodes)
266         {
267             if (aces == null)
268             {
269                 // Delete all the permissions
270
this.permissionService.deletePermissions(node);
271             }
272             else
273             {
274                 // Delete the permissions for each ACE
275
for (ACE ace : aces)
276                 {
277                     boolean allow = false;
278                     if (ace.getAccessStatus().equals(org.alfresco.repo.webservice.accesscontrol.AccessStatus.acepted) == true)
279                     {
280                         allow = true;
281                     }
282                     this.permissionService.deletePermission(node, ace.getAuthority(), ace.getPermission(), allow);
283                 }
284             }
285             
286             // Add the ACL forthis node to the returned array
287
acls[count] = getACLFromNodeRef(node, null);
288             count++;
289         }
290         
291         return acls;
292     }
293
294     /**
295      * @see org.alfresco.repo.webservice.accesscontrol.AccessControlServiceSoapPort#getPermissions(org.alfresco.repo.webservice.types.Predicate)
296      */

297     public GetPermissionsResult[] getPermissions(final Predicate predicate) throws RemoteException JavaDoc, AccessControlFault
298     {
299         try
300         {
301             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<GetPermissionsResult[]>()
302             {
303                 public GetPermissionsResult[] doWork() throws Exception JavaDoc
304                 {
305                     return getPermissionsImpl(predicate);
306                 }
307             });
308         }
309         catch (Throwable JavaDoc exception)
310         {
311             if (logger.isDebugEnabled())
312             {
313                 logger.error("Unexpected error occurred", exception);
314             }
315             
316             throw new ActionFault(0, exception.getMessage());
317         }
318     }
319
320     /**
321      * Get the permissions
322      *
323      * @param predicate the predicate
324      * @return the permissions available
325      */

326     private GetPermissionsResult[] getPermissionsImpl(Predicate predicate)
327     {
328         // Resolve the predicate
329
List JavaDoc<NodeRef> nodes = Utils.resolvePredicate(predicate, this.nodeService, this.searchService, this.namespaceService);
330         GetPermissionsResult[] results = new GetPermissionsResult[nodes.size()];
331         
332         int count = 0;
333         for (NodeRef node : nodes)
334         {
335             // Get the permissions
336
Set JavaDoc<String JavaDoc> permissions = this.permissionService.getSettablePermissions(node);
337             
338             // Create the permissions result object
339
GetPermissionsResult result = new GetPermissionsResult();
340             result.setReference(Utils.convertToReference(node));
341             result.setPermissions((String JavaDoc[])permissions.toArray(new String JavaDoc[permissions.size()]));
342             
343             // Add result to array
344
results[count] = result;
345             count ++;
346         }
347         
348         return results;
349     }
350
351     /**
352      * @see org.alfresco.repo.webservice.accesscontrol.AccessControlServiceSoapPort#getClassPermissions(java.lang.String[])
353      */

354     public GetClassPermissionsResult[] getClassPermissions(final String JavaDoc[] classNames) throws RemoteException JavaDoc, AccessControlFault
355     {
356         try
357         {
358             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<GetClassPermissionsResult[]>()
359             {
360                 public GetClassPermissionsResult[] doWork() throws Exception JavaDoc
361                 {
362                     return getClassPermissionsImpl(classNames);
363                 }
364             });
365         }
366         catch (Throwable JavaDoc exception)
367         {
368             if (logger.isDebugEnabled())
369             {
370                 logger.error("Unexpected error occurred", exception);
371             }
372             
373             throw new ActionFault(0, exception.getMessage());
374         }
375     }
376
377     /**
378      * Get the permissions based on type
379      *
380      * @param classNames the class names
381      * @return the permission results
382      */

383     private GetClassPermissionsResult[] getClassPermissionsImpl(String JavaDoc[] classNames)
384     {
385         // Resolve the predicate
386
GetClassPermissionsResult[] results = new GetClassPermissionsResult[classNames.length];
387         
388         int count = 0;
389         for (String JavaDoc className : classNames)
390         {
391             // Get the permissions
392
Set JavaDoc<String JavaDoc> permissions = this.permissionService.getSettablePermissions(QName.createQName(className));
393             
394             // Create the permissions result object
395
GetClassPermissionsResult result = new GetClassPermissionsResult();
396             result.setClassName(className);
397             result.setPermissions((String JavaDoc[])permissions.toArray(new String JavaDoc[permissions.size()]));
398             
399             // Add result to array
400
results[count] = result;
401             count ++;
402         }
403         
404         return results;
405     }
406
407     /**
408      * @see org.alfresco.repo.webservice.accesscontrol.AccessControlServiceSoapPort#hasPermissions(org.alfresco.repo.webservice.types.Predicate, java.lang.String[])
409      */

410     public HasPermissionsResult[] hasPermissions(final Predicate predicate, final String JavaDoc[] permissions) throws RemoteException JavaDoc, AccessControlFault
411     {
412         try
413         {
414             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<HasPermissionsResult[]>()
415             {
416                 public HasPermissionsResult[] doWork() throws Exception JavaDoc
417                 {
418                     return hasPermissionsImpl(predicate, permissions);
419                 }
420             });
421         }
422         catch (Throwable JavaDoc exception)
423         {
424             if (logger.isDebugEnabled())
425             {
426                 logger.error("Unexpected error occurred", exception);
427             }
428             
429             throw new ActionFault(0, exception.getMessage());
430         }
431     }
432
433     /**
434      * Determines whether a set of node has a given set of permissions.
435      *
436      * @param predicate the predicate
437      * @param permissions the permissions
438      * @return the permissions result
439      */

440     private HasPermissionsResult[] hasPermissionsImpl(Predicate predicate, String JavaDoc[] permissions)
441     {
442         // Resolve the predicate
443
List JavaDoc<NodeRef> nodes = Utils.resolvePredicate(predicate, this.nodeService, this.searchService, this.namespaceService);
444         List JavaDoc<HasPermissionsResult> results = new ArrayList JavaDoc<HasPermissionsResult>(20);
445         
446         for (NodeRef node : nodes)
447         {
448             for (String JavaDoc permission : permissions)
449             {
450                 // Detemine whether the node has the permissions
451
AccessStatus accessStatus = this.permissionService.hasPermission(node, permission);
452                 org.alfresco.repo.webservice.accesscontrol.AccessStatus accessState = org.alfresco.repo.webservice.accesscontrol.AccessStatus.declined;
453                 if (AccessStatus.ALLOWED.equals(accessStatus) == true)
454                 {
455                     accessState = org.alfresco.repo.webservice.accesscontrol.AccessStatus.acepted;
456                 }
457                 
458                 // Add to the results list
459
results.add(new HasPermissionsResult(Utils.convertToReference(node), permission, accessState));
460             }
461         }
462         
463         return (HasPermissionsResult[])results.toArray(new HasPermissionsResult[results.size()]);
464     }
465
466     /**
467      * @see org.alfresco.repo.webservice.accesscontrol.AccessControlServiceSoapPort#setInheritPermission(org.alfresco.repo.webservice.types.Predicate, boolean)
468      */

469     public ACL[] setInheritPermission(final Predicate predicate, final boolean inheritPermission) throws RemoteException JavaDoc, AccessControlFault
470     {
471         try
472         {
473             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<ACL[]>()
474             {
475                 public ACL[] doWork() throws Exception JavaDoc
476                 {
477                     return setInheritPermissionImpl(predicate, inheritPermission);
478                 }
479             });
480         }
481         catch (Throwable JavaDoc exception)
482         {
483             if (logger.isDebugEnabled())
484             {
485                 logger.error("Unexpected error occurred", exception);
486             }
487             
488             throw new ActionFault(0, exception.getMessage());
489         }
490     }
491
492     /**
493      * Set the inherit permissions flag
494      *
495      * @param predicate the predicate
496      * @param inheritPermission indicates whether the permissions are inherited or not
497      * @return the updated acl's
498      */

499     private ACL[] setInheritPermissionImpl(Predicate predicate, boolean inheritPermission)
500     {
501         // Resolve the predicate
502
List JavaDoc<NodeRef> nodes = Utils.resolvePredicate(predicate, this.nodeService, this.searchService, this.namespaceService);
503         ACL[] acls = new ACL[nodes.size()];
504         
505         int count = 0;
506         for (NodeRef node : nodes)
507         {
508             // Set the inherited permission value
509
this.permissionService.setInheritParentPermissions(node, inheritPermission);
510             
511             // Add the ACL of the modified node to the result
512
acls[count] = getACLFromNodeRef(node, null);
513             count ++;
514         }
515         
516         return acls;
517     }
518
519     /**
520      * @see org.alfresco.repo.webservice.accesscontrol.AccessControlServiceSoapPort#getOwners(org.alfresco.repo.webservice.types.Predicate)
521      */

522     public OwnerResult[] getOwners(final Predicate predicate) throws RemoteException JavaDoc, AccessControlFault
523     {
524         try
525         {
526             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<OwnerResult[]>()
527             {
528                 public OwnerResult[] doWork() throws Exception JavaDoc
529                 {
530                     return getOwnersImpl(predicate);
531                 }
532             });
533         }
534         catch (Throwable JavaDoc exception)
535         {
536             if (logger.isDebugEnabled())
537             {
538                 logger.error("Unexpected error occurred", exception);
539             }
540             
541             throw new ActionFault(0, exception.getMessage());
542         }
543     }
544
545     /**
546      * Gets the owners of the nodes
547      *
548      * @param predicate the predicate
549      * @return the owner details
550      */

551     private OwnerResult[] getOwnersImpl(Predicate predicate)
552     {
553         // Convert predicate
554
List JavaDoc<NodeRef> nodes = Utils.resolvePredicate(predicate, this.nodeService, this.searchService, this.namespaceService);
555         OwnerResult[] result = new OwnerResult[nodes.size()];
556         
557         int count = 0;
558         for (NodeRef node : nodes)
559         {
560             // Get the current owner of the node
561
String JavaDoc owner = this.ownableService.getOwner(node);
562             
563             // Marshal into result
564
result[count] = new OwnerResult(Utils.convertToReference(node), owner);
565             count ++;
566         }
567         
568         return result;
569     }
570
571     /**
572      * @see org.alfresco.repo.webservice.accesscontrol.AccessControlServiceSoapPort#setOwners(org.alfresco.repo.webservice.types.Predicate, java.lang.String)
573      */

574     public OwnerResult[] setOwners(final Predicate predicate, final String JavaDoc owner) throws RemoteException JavaDoc, AccessControlFault
575     {
576         try
577         {
578             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<OwnerResult[]>()
579             {
580                 public OwnerResult[] doWork() throws Exception JavaDoc
581                 {
582                     return setOwnersImpl(predicate, owner);
583                 }
584             });
585         }
586         catch (Throwable JavaDoc exception)
587         {
588             if (logger.isDebugEnabled())
589             {
590                 logger.error("Unexpected error occurred", exception);
591             }
592             
593             throw new ActionFault(0, exception.getMessage());
594         }
595     }
596
597     /**
598      * Set the owner of a nodes
599      *
600      * @param predicate the predicate
601      * @param owner the owner
602      * @return the owner results updated
603      */

604     private OwnerResult[] setOwnersImpl(Predicate predicate, String JavaDoc owner)
605     {
606         // Convert predicate
607
List JavaDoc<NodeRef> nodes = Utils.resolvePredicate(predicate, this.nodeService, this.searchService, this.namespaceService);
608         OwnerResult[] result = new OwnerResult[nodes.size()];
609         
610         int count = 0;
611         for (NodeRef node : nodes)
612         {
613             // Set the owner of the node
614
this.ownableService.setOwner(node, owner);
615             
616             // Marshal into result
617
result[count] = new OwnerResult(Utils.convertToReference(node), owner);
618             count ++;
619         }
620         
621         return result;
622     }
623 }
624
Popular Tags