View Javadoc

1   /*
2    * GridUser.java
3    *
4    * Created on August 31, 2004, 4:00 PM
5    */
6   
7   package gov.bnl.gums;
8   
9   import org.apache.log4j.Logger;
10  
11  /** 
12   * Represent a GRID Identity in GUMS, which is a certificate with its DN and FQAN.
13   *
14   * @author  Gabriele Carcassi, Jay Packard
15   */
16  public class GridUser {
17      private Logger log = Logger.getLogger(GridUser.class);
18      private String certificateDN;
19      private FQAN voFQAN;
20      private String email;
21      
22      /**
23       * Creates a GRID credentail with no DN and FQAN.
24       */
25      public GridUser() {
26      }
27      
28      /**
29       * Creates a GRID credential with DN.
30       */
31      public GridUser(String userDN) {
32      	this(userDN, null, null, false);
33      }
34      
35      /**
36       * Creates a GRID credential with DN and FQAN.
37       */
38      public GridUser(String userDN, String fqan) {
39          this(userDN, fqan, null, true);
40      }
41      
42      /**
43       * Creates a GRID credential with DN and FQAN.
44       */
45      public GridUser(String userDN, String fqan, boolean enforceFqanWellFormedness) {
46          this(userDN, fqan, null, enforceFqanWellFormedness);
47      }
48   
49      /**
50       * Creates a new object representing a Grid credential.
51       * 
52       * @param userDN the DN of the user certificate (i.e. "/DC=org/DC=doegrids/OU=People/CN=John Smith")
53       * @param fqan The Fully Qualified Attribute name (i.e. "/atlas/production/Role=Leader")
54       * @param email
55       */
56      public GridUser(String userDN, String fqan, String email) {
57      	this(userDN, fqan, email, true);
58      }
59      
60      /**
61       * Creates a new object representing a Grid credential.
62       * 
63       * @param userDN the DN of the user certificate (i.e. "/DC=org/DC=doegrids/OU=People/CN=John Smith")
64       * @param fqan The Fully Qualified Attribute name (i.e. "/atlas/production/Role=Leader")
65       * @param email
66       */
67      public GridUser(String userDN, String fqan, String email, boolean enforceFqanWellFormedness) {
68          setCertificateDN(userDN);
69          if (fqan!=null && fqan.length()>0)
70          	setVoFQAN(new FQAN(fqan, enforceFqanWellFormedness));
71         	setEmail(email);
72      }
73  
74    /**
75       * @param user
76       * @return true if user DN element matches
77       */
78      public int compareDn(GridUser user) {
79  		if (this.certificateDN == null || user.certificateDN == null)
80  			return (this.certificateDN==user.certificateDN ? 0 : (user.certificateDN==null ? -1: 1));
81      	return this.compareDn( user.getCertificateDN() );
82      }
83      
84      /**
85       * @param userDn
86       * @return true if user DN element matches
87       */
88      public int compareDn(String userDn) {
89  		if (this.certificateDN == null || userDn == null)
90  			return (this.certificateDN==userDn ? 0 : (userDn==null ? -1: 1));
91      	return this.certificateDN.compareTo( userDn );//compareToIgnoreCase( userDn );
92      }
93      
94      /**
95       * A GridUser will be equal only to another GridUser with the same DN and FQAN.
96       * 
97       * @param obj another object
98       * @return true if the object was a GridUser with equivalent credentials
99       */
100     public boolean equals(Object obj) {
101         GridUser user = (GridUser) obj;
102         if ((user.getCertificateDN() == null) ? certificateDN != null : (user.compareDn(certificateDN)!=0)) {
103             if (log.isTraceEnabled()) {
104                 log.trace(this + " !equals " + obj + " for different DN");
105             }
106             return false;
107         }
108         if ((user.voFQAN == null) ? voFQAN != null : (!user.voFQAN.equals(voFQAN))) {
109             if (log.isTraceEnabled()) {
110                 log.trace(this + " !equals " + obj + " for different FQAN");
111             }
112             return false;
113         }
114         if (log.isTraceEnabled()) {
115             log.trace(this + " equals " + obj);
116         }
117         return true;
118     }
119     
120     /**
121      * Retrieve the certificate DN of the user.
122      * 
123      * @return The certificate DN (i.e. "/DC=org/DC=doegrids/OU=People/CN=John Smith")
124      */
125     public String getCertificateDN() {
126         return this.certificateDN;
127     }
128     
129     /**
130      * Retrieve the email of the user.
131      * 
132      * @return The email
133      */
134     public String getEmail() {
135         return this.email;
136     }
137 
138     /**
139      * Retrieve the VOMS Fully Qualified Attribute name.
140      * 
141      * @return The VOMS FQAN selected with voms-proxy-init (i.e. "/atlas/production/Role=Leader")
142      */
143     public FQAN getVoFQAN() {
144         return this.voFQAN;
145     }
146     
147     /**
148      * Changed to reflect the change in equals, as in Object contract.
149      * 
150      * @return A hash created from the DN and FQAN.
151      */
152     public int hashCode() {
153         if (certificateDN != null) {
154             return certificateDN.hashCode();
155         }
156         if (voFQAN != null) {
157             return voFQAN.getFqan().hashCode();
158         }
159         return 0;
160     }
161     
162     /**
163      * Changes the certificate DN for the Grid credential.
164      * 
165      * @param certificateDN A GRID certificate DN (i.e. "/DC=org/DC=doegrids/OU=People/CN=Gabriele Carcassi")
166      */
167     public void setCertificateDN(String certificateDN) {
168         this.certificateDN = certificateDN;//removeSpaces(certificateDN);
169     }
170     
171     /**
172      * Changes the email.
173      * 
174      * @param email
175      */
176     public void setEmail(String email) {
177         this.email = email;
178     }
179     
180     /**
181      * Sets the VOMS Fully Qualified Attribute name for the credential.
182      * 
183      * @param voFQAN The VOMS FQAN selected with voms-proxy-init (i.e. "/atlas/production/Role=Leader")
184      */
185     public void setVoFQAN(FQAN voFQAN) {
186         this.voFQAN = voFQAN;
187     }
188     
189     /**
190      * Returns a legible String representation for the credentail.
191      * 
192      * @return String reprentation of the credential (i.e. "GridID[/DC=org/DC=doegrids/OU=People/CN=Gabriele Carcassi]")
193      */
194     public String toString() {
195         if (voFQAN == null) {
196             return "GridID[" + certificateDN + "]";
197         }
198         return "GridID[" + certificateDN + ", " + voFQAN + "]";
199     }
200     
201     /**
202      * Trim and remove two or more consecutive strings
203      * 
204      * @param str
205      * @return new string
206      */
207     private String removeSpaces(String str) {
208     	if (str!=null) {
209     		str = str.trim();
210 	    	String tempStr;
211 	    	while ( !(tempStr=str.replaceAll("\\s\\s", " ")).equals(str) )
212 	    		str = tempStr;	   	    	
213     	}
214    		return str;
215     }
216     
217 }