KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > webdav > stores > JahiaDescriptorsStore


1 package org.jahia.services.webdav.stores;
2
3 import org.apache.slide.common.*;
4 import org.apache.slide.content.*;
5 import org.apache.slide.store.*;
6 import org.apache.slide.store.impl.rdbms.AbstractRDBMSStore;
7
8 import org.apache.slide.structure.*;
9 import org.apache.slide.util.logger.Logger;
10 import org.apache.slide.security.NodePermission;
11 import org.jahia.exceptions.JahiaException;
12 import org.jahia.registries.ServicesRegistry;
13 import org.jahia.services.database.ConnectionDispenser;
14 import org.jahia.services.usermanager.JahiaUser;
15 import org.jahia.services.usermanager.JahiaGroup;
16 import org.jahia.services.usermanager.JahiaGroupManagerService;
17 import org.jahia.services.usermanager.JahiaUserManagerService;
18 import org.jahia.services.version.EntryLoadRequest;
19 import org.jahia.services.sites.JahiaSite;
20
21 import javax.transaction.xa.Xid JavaDoc;
22 import javax.transaction.xa.XAException JavaDoc;
23 import java.sql.*;
24 import java.util.Date JavaDoc;
25 import java.util.*;
26
27 import slideroles.basic.GuestRoleImpl;
28 import slideroles.basic.RootRoleImpl;
29 import slideroles.basic.UserRoleImpl;
30
31 /**
32  * JDBC 1.0 and 2.0 compliant store implementation.
33  *
34  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
35  * @author Dirk Verbeeck
36  * @version $Revision: 14990 $
37  */

38
39 public class JahiaDescriptorsStore extends AbstractRDBMSStore
40         implements LockStore, NodeStore, RevisionDescriptorsStore, RevisionDescriptorStore, SecurityStore, ContentStore {
41
42     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (
43             JahiaDescriptorsStore.class);
44
45     // -------------------------------------------------------------- Constants
46
protected int siteId = -1;
47
48     public static final int INTERNAL_FOLDER = 32;
49     public static final int FILES_CONTAINER = 64;
50     public static final int FILES = 128;
51     public static final int UNCACHEABLE = 256;
52     public static final int TEMP_CACHEABLE = 512;
53
54     public static final int ROOT = 22 | FILES;
55     public static final int ACTIONS = 23 | INTERNAL_FOLDER;
56     public static final int ACTION = 24 | INTERNAL_FOLDER;
57
58     public static final int USERS = 1 | INTERNAL_FOLDER;
59     public static final int USER = 2 | INTERNAL_FOLDER;
60     public static final int USER_FOLDER = 3 | FILES_CONTAINER;
61     public static final int USER_GROUPS = 10 | INTERNAL_FOLDER | TEMP_CACHEABLE;
62     public static final int USER_GROUP = 11 | INTERNAL_FOLDER;
63     public static final int USER_GROUP_PP = 12 | FILES_CONTAINER;
64     public static final int GROUPS = 4 | INTERNAL_FOLDER;
65     public static final int GROUP = 5 | INTERNAL_FOLDER;
66     public static final int GROUP_MEMBERS = 6 | INTERNAL_FOLDER | TEMP_CACHEABLE;
67     public static final int GROUP_MEMBER = 7 | INTERNAL_FOLDER;
68     public static final int GROUP_MEMBER_PP = 13 | FILES_CONTAINER;
69     public static final int GROUP_FOLDER = 8 | FILES_CONTAINER;
70     public static final int LINKED_FOLDER = 9 | INTERNAL_FOLDER;
71
72     protected ContentStore contentStore;
73
74     /**
75      * Initializes the data source with a set of parameters.
76      *
77      * @param parameters Hashtable containing the parameters' name
78      * and associated value
79      *
80      * @throws org.apache.slide.common.ServiceParameterErrorException Incorrect service parameter
81      * @throws org.apache.slide.common.ServiceParameterMissingException
82      * Service parameter missing
83      */

84     public void setParameters (Hashtable parameters)
85             throws ServiceParameterErrorException,
86             ServiceParameterMissingException {
87         adapter = new JahiaJDBCAdapter(this, getLogger());
88         useRdbmsExpressionFactory = false;
89
90         String JavaDoc className = (String JavaDoc) parameters.get("contentStore");
91         if (className == null) {
92             contentStore = new FileContentStore();
93         } else {
94             try {
95                 contentStore = (ContentStore) Class.forName(className).newInstance();
96             } catch (Exception JavaDoc e) {
97                 throw new ServiceParameterErrorException(this, "contentStore");
98             }
99         }
100         Hashtable contentParams = new Hashtable();
101         for (Iterator iterator = parameters.keySet().iterator(); iterator.hasNext();) {
102             String JavaDoc key = (String JavaDoc) iterator.next();
103             if (key.startsWith("contentStore_")) {
104                 contentParams.put(key.substring("contentStore_".length()), parameters.get(key));
105             }
106         }
107         contentStore.setParameters(contentParams);
108
109         tmCommits = true;
110     }
111
112     /**
113      * Connects to JDBC and creates the basic table structure.
114      *
115      * @throws org.apache.slide.common.ServiceConnectionFailedException
116      * Connection to the
117      * database failed
118      */

119     public synchronized void connect ()
120             throws ServiceConnectionFailedException {
121         contentStore.connect();
122     }
123
124     /**
125      * Disconnects from data source.
126      *
127      * @throws org.apache.slide.common.ServiceDisconnectionFailedException
128      * Disconnection
129      * from database failed
130      */

131     public void disconnect ()
132             throws ServiceDisconnectionFailedException {
133         contentStore.disconnect();
134     }
135
136     /**
137      * Initializes data source.
138      * <p/>
139      * Occurs in four steps :
140      * <li>Driver class is loaded</li>
141      * <li>Driver is intantiated</li>
142      * <li>Driver registration in the driver manager</li>
143      * <li>Creation of the basic tables, if they didn't exist before</li>
144      *
145      * @throws org.apache.slide.common.ServiceInitializationFailedException
146      * Throws an exception
147      * if the data source has already been initialized before
148      */

149     public synchronized void initialize (NamespaceAccessToken token)
150             throws ServiceInitializationFailedException {
151         try {
152             if (namespace != null) {
153                 siteId =
154                         ServicesRegistry.getInstance ().getJahiaSitesService ().getSiteByKey (
155                                 namespace.getName ())
156                         .getID ();
157             }
158
159             contentStore.initialize(token);
160         } catch (JahiaException e) {
161             e.printStackTrace (); //To change body of catch statement use Options | File Templates.
162
throw new ServiceInitializationFailedException (this, e);
163         }
164     }
165
166     protected Connection getCurrentConnection() {
167         return ConnectionDispenser.getConnection();
168     }
169
170     protected Connection getNewConnection () {
171         return getCurrentConnection();
172     }
173
174     /**
175      * Close specified statement.
176      */

177     protected void closeStatement (Statement statement) {
178         if (statement != null) {
179             try {
180                 statement.close ();
181             } catch (SQLException e) {
182             }
183         }
184     }
185
186     public PathItem getPathItem(Uri uri) throws ServiceAccessException {
187         try {
188             JahiaSite site = ServicesRegistry.getInstance().getJahiaSitesService().getSiteByKey(uri.getNamespace().getName());
189             List path = new ArrayList();
190             StringTokenizer tok = new StringTokenizer(uri.toString(), "/");
191
192             if (!tok.hasMoreTokens()) {
193                 return new PathItem(null, ROOT, site, path);
194             }
195
196             String JavaDoc r = tok.nextToken();
197             path.add(r);
198             EntryLoadRequest loadRequest;
199             if ("users".equals(r)) {
200                 return getUsersPathItem(uri, site, tok, r, path);
201             } else if ("groups".equals(r)) {
202                 return getUsersPathItem(uri, site, tok, r, path);
203             } else if ("actions".equals(r)) {
204                 return getActionsPathItem(uri, site, tok, r, path);
205             } else {
206                 return new PathItem(r, FILES, site, path);
207             }
208         } catch (JahiaException ex) {
209             ex.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
210
}
211         throw new ServiceAccessException(this, "Invalid uri - "+uri);
212     }
213
214     private PathItem getUsersPathItem(Uri uri, JahiaSite site, StringTokenizer tok, String JavaDoc root, List path) {
215         if (root.equals ("users")) {
216             if (tok.countTokens () == 0)
217                 return new PathItem(root, USERS, site, path);
218
219             String JavaDoc userName = tok.nextToken ();
220             path.add (userName);
221
222             // /users/username
223
if (tok.countTokens () == 0)
224                 return new PathItem(root, USER, site, path);
225
226             String JavaDoc folderName = tok.nextToken ();
227             path.add (folderName);
228
229             if (folderName.equals ("mygroups")) {
230                 if (tok.countTokens () == 0)
231                     return new PathItem(root, USER_GROUPS, site, path);
232                 path.add (tok.nextToken ());
233                 if (tok.countTokens () == 0)
234                     return new PathItem(root, USER_GROUP, site, path);
235
236                 path.add (tok.nextToken ());
237                 if (tok.countTokens () == 0)
238                     return new PathItem(root, USER_GROUP_PP, site, path);
239
240                 return new PathItem(root, LINKED_FOLDER, site, path);
241             } else {
242                 if (tok.countTokens () == 0)
243                     return new PathItem(root, USER_FOLDER, site, path);
244                 path.add (tok.nextToken ());
245                 return new PathItem(root, FILES, site, path);
246             }
247         } else if (root.equals ("groups")) {
248             // /groups/**
249
if (tok.countTokens () == 0)
250                 return new PathItem(root, GROUPS, site, path);
251
252             String JavaDoc groupName = tok.nextToken ();
253             path.add (groupName);
254
255             if (tok.countTokens () == 0)
256                 return new PathItem(root, GROUP, site, path);
257
258             String JavaDoc folderName = tok.nextToken ();
259             path.add (folderName);
260
261             if (folderName.equals ("members")) {
262                 if (tok.countTokens () == 0)
263                     return new PathItem(root, GROUP_MEMBERS, site, path);
264                 path.add (tok.nextToken ());
265                 if (tok.countTokens () == 0)
266                     return new PathItem(root, GROUP_MEMBER, site, path);
267
268                 path.add (tok.nextToken ());
269                 if (tok.countTokens () == 0)
270                     return new PathItem(root, GROUP_MEMBER_PP, site, path);
271
272                 return new PathItem(root, LINKED_FOLDER, site, path);
273             } else {
274                 if (tok.countTokens () == 0)
275                     return new PathItem(root, GROUP_FOLDER, site, path);
276                 path.add (tok.nextToken ());
277                 return new PathItem(root, FILES, site, path);
278             }
279         }
280         return new PathItem(root, FILES, site, path);
281     }
282
283     private PathItem getActionsPathItem(Uri uri, JahiaSite site, StringTokenizer tok, String JavaDoc root, List path) {
284         if (tok.countTokens () == 0) {
285             return new PathItem(root, ACTIONS, site, path);
286         } else {
287             return new PathItem(root, ACTION, site, path);
288         }
289     }
290
291     protected NodeRevisionDescriptors getCollectionDescriptors(Uri uri) {
292         NodeRevisionNumber rev = new NodeRevisionNumber(1, 0);
293         Hashtable workingRevisions = new Hashtable();
294         Hashtable latestRevisionNumbers = new Hashtable();
295         latestRevisionNumbers.put("main", rev);
296         Hashtable branches = new Hashtable();
297         branches.put(rev, new Vector());
298         return new NodeRevisionDescriptors(uri.toString(),rev,workingRevisions,latestRevisionNumbers,
299                 branches,false);
300     }
301
302     protected NodeRevisionDescriptor getCollectionDescriptor(Uri uri) {
303         NodeRevisionDescriptor revisionDescriptor = new NodeRevisionDescriptor(new NodeRevisionNumber(),"",new Vector(), new Hashtable());
304         revisionDescriptor.setContentLength(0);
305         revisionDescriptor.setName(new UriPath(uri.toString()).lastSegment());
306         revisionDescriptor.setResourceType("<collection/>");
307         revisionDescriptor.setCreationDate(new Date JavaDoc());
308         revisionDescriptor.setLastModified(new Date JavaDoc());
309         revisionDescriptor.setSource("");
310         return revisionDescriptor;
311     }
312
313     protected boolean includeBranchInXid() {
314         return false;
315     }
316
317     public int getSiteId() {
318         return siteId;
319     }
320
321     /********************************************************************
322      * Nodestore
323      ********************************************************************/

324
325     public ObjectNode retrieveObject (Uri uri)
326             throws ServiceAccessException, ObjectNotFoundException {
327         PathItem item = null;
328         try {
329             item = getPathItem(uri);
330         } catch (ServiceAccessException e) {
331             throw new ObjectNotFoundException(uri);
332         }
333         List path = item.path;
334         switch (item.type) {
335             case ROOT:
336                 return processRoot(uri);
337             case ACTIONS:
338                 return processActions(uri);
339             case ACTION:
340                 return processAction(uri);
341             case USERS:
342                 return processUsersDirectory (uri);
343             case USER:
344                 return processUserDirectory (uri, (String JavaDoc) path.get (1));
345             case USER_FOLDER:
346                 return processUserFolderDirectory (uri, (String JavaDoc) path.get (1),
347                         (String JavaDoc) path.get (2));
348             case USER_GROUPS:
349                 return processUserFolderDirectory (uri, (String JavaDoc) path.get (1),
350                         (String JavaDoc) path.get (2));
351             case USER_GROUP:
352                 return processUserGroupDirectory (uri, (String JavaDoc) path.get (1),
353                         (String JavaDoc) path.get (3));
354             case USER_GROUP_PP:
355                 return processUserGroupSubDirectory (uri, (String JavaDoc) path.get (1),
356                         (String JavaDoc) path.get (3), (String JavaDoc) path.get (4));
357             case GROUPS:
358                 return processGroupsDirectory (uri);
359             case GROUP:
360                 return processGroupDirectory (uri, (String JavaDoc) path.get (1));
361             case GROUP_FOLDER:
362                 return processGroupFolderDirectory (uri, (String JavaDoc) path.get (1),
363                         (String JavaDoc) path.get (2));
364             case GROUP_MEMBERS:
365                 return processGroupFolderDirectory (uri, (String JavaDoc) path.get (1),
366                         (String JavaDoc) path.get (2));
367             case GROUP_MEMBER:
368                 return processGroupMemberDirectory (uri, (String JavaDoc) path.get (1),
369                         (String JavaDoc) path.get (3));
370             case GROUP_MEMBER_PP:
371                 return processGroupMemberSubDirectory (uri, (String JavaDoc) path.get (1),
372                         (String JavaDoc) path.get (3), (String JavaDoc) path.get (4));
373             case FILES:
374                 return super.retrieveObject (uri);
375             case LINKED_FOLDER:
376                 throw new ObjectNotFoundException (uri);
377         }
378         throw new ServiceAccessException (this, uri.toString ());
379     }
380
381     public void createObject (Uri uri, ObjectNode object)
382             throws ServiceAccessException, ObjectAlreadyExistsException {
383         PathItem item = getPathItem(uri);
384
385         if ((item.type & FILES) == FILES) {
386             super.createObject (uri, object);
387         } else {
388             throw new ServiceAccessException (this, "Cannot create anything here");
389         }
390     }
391
392     public void storeObject (Uri uri, ObjectNode object)
393             throws ServiceAccessException, ObjectNotFoundException {
394         PathItem item = getPathItem(uri);
395         List path = item.path;
396         int i = item.type;
397
398         if ((i & FILES_CONTAINER) == FILES_CONTAINER) {
399             try {
400                 super.createObject(uri, object);
401             } catch (ObjectAlreadyExistsException e) {
402                 super.storeObject (uri, object);
403             }
404         } else if ((i & FILES) == FILES) {
405             super.storeObject (uri, object);
406         } else {
407             throw new ServiceAccessException (this, "Cannot create anything here");
408         }
409
410         // A file may have been added or removed, need to recalculate the main folder (/users or /groups)
411
if (path.size () > 1) {
412             Uri rootUri = new Uri (uri.getNamespace (), "/" + (String JavaDoc) path.get (0));
413             Store store = rootUri.getStore ();
414             if (store instanceof CachedStore) {
415                 ((CachedStore) store).removeObjectFromCache (rootUri);
416             }
417         }
418     }
419
420     public void removeObject (Uri uri, ObjectNode object)
421             throws ServiceAccessException, ObjectNotFoundException {
422         PathItem item = getPathItem(uri);
423         if ((item.type & FILES) == FILES) {
424             super.removeObject (uri, object);
425         } else {
426             throw new ServiceAccessException (this, "Cannot create anything here");
427         }
428     }
429
430     /********************************************************************
431      * RevisionDescriptorsStore
432      ********************************************************************/

433
434     public NodeRevisionDescriptors retrieveRevisionDescriptors (Uri uri)
435             throws ServiceAccessException, RevisionDescriptorNotFoundException {
436         PathItem item = getPathItem(uri);
437
438         if ((item.type & (INTERNAL_FOLDER|FILES_CONTAINER)) == 0) {
439             return super.retrieveRevisionDescriptors (uri);
440         }
441
442         return getCollectionDescriptors(uri);
443     }
444
445     public void createRevisionDescriptors (Uri uri,
446                                            NodeRevisionDescriptors revisionDescriptors)
447             throws ServiceAccessException {
448         PathItem item = getPathItem(uri);
449
450         if ((item.type & (INTERNAL_FOLDER|FILES_CONTAINER)) == 0) {
451             super.createRevisionDescriptors (uri, revisionDescriptors);
452         }
453     }
454
455     public void storeRevisionDescriptors (Uri uri,
456                                           NodeRevisionDescriptors revisionDescriptors)
457             throws ServiceAccessException, RevisionDescriptorNotFoundException {
458         PathItem item = getPathItem(uri);
459         if ((item.type & (INTERNAL_FOLDER|FILES_CONTAINER)) == 0) {
460             super.storeRevisionDescriptors (uri, revisionDescriptors);
461         }
462     }
463
464     public void removeRevisionDescriptors (Uri uri)
465             throws ServiceAccessException {
466         PathItem item = getPathItem(uri);
467         if ((item.type & (INTERNAL_FOLDER|FILES_CONTAINER)) == 0) {
468             super.removeRevisionDescriptors (uri);
469         }
470     }
471
472     /********************************************************************
473      * RevisionDescriptorStore
474      ********************************************************************/

475
476     public NodeRevisionDescriptor retrieveRevisionDescriptor
477             (Uri uri, NodeRevisionNumber revisionNumber)
478             throws ServiceAccessException, RevisionDescriptorNotFoundException {
479
480         PathItem item = getPathItem(uri);
481         if ((item.type & (INTERNAL_FOLDER|FILES_CONTAINER)) == 0) {
482             return super.retrieveRevisionDescriptor (uri, revisionNumber);
483         }
484
485         return getCollectionDescriptor(uri);
486     }
487
488     public void createRevisionDescriptor (Uri uri,
489                                           NodeRevisionDescriptor revisionDescriptor)
490             throws ServiceAccessException {
491         if ((getPathItem(uri).type & (INTERNAL_FOLDER|FILES_CONTAINER)) == 0) {
492             super.createRevisionDescriptor (uri, revisionDescriptor);
493         }
494     }
495
496     public void storeRevisionDescriptor (Uri uri,
497                                          NodeRevisionDescriptor revisionDescriptor)
498             throws ServiceAccessException, RevisionDescriptorNotFoundException {
499         if ((getPathItem(uri).type & (INTERNAL_FOLDER|FILES_CONTAINER)) == 0) {
500             super.storeRevisionDescriptor (uri, revisionDescriptor);
501         }
502     }
503
504     public void removeRevisionDescriptor (Uri uri, NodeRevisionNumber number)
505             throws ServiceAccessException {
506         if ((getPathItem(uri).type & (INTERNAL_FOLDER|FILES_CONTAINER)) == 0) {
507             super.removeRevisionDescriptor (uri, number);
508         }
509     }
510
511
512     /********************************************************************
513      * ContentStore
514      ********************************************************************/

515
516     public NodeRevisionContent retrieveRevisionContent(Uri uri, NodeRevisionDescriptor revisionDescriptor) throws ServiceAccessException, RevisionNotFoundException {
517         PathItem item = getPathItem(uri);
518         if ((item.type & FILES) == FILES) {
519             return contentStore.retrieveRevisionContent(uri, revisionDescriptor);
520         }
521         throw new RevisionNotFoundException(uri.toString(), revisionDescriptor.getRevisionNumber());
522     }
523
524     public void createRevisionContent(Uri uri, NodeRevisionDescriptor revisionDescriptor, NodeRevisionContent revisionContent) throws ServiceAccessException, RevisionAlreadyExistException {
525         PathItem item = getPathItem(uri);
526         if ((item.type & FILES) == FILES) {
527             contentStore.createRevisionContent(uri, revisionDescriptor, revisionContent);
528         }
529     }
530
531     public void storeRevisionContent(Uri uri, NodeRevisionDescriptor revisionDescriptor, NodeRevisionContent revisionContent) throws ServiceAccessException, RevisionNotFoundException {
532         PathItem item = getPathItem(uri);
533         if ((item.type & FILES) == FILES) {
534             contentStore.storeRevisionContent(uri, revisionDescriptor, revisionContent);
535         }
536     }
537
538     public void removeRevisionContent(Uri uri, NodeRevisionDescriptor revisionDescriptor) throws ServiceAccessException {
539         PathItem item = getPathItem(uri);
540         if ((item.type & FILES) == FILES) {
541             contentStore.removeRevisionContent(uri, revisionDescriptor);
542         }
543     }
544
545
546
547     /********************************************************************
548      * SecurityStore
549      ********************************************************************/

550
551     public void grantPermission (Uri uri, NodePermission permission)
552             throws ServiceAccessException {
553         if ((getPathItem(uri).type & FILES) == FILES) {
554             super.grantPermission (uri, permission);
555         } else {
556             throw new ServiceAccessException (this, "Cannot create anything here");
557         }
558     }
559
560     public void revokePermission (Uri uri, NodePermission permission)
561             throws ServiceAccessException {
562         if ((getPathItem(uri).type & FILES) == FILES) {
563             super.revokePermission (uri, permission);
564         } else {
565             throw new ServiceAccessException (this, "Cannot create anything here");
566         }
567     }
568
569     public void revokePermissions (Uri uri)
570             throws ServiceAccessException {
571         if ((getPathItem(uri).type & FILES) == FILES) {
572             super.revokePermissions (uri);
573         } else {
574             throw new ServiceAccessException (this, "Cannot create anything here");
575         }
576     }
577
578     public Enumeration enumeratePermissions (Uri uri)
579             throws ServiceAccessException {
580         Vector perms = new Vector ();
581         PathItem pathItem = getPathItem(uri);
582         int type = pathItem.type;
583         List path = pathItem.path;
584         switch (type) {
585             case USER_FOLDER:
586                 grantAll (uri, "/users/" + (String JavaDoc) path.get (1), perms);
587                 if (path.get (2).equals ("private")) {
588                     denyGuest (uri, perms);
589                 }
590                 return perms.elements ();
591             case GROUP_FOLDER:
592                 grantAll (uri, "+/groups/" + (String JavaDoc) path.get (1) + "/members", perms);
593                 if (path.get (2).equals ("private")) {
594                     denyGuest (uri, perms);
595                 }
596                 return perms.elements ();
597             case FILES:
598                 return super.enumeratePermissions (uri);
599             default:
600                 denyChanges (uri, perms);
601                 return perms.elements ();
602         }
603     }
604
605     private void grantAll (Uri uri, String JavaDoc user, Vector perms) {
606         perms.add (new NodePermission (uri.toString (), null, user,
607                 "/actions/read", true, false));
608         perms.add (new NodePermission (uri.toString (), null, user,
609                 "/actions/write", true, false));
610         perms.add (new NodePermission (uri.toString (), null, user,
611                 "/actions/manage", true, false));
612     }
613
614     private void denyGuest (Uri uri, Vector perms) {
615         perms.add (new NodePermission (uri.toString (), null, "all",
616                 "/actions/read", true, true));
617         perms.add (new NodePermission (uri.toString (), null, "all",
618                 "/actions/write", true, true));
619         perms.add (new NodePermission (uri.toString (), null, "all",
620                 "/actions/manage", true, true));
621     }
622
623     private void denyChanges (Uri uri, Vector perms) {
624         perms.add (new NodePermission (uri.toString (), null, "all",
625                 "/actions/read", true, false));
626         perms.add (new NodePermission (uri.toString (), null, "all",
627                 "/actions/write", true, true));
628         perms.add (new NodePermission (uri.toString (), null, "all",
629                 "/actions/manage", true, true));
630     }
631
632     private JahiaUser getUser (Uri uri, String JavaDoc memberName) throws ObjectNotFoundException,
633             ServiceAccessException {
634 // JahiaUserManagerService userManager = ServicesRegistry.getInstance().getJahiaUserManagerService();
635
// JahiaUser user = userManager.lookupUser(siteId, memberName);
636
JahiaUser user = null;
637         try {
638             user =
639                     ServicesRegistry.getInstance ().getJahiaSiteUserManagerService ().getMember (
640                             siteId, memberName);
641         } catch (JahiaException je) {
642             logger.warn ("Couldn't find user " + memberName + " on site " + siteId, je);
643             if (!getUsersFolders ("/users/").contains (memberName)) {
644                 throw new ObjectNotFoundException (uri);
645             }
646         }
647         return user;
648     }
649
650     private JahiaGroup getGroup (Uri uri, String JavaDoc groupName) throws ObjectNotFoundException,
651             ServiceAccessException {
652         JahiaGroupManagerService groupService = ServicesRegistry.getInstance ()
653                 .getJahiaGroupManagerService ();
654         JahiaGroup group = groupService.lookupGroup (siteId, groupName);
655         if (group == null) {
656             if (!getUsersFolders ("/groups/").contains (groupName)) {
657                 throw new ObjectNotFoundException (uri);
658             }
659         }
660         return group;
661     }
662
663    private ObjectNode processRoot(Uri uri) throws ObjectNotFoundException, ServiceAccessException {
664         ObjectNode r = super.retrieveObject(uri);
665         Vector bindings = new Vector();
666         Enumeration enumeration = r.enumerateBindings();
667         while (enumeration.hasMoreElements()) {
668             ObjectNode.Binding binding = (ObjectNode.Binding)enumeration.nextElement();
669             if (binding.getName().equals("shared")) {
670                 bindings.add(binding);
671             }
672         }
673         bindings.add(new SubjectNode.Binding("users","/users"));
674         bindings.add(new SubjectNode.Binding("groups","/groups"));
675 // bindings.add(new SubjectNode.Binding("shared","/shared"));
676
// bindings.add(new SubjectNode.Binding("content","/content"));
677
// bindings.add(new SubjectNode.Binding("liveContent","/liveContent"));
678
bindings.add(new SubjectNode.Binding("actions","/actions"));
679         r = new SubjectNode("/",bindings,new Vector(), new Vector());
680         r.setUri("/");
681         return r;
682     }
683
684     private ObjectNode processActions(Uri uri) throws ObjectNotFoundException, ServiceAccessException {
685         Vector bindings = new Vector();
686         bindings.add(new SubjectNode.Binding("read","/actions/read"));
687         bindings.add(new SubjectNode.Binding("write","/actions/write"));
688         bindings.add(new SubjectNode.Binding("manage","/actions/manage"));
689         ObjectNode r = new SubjectNode("/actions",bindings,new Vector(), new Vector());
690         r.setUri("/actions");
691         return r;
692     }
693
694     private ObjectNode processAction(Uri uri) throws ObjectNotFoundException, ServiceAccessException {
695         Vector bindings = new Vector();
696         ObjectNode r = new ActionNode(uri.toString() ,bindings,new Vector(), new Vector());
697         r.setUri(uri.toString());
698         return r;
699     }
700
701     private SubjectNode processUsersDirectory (Uri uri) throws ObjectNotFoundException,
702             ServiceAccessException {
703         Vector children = new Vector ();
704
705         Set users = getUsersFolders("/users/");
706         for (Iterator iterator = users.iterator (); iterator.hasNext ();) {
707             String JavaDoc key = (String JavaDoc) iterator.next ();
708             if (!JahiaUserManagerService.GUEST_USERNAME.equals (key)) {
709                 children.add (new SubjectNode.Binding(key,"/users/"+key));
710             }
711         }
712         Vector parents = new Vector();
713         parents.add(new SubjectNode.ParentBinding("users","/"));
714         SubjectNode subject = new UsersNode (uri.toString (), children, parents, new Vector (), siteId);
715         subject.setUri(uri.toString());
716         return subject;
717     }
718
719     private SubjectNode processUserDirectory (Uri uri, String JavaDoc userName)
720             throws ObjectNotFoundException, ServiceAccessException {
721         // /users/username - INTERNAL_FOLDER
722
SubjectNode subject;
723         Vector childrenVector = new Vector ();
724         Vector parentsVector = new Vector ();
725         Vector linksVector = new Vector ();
726         parentsVector.add (new SubjectNode.ParentBinding(userName,"/users"));
727         if (userName.equals (JahiaUserManagerService.GUEST_USERNAME)) {
728             subject = new GuestRoleImpl (uri.toString (), childrenVector, parentsVector, linksVector);
729         } else {
730             childrenVector.add (new SubjectNode.Binding("public","/users/" + userName + "/public"));
731             childrenVector.add (new SubjectNode.Binding("private","/users/" + userName + "/private"));
732
733             JahiaUser user = getUser (uri, userName);
734             if (user == null) {
735                 if (!getUsersFolders("/users/").contains(userName)) {
736                     throw new ObjectNotFoundException(uri);
737                 }
738                 subject = new SubjectNode(uri.toString(), childrenVector, parentsVector, linksVector);
739             } else {
740                 childrenVector.add (new SubjectNode.Binding("mygroups","/users/" + userName + "/mygroups"));
741                 if (user.isAdminMember (siteId)) {
742                     subject = new RootRoleImpl (uri.toString (), childrenVector, parentsVector, linksVector);
743                 } else {
744                     subject = new UserRoleImpl (uri.toString (), childrenVector, parentsVector, linksVector);
745                 }
746             }
747         }
748         subject.setUri(uri.toString());
749         return subject;
750     }
751
752     private SubjectNode processUserFolderDirectory (Uri uri, String JavaDoc userName,
753                                                     String JavaDoc folderName)
754             throws ObjectNotFoundException, ServiceAccessException {
755         // /users/username/[private|public] - FILES_CONTAINER
756
SubjectNode subject;
757         if (userName.equals(JahiaUserManagerService.GUEST_USERNAME)) {
758             throw new ObjectNotFoundException (uri);
759         }
760         JahiaUser user = getUser (uri, userName);
761
762         Vector parents = new Vector();
763         parents.add(new SubjectNode.ParentBinding(folderName, "/users/"+userName));
764
765         if (folderName.equals ("mygroups") && (user != null)) {
766             // /users/username/groups
767
Vector children = new Vector ();
768
769             JahiaGroupManagerService groupService = ServicesRegistry.getInstance ()
770                     .getJahiaGroupManagerService ();
771             Set groupNames = new HashSet ();
772             Enumeration groups = groupService.getUserMembership (user).elements ();
773             while (groups.hasMoreElements ()) {
774                 String JavaDoc jahiaGroup = (String JavaDoc) groups.nextElement ();
775                 JahiaGroup g = groupService.lookupGroup(jahiaGroup);
776                 if (g != null) {
777                     groupNames.add (g.getGroupname ());
778                 }
779             }
780             for (Iterator iterator = groupNames.iterator (); iterator.hasNext ();) {
781                 String JavaDoc key = (String JavaDoc) iterator.next ();
782                 if (!JahiaGroupManagerService.GUEST_GROUPNAME.equals (key) && !JahiaGroupManagerService.USERS_GROUPNAME.equals (
783                         key)) {
784                     children.add (new SubjectNode.Binding(key, "/users/" + userName + "/mygroups/" + key));
785                 }
786             }
787             subject = new SubjectNode (uri.toString (), children, parents, new Vector ());
788         } else if (folderName.equals ("public") || folderName.equals ("private")) {
789             Vector childrenVector = new Vector ();
790             Vector linksVector = new Vector ();
791             try {
792                 subject = (SubjectNode) super.retrieveObject(uri);
793                 Vector v = subject.getChildren();
794                 for (Iterator iterator = v.iterator(); iterator.hasNext();) {
795                     String JavaDoc s = (String JavaDoc) iterator.next();
796                     s = s.substring(s.lastIndexOf('/')+1);
797                     childrenVector.add(new SubjectNode.Binding(s,subject.getBindingUuri(s)));
798                 }
799             } catch (ObjectNotFoundException e) {
800             }
801             subject = new SubjectNode (uri.toString (), childrenVector, parents, linksVector);
802         } else {
803             throw new ObjectNotFoundException (uri);
804         }
805         subject.setUri(uri.toString());
806         return subject;
807     }
808
809     private ObjectNode processUserGroupDirectory (Uri uri, String JavaDoc userName, String JavaDoc groupName)
810             throws ObjectNotFoundException, ServiceAccessException {
811         if (userName.equals(JahiaUserManagerService.GUEST_USERNAME)) {
812             throw new ObjectNotFoundException (uri);
813         }
814         JahiaUser user = getUser (uri, userName);
815         if (user == null || !user.isMemberOfGroup (siteId, groupName)) {
816             throw new ObjectNotFoundException (uri);
817         }
818         Vector v = new Vector ();
819
820         Vector parents = new Vector();
821         parents.add(new SubjectNode.ParentBinding(groupName, "/users/"+userName+"/mygroups"));
822
823         Vector children = new Vector ();
824         children.add (new SubjectNode.Binding("private","/users/" + userName + "/mygroups/" + groupName + "/private"));
825         children.add (new SubjectNode.Binding("public","/users/" + userName + "/mygroups/" + groupName + "/public"));
826         SubjectNode subject = new SubjectNode ("/users/" + userName + "/mygroups/" + groupName, children, parents, v);
827         subject.setUri(uri.toString());
828         return subject;
829     }
830
831     private ObjectNode processUserGroupSubDirectory (Uri uri, String JavaDoc userName,
832                                                      String JavaDoc groupName, String JavaDoc folderName)
833             throws ObjectNotFoundException, ServiceAccessException {
834         if (userName.equals(JahiaUserManagerService.GUEST_USERNAME)) {
835             throw new ObjectNotFoundException (uri);
836         }
837         JahiaUser user = getUser (uri, userName);
838         if (user == null || !user.isMemberOfGroup (siteId, groupName)) {
839             throw new ObjectNotFoundException (uri);
840         }
841         if (!folderName.equals ("public") && !folderName.equals ("private")) {
842             throw new ObjectNotFoundException (uri);
843         }
844         Vector v = new Vector ();
845         Vector children = new Vector ();
846         Uri linkedUri = new Uri (uri.getNamespace (), "/groups/" + groupName + "/" + folderName);
847 // getChildren (linkedUri, children, v);
848
Vector myChildren = new Vector ();
849         for (Iterator iterator = children.iterator (); iterator.hasNext ();) {
850             String JavaDoc s = (String JavaDoc) iterator.next ();
851             myChildren.add ("/users/" + userName + "/my" + s.substring (1));
852         }
853         ObjectNode object = new LinkNode (
854                 "/users/" + userName + "/mygroups/" + groupName + "/" + folderName, myChildren,
855                 v, linkedUri.toString ());
856         object.setUri(uri.toString());
857         return object;
858     }
859
860     private SubjectNode processGroupsDirectory (Uri uri) throws ObjectNotFoundException,
861             ServiceAccessException {
862         Vector children = new Vector ();
863
864         Set users = getUsersFolders("/groups/");
865         for (Iterator iterator = users.iterator (); iterator.hasNext ();) {
866             String JavaDoc key = (String JavaDoc) iterator.next ();
867             if (!JahiaGroupManagerService.GUEST_GROUPNAME.equals (key)) {
868                 children.add (new SubjectNode.Binding(key,"/groups/"+key));
869             }
870         }
871         Vector parents = new Vector();
872         parents.add(new SubjectNode.ParentBinding("groups","/"));
873         SubjectNode subject = new GroupsNode (uri.toString (), children, parents, new Vector (), siteId);
874         subject.setUri(uri.toString());
875         return subject;
876     }
877
878     private SubjectNode processGroupDirectory (Uri uri, String JavaDoc groupName)
879             throws ObjectNotFoundException, ServiceAccessException {
880
881         // /groups/groupname - INTERNAL_FOLDER
882
JahiaGroup group = getGroup (uri, groupName);
883         Vector children = new Vector ();
884         Vector parentsVector = new Vector ();
885         parentsVector.add (new SubjectNode.ParentBinding(groupName,"/groups"));
886
887         if (group != null) {
888             if (!JahiaGroupManagerService.GUEST_GROUPNAME.equals (groupName) && !JahiaGroupManagerService.USERS_GROUPNAME.equals (groupName)) {
889                 children.add (new SubjectNode.Binding("private","/groups/" + groupName + "/private"));
890                 children.add (new SubjectNode.Binding("public","/groups/" + groupName + "/public"));
891             }
892             children.add (new SubjectNode.Binding("members","/groups/" + groupName + "/members"));
893         }
894         SubjectNode subject = new SubjectNode (uri.toString (), children, parentsVector, new Vector ());
895         subject.setUri(uri.toString());
896         return subject;
897     }
898
899     private ObjectNode processGroupFolderDirectory (Uri uri, String JavaDoc groupName,
900                                                     String JavaDoc folderName)
901             throws ObjectNotFoundException, ServiceAccessException {
902         ObjectNode subject;
903         JahiaGroup group = getGroup (uri, groupName);
904
905         Vector parents = new Vector();
906         parents.add(new SubjectNode.ParentBinding(folderName, "/groups/"+groupName));
907
908         if (folderName.equals ("members") && (group != null)) {
909             // /groups/groupname/members
910
Vector children = new Vector ();
911             Enumeration members = group.members ();
912             while (members.hasMoreElements ()) {
913                 JahiaUser user = (JahiaUser) members.nextElement ();
914                 if (!JahiaUserManagerService.GUEST_USERNAME.equals (user.getUsername ())) {
915                     children.add (new SubjectNode.Binding(user.getUsername (),"/groups/" + groupName + "/members/" + user.getUsername ()));
916                 }
917             }
918             subject = new JahiaGroupNode (uri.toString (), children, parents, new Vector ());
919         } else if ((folderName.equals ("public") || folderName.equals ("private")) &&
920                 !JahiaGroupManagerService.GUEST_GROUPNAME.equals (groupName) &&
921                 !JahiaGroupManagerService.USERS_GROUPNAME.equals (groupName)) {
922             // /groups/groupname/[private|public]
923
Vector childrenVector = new Vector ();
924             Vector linksVector = new Vector ();
925             try {
926                 subject = (SubjectNode) super.retrieveObject(uri);
927                 Vector v = subject.getChildren();
928                 for (Iterator iterator = v.iterator(); iterator.hasNext();) {
929                     String JavaDoc s = (String JavaDoc) iterator.next();
930                     s = s.substring(s.lastIndexOf('/')+1);
931                     childrenVector.add(new SubjectNode.Binding(s,subject.getBindingUuri(s)));
932                 }
933             } catch (ObjectNotFoundException e) {
934             }
935             subject = new SubjectNode (uri.toString (), childrenVector, parents, linksVector);
936         } else {
937             throw new ObjectNotFoundException (uri);
938         }
939         subject.setUri(uri.toString());
940         return subject;
941     }
942
943     private ObjectNode processGroupMemberDirectory (Uri uri, String JavaDoc groupName,
944                                                     String JavaDoc memberName)
945             throws ObjectNotFoundException, ServiceAccessException {
946         JahiaUser user = getUser (uri, memberName);
947         if (user == null || !user.isMemberOfGroup (siteId, groupName)) {
948             throw new ObjectNotFoundException (uri);
949         }
950         Vector v = new Vector ();
951
952         Vector parents = new Vector ();
953         parents.add(new SubjectNode.ParentBinding(memberName, "/groups/" + groupName + "/members"));
954
955         Vector children = new Vector ();
956         if (!memberName.equals(JahiaUserManagerService.GUEST_USERNAME)) {
957             children.add (new SubjectNode.Binding("private","/groups/" + groupName + "/members/" + memberName + "/private"));
958             children.add (new SubjectNode.Binding("public", "/groups/" + groupName + "/members/" + memberName + "/public"));
959         }
960         ObjectNode object = new UserLinkNode (
961                 "/groups/" + groupName + "/members/" + memberName, children, parents, v,
962                 "/users/" + memberName);
963         object.setUri(uri.toString());
964         return object;
965     }
966
967
968     private ObjectNode processGroupMemberSubDirectory (Uri uri, String JavaDoc groupName,
969                                                        String JavaDoc memberName, String JavaDoc folderName)
970             throws ObjectNotFoundException, ServiceAccessException {
971         JahiaUser user = getUser (uri, memberName);
972         if (user == null || !user.isMemberOfGroup (siteId, groupName) || memberName.equals(JahiaUserManagerService.GUEST_USERNAME)) {
973             throw new ObjectNotFoundException (uri);
974         }
975         if (!folderName.equals ("public") && !folderName.equals ("private")) {
976             throw new ObjectNotFoundException (uri);
977         }
978         Vector v = new Vector ();
979         Vector children = new Vector ();
980         Uri linkedUri = new Uri (uri.getNamespace (), "/users/" + memberName + "/" + folderName);
981 // getChildren (linkedUri, children, v);
982
Vector myChildren = new Vector ();
983         for (Iterator iterator = children.iterator (); iterator.hasNext ();) {
984             String JavaDoc s = (String JavaDoc) iterator.next ();
985             myChildren.add ("/groups/" + groupName + s);
986         }
987         ObjectNode object = new LinkNode (uri.toString (), myChildren, v, linkedUri.toString ());
988         object.setUri(uri.toString());
989         return object;
990     }
991
992     private Set getUsersFolders (String JavaDoc baseDir) throws ServiceAccessException {
993         Set s = new HashSet ();
994         PreparedStatement statement = null;
995         Connection connection = getNewConnection ();
996         try {
997             statement = connection.prepareStatement("select uri_string from jahia_sl2_uri where uri_string like ? and namespace=?");
998             statement.setString (1, baseDir + "%");
999             statement.setInt(2, siteId);
1000            ResultSet res = statement.executeQuery ();
1001
1002            // Parse result set
1003
while (res.next ()) {
1004                // Load each permission
1005
String JavaDoc folder = res.getString (1);
1006                folder = folder.substring (folder.indexOf ('/', 1) + 1);
1007                if (folder.indexOf('/') != -1) {
1008                    folder = folder.substring (0, folder.indexOf ('/'));
1009                    s.add (folder);
1010                }
1011            }
1012        } catch (SQLException e) {
1013            getLogger ().log (e, LOG_CHANNEL, Logger.ERROR);
1014            throw new ServiceAccessException (this, e);
1015        } finally {
1016            closeStatement (statement);
1017        }
1018        return s;
1019    }
1020
1021    public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
1022        contentStore.forget(xid);
1023    }
1024
1025    public void commit(Xid JavaDoc xid, boolean b) throws XAException JavaDoc {
1026        contentStore.commit(xid, b);
1027    }
1028
1029    public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
1030        contentStore.rollback(xid);
1031    }
1032
1033    public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
1034        return contentStore.prepare(xid);
1035    }
1036
1037    public void end(Xid JavaDoc xid, int i) throws XAException JavaDoc {
1038        contentStore.end(xid, i);
1039    }
1040
1041    public void start(Xid JavaDoc xid, int i) throws XAException JavaDoc {
1042        contentStore.start(xid, i);
1043    }
1044
1045}
Popular Tags