Intercepting RequestFactory requests

by Stefan

With RequestContext#fire() it is possible to provide a Receiver instance (“Callback”) to handle the result of a request to the server. One could think of some use cases where the same processing should be done for all RequestFactory calls, e.g.

  • Indicate a running request, e.g. change the mouse cursor, display a status or similar.
  • Handle http error codes, e.g. session time-outs or server unavailability.
  • Handle request time-outs.

To intercept all requests made via a RequestFactory it is possible to provide a customized RequestTransport implementation. This is done during the initialization:

import com.google.gwt.core.client.GWT;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.SimpleEventBus;
import com.google.web.bindery.requestfactory.shared.RequestTransport;

import cleancodematters.requestfactory.tutorial.client.PizzaRequestFactory;

public class CustomTransportDemo {

  public PizzaRequestFactory initializeRequestFactory() {
    
    EventBus eventBus = new SimpleEventBus();
    RequestTransport transport = new CustomRequestTransport();
    
    PizzaRequestFactory factory = GWT.create( PizzaRequestFactory.class );
    factory.initialize( eventBus, transport );
    return factory;
  }
}

CustomRequestTransport extends the DefaultRequestTransport implementation and can do the pre- and post processing for all requests. See this very basic example to change the cursor during a running request:

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style.Cursor;
import com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

public class CustomRequestTransport extends DefaultRequestTransport {

  @Override
  public void send( String payload, TransportReceiver receiver ) {
    doBeforeSend();
    super.send( payload, wrap( receiver ) );
  }
  
  private TransportReceiver wrap( final TransportReceiver delegate ) {
    return new TransportReceiver() {
      
      public void onTransportSuccess( String payload ) {
        doOnSuccess();
        delegate.onTransportSuccess( payload );
      }
      
      public void onTransportFailure( ServerFailure failure ) {
        doOnFailure( failure );
        delegate.onTransportFailure( failure );
      }
    };
  }

  protected void doBeforeSend() {
    // Some processing before the request is send
    Document.get().getBody().getStyle().setCursor( Cursor.WAIT );
  }

  protected void doOnSuccess() {
    // Some processing on success
    Document.get().getBody().getStyle().setCursor( Cursor.DEFAULT );
  }
  
  protected void doOnFailure( ServerFailure failure ) {
    // Some processing on failure
    Document.get().getBody().getStyle().setCursor( Cursor.DEFAULT );
  }
}
Advertisements

2 Comments to “Intercepting RequestFactory requests”

  1. Hi Stefan,
    I’m a new to gwt & request factory, i use Eclipse with GWT plugin, i’m building a simple program, i want some help on “HowTo” sending data from server to client through RequestFactory. I’ve to place the data on a ListBbox in client. Like list list of cities, list of hotels.. i need a simple tutorial or a simple example code so that i can understand…

  2. Aro Palace Brasov

    Thank you a bunch for sharing this with all of us you
    actually recognize what you’re speaking about! Bookmarked.
    Kindly additionally talk over with my web site =). We will have a link alternate arrangement between us

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: