1
2
3
4
5
6
7
8
9
10
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228