Tuesday, May 3, 2011

gwt-dispatch and Custom Header or Basic Authentification

gwt-dispatch is an open source implementation of the command pattern, which is very well suited for use with GWT RPC.

In order to set Custom Headers in GWT you need to get the RequestBuilder instance which will perform the request under the covers for you.

There are two ways to do this:
  1. Let your Async Interface return RequestBuilder instead of void
  2. You can provide an instance of RPCRequestBuilder to your service, which will construct the RequestBuilderInstance

Using option one has the disadvantages that you have to send your request manually, thus changing the DispachAsync interface would break a lot of current applications (and introduce a lot of redundant code). So I was looking for another options.

You can downcast your service to ServiceDefTarget and set an instance of RPCRequestBuilder like this:

service = GWT.create(Dispatch.class);
((ServiceDefTarget) service).setRpcRequestBuilder(this.authenticatingRequestBuilder);

With and RPCRequestBuilder looking like this:

public class ACustomRequestBuilder extends RpcRequestBuilder {

protected RequestBuilder doCreate(String serviceEntryPoint) {
RequestBuilder requestBuilder = super.doCreate(serviceEntryPoint);
requestBuilder.setHeader("your header", "your value");
return requestBuilder;

Now you can easily control all aspects of your request.

Thanks to Robert Muntenau this is now also in the gwt-dispatch docs: http://code.google.com/docreader/#p=gwt-dispatch&s=gwt-dispatch&t=RequestBuilder


  1. Your work is very good and I appreciate you and hopping for some more informative posts. Thank you for sharing great information to us. Private Pflegeversicherung

  2. i love reading this article so beautiful!!great job! apartment kroatien

  3. I needed acquired a windows check safeguard and in addition it broke every week afterwards as a result of my disregard contacted SOSav Ersatzteile iPhone and so they acquired questioned the actual way it give and shattered a i and snapshot also knowledgeable a new monitor protector within a few days cheers

  4. Great write-up, I am a big believer in commenting on blogs to inform the blog writers know that they’ve added something worthwhile to the world wide web!.. weblink

  5. This comment has been removed by the author.

  6. I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article. Our site

  7. Danke für den schönen Bericht. In der Tat, es war ein unterhaltsamer Bericht. ich hoffe auf viele schöne Aggregate von Ihnen! Übrigens, wie könnten wir kommunizieren? Kaufen