View Javadoc

1   package org.astrogrid.community.client.delegate ;
2   
3   import org.apache.commons.logging.Log ;
4   import org.apache.commons.logging.LogFactory ;
5   
6   import org.astrogrid.community.common.policy.data.GroupData;
7   import org.astrogrid.community.common.policy.data.ResourceData;
8   import org.astrogrid.community.common.policy.data.AccountData;
9   import org.astrogrid.community.common.policy.data.PolicyPermission;
10  
11  import org.astrogrid.community.common.config.CommunityConfig;
12  
13  import org.astrogrid.community.common.policy.manager.PolicyManager ;
14  import org.astrogrid.community.common.policy.manager.PolicyManagerService ;
15  import org.astrogrid.community.common.policy.manager.PolicyManagerServiceLocator ;
16  
17  import java.net.URL;
18  import java.util.regex.*;
19  
20  import java.util.ArrayList;
21  /***
22   * The AdministrationDelegate is the gateway to the webservice, having mappped methods to the webservice methods. 
23   * This particular class deals with administration piece of the community.  Many webservice methods are requred for 
24   * administration such as inserting permissions, resources, accounts, groups and others.
25   * @author Kevin Benson
26   *
27   * This API is going to be replaced during iter 5, and will be deprecated in iter 6.
28   *
29   */
30  public class AdministrationDelegate {
31  
32      /***
33       * Our debug logger.
34       *
35       */
36      private static Log log = LogFactory.getLog(AdministrationDelegate.class);
37  
38     /***
39      *  service variable to our PolicyManager.
40      */
41     PolicyManager service = null;
42  
43      //
44      // Load our community config.
45      static
46          {
47          CommunityConfig.loadConfig() ;
48          }
49     
50     /***
51      * Public constructor deals with getting our service (link) to the webservice.
52      *
53      */
54     public AdministrationDelegate() {
55        try {
56           service = getService(CommunityConfig.getManagerUrl());
57        }catch(Exception e) {
58           e.printStackTrace();
59           service = null;
60        }      
61     }
62     
63     private static final String REGEX_SECUREPORT = "://d+";
64     private String getSecureURL() {
65        String communitySecurity = CommunityConfig.getProperty("community.security","on");
66        if(communitySecurity == null || !communitySecurity.equals("on")) {
67           return CommunityConfig.getManagerUrl();
68        }
69        String policyURL = CommunityConfig.getProperty("policy.manager.secure.url");
70        if(policyURL != null && policyURL.trim().length() > 0) {
71           return policyURL;
72        }
73        
74        policyURL = CommunityConfig.getManagerUrl();
75        
76        log.debug("manager url = " + policyURL);
77  
78        
79        String securePort = CommunityConfig.getProperty("community.secure.port");
80        log.debug("the secure port = " + securePort);      
81        if(securePort != null && securePort.length() > 0) {
82           policyURL = policyURL.replaceAll("http","https");         
83           Pattern p = Pattern.compile(REGEX_SECUREPORT);
84           Matcher m = p.matcher(policyURL); // get a matcher object
85           policyURL = m.replaceAll((":" + securePort));         
86        }else {
87           return null;
88        }
89        log.debug("The PolicyURL = " + policyURL);
90        return policyURL;      
91     }
92     
93  
94    /*
95     * Removed for refactoring.
96     *
97     public String getPassword(String name) throws Exception {
98        String policyURL = getSecureURL();
99        if(policyURL == null) {
100          throw new Exception("This operation requires a secure ssl connection which cannot be found for retrieving the password.");
101       }
102       PolicyManager secureService = getService(policyURL);
103       return secureService.getPassword(name);      
104    }
105   *
106   */
107 
108   /*
109    * Removed for refactoring.
110    *
111    public AccountData setPassword(String account,String password) throws Exception {
112       String policyURL = getSecureURL();
113       if(policyURL == null) {
114          throw new Exception("This operation requires a secure ssl connection which cannot be found for retrieving the password.");
115       }
116       PolicyManager secureService = getService(policyURL);
117       return secureService.setPassword(account,password);      
118    }
119   *
120   */
121 
122    
123    /***
124     * Creates a new permission in the database tying a resource to a group for a specefic action.
125     * @param resource 
126     * @param group
127     * @param action
128     * @return
129     * @throws Exception
130     */
131    public PolicyPermission addPermission(String resource, String group, String action)
132       throws Exception
133       {
134       return service.addPermission(resource, group, action) ;
135       }
136       
137       /***
138        * Deletes a permission from teh database.
139        * @param resource
140        * @param group
141        * @param action
142        * @return
143        * @throws Exception
144        */
145    public boolean delPermission(String resource, String group, String action)
146       throws Exception
147       {
148       return service.delPermission(resource, group, action) ;
149       }
150       
151    
152    /***
153     * Adds a  user/account to a particular MULTI group.
154     * @param account
155     * @param group
156     * @return
157     * @throws Exception
158     */
159    public boolean addGroupMember(String account,String group) throws Exception {
160       return (null != service.addGroupMember(account,group)) ;
161    }
162    
163    /***
164     * Deletes a user from a group.
165     * @param account
166     * @param group
167     * @return
168     * @throws Exception
169     */
170    public boolean delGroupMember(String account,String group) throws Exception {
171       return (null != service.delGroupMember(account,group)) ;
172    }
173 
174 /***
175  * Adds an account. 
176  * @param name
177  * @return
178  * @throws Exception
179  */
180    public AccountData addAccount(String name) throws Exception {
181       return service.addAccount(name);
182    }
183 
184 /***
185  * Deletes an account.
186  * @param name
187  * @return
188  * @throws Exception
189  */
190    public boolean delAccount(String name) throws Exception {
191       return (null != service.delAccount(name));
192    }
193    
194    /***
195     * Get an Account object which consists of a name, and description.
196     * @param name
197     * @return
198     * @throws Exception
199     * @see org.astrogrid.community.policy.data.AccountData
200     */
201    public AccountData getAccount(String name) throws Exception {
202       return service.getAccount(name);
203    }
204    
205    /***
206     * Gets a local account list. 
207     * @deprecated because getRemoteAccounts using the local url, would get the same results.
208     * @return
209     * @throws Exception
210     */
211    public ArrayList getAccountList() throws Exception {
212       return createArrayList(service.getLocalAccounts());
213    }
214    
215    /***
216     * Gets an account list based off of a community name.
217     * @param community
218     * @return
219    public ArrayList getAccountList(String community) throws Exception {
220       return createArrayList(service.getRemoteAccounts(community));
221    }
222     */
223    
224    /***
225     * Converts an array of AccountData objects to an ArrayList.
226     * @param list
227     * @return
228     */
229    private ArrayList createArrayList(Object []list) {
230       ArrayList al = null;
231       if(list != null && list.length > 0) {
232          al = new ArrayList(list.length);
233          for(int i = 0;i < list.length;i++) {
234             al.add(list[i]);
235          }
236       }
237       return al;         
238    }
239 
240 /*
241  *   
242    public ArrayList getAccountGroups(String account, String community) throws Exception {
243       return createArrayList(service.getRemoteAccountGroups(account,community));
244    } 
245  */   
246 /*
247  *   
248    public boolean isAdminAccount(String account,String community) throws Exception {
249       ArrayList al = getAccountGroups(account,community);
250       String adminGroup = "Admin@" + community;
251       adminGroup = adminGroup.toLowerCase();
252       
253       if(al != null && al.size() > 0) {
254          for(int i = 0;i < al.size();i++) {
255             GroupMemberData data = (GroupMemberData)al.get(i);
256             if(adminGroup.equals(data.getGroup().toLowerCase())) {
257                return true;            
258             }//if
259          }//for
260       }//if
261       return false;
262    }
263  *
264  */   
265    /***
266     * Updates an account.
267     * @param ad
268     * @return
269     * @throws Exception
270     */
271    public AccountData setAccount(AccountData ad) throws Exception {
272       String policyURL = getSecureURL();
273       if(policyURL == null) {
274          throw new Exception("A Secure ssl connection is required for updating an account, and cannot be found.");
275       }
276       PolicyManager secureService = getService(policyURL);
277       return secureService.setAccount(ad);  
278    }
279    
280    /***
281     * Adds a resource.
282     * @param name
283     * @return
284     * @throws Exception
285     */
286    public ResourceData addResource(String name) throws Exception {
287       return service.addResource();      
288    }
289 
290 /***
291  * Deletes a resource.
292  * @param name
293  * @return
294  * @throws Exception
295  */
296    public ResourceData delResource(String name) throws Exception {
297       return service.delResource(name);      
298    }
299    
300    /***
301     * Returns a resource information.
302     * @param name
303     * @return
304     * @throws Exception
305     * @see org.astrogrid.community.policy.data.ResourceData
306     */
307    public ResourceData getResource(String name) throws Exception {
308       return service.getResource(name);  
309    }
310    
311    
312    /***
313     * Returna an array of ResourceData objects.
314     * @return
315     * @throws Exception
316     */
317    public ArrayList getResourceList() throws Exception {
318       return new ArrayList() ;
319    }
320    
321    /***
322     * Update a resource.
323     * @param ad
324     * @return
325     * @throws Exception
326     */
327    public ResourceData setResource(ResourceData ad) throws Exception {
328       return service.setResource(ad);  
329    }
330       
331    /***
332     * Get a group list.
333     * @return
334     * @throws Exception
335     * @deprecated
336     */
337    public ArrayList getGroupList() throws Exception  {
338       return createArrayList(service.getLocalGroups());
339    }
340    
341    /***
342     * Return a grou list in a particular community.  Might be the local or remote community.
343     * @param community
344     * @return
345    public ArrayList getGroupList(String community) throws Exception {
346       return createArrayList(service.getRemoteGroups(community));  
347    }
348     */
349       
350    /***
351     * Delete a group.
352     * @param ident
353     * @return
354     * @throws Exception
355     */
356    public boolean delGroup(String ident) throws Exception  {
357       return (null != service.delGroup(ident));
358    }
359    
360    /***
361     * Add a group.
362     * @param ident
363     * @return
364     * @throws Exception
365     */
366    public GroupData addGroup(String ident) throws Exception  {
367       return service.addGroup(ident);
368    }
369    
370    /***
371     * Get a group.
372     * @param ident
373     * @return
374     * @throws Exception
375     */
376    public GroupData getGroup(String ident) throws Exception  {
377       return service.getGroup(ident);
378    }
379    
380    /***
381     * Update a group.
382     * @param groupData
383     * @return
384     * @throws Exception
385     */
386    public GroupData setGroup(GroupData groupData) throws Exception  {
387       return service.setGroup(groupData);
388    }
389    
390    /*** 
391     * Add a community.
392     * @param ident
393     * @return
394     * @throws Exception
395    public CommunityData addCommunity(String ident) throws Exception  {
396       return service.addCommunity(ident);
397    }
398     */
399 
400    /***
401     * Get a community
402     * @param ident
403     * @return
404     * @throws Exception
405    public CommunityData getCommunity(String ident) throws Exception  {
406       return service.getCommunity(ident);
407    }
408     */
409 
410    /***
411     * Set a community.
412     * @param commData
413     * @return
414     * @throws Exception
415    public CommunityData setCommunity(CommunityData commData) throws Exception  {
416       return service.setCommunity(commData);
417    }
418     */
419    
420    /***
421     * Delete a community
422     * @param ident
423     * @return
424     * @throws Exception
425    public boolean delCommunity(String ident) throws Exception  {
426       return (null != service.delCommunity(ident));
427    }
428     */
429    /***
430     * Get a community list.
431     * @return
432     * @throws Exception
433    public ArrayList getCommunityList()  throws Exception {
434       return createArrayList(service.getCommunityList());
435    }
436     */
437    
438    public ArrayList getGroupMembers(String group) throws Exception {
439       return createArrayList(service.getGroupMembers(group));   
440    }
441 
442    /***
443     * Return our service object which is our link to the webservice.
444     * @return
445     * @throws Exception
446     */
447    private PolicyManager getService(String targetEndPoint)
448       throws Exception
449       {
450       log.debug("") ;
451       log.debug("----\"----") ;
452       log.debug("setUp()") ;
453 
454       PolicyManagerService locator = null;
455       PolicyManager service = null;
456       //
457       // Create our service locator.
458       locator = new PolicyManagerServiceLocator();
459       
460       //
461       // Create our service.
462       service = locator.getPolicyManager(new URL(targetEndPoint));
463 
464       log.debug("----\"----") ;
465       log.debug("") ;
466       return service;
467    }     
468 }