Wednesday, May 19, 2010

Accessing Google UserService from GWT client through RPC

There are times when you create a web app, you would want users to log in to provide user-contextual features. However, you don't wish to manage the log-in accounts. To do that, you could use OpenID or Google Accounts. The strategy is to have Google or an OpenID provider maintain and authenticate user existence. By doing that you do not carry the liability of lost passwords, log-in security, etc.

This tutorial concerns using Google Accounts to maintain the existence of your users in a Google App Engine application.

Google App Engine provides the class UserServiceFactory to facilitate that. UserServiceFactory is then used to generate UserService object, which in turn provides the following features
createLoginURL
createLogoutURL
getCurrentUser
isUserAdmin
isUserLoggedIn

You would use UserService object to generate the login URL for the browser. The browser would be directed/redirected to this URL. On reaching this URL, the Google log-in prompt would be displayed by Google's server.

Once logged in, your application could use the UserService object to inquire about the current user.

However, UserServiceFactory should be used on the server side not the client side. You cannot include it as part of your GWT module because

  • GWT compiler would request for the source code of UserServiceFactory and UserService - because GWT compiler needs to be presented all source code that is to be compiled into Javascript.
  • You cannot run UserServiceFactory and UserService on GWT client because they need to access Google's internal server-side objects.


If that is the case, how then do you pass UserService information to the GWT client? Simple - use RPC.

Server-side interface
You need to create a server-side GWT RPC interface similar to the following.
public interface UserServiceWrapper
extends RemoteService{
  String createLoginURL(String callbackUrl);
  String createLoginURL(String callbackUrl, String authDomain);
  String createLogoutURL(String callbackUrl);
  String createLogoutURL(String destinationURL, String authDomain);
  UserInfo getCurrentUser();
  boolean isUserAdmin();
  boolean isUserLoggedIn();
}

RPC servlet
Then your RPC server-side servlet would implement that interface
public class UserServiceServlet
extends RemoteServiceServlet
implements UserServiceWrapper
{
  final static public UserService userService = UserServiceFactory.getUserService();

  @Override
  public String createLoginURL(String callbackUrl) {
    return userService.createLoginURL(callbackUrl);
  }
  @Override
  public String createLoginURL(String callbackUrl, String authDomain) {
    return userService.createLoginURL(callbackUrl, authDomain);
  }
  @Override
  public String createLogoutURL(String callbackUrl) {
    return userService.createLogoutURL(callbackUrl);
  }
  @Override
  public String createLogoutURL(String destinationURL, String authDomain) {
    return userService.createLogoutURL(destinationURL, authDomain);
  }
  @Override
  public UserInfo getCurrentUser() {
    return mkUserInfo(userService.getCurrentUser());
  }
  @Override
  public boolean isUserAdmin() {
    return userService.isUserAdmin();
  }
  @Override
  public boolean isUserLoggedIn() {
    return userService.isUserLoggedIn();
  }
 
  static public SeriUser mkSeriUser(User user){
    return new SeriUser(
      user.getAuthDomain(),
      user.getEmail(),
      user.getNickname(),
      user.getUserId()
      );
  }
}

You cannot pass Google User object to a GWT client, unless you have the source code or fake the source code for User class. The simple way is to define a serializable SeriUser class and pass an instance of that class to the GWT client.

Client-side interface
Google Plugin for Eclipse would then aid you to create the client-side RPC interface:
public interface UserServiceWrapperAsync{
  void createLoginURL(
    String callbackUrl,
    AsyncCallback<String> callback);
  void createLoginURL(
    String callbackUrl, String authDomain,
    AsyncCallback<String> callback);
  void createLogoutURL(
    String callbackUrl,
    AsyncCallback<String> callback);
  void createLogoutURL(
    String destinationURL, String authDomain,
    AsyncCallback<String> callback);
  void getCurrentUser(
    AsyncCallback<UserInfo> callback);
  void isUserAdmin(
    AsyncCallback<Boolean> callback);
  void isUserLoggedIn(
    AsyncCallback<Boolean> callback);
}

Client-side call
Let us say you have a GWT main client window with a MenuBar.
public class MainMenuBar
extends Composite{
  private final UserServiceWrapperAsync userInfo =
    GWT.create(UserServiceWrapper.class);
  .....

  final Command doLogin = new Command(){
    @Override public void execute() {
      userInfo.createLoginURL(
        "googleLoggedin.jsp",
        loginCallback
      );
    }
   
    AsyncCallback<String> loginCallback = new AsyncCallback<String>()
    {
      @Override
      public void onSuccess(String loginUrl) {
        popupFrame.showPage(loginUrl, "Google Log-in");
      }
     
      @Override
      public void onFailure(Throwable caught) {
        Window.alert(caught.getMessage());
      }
    };
  ....
}

On your MenuBar, you would have a MenuItem [Log in to Google].

  • On clicking this item, it would execute a Command doLogin to either create a NamedFrame in the main window or show a DialogBox embedded with a NamedFrame.
  • Here, popupFrame is the instance of a class extension of DialogBox, which creates a NamedFrame by providing it with the Google login URL.
  • The NamedFrame would access that URL where a Google server would display the Google Accounts log-in page.
  • When users authenticate their account with Google successfully, Google server would redirect the page to the URL of your page which is stored in the String entryPointURL.
  • Hopefully, you have provided a close button on your DialogBox or NamedFrame to either close the frame or the DialogBox.
  • On closing the DialogBox or Frame, the onClose listener would trigger the MenuBar to update itself to show the current user.

The MenuBar would fire a RPC call
userInfo.getCurrentUser()
to the server in order to list the user.

To logout, a MenuItem [Log out] would similarly fire a RPC call to the server
userInfo.createLogoutURL
go through the whole whatnots similar to logging in.

19 comments:

  1. thank you, article is exactly that i am looking for.

    ReplyDelete
  2. thank you so much, great article. You just save my life

    ReplyDelete
  3. Nice article, do you have the code to download and play with? Tks.

    ReplyDelete
  4. what classes are you using for SeriUser and UserInfo

    ReplyDelete
  5. Please refer to this similar but complete example here
    http://code.google.com/webtoolkit/doc/latest/tutorial/appengine.html

    ReplyDelete
  6. Would this be too slow if you want to use their real time API? I would think that it takes a lot longer because you'd have to wait for the message to arrive at the server.

    ReplyDelete
  7. Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training India . Nowadays Java has tons of job opportunities on various vertical industry.

      Delete

  8. "It's about the game, it's about the team, it's about us doing our job," Pagano said Monday. "We know how great a team we're going to play. You've got a Hall of Fame coach, a Hall of Fame quarterback and a bunch of great players on both sides of the ball. It's going to be a tall order." شركة نقل اثاث بالمدينة المنورة شركة تتنظيف بالمدينة المنورة in Indianapolis as longtime friends Wayne, Redding and Bruce Arians and new friends such as Robert Mathis, Andrew Luck شركة تنظيف فلل بالمدينة المنورة Grigson and team owner Jim Irsay rallied the team. After returning home, the treatments continued for two more months.
    He's just focused on trying to get the biggest شركة تنظيف شقق بالمدينة المنورة win ? and best memory ? of his coaching career. شركة الصفرات لنقل الاثاث بالرياض
    General manager Ryan Grigson didn't go that far this week, though he شركة الصفرات لتنظيف الشقق بالرياض did recall watching the game and going through some old notes about Pagano almost immediately after Cundiff's kick sailed شركة تنظيف مجالس بالمدينة المنورة wide left. Within 24 hours, Grigson had contacted Ravens coach John شركة تنظيف بابها Harbaugh for permission to interview Pagano, who was still angry.

    ReplyDelete

  9. Skilled Contract / Commercial Workplace Cleaning Companies in London, Essex and a Maid to meet your own home-administration wants. You is perhaps tempted to save by selecting probably the most affordable service you will شركة كشف تسربات المياة بالمدينة المنورةdiscover, but you need to understand that you'll probably get better results in the event you focus on discovering a awhile it will get to be too much trying to scrub on a regular basis and at the identical time giving estimates, answering calls, scheduling, doing e book work, acquiring new shoppers, and so شركة تنظيف منازل بالمدينة المنورة forth. I made the mistake of pricing my work too low when I first began out. She is good with my dogs, and we trust her and her husband with our residence. If your online business is a sole proprietor cleaning service option for you. We provide a reliable, skilled and discreet cleansing service - allowing you or your small business to focus on your core exercise شركة تسليك مجارى بالمدينة المنورة while we restore and rejuvenate your property شركة الصفرات لكشف تسربات المياة بالرياض . Allow us to make your house or office germ-free, you and your family will keep healthy, as a result of our cleaners know how to throw mortgage you money they can't afford to lose. Denver Cleansing Service Company provides deep, steam carpet cleansing for customers involved افضل شركة تنظيف بجازانin placing some further care in their carpet. Do a regular test the quality of the worker's work, to reassure yourself that your requirements are being a professional or not. They have extensive experience in cleaning houses, flats, and commercial buildings. In case شركة نقل اثاث بابهاyou need a couple of of these companies, Allbrite gives shoppers the possibility to create a tailor-made custom package that solely consists of the issues they special occasion, or should you're in search of a maid that can assist you hold up to the mark when life gets crazy, we might help. Should شركة تنظيف خزانات بالطائفyou're nonetheless having bother, check out Firefox's help web page You may as well search near a metropolis, place, or handle as an alternative. you need to portray in your advertising material. We continuously get calls from different vendors on the market and over the previous few years we haven't شركة نقل اثاث بالظائفseen a fee increase at all. Glorious customer support, شركة تنظيف بالظائف accessible every weekday. Our work is environment friendly,

    ReplyDelete