KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > slide > util > AdminHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.slide.util;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.slide.authenticate.CredentialsToken;
26 import org.apache.slide.common.NamespaceAccessToken;
27 import org.apache.slide.common.SlideToken;
28 import org.apache.slide.common.SlideTokenImpl;
29 import org.apache.slide.content.Content;
30 import org.apache.slide.content.NodeProperty;
31 import org.apache.slide.content.NodeRevisionDescriptor;
32 import org.apache.slide.content.NodeRevisionDescriptors;
33 import org.apache.slide.lock.Lock;
34 import org.apache.slide.lock.NodeLock;
35 import org.apache.slide.macro.Macro;
36 import org.apache.slide.macro.MacroParameters;
37 import org.apache.slide.security.NodePermission;
38 import org.apache.slide.security.Security;
39 import org.apache.slide.structure.ObjectNode;
40 import org.apache.slide.structure.ObjectNotFoundException;
41 import org.apache.slide.structure.Structure;
42 import org.apache.slide.structure.SubjectNode;
43
44 /**
45  * Helper class for the slide samples administration application.
46  */

47 public class AdminHelper {
48     
49     private static final SlideToken ROOT = new SlideTokenImpl(new CredentialsToken("root"));
50     
51     public static boolean login(NamespaceAccessToken nat,
52                                 String JavaDoc userId,
53                                 String JavaDoc password) throws Exception JavaDoc {
54         
55         String JavaDoc usersPath = nat.getNamespaceConfig().getUsersPath();
56         String JavaDoc userUri = usersPath + "/" + userId;
57         
58         Content content = nat.getContentHelper();
59         
60         try {
61             NodeRevisionDescriptors revisions = content.retrieve(ROOT,userUri);
62             NodeRevisionDescriptor revision = content.retrieve(ROOT,revisions);
63             NodeProperty property = revision.getProperty(
64                 "password",NodeProperty.SLIDE_NAMESPACE);
65             
66             return property.getValue().equals(password);
67         }
68         catch (Exception JavaDoc e) {
69             e.printStackTrace();
70             throw e;
71         }
72     }
73     
74     public static void addUser(NamespaceAccessToken nat,
75                                String JavaDoc caller,
76                                String JavaDoc username,
77                                String JavaDoc password) throws Exception JavaDoc {
78         
79         String JavaDoc usersPath = nat.getNamespaceConfig().getUsersPath();
80         String JavaDoc userUri = usersPath + "/" + username;
81         
82         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
83         Structure structure = nat.getStructureHelper();
84         Content content = nat.getContentHelper();
85         
86         try {
87             
88             nat.begin();
89             
90             ObjectNode user = new SubjectNode();
91             structure.create(slideToken,user,userUri);
92             
93             // create the user descriptor
94
NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor();
95             descriptor.setCreationDate(new Date JavaDoc());
96             descriptor.setLastModified(new Date JavaDoc());
97             descriptor.setProperty(new NodeProperty(
98                 "password",password,NodeProperty.SLIDE_NAMESPACE));
99             content.create(slideToken,userUri,descriptor,null);
100             
101             nat.commit();
102         }
103         catch (Exception JavaDoc e) {
104             try {
105                 nat.rollback();
106             }
107             catch (Exception JavaDoc f) {
108                 f.printStackTrace();
109             }
110             throw e;
111         }
112         
113     }
114     
115     public static void addGroup(NamespaceAccessToken nat,
116                                 String JavaDoc caller,
117                                 String JavaDoc groupname) throws Exception JavaDoc {
118         
119         String JavaDoc groupsPath = nat.getNamespaceConfig().getGroupsPath();
120         String JavaDoc groupUri = groupsPath + "/" + groupname;
121         
122         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
123         Structure structure = nat.getStructureHelper();
124         Content content = nat.getContentHelper();
125         
126         try {
127             nat.begin();
128             
129             ObjectNode group = new SubjectNode();
130             structure.create(slideToken,group,groupUri);
131             
132             NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor();
133             descriptor.setCreationDate(new Date JavaDoc());
134             descriptor.setLastModified(new Date JavaDoc());
135             
136             content.create(slideToken,groupUri,descriptor,null);
137             
138             nat.commit();
139         }
140         catch (Exception JavaDoc e) {
141             try {
142                 nat.rollback();
143             }
144             catch (Exception JavaDoc f) {
145                 f.printStackTrace();
146             }
147             throw e;
148         }
149         
150     }
151     
152     public static void addRole(NamespaceAccessToken nat,
153                                 String JavaDoc caller,
154                                 String JavaDoc rolename) throws Exception JavaDoc {
155         
156         String JavaDoc rolesPath = nat.getNamespaceConfig().getRolesPath();
157         String JavaDoc roleUri = rolesPath + "/" + rolename;
158         
159         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
160         Structure structure = nat.getStructureHelper();
161         Content content = nat.getContentHelper();
162         
163         try {
164             nat.begin();
165             
166             ObjectNode role = new SubjectNode();
167             structure.create(slideToken,role,roleUri);
168             
169             NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor();
170             descriptor.setCreationDate(new Date JavaDoc());
171             descriptor.setLastModified(new Date JavaDoc());
172             
173             content.create(slideToken,roleUri,descriptor,null);
174             
175             nat.commit();
176         }
177         catch (Exception JavaDoc e) {
178             try {
179                 nat.rollback();
180             }
181             catch (Exception JavaDoc f) {
182                 f.printStackTrace();
183             }
184             throw e;
185         }
186         
187     }
188     
189     public static void removeObject(NamespaceAccessToken nat,
190                                     String JavaDoc caller,
191                                     String JavaDoc objectUri) throws Exception JavaDoc {
192         
193         String JavaDoc usersPath = nat.getNamespaceConfig().getUsersPath();
194         String JavaDoc callerUri = usersPath + "/" + caller;
195                                         
196         // user cannot delete itself
197
if (callerUri.equals(objectUri)) {
198             return;
199         }
200     
201         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
202         Macro macro = nat.getMacroHelper();
203     
204         try {
205             nat.begin();
206     
207             boolean recursive = true;
208             boolean overwrite = false;
209             MacroParameters parameters = new MacroParameters(recursive,overwrite);
210     
211             macro.delete(slideToken,objectUri,parameters);
212     
213             nat.commit();
214         }
215         catch (Exception JavaDoc e) {
216             try {
217                 nat.rollback();
218             }
219             catch (Exception JavaDoc f) {
220                 f.printStackTrace();
221             }
222             throw e;
223         }
224     }
225     
226     public static void addMember(NamespaceAccessToken nat,
227                                  String JavaDoc caller,
228                                  String JavaDoc objectUri,
229                                  String JavaDoc subjectUri) throws Exception JavaDoc {
230         
231         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
232         Structure structure = nat.getStructureHelper();
233         Content content = nat.getContentHelper();
234         
235         try {
236             
237             // check if the subject exists
238
structure.retrieve(slideToken,subjectUri);
239             
240             NodeRevisionDescriptors descriptors = content.retrieve(slideToken,objectUri);
241             NodeRevisionDescriptor descriptor = content.retrieve(slideToken,descriptors);
242             NodeProperty property = descriptor.getProperty("group-member-set","DAV:");
243             
244             String JavaDoc value = null;
245             if (property != null) {
246                 value = (String JavaDoc) property.getValue();
247                 if (value.indexOf(subjectUri) != -1) {
248                     // user already a member of this group
249
return;
250                 }
251             }
252             else {
253                 value = "";
254             }
255             value = value + "<D:href xmlns:D='DAV:'>" + subjectUri + "</D:href>";
256             
257             descriptor.setProperty("group-member-set","DAV:",value);
258             nat.begin();
259             content.store(slideToken,objectUri,descriptor,null);
260             nat.commit();
261         }
262         catch (ObjectNotFoundException e) {
263             // no such user or group
264
}
265         catch (Exception JavaDoc e) {
266             try {
267                 nat.rollback();
268             }
269             catch (Exception JavaDoc f) {
270                 f.printStackTrace();
271             }
272             throw e;
273         }
274     }
275     
276     public static void removeMember(NamespaceAccessToken nat,
277                                     String JavaDoc caller,
278                                     String JavaDoc objectUri,
279                                     String JavaDoc subjectUri) throws Exception JavaDoc {
280         
281         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
282         Content content = nat.getContentHelper();
283         
284         try {
285             
286             NodeRevisionDescriptors revisions = content.retrieve(slideToken,objectUri);
287             NodeRevisionDescriptor revision = content.retrieve(slideToken,revisions);
288             NodeProperty property = revision.getProperty("group-member-set","DAV:");
289             
290             if (property == null) {
291                 // group has no members
292
return;
293             }
294             String JavaDoc value = (String JavaDoc) property.getValue();
295             
296             int index = value.indexOf(subjectUri);
297             if (index == -1) {
298                 // subject is not a member of this group
299
return;
300             }
301             
302             // looking for the end of </D:href> after subjectUri
303
int end = index + subjectUri.length();
304             do {
305                 end++;
306             }
307             while (value.charAt(end) != '>');
308             
309             // looking for the start of <D:href> before subjectUri
310
int from = index;
311             do {
312                 from--;
313             }
314             while(value.charAt(from) != '<');
315             
316             // snip out the user
317
String JavaDoc before = value.substring(0,from);
318             String JavaDoc after = value.substring(end+1);
319             value = before + after;
320             
321             revision.setProperty("group-member-set","DAV:",value);
322             nat.begin();
323             content.store(slideToken,objectUri,revision,null);
324             nat.commit();
325         }
326         catch (ObjectNotFoundException e) {
327             // no such user or group
328
}
329         catch (Exception JavaDoc e) {
330             try {
331                 nat.rollback();
332             }
333             catch (Exception JavaDoc f) {
334                 f.printStackTrace();
335             }
336             throw e;
337         }
338     }
339     
340     public static void changePassword(NamespaceAccessToken nat,
341                                       String JavaDoc caller,
342                                       String JavaDoc userUri,
343                                       String JavaDoc password) throws Exception JavaDoc {
344
345         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
346         Content content = nat.getContentHelper();
347
348         try {
349             nat.begin();
350
351             NodeRevisionDescriptors revisions = content.retrieve(slideToken, userUri);
352             NodeRevisionDescriptor revision = content.retrieve(slideToken, revisions);
353             revision.setLastModified(new Date JavaDoc());
354             revision.setProperty(new NodeProperty("password", password, NodeProperty.SLIDE_NAMESPACE));
355             content.store(slideToken, userUri, revision, null);
356
357             nat.commit();
358         }
359         catch (Exception JavaDoc e) {
360             try {
361                 nat.rollback();
362             }
363             catch (Exception JavaDoc f) {
364                 f.printStackTrace();
365             }
366             throw e;
367         }
368     }
369         
370     public static List JavaDoc listPermissions(NamespaceAccessToken nat,
371                                        String JavaDoc caller,
372                                        String JavaDoc path) throws Exception JavaDoc {
373                                            
374         String JavaDoc uri = getUriFromPath(nat,path);
375         
376         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
377         Security security = nat.getSecurityHelper();
378         
379         List JavaDoc result = new ArrayList JavaDoc();
380         try {
381             nat.begin();
382             Enumeration JavaDoc permissions = security.enumeratePermissions(slideToken,uri,false);
383             while (permissions.hasMoreElements()) {
384                 result.add(permissions.nextElement());
385             }
386             nat.commit();
387             return result;
388         }
389         catch (Exception JavaDoc e) {
390             try {
391                 nat.rollback();
392             }
393             catch (Exception JavaDoc f) {
394                 f.printStackTrace();
395             }
396             throw e;
397         }
398     }
399     
400     public static List JavaDoc listLocks(NamespaceAccessToken nat,
401                                  String JavaDoc caller,
402                                  String JavaDoc path) throws Exception JavaDoc {
403
404         String JavaDoc uri = getUriFromPath(nat,path);
405         
406         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
407         Lock lock = nat.getLockHelper();
408         
409         List JavaDoc result = new ArrayList JavaDoc();
410         try {
411             nat.begin();
412             Enumeration JavaDoc locks = lock.enumerateLocks(slideToken,uri,false);
413             while(locks.hasMoreElements()) {
414                 result.add(locks.nextElement());
415             }
416             nat.commit();
417             return result;
418         } catch (Exception JavaDoc e) {
419             try {
420                 nat.rollback();
421             }
422             catch (Exception JavaDoc f) {
423                 f.printStackTrace();
424             }
425             throw e;
426         }
427     }
428     
429     public static List JavaDoc listGroups(NamespaceAccessToken nat, String JavaDoc caller, String JavaDoc path) throws Exception JavaDoc {
430         List JavaDoc result = new ArrayList JavaDoc();
431
432         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
433         Structure structure = nat.getStructureHelper();
434         Content content = nat.getContentHelper();
435         
436         ObjectNode object = structure.retrieve(slideToken,path);
437         Enumeration JavaDoc enumeration = structure.getChildren(slideToken,object);
438         while (enumeration.hasMoreElements()) {
439             String JavaDoc uri = ((ObjectNode) enumeration.nextElement()).getUri();
440             NodeRevisionDescriptors revisions = content.retrieve(slideToken, uri);
441             NodeRevisionDescriptor revision = content.retrieve(slideToken, revisions);
442             NodeProperty property = revision.getProperty("group-member-set","DAV:");
443             List JavaDoc members;
444             if (property != null) {
445                 String JavaDoc value = (String JavaDoc) property.getValue();
446                 members = new ArrayList JavaDoc(10);
447                 int start = value.indexOf('>'), end = 0;
448                 while (start != -1) {
449                     end = value.indexOf('<',start);
450                     if (end != -1) {
451                         members.add(value.substring(start+1,end));
452                     }
453                     end = value.indexOf('>',start+1);
454                     start = value.indexOf('>',end+1);
455                 }
456             }
457             else {
458                 members = Collections.EMPTY_LIST;
459             }
460             result.add(new Group(uri,members));
461         }
462
463         return result;
464     }
465     
466     public static List JavaDoc listUsers(NamespaceAccessToken nat,
467                                  String JavaDoc caller) throws Exception JavaDoc {
468         return listObjects(nat,caller,nat.getNamespaceConfig().getUsersPath());
469     }
470     
471     public static List JavaDoc listPrivileges(NamespaceAccessToken nat,
472                                    String JavaDoc caller) throws Exception JavaDoc {
473         return listObjects(nat,caller,nat.getNamespaceConfig().getActionsPath());
474     }
475     
476     private static List JavaDoc listObjects(NamespaceAccessToken nat,
477                                     String JavaDoc caller,
478                                     String JavaDoc path) throws Exception JavaDoc {
479         
480         List JavaDoc result = new ArrayList JavaDoc();
481         
482         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
483         Structure structure = nat.getStructureHelper();
484         
485         ObjectNode object = structure.retrieve(slideToken,path);
486         Enumeration JavaDoc enumeration = structure.getChildren(slideToken,object);
487         while (enumeration.hasMoreElements()) {
488             result.add(((ObjectNode) enumeration.nextElement()).getUri());
489         }
490         
491         return result;
492     }
493     
494     public static void removePermission(NamespaceAccessToken nat,
495                                         String JavaDoc caller,
496                                         String JavaDoc path,
497                                         String JavaDoc subject,
498                                         String JavaDoc action) throws Exception JavaDoc {
499
500         String JavaDoc uri = getUriFromPath(nat,path);
501         
502         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
503         Security security = nat.getSecurityHelper();
504         
505         try {
506             NodePermission permission = new NodePermission(uri, subject, action);
507             nat.begin();
508             security.revokePermission(slideToken, permission);
509             nat.commit();
510         }
511         catch (Exception JavaDoc e) {
512             try {
513                 nat.rollback();
514             }
515             catch (Exception JavaDoc f) {
516                 f.printStackTrace();
517             }
518             throw e;
519         }
520         
521     }
522     
523     public static void addPermission(NamespaceAccessToken nat,
524                                      String JavaDoc caller,
525                                      String JavaDoc path,
526                                      String JavaDoc subject,
527                                      String JavaDoc action,
528                                      String JavaDoc inheritable,
529                                      String JavaDoc negative) throws Exception JavaDoc {
530                                          
531         String JavaDoc uri = getUriFromPath(nat,path);
532         
533         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
534         Security security = nat.getSecurityHelper();
535         
536         boolean isInheritable = Boolean.valueOf(inheritable).booleanValue();
537         boolean isNegative = Boolean.valueOf(negative).booleanValue();
538         
539         try {
540             NodePermission permission = new NodePermission(uri,subject,action,isInheritable,isNegative);
541             
542             nat.begin();
543             if (isNegative) {
544                 security.denyPermission(slideToken,permission);
545             }
546             else {
547                 security.grantPermission(slideToken,permission);
548             }
549             nat.commit();
550         } catch (Exception JavaDoc e) {
551             try {
552                 nat.rollback();
553             }
554             catch (Exception JavaDoc f) {
555                 f.printStackTrace();
556             }
557             throw e;
558         }
559     }
560     
561     public static void removeLock(NamespaceAccessToken nat,
562                                   String JavaDoc caller,
563                                   String JavaDoc uri,
564                                   String JavaDoc lockId) throws Exception JavaDoc {
565         
566         SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
567         Lock lock = nat.getLockHelper();
568         
569         try {
570             nat.begin();
571             lock.unlock(slideToken,uri,lockId);
572             nat.commit();
573         }
574         catch (Exception JavaDoc e) {
575             try {
576                 nat.rollback();
577             }
578             catch (Exception JavaDoc f) {
579                 f.printStackTrace();
580             }
581             throw e;
582         }
583     }
584     
585     public static void addLock(NamespaceAccessToken nat,
586                                String JavaDoc caller,
587                                String JavaDoc path,
588                                String JavaDoc subject,
589                                String JavaDoc type,
590                                String JavaDoc expiration,
591                                String JavaDoc exclusive,
592                                String JavaDoc inherit) throws Exception JavaDoc {
593
594        String JavaDoc uri = getUriFromPath(nat,path);
595        boolean isExclusive = Boolean.valueOf(exclusive).booleanValue();
596        boolean isInherit = Boolean.valueOf(inherit).booleanValue();
597        
598        // expiration in minutes
599
int intExpiration = Integer.valueOf(expiration).intValue();
600        Date JavaDoc expire = new Date JavaDoc(System.currentTimeMillis() + intExpiration*1000*60);
601        
602        SlideToken slideToken = new SlideTokenImpl(new CredentialsToken(caller));
603        Lock lock = nat.getLockHelper();
604        
605        try {
606            nat.begin();
607            lock.lock(slideToken, new NodeLock(uri, subject, type, expire, isInherit, isExclusive, uri));
608            nat.commit();
609        }
610        catch (Exception JavaDoc e) {
611            try {
612                nat.rollback();
613            }
614            catch (Exception JavaDoc f) {
615                f.printStackTrace();
616            }
617            throw e;
618        }
619     }
620     
621     private static String JavaDoc getUriFromPath(NamespaceAccessToken nat,
622                                          String JavaDoc path) {
623         String JavaDoc filesPath = nat.getNamespaceConfig().getFilesPath();
624         String JavaDoc uri;
625         if (path.equals("/") || path.length() == 0) {
626             uri = filesPath;
627         }
628         else {
629             uri = filesPath + "/" + path;
630         }
631         return uri;
632     }
633     
634     public static class Group {
635         private final String JavaDoc m_uri;
636         private final List JavaDoc m_members;
637         
638         private Group(String JavaDoc uri, List JavaDoc members) {
639             m_uri = uri;
640             m_members = members;
641         }
642         
643         public String JavaDoc getUri() {
644             return m_uri;
645         }
646         
647         public List JavaDoc getMembers() {
648             return m_members;
649         }
650         
651         public String JavaDoc toString() {
652             return m_uri;
653         }
654     }
655 }
656
Popular Tags