View Javadoc

1   /*
2    * CertificateHostGroup.java
3    *
4    * Created on May 10, 2005, 3:56 PM
5    *
6    * To change this template, choose Tools | Options and locate the template under
7    * the Source Creation and Management node. Right-click the template and choose
8    * Open. You can then make changes to the template in the Source Editor.
9    */
10  
11  package gov.bnl.gums.hostToGroup;
12  
13  import gov.bnl.gums.configuration.Configuration;
14  
15  import java.util.ArrayList;
16  import java.util.Iterator;
17  import java.util.List;
18  import java.util.StringTokenizer;
19  
20  import javax.persistence.Entity;
21  import javax.persistence.Transient;
22  
23  /**
24   * Represent a set of services identified by a wildcard on their CN or DN.
25   * 
26   * @author Gabriele Carcassi, Jay Packard
27   */
28  @Entity
29  public class CertificateHostToGroupMapping extends HostToGroupMapping {
30  	private List<String> regexs;
31  
32  	// persistent variables
33  	protected String cn;
34  	protected String dn;
35  
36  	/**
37  	 * Create a new CertificateHostToGroupMapping object.
38  	 */
39  	public CertificateHostToGroupMapping() {
40  		super();
41  	}
42  
43  	/**
44  	 * Create a new CertificateHostToGroupMapping object.
45  	 * 
46  	 * @param configuration
47  	 */
48  	public CertificateHostToGroupMapping(Configuration configuration) {
49  		super(configuration, null);
50  	}
51  
52  	/**
53  	 * Retrieves the wildcard that will be used to match the CN.
54  	 * 
55  	 * @return The wildcard (i.e. '*.mycompany.com').
56  	 */
57  	public String getCn() {
58  		return this.cn;
59  	}
60  
61  	/**
62  	 * Retrieves the wildcard that will be used to match the DN.
63  	 * 
64  	 * @return The wildcard (i.e.
65  	 *         '/DC=org/DC=doegrids/OU=Services/CN=*.mycompany.com').
66  	 */
67  	public String getDn() {
68  		return this.dn;
69  	}
70  
71  	public boolean isInGroup(String hostname) {
72  		Iterator<String> iter = regexs.iterator();
73  		while (iter.hasNext()) {
74  			if (hostname.matches((String) iter.next()))
75  				return true;
76  		}
77  		return false;
78  	}
79  
80  	/**
81  	 * Changes the wildcard that will be used to match the CN(s).
82  	 * 
83  	 * @param cn
84  	 *            The new wildcard (i.e. '*.mycompany.com, *othercompany.com').
85  	 */
86  	@ConfigFieldAnnotation(label="CNs", example="*.host1.com, *.host2.com", help="only requests from matching hosts are accepted")
87  	public void setCn(String cn) {
88  		this.cn = cn;
89  		updateRegEx();
90  		if (cn!=null)
91  			setName(cn);
92  	}
93  
94  	/**
95  	 * Changes the wildcard that will be used to match the DN(s).
96  	 * 
97  	 * @param wildcard
98  	 *            The new wildcard (i.e.
99  	 *            '/DC=org/DC=doegrids/OU=Services/CN=*.mycompany.com,
100 	 *            /DC=org/DC=doegrids/OU=Services/CN=*.othercompany.com').
101 	 */
102 	@ConfigFieldAnnotation(label="DNs", example="*.host1.com, *.host2.com", help="only requests from matching host DNs are accepted")
103 	public void setDn(String dn) {
104 		this.dn = dn;
105 		updateRegEx();
106 		if (dn!=null)
107 			setName(dn);
108 	}
109 	
110 	private void updateRegEx() {
111 		regexs = new ArrayList<String>();
112 		if (cn != null) {
113 			StringTokenizer tokens = new StringTokenizer(cn, ",");
114 			while (tokens.hasMoreTokens()) {
115 				String regex = tokens.nextToken();
116 				regex = regex.trim();
117 				regex = regex.replaceAll("\\.", "\\.");
118 				regex = regex.replaceAll("\\*", "[^\\./=]*");
119 				regexs.add("(/[^=]*=[^=]*)*/CN=" + regex + "(/[^=]*=[^=]*)*");
120 			}
121 		}
122 		if (dn != null) {
123 			StringTokenizer tokens = new StringTokenizer(dn, ",");
124 			while (tokens.hasMoreTokens()) {
125 				String regex = tokens.nextToken();
126 				regex = regex.trim();
127 				regex = regex.replaceAll("\\.", "\\.");
128 				regex = regex.replaceAll("\\*", "[^\\./=]*");
129 				regexs.add(regex);
130 			}
131 		}
132 	}
133 }