forked from google/gdata-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppsForYourDomainClient.java
More file actions
1202 lines (1035 loc) · 48.2 KB
/
Copy pathAppsForYourDomainClient.java
File metadata and controls
1202 lines (1035 loc) · 48.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.appsforyourdomain;
import com.google.gdata.client.appsforyourdomain.AppsForYourDomainQuery;
import com.google.gdata.client.appsforyourdomain.AppsGroupsService;
import com.google.gdata.client.appsforyourdomain.EmailListRecipientService;
import com.google.gdata.client.appsforyourdomain.EmailListService;
import com.google.gdata.client.appsforyourdomain.NicknameService;
import com.google.gdata.client.appsforyourdomain.UserService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.Link;
import com.google.gdata.data.appsforyourdomain.AppsForYourDomainErrorCode;
import com.google.gdata.data.appsforyourdomain.AppsForYourDomainException;
import com.google.gdata.data.appsforyourdomain.EmailList;
import com.google.gdata.data.appsforyourdomain.Login;
import com.google.gdata.data.appsforyourdomain.Name;
import com.google.gdata.data.appsforyourdomain.Nickname;
import com.google.gdata.data.appsforyourdomain.Quota;
import com.google.gdata.data.appsforyourdomain.generic.GenericEntry;
import com.google.gdata.data.appsforyourdomain.generic.GenericFeed;
import com.google.gdata.data.appsforyourdomain.provisioning.EmailListEntry;
import com.google.gdata.data.appsforyourdomain.provisioning.EmailListFeed;
import com.google.gdata.data.appsforyourdomain.provisioning.EmailListRecipientEntry;
import com.google.gdata.data.appsforyourdomain.provisioning.EmailListRecipientFeed;
import com.google.gdata.data.appsforyourdomain.provisioning.NicknameEntry;
import com.google.gdata.data.appsforyourdomain.provisioning.NicknameFeed;
import com.google.gdata.data.appsforyourdomain.provisioning.UserEntry;
import com.google.gdata.data.appsforyourdomain.provisioning.UserFeed;
import com.google.gdata.data.extensions.Who;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
import java.security.SecureRandom;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This is the client library for the Google Apps for Your Domain GData API.
* It shows how the AppsForYourDomainService can be used to manage users on a
* domain. This class contains a number of methods which can be used to
* create, update, retrieve, and delete entities such as users, email lists
* and nicknames. Also included is sample usage of the client library.
*
*/
public class AppsForYourDomainClient {
private static final Logger LOGGER = Logger.getLogger(
AppsForYourDomainClient.class.getName());
private static final String APPS_FEEDS_URL_BASE =
"https://apps-apis.google.com/a/feeds/";
protected static final String SERVICE_VERSION = "2.0";
protected String domainUrlBase;
protected EmailListRecipientService emailListRecipientService;
protected EmailListService emailListService;
protected NicknameService nicknameService;
protected UserService userService;
protected AppsGroupsService groupService;
/**
* Public getter for AppsGroupsService
* @return the groupService
*/
public AppsGroupsService getGroupService() {
return groupService;
}
protected final String domain;
protected AppsForYourDomainClient(String domain) {
this.domain = domain;
this.domainUrlBase = APPS_FEEDS_URL_BASE + domain + "/";
}
/**
* Constructs an AppsForYourDomainClient for the given domain using the
* given admin credentials.
*
* @param adminEmail An admin user's email address such as admin@domain.com
* @param adminPassword The admin's password
* @param domain The domain to administer
*/
public AppsForYourDomainClient(String adminEmail, String adminPassword,
String domain) throws Exception {
this(domain);
// Configure all of the different Provisioning services
userService = new UserService(
"gdata-sample-AppsForYourDomain-UserService");
userService.setUserCredentials(adminEmail, adminPassword);
nicknameService = new NicknameService(
"gdata-sample-AppsForYourDomain-NicknameService");
nicknameService.setUserCredentials(adminEmail, adminPassword);
emailListService = new EmailListService(
"gdata-sample-AppsForYourDomain-EmailListService");
emailListService.setUserCredentials(adminEmail, adminPassword);
emailListRecipientService = new EmailListRecipientService(
"gdata-sample-AppsForYourDomain-EmailListRecipientService");
emailListRecipientService.setUserCredentials(adminEmail, adminPassword);
groupService = new AppsGroupsService(adminEmail, adminPassword, domain,
"gdata-sample-AppsForYourDomain-AppsGroupService");
}
/**
* Driver for the sample.
*/
public void run() throws Exception {
String randomFactor =
Integer.toString(1000 + (new SecureRandom()).nextInt(9000));
// Create a new user.
String username = "SusanJones-" + randomFactor;
String givenName = "Susan";
String familyName = "Jones";
String password = "123$$abc";
String testGroupName = "discuss_general";
String testGroupId = "newgroup-" + randomFactor;
String testGroupDescription = "Discuss";
String memberUserName = "john.doe." + randomFactor;
String memberFirstName = "John";
String memberLastName = "Doe";
String memberPassword = "123$$$abc";
String ownerUserName = "jane.doe." + randomFactor;
String ownerFirstName = "Jane";
String ownerLastName = "Doe";
String ownerPassword = "123$$$abc";
UserEntry createdUserEntry =
createUser(username, givenName, familyName, password);
// Update the user's family name.
String newFamilyName = "Smith";
createdUserEntry.getName().setFamilyName(newFamilyName);
UserEntry updatedUserEntry = updateUser(username, createdUserEntry);
// Create a nickname for the user.
String nickname0 = "Susy-" + randomFactor;
NicknameEntry createdNicknameEntry0 = createNickname(username, nickname0);
// Create another nickname for the user.
String nickname1 = "Suse-" + randomFactor;
NicknameEntry createdNicknameEntry1 = createNickname(username, nickname1);
// Retrieve the nicknames for user.
NicknameFeed retrievedNicknameFeed = retrieveNicknames(username);
StringBuffer nicknames = new StringBuffer();
Iterator<NicknameEntry> nicknameIterator =
retrievedNicknameFeed.getEntries().iterator();
while (nicknameIterator.hasNext()) {
nicknames.append(nicknameIterator.next().getNickname().getName());
if (nicknameIterator.hasNext()) {
nicknames.append(", ");
}
}
LOGGER.log(Level.INFO,
"User '" + username + "' has the following nicknames: {" +
nicknames.toString() + "}.");
// Delete the nicknames.
deleteNickname(nickname0);
deleteNickname(nickname1);
// Create an email list.
String emailList = "Staff-" + randomFactor;
EmailListEntry createdEmailListEntry = createEmailList(emailList);
// Add the user to the email list.
addRecipientToEmailList(username + "@" + domain, emailList);
// Add an external email address to the list.
addRecipientToEmailList("jane.doe@externaldomain.com", emailList);
// Retrieve the email lists for which this user is subscribed.
EmailListFeed retrievedEmailListFeed = retrieveEmailLists(username);
StringBuffer emailLists = new StringBuffer();
Iterator<EmailListEntry> emailListIterator =
retrievedEmailListFeed.getEntries().iterator();
while (emailListIterator.hasNext()) {
emailLists.append(emailListIterator.next().getEmailList().getName());
if (emailListIterator.hasNext()) {
emailLists.append(", ");
}
}
LOGGER.log(Level.INFO,
"User '" + username + "' is in the following emailLists: {" +
emailLists.toString() + "}.");
//*/
LOGGER.log(Level.INFO, "Creating users for groups sample run");
createUser(memberUserName, memberFirstName, memberLastName, memberPassword);
createUser(ownerUserName, ownerFirstName, ownerLastName, ownerPassword);
GenericFeed groupsFeed = null;
GenericEntry groupsEntry = null;
Iterator<GenericEntry> groupsEntryIterator = null;
LOGGER.log(Level.INFO, "Creating group: " + testGroupId);
groupsEntry =
groupService.createGroup(testGroupId, testGroupName,
testGroupDescription, "");
LOGGER.log(Level.INFO, "Group created with following properties:\n"
+ groupsEntry.getAllProperties());
groupsEntry =
groupService.addMemberToGroup(testGroupId, memberUserName);
LOGGER.log(Level.INFO, "Added member: \n" + groupsEntry.getAllProperties());
groupsEntry = groupService.addOwnerToGroup(testGroupId, ownerUserName);
LOGGER.log(Level.INFO, "Added owner: \n" + groupsEntry.getAllProperties());
groupsEntry =
groupService.updateGroup(testGroupId, testGroupName,
testGroupDescription + "Updated: ", "");
LOGGER.log(Level.INFO, "Updated group description:\n"
+ groupsEntry.getAllProperties());
groupsFeed = groupService.retrieveAllMembers(testGroupId);
groupsEntryIterator = groupsFeed.getEntries().iterator();
StringBuffer members = new StringBuffer();
while (groupsEntryIterator.hasNext()) {
members.append(groupsEntryIterator.next().getProperty(
AppsGroupsService.APPS_PROP_GROUP_MEMBER_ID));
if (groupsEntryIterator.hasNext()) {
members.append(", ");
}
}
LOGGER.log(Level.INFO, testGroupId + " has these members: "
+ members.toString());
groupsFeed = groupService.retreiveGroupOwners(testGroupId);
groupsEntryIterator = groupsFeed.getEntries().iterator();
StringBuffer owners = new StringBuffer();
while (groupsEntryIterator.hasNext()) {
owners.append(groupsEntryIterator.next().getProperty(
AppsGroupsService.APPS_PROP_GROUP_EMAIL));
if (groupsEntryIterator.hasNext()) {
owners.append(", ");
}
}
LOGGER.log(Level.INFO, testGroupName + " has these owners: "
+ owners.toString());
groupsFeed = groupService.retrieveAllGroups();
groupsEntryIterator = groupsFeed.getEntries().iterator();
StringBuffer groups = new StringBuffer();
while (groupsEntryIterator.hasNext()) {
groups.append(groupsEntryIterator.next().getProperty(
AppsGroupsService.APPS_PROP_GROUP_ID));
if (groupsEntryIterator.hasNext()) {
groups.append(", ");
}
}
LOGGER.log(Level.INFO, "Domain has these groups:\n" + groups.toString());
groupsFeed = groupService.retrieveGroups(memberUserName, true);
groupsEntryIterator = groupsFeed.getEntries().iterator();
groups = new StringBuffer();
while (groupsEntryIterator.hasNext()) {
groups.append(groupsEntryIterator.next().getProperty(
AppsGroupsService.APPS_PROP_GROUP_ID));
if (groupsEntryIterator.hasNext()) {
groups.append(", ");
}
}
LOGGER.log(Level.INFO, memberUserName + " is subscribed to these groups:\n"
+ groups.toString());
boolean isMember = groupService.isMember(testGroupId, memberUserName);
LOGGER.log(Level.INFO, memberUserName + " is member of " + testGroupId
+ "?: " + isMember);
boolean isOwner = groupService.isOwner(testGroupId, ownerUserName);
LOGGER.log(Level.INFO, ownerUserName + " is owner of " + testGroupId
+ "?: " + isOwner);
groupService.removeOwnerFromGroup(ownerUserName + "@" + domain,
testGroupId);
LOGGER.log(Level.INFO, "Removing " + ownerUserName + " as owner from group "
+ testGroupId);
groupService.deleteGroup(testGroupId);
deleteUser(memberUserName);
deleteUser(ownerUserName);
// Delete the email list.
deleteEmailList(emailList);
// Delete the user.
deleteUser(username);
// Deleting a non-existent user and then catching the Exception.
String fakeUsername = "SusanJones-fake";
try {
deleteUser(fakeUsername);
} catch (AppsForYourDomainException e) {
if (e.getErrorCode() == AppsForYourDomainErrorCode.EntityDoesNotExist) {
// Do some post-error processing or logging.
LOGGER.log(Level.INFO, "Do some post-error processing or logging.");
}
}
}
/**
* Creates a new user with an email account.
*
* @param username The username of the new user.
* @param givenName The given name for the new user.
* @param familyName the family name for the new user.
* @param password The password for the new user.
* @return A UserEntry object of the newly created user.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserEntry createUser(String username, String givenName,
String familyName, String password) throws AppsForYourDomainException,
ServiceException, IOException {
return createUser(username, givenName, familyName, password, null, null);
}
/**
* Creates a new user with an email account.
*
* @param username The username of the new user.
* @param givenName The given name for the new user.
* @param familyName the family name for the new user.
* @param password The password for the new user.
* @param quotaLimitInMb User's quota limit in megabytes. This field is only
* used for domains with custom quota limits.
* @return A UserEntry object of the newly created user.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserEntry createUser(String username, String givenName,
String familyName, String password, Integer quotaLimitInMb)
throws AppsForYourDomainException, ServiceException, IOException {
return createUser(username, givenName, familyName, password, null,
quotaLimitInMb);
}
/**
* Creates a new user with an email account.
*
* @param username The username of the new user.
* @param givenName The given name for the new user.
* @param familyName the family name for the new user.
* @param password The password for the new user.
* @param passwordHashFunction The name of the hash function to hash the
* password
* @return A UserEntry object of the newly created user.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserEntry createUser(String username, String givenName,
String familyName, String password, String passwordHashFunction)
throws AppsForYourDomainException, ServiceException, IOException {
return createUser(username, givenName, familyName, password,
passwordHashFunction, null);
}
/**
* Creates a new user with an email account.
*
* @param username The username of the new user.
* @param givenName The given name for the new user.
* @param familyName the family name for the new user.
* @param password The password for the new user.
* @param passwordHashFunction Specifies the hash format of the password
* parameter
* @param quotaLimitInMb User's quota limit in megabytes. This field is only
* used for domains with custom quota limits.
* @return A UserEntry object of the newly created user.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserEntry createUser(String username, String givenName,
String familyName, String password, String passwordHashFunction,
Integer quotaLimitInMb)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO,
"Creating user '" + username + "'. Given Name: '" + givenName +
"' Family Name: '" + familyName +
(passwordHashFunction != null
? "' Hash Function: '" + passwordHashFunction : "") +
(quotaLimitInMb != null
? "' Quota Limit: '" + quotaLimitInMb + "'." : "'.")
);
UserEntry entry = new UserEntry();
Login login = new Login();
login.setUserName(username);
login.setPassword(password);
if (passwordHashFunction != null) {
login.setHashFunctionName(passwordHashFunction);
}
entry.addExtension(login);
Name name = new Name();
name.setGivenName(givenName);
name.setFamilyName(familyName);
entry.addExtension(name);
if (quotaLimitInMb != null) {
Quota quota = new Quota();
quota.setLimit(quotaLimitInMb);
entry.addExtension(quota);
}
URL insertUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION );
return userService.insert(insertUrl, entry);
}
/**
* Retrieves a user.
*
* @param username The user you wish to retrieve.
* @return A UserEntry object of the retrieved user.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserEntry retrieveUser(String username)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO,
"Retrieving user '" + username + "'.");
URL retrieveUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/" + username);
return userService.getEntry(retrieveUrl, UserEntry.class);
}
/**
* Retrieves all users in domain. This method may be very slow for domains
* with a large number of users. Any changes to users, including creations
* and deletions, which are made after this method is called may or may not be
* included in the Feed which is returned.
*
* @return A UserFeed object of the retrieved users.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserFeed retrieveAllUsers()
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO,
"Retrieving all users.");
URL retrieveUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/");
UserFeed allUsers = new UserFeed();
UserFeed currentPage;
Link nextLink;
do {
currentPage = userService.getFeed(retrieveUrl, UserFeed.class);
allUsers.getEntries().addAll(currentPage.getEntries());
nextLink = currentPage.getLink(Link.Rel.NEXT, Link.Type.ATOM);
if (nextLink != null) {
retrieveUrl = new URL(nextLink.getHref());
}
} while (nextLink != null);
return allUsers;
}
/**
* Retrieves one page (100) of users in domain. Any changes to users,
* including creations and deletions, which are made after this method is
* called may or may not be included in the Feed which is returned. If the
* optional startUsername parameter is specified, one page of users is
* returned which have usernames at or after the startUsername as per ASCII
* value ordering with case-insensitivity. A value of null or empty string
* indicates you want results from the beginning of the list.
*
* @param startUsername The starting point of the page (optional).
* @return A UserFeed object of the retrieved users.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserFeed retrievePageOfUsers(String startUsername)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO, "Retrieving one page of users"
+ (startUsername != null ? " starting at " + startUsername : "") + ".");
URL retrieveUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/");
AppsForYourDomainQuery query = new AppsForYourDomainQuery(retrieveUrl);
query.setStartUsername(startUsername);
return userService.query(query, UserFeed.class);
}
/**
* Updates a user.
*
* @param username The user to update.
* @param userEntry The updated UserEntry for the user.
* @return A UserEntry object of the newly updated user.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserEntry updateUser(String username, UserEntry userEntry)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO, "Updating user '" + username + "'.");
URL updateUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/" + username);
return userService.update(updateUrl, userEntry);
}
/**
* Deletes a user.
*
* @param username The user you wish to delete.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public void deleteUser(String username)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO, "Deleting user '" + username + "'.");
URL deleteUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/" + username);
userService.delete(deleteUrl);
}
/**
* Suspends a user. Note that executing this method for a user who is already
* suspended has no effect.
*
* @param username The user you wish to suspend.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserEntry suspendUser(String username)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO, "Suspending user '" + username + "'.");
URL retrieveUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/" + username);
UserEntry userEntry = userService.getEntry(retrieveUrl, UserEntry.class);
userEntry.getLogin().setSuspended(true);
URL updateUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/" + username);
return userService.update(updateUrl, userEntry);
}
/**
* Restores a user. Note that executing this method for a user who is not
* suspended has no effect.
*
* @param username The user you wish to restore.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserEntry restoreUser(String username)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO, "Restoring user '" + username + "'.");
URL retrieveUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/" + username);
UserEntry userEntry = userService.getEntry(retrieveUrl, UserEntry.class);
userEntry.getLogin().setSuspended(false);
URL updateUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/" + username);
return userService.update(updateUrl, userEntry);
}
/**
* Set admin privilege for user. Note that executing this method for a user
* who is already an admin has no effect.
*
* @param username The user you wish to make an admin.
* @throws AppsForYourDomainException If a Provisioning API specific error
* occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserEntry addAdminPrivilege(String username)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO, "Setting admin privileges for user '" + username + "'.");
URL retrieveUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/" + username);
UserEntry userEntry = userService.getEntry(retrieveUrl, UserEntry.class);
userEntry.getLogin().setAdmin(true);
URL updateUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/" + username);
return userService.update(updateUrl, userEntry);
}
/**
* Remove admin privilege for user. Note that executing this method for a user
* who is not an admin has no effect.
*
* @param username The user you wish to remove admin privileges.
* @throws AppsForYourDomainException If a Provisioning API specific error
* occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserEntry removeAdminPrivilege(String username)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO, "Removing admin privileges for user '" + username + "'.");
URL retrieveUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/" + username);
UserEntry userEntry = userService.getEntry(retrieveUrl, UserEntry.class);
userEntry.getLogin().setAdmin(false);
URL updateUrl = new URL(domainUrlBase + "user/" + SERVICE_VERSION + "/" + username);
return userService.update(updateUrl, userEntry);
}
/**
* Require a user to change password at next login. Note that executing this
* method for a user who is already required to change password at next login
* as no effect.
*
* @param username The user who must change his or her password.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public UserEntry forceUserToChangePassword(String username)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO, "Requiring " + username + " to change password at " +
"next login.");
URL retrieveUrl = new URL(domainUrlBase + "user/"
+ SERVICE_VERSION + "/" + username);
UserEntry userEntry = userService.getEntry(retrieveUrl, UserEntry.class);
userEntry.getLogin().setChangePasswordAtNextLogin(true);
URL updateUrl = new URL(domainUrlBase + "user/"
+ SERVICE_VERSION + "/" + username);
return userService.update(updateUrl, userEntry);
}
/**
* Creates a nickname for the username.
*
* @param username The user for which we want to create a nickname.
* @param nickname The nickname you wish to create.
* @return A NicknameEntry object of the newly created nickname.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public NicknameEntry createNickname(String username, String nickname)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO,
"Creating nickname '" + nickname +
"' for user '" + username + "'.");
NicknameEntry entry = new NicknameEntry();
Nickname nicknameExtension = new Nickname();
nicknameExtension.setName(nickname);
entry.addExtension(nicknameExtension);
Login login = new Login();
login.setUserName(username);
entry.addExtension(login);
URL insertUrl = new URL(domainUrlBase + "nickname/" + SERVICE_VERSION);
return nicknameService.insert(insertUrl, entry);
}
/**
* Retrieves a nickname.
*
* @param nickname The nickname you wish to retrieve.
* @return A NicknameEntry object of the newly created nickname.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public NicknameEntry retrieveNickname(String nickname) throws AppsForYourDomainException,
ServiceException, IOException {
LOGGER.log(Level.INFO, "Retrieving nickname '" + nickname + "'.");
URL retrieveUrl = new URL(domainUrlBase + "nickname/" + SERVICE_VERSION + "/" + nickname);
return nicknameService.getEntry(retrieveUrl, NicknameEntry.class);
}
/**
* Retrieves all nicknames for the given username.
*
* @param username The user for which you want all nicknames.
* @return A NicknameFeed object with all the nicknames for the user.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public NicknameFeed retrieveNicknames(String username)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO,
"Retrieving nicknames for user '" + username + "'.");
URL feedUrl = new URL(domainUrlBase + "nickname/" + SERVICE_VERSION);
AppsForYourDomainQuery query = new AppsForYourDomainQuery(feedUrl);
query.setUsername(username);
return nicknameService.query(query, NicknameFeed.class);
}
/**
* Retrieves one page (100) of nicknames in domain. Any changes to
* nicknames, including creations and deletions, which are made after
* this method is called may or may not be included in the Feed which is
* returned. If the optional startNickname parameter is specified, one page
* of nicknames is returned which have names at or after startNickname as per
* ASCII value ordering with case-insensitivity. A value of null or empty
* string indicates you want results from the beginning of the list.
*
* @param startNickname The starting point of the page (optional).
* @return A NicknameFeed object of the retrieved nicknames.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public NicknameFeed retrievePageOfNicknames(String startNickname)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO, "Retrieving one page of nicknames"
+ (startNickname != null ? " starting at " + startNickname : "") + ".");
URL retrieveUrl = new URL(
domainUrlBase + "nickname/" + SERVICE_VERSION + "/");
AppsForYourDomainQuery query = new AppsForYourDomainQuery(retrieveUrl);
query.setStartNickname(startNickname);
return nicknameService.query(query, NicknameFeed.class);
}
/**
* Retrieves all nicknames in domain. This method may be very slow for
* domains with a large number of nicknames. Any changes to nicknames,
* including creations and deletions, which are made after this method is
* called may or may not be included in the Feed which is returned.
*
* @return A NicknameFeed object of the retrieved nicknames.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public NicknameFeed retrieveAllNicknames()
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO,
"Retrieving all nicknames.");
URL retrieveUrl = new URL(domainUrlBase + "nickname/"
+ SERVICE_VERSION + "/");
NicknameFeed allNicknames = new NicknameFeed();
NicknameFeed currentPage;
Link nextLink;
do {
currentPage = nicknameService.getFeed(retrieveUrl, NicknameFeed.class);
allNicknames.getEntries().addAll(currentPage.getEntries());
nextLink = currentPage.getLink(Link.Rel.NEXT, Link.Type.ATOM);
if (nextLink != null) {
retrieveUrl = new URL(nextLink.getHref());
}
} while (nextLink != null);
return allNicknames;
}
/**
* Deletes a nickname.
*
* @param nickname The nickname you wish to delete.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
*/
public void deleteNickname(String nickname)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO, "Deleting nickname '" + nickname + "'.");
URL deleteUrl = new URL(domainUrlBase + "nickname/" + SERVICE_VERSION + "/" + nickname);
nicknameService.delete(deleteUrl);
}
/**
* Creates an empty email list.
*
* @param emailList The name of the email list you wish to create.
* @return An EmailListEntry object of the newly created email list.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
* @deprecated Email lists have been replaced by Groups. Use
* {@link AppsGroupsService#createGroup(String,String,String,String)}
* with Groups instead.
*/
@Deprecated
public EmailListEntry createEmailList(String emailList)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO,
"Creating email list '" + emailList + "'.");
EmailListEntry entry = new EmailListEntry();
EmailList emailListExtension = new EmailList();
emailListExtension.setName(emailList);
entry.addExtension(emailListExtension);
URL insertUrl = new URL(domainUrlBase + "emailList/" + SERVICE_VERSION);
return emailListService.insert(insertUrl, entry);
}
/**
* Retrieves all email lists in which the recipient is subscribed. Recipient
* can be given as a username or an email address of a hosted user.
*
* @param recipient The email address or username of the recipient.
* @return An EmailListFeed object containing the email lists.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
* @deprecated Email lists have been replaced by Groups. Use
* {@link AppsGroupsService#retrieveGroups(String,boolean)}
* with Groups.instead
*/
@Deprecated
public EmailListFeed retrieveEmailLists(String recipient)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO,
"Retrieving email lists for '" + recipient + "'.");
URL feedUrl = new URL(domainUrlBase + "emailList/"
+ SERVICE_VERSION);
AppsForYourDomainQuery query = new AppsForYourDomainQuery(feedUrl);
query.setRecipient(recipient);
return emailListService.query(query, EmailListFeed.class);
}
/**
* Retrieves all email lists in domain. This method may be very slow for
* domains with a large number of email lists. Any changes to email lists,
* including creations and deletions, which are made after this method is
* called may or may not be included in the Feed which is returned.
*
* @return A EmailListFeed object of the retrieved email lists.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
* @deprecated Email lists have been replaced by Groups. Use
* {@link AppsGroupsService#retrieveAllGroups()}
* with Groups instead.
*/
@Deprecated
public EmailListFeed retrieveAllEmailLists()
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO,
"Retrieving all email lists.");
URL retrieveUrl = new URL(domainUrlBase + "emailList/"
+ SERVICE_VERSION + "/");
EmailListFeed allEmailLists = new EmailListFeed();
EmailListFeed currentPage;
Link nextLink;
do {
currentPage = emailListService.getFeed(retrieveUrl, EmailListFeed.class);
allEmailLists.getEntries().addAll(currentPage.getEntries());
nextLink = currentPage.getLink(Link.Rel.NEXT, Link.Type.ATOM);
if (nextLink != null) {
retrieveUrl = new URL(nextLink.getHref());
}
} while (nextLink != null);
return allEmailLists;
}
/**
* Retrieves one page (100) of email lists in domain. Any changes to email
* lists, including creations and deletions, which are made after this method
* is called may or may not be included in the Feed which is returned. If the
* optional startEmailListName parameter is specified, one page of email lists
* is returned which have names at or after startEmailListName as per ASCII
* value ordering with case-insensitivity. A value of null or empty string
* indicates you want results from the beginning of the list.
*
* @param startEmailListName The starting point of the page (optional).
* @return A EmailListFeed object of the retrieved email lists.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
* @deprecated Email lists have been replaced by Groups. Use
* {@link AppsGroupsService#retrievePageOfGroups(Link)}
* with Groups instead.
*/
@Deprecated
public EmailListFeed retrievePageOfEmailLists(String startEmailListName)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO, "Retrieving one page of email lists"
+ (startEmailListName != null ? " starting at " + startEmailListName : "") + ".");
URL retrieveUrl = new URL(
domainUrlBase + "emailList/" + SERVICE_VERSION + "/");
AppsForYourDomainQuery query = new AppsForYourDomainQuery(retrieveUrl);
query.setStartEmailListName(startEmailListName);
return emailListService.query(query, EmailListFeed.class);
}
/**
* Retrieves an email list.
*
* @param emailList The name of the email list you want to retrieve.
* @return An EmailListEntry object of the retrieved email list.
* @throws AppsForYourDomainException If a Provisioning API specific occurs.
* @throws ServiceException If a generic GData framework error occurs.
* @throws IOException If an error occurs communicating with the GData
* service.
* @deprecated Email lists have been replaced by Groups. Use
* {@link AppsGroupsService#retrieveGroup(String)}
* with Groups instead.
*/
@Deprecated
public EmailListEntry retrieveEmailList(String emailList)
throws AppsForYourDomainException, ServiceException, IOException {
LOGGER.log(Level.INFO, "Retrieving email list '" + emailList + "'.");
URL retrieveUrl = new URL(domainUrlBase + "emailList/" + SERVICE_VERSION + "/" + emailList);