View Javadoc

1   /*
2    * $Id: User.java,v 1.15 2004/08/25 23:05:27 jdt Exp $
3    *
4    * Created on 27-Nov-2003 by Paul Harrison (pah@jb.man.ac.uk)
5    *
6    * Copyright 2003 AstroGrid. All rights reserved.
7    *
8    * This software is published under the terms of the AstroGrid
9    * Software License version 1.2, a copy of which has been included
10   * with this distribution in the LICENSE.txt file.
11   *
12   */
13  
14  package org.astrogrid.community;
15  
16  import org.astrogrid.community.common.util.CommunityMessage;
17  
18  /***
19   * A bean to hold what is passed in the "community snippet".
20   * 
21   * Note that the current interpretation of this is that the account contains an identifier of the form user@community - The group relates to the
22   *  group that the user wishes to use for its credentials, this may be a cross community group. 
23   * 
24   * note that this should really be merged with the @link org.astrogrid.community.common.util.CommunityMessage class, which effectively does xml deserializtion
25   * (should be deprecated From iteration 5 - use @link org.astrogrid.community.beans.v1.Credentials, but may still be needed)
26   * @author Paul Harrison (pah@jb.man.ac.uk), mch
27   * @version $Name:  $
28   * @since iteration4
29   */
30  public class User {
31  
32      String account = null;
33      String group = null;
34      String token = null;
35     
36      public final static User ANONYMOUS = new User("Anon@nowhere","Anonymous","None");
37  
38     /*** Default constructor - creates null user
39      */
40      public User(){
41         this("Anon@nowhere","Anonymous", "None");
42      }
43  
44      /***
45       * Creates a user from the given account group & token.  The account &
46       * group cannot be null
47       * @param givenAccount the account in the form userid@community.
48       * @param givenGroup The group credential.
49       * @param givenToken The security token.
50       */
51      public User(String givenAccount, String givenGroup, String givenToken)
52      {
53         this.account = givenAccount;
54         assert givenAccount != null : "account must be supplied"; 
55         assert givenAccount.indexOf('@') != -1 : "account must be of the form user@community";
56         this.group = givenGroup;
57         
58         this.token = givenToken;
59      }
60      
61      /***
62      * @param userId the user part of the account
63      * @param community the community part of the account
64      * @param group the group
65      * @param token the security token
66      */
67     public User(String userId, String community, String group, String token)
68      {
69         this.account = userId+"@"+community;
70         this.group = group;
71         this.token = token;
72      }
73      
74      /***
75       * Creates a user from "community snippet" representation.
76       * @param snippet The community snippet - this is an xml fragment.
77       */
78      public User(String snippet)
79      {
80         this(CommunityMessage.getAccount(snippet),CommunityMessage.getGroup(snippet),CommunityMessage.getToken(snippet));
81      }
82      
83     /***
84      * @return
85      */
86     public String getAccount() {
87        return account;
88     }
89  
90     /***
91      * @return
92      */
93     public String getGroup() {
94        return group;
95     }
96  
97     /***
98      * @return
99      */
100    public String getToken() {
101       return token;
102    }
103 
104    /***
105     * @param string
106     */
107    public void setAccount(String string) {
108       account = string;
109    }
110 
111    /***
112     * @param string
113     */
114    public void setGroup(String string) {
115       group = string;
116    }
117 
118    /***
119     * @param string
120     */
121    public void setToken(String string) {
122       token = string;
123    }
124 
125 
126    /***
127     * Checks to see if this user refers to the same account as the given
128     * user
129     */
130    public boolean equals(User user)
131    {
132       return (user.getAccount().equals(this.account) && (user.getGroup().equals(this.group)));
133    }
134 
135    /***
136     * Returns a string with the 'normal' representation of account@community
137     */
138    public String toString()
139    {
140       return account;
141    }
142    
143    /***
144     * Create a "community snippet" from this User.
145     * @return The XML string that is the community snippet.
146     */
147    public String toSnippet()
148    {
149       return CommunityMessage.getMessage(token, account, group);
150    }
151    
152    /***
153     * Return the community part of the account string
154     * @return will return null if the account is null or the community part does not exist - i.e. there is no @ in the string.
155     */
156    public String getCommunity()
157    {
158       if(account != null)
159       {
160          int i;
161          if((i= account.indexOf("@"))!= -1)
162          {
163             return account.substring(i+1);
164          }
165       }
166       return null;
167    }
168    /***
169     * gets the userid part of the account string - i.e. that before the @
170     * @return
171     */
172    public String getUserId()
173    {
174       if(account!=null)
175       {
176          int i;
177          if((i= account.indexOf("@"))!= -1)
178          {
179             return account.substring(0, i);
180          }
181          else
182          {
183             return account;
184          }
185       }
186       return null;
187    }
188 }
189 
190 /* $Log: User.java,v $
191 /* Revision 1.15  2004/08/25 23:05:27  jdt
192 /* removed deprecation warning....I'm such a rebel
193 /*
194 /* Revision 1.14  2004/05/11 08:15:53  pah
195 /* put in assertions to try to get correct form used
196 /*
197 /* Revision 1.13  2004/03/05 09:34:47  pah
198 /* deprecated in favour of using the workflow objects generated bean...
199 /*
200 /* Revision 1.12  2004/02/16 18:26:37  pah
201 /*
202 /*
203 /* 1. This is a bean - it needs a default constructor
204 /* 2. His idea of what a group was is wrong - this represents an instance of the credentials - a user can only  have present themselves in one group
205 /* 3. The meanings of terms/methods it is defined in this bean are used throughout jes/mySpace and applicationController - any changes here must be debugged with full integration tests of these three components
206 /*
207 /* Revision 1.9  2004/01/09 21:38:13  pah
208 /* added new constructor
209 /*
210 /* Revision 1.8  2004/01/09 00:25:54  pah
211 /* added get userid
212 /*
213 /* Revision 1.7  2004/01/08 22:14:32  pah
214 /* add a method to return the community part of the user account
215 /*
216 /* Revision 1.6  2004/01/08 21:19:30  pah
217 /* changed to reflect the fact that the account should be user@community
218 /*
219 /* Revision 1.5  2003/12/30 10:40:02  pah
220 /* made the default constructor non-deprecated - this is a bean it has to have a default constructor - make it acutally put in some values
221 /*
222 /* Revision 1.4  2003/12/16 15:49:17  mch
223 /* Extended to include Certification functionality - in prep to replace it
224 /*
225 /* Revision 1.3  2003/12/16 13:35:06  mch
226 /* Added anonymous class constant for 'default' user
227 /* */
228