View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
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.gwe.utils.security;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  
24  /**
25   * @author Marco Ruiz
26   * @since Oct 27, 2008
27   */
28  public class AccessControl implements Serializable {
29  	
30  	public static AccessControl createDefaultAC(ProtocolScheme scheme, String host) {
31  		return new AccessControl(AccountInfo.createLocalAccount(), new Realm(scheme.toString(), host, "localhost"));
32  	}
33  
34  	private static List<Realm> createRealmList(Realm... realms) {
35  	    ArrayList<Realm> result = new ArrayList<Realm>();
36  	    for (Realm realm : realms) result.add(realm);
37  	    return result;
38      }
39  
40  	private AccountInfo account;
41  	private List<Realm> realms;
42  	
43      public AccessControl() { super(); }
44  
45  	public AccessControl(AccountInfo account, Realm... realms) {
46  		this(account, createRealmList(realms));
47      }
48  
49  	public AccessControl(AccountInfo account, List<Realm> realms) {
50  		this.account = account;
51  	    this.realms = realms;
52  	    for (Realm realm : realms) realm.setAccount(account);
53      }
54  
55  	public AccountInfo getAccount() {
56      	return account;
57      }
58  
59  	public void setAccount(AccountInfo account) {
60      	this.account = account;
61      }
62  
63  	public List<Realm> getRealms() {
64      	return realms;
65      }
66  	
67  	public Realm findMatchingRealm(ProtocolScheme scheme, String host) {
68  	    for (Realm currRealm : getRealms()) {
69  	        boolean currRealmMatches = currRealm.implies(scheme.toString(), host);
70  	        if (currRealmMatches) return currRealm;
71  	    }
72  	    return null;
73      }
74  	
75  	public List<String> getAccountIds() {
76  		List<String> result = new ArrayList<String>();
77  		String privateKeyFileName = account.getPrivateKeyFileName();
78      	if (privateKeyFileName != null && !"".equals(privateKeyFileName)) {
79          	result.add(account.getPrivateKeyFileName());
80          } else {
81          	for (Realm	realm : realms)
82          		result.add((account.getUser() + "@" + realm.toString()));
83          }
84      	return result;
85  	}
86  }
87