| Index: src/com/google/api/data/provisioning/v2/ProvisioningService.java |
| =================================================================== |
| --- src/com/google/api/data/provisioning/v2/ProvisioningService.java (revision 0) |
| +++ src/com/google/api/data/provisioning/v2/ProvisioningService.java (revision 0) |
| @@ -0,0 +1,204 @@ |
| +// Copyright 2011 Google Inc. All Rights Reserved. |
| + |
| +package com.google.api.data.provisioning.v2; |
| + |
| +import com.google.api.client.http.HttpExecuteIntercepter; |
| +import com.google.api.client.http.HttpRequest; |
| +import com.google.api.data.AbstractAtomService; |
| +import com.google.api.data.LoggingUtils; |
| +import com.google.api.data.provisioning.v2.model.GroupMemberList; |
| +import com.google.api.data.provisioning.v2.model.User; |
| +import com.google.api.data.provisioning.v2.model.UserList; |
| +import com.google.api.data.provisioning.v2.model.Group; |
| +import com.google.api.data.provisioning.v2.model.GroupList; |
| +import com.google.api.data.provisioning.v2.model.GroupMember; |
| +import java.io.IOException; |
| + |
| +/** |
| + * @author shraddhag@google.com (Shraddha Gupta) |
| + * |
| + */ |
| +public class ProvisioningService extends AbstractAtomService { |
| + |
| + public int maxRedirectAttempt = 5; |
| + private String gsessionid = ""; |
| + |
| + private class RedirectHandler implements HttpExecuteIntercepter { |
| + |
| + private ProvisioningService service; |
| + |
| + public RedirectHandler(ProvisioningService s) { |
| + if (s == null) throw new NullPointerException(); |
| + service = s; |
| + } |
| + |
| + public void intercept(HttpRequest request) { |
| + if (service.gsessionid != null) { |
| + request.url.set("gsessionid", service.gsessionid); |
| + } |
| + } |
| + } |
| + |
| + /** |
| + * Constructs a Google Provisioning API service. |
| + * |
| + * @param applicationName The application Name |
| + */ |
| + public ProvisioningService(String applicationName) { |
| + super(applicationName, ProvisioningApiInfo.NAMESPACE_DICTIONARY); |
| + getTransport().intercepters.add(0, new RedirectHandler(this)); |
| + } |
| + |
| + // =============== Setting up Service Information ================ |
| + |
| + @Override |
| + protected String getDefaultApiVersion() { |
| + return ProvisioningApiInfo.VERSION; |
| + } |
| + |
| + @Override |
| + protected String getClientLoginServiceCode() { |
| + return ProvisioningApiInfo.AUTH_TOKEN_TYPE; |
| + } |
| + |
| + @Override |
| + protected String getServiceAuthenticationScope() { |
| + return ProvisioningApiInfo.AUTH_SCOPE; |
| + } |
| + |
| + /** Returns User Entry with the given fields |
| + * @param username |
| + * @param password |
| + * @param givenName |
| + * @param familyName |
| + * @param quota |
| + * @param suspend |
| + * @param admin |
| + * @return UserEntry |
| + */ |
| + public User createUserEntry(String username, String password, String givenName, |
| + String familyName, int quota, Boolean suspend, Boolean admin) |
| + { |
| + User u = new User(); |
| + u.login.userName = username; |
| + u.login.password = password; |
| + u.name.givenName = givenName; |
| + u.name.familyName = familyName; |
| + u.quota.limit = quota; |
| + u.login.suspended = suspend; |
| + u.login.admin = admin; |
| + return u; |
| + } |
| + |
| + /** Returns the list of user in the given domain |
| + * @param domain |
| + * @return UserList: List of Users |
| + */ |
| + public UserList getUserFeed(String domain) throws IOException { |
| + return super.executeGet(UrlFactory.getAllUsersFeedUrl(domain), null, false, UserList.class); |
| + } |
| + |
| + /** Returns the user in the given domain with the given username |
| + * @param domain |
| + * @param username |
| + * @return User: The User entry |
| + */ |
| + public User getUser(String domain, String username) throws IOException { |
| + return super.executeGet(UrlFactory.getUserUrl(domain, username), null, false, User.class); |
| + } |
| + |
| + /** Updates the user in the given domain with the given username |
| + * @param entry |
| + * @param domain |
| + * @param username |
| + * @return User: The updated User entry |
| + */ |
| + public User updateUser(User entry, String domain, String username) throws IOException { |
| + return super.executeUpdate(entry, UrlFactory.getUserUrl(domain, username), null); |
| + } |
| + |
| + /** Creates the user with the given user entry |
| + * @param entry |
| + * @param domain |
| + * @return User: The created User entry |
| + */ |
| + public User insertUser(User entry, String domain) throws IOException { |
| + return super.executeInsert(entry, UrlFactory.getAllUsersFeedUrl(domain)); |
| + } |
| + |
| + /** Retrieves the Group with the given group id from the domain |
| + * @param domain |
| + * @param groupId |
| + * @return Group |
| + */ |
| + public Group getGroup(String domain, String groupId) throws IOException { |
| + return executeGet(UrlFactory.getGroupUrl(domain, groupId), null, false, Group.class); |
| + } |
| + |
| + /** Retrieves the List of Groups from the domain |
| + * @param domain |
| + * @return GroupList |
| + */ |
| + public GroupList getGroupFeed(String domain) throws IOException { |
| + return executeGet(UrlFactory.getGroupFeedUrl(domain), null, false, GroupList.class); |
| + } |
| + |
| + /** Retrieves the Group Member with the given memberId from the given group |
| + * @param domain |
| + * @param groupId |
| + * @param memberId |
| + * @return GroupMember Entry |
| + */ |
| + public GroupMember getGroupMember(String domain, String groupId, String memberId) |
| + throws IOException { |
| + return executeGet(UrlFactory.getGroupMemberUrl(domain, groupId, memberId), null, false, |
| + GroupMember.class); |
| + } |
| + |
| + /** Retrieves the List of Group Member from the given group |
| + * @param domain |
| + * @param groupId |
| + * @return GroupMemberList The list of Group members |
| + */ |
| + public GroupMemberList getGroupMemberFeed(String domain, String groupId) throws IOException { |
| + return executeGet(UrlFactory.getGroupMemberFeedUrl(domain, groupId), null, false, |
| + GroupMemberList.class); |
| + } |
| + |
| + /**Sample execution of functions */ |
| + public static void main(String[] args) throws IOException { |
| + LoggingUtils.enableLogging(); |
| + String domain = "srkapps.com"; |
| + String username = "shr"; |
| + ProvisioningService ps = new ProvisioningService("myTestApp"); |
| + ps.setClientLoginCredentials("admin@srkapps.com", "pamudi_1987"); |
| + User u1 = ps.getUser(domain, username); |
| + System.out.println(" username: " + u1.name.familyName); |
| + System.out.println(" admin: " + u1.login.admin + " suspended: " + u1.login.suspended |
| + + " agreed " + u1.login.agreedToTerms + " name: " + u1.name.familyName + " quota = " + u1.quota.limit); |
| + |
| + /** Creating a user |
| + User u = new User(); |
| + u.login.userName = "newUser"; |
| + u.login.password = "newUserPassword"; |
| + u.name.givenName = "New"; |
| + u.name.familyName = "User"; |
| + ps.insertUser(u, domain); |
| + */ |
| + |
| + /** List all users of a domain |
| + UserList list = ps.getUserFeed("srkapps.com"); |
| + for(int i=0;i< list.Users.size();i++) |
| + System.out.println(i + " " + list.Users.get(i).login.userName); |
| + */ |
| + |
| + /** Retrieve a group, print property by giving code name |
| + Group g = ps.getGroup("srkapps.com", "trial"); |
| + System.out.println("Group " + g.find(g.property, "groupId")); |
| + for(int i = 0; i < g.property.size(); i++) { |
| + System.out.println(i + " " + g.property.get(i).name + " " + g.property.get(i).value); |
| + } |
| + */ |
| + } |
| +} |
| + |