Sunday, January 20, 2013

Intercept GWT RPC Request and Response


Some times you want to do some generic things like setting some header or adding some log or may be even showing a progress bar while rpc call is in progress and hide it once you receive the response. I came across one of such requirement for showing progress bar while service call is in progress. Following is complete end to end solution I found after searching for some solutions. I got some help at following threads https://groups.google.com/forum/?fromgroups=#!msg/google-web-toolkit/VeiJUPBUsi4/Hq2VDMF9W1kJ and http://stackoverflow.com/questions/5309624/how-to-intercept-the-onsuccess-method-in-gwt

I created one complete end  to end working example from that help.

1:  public class LoaderRpcRequestBuilder extends RpcRequestBuilder {  
2:       private Loader     loader;  
3:       private class RequestCallbackWrapper implements RequestCallback {  
4:            private RequestCallback     callback;  
5:            RequestCallbackWrapper(RequestCallback aCallback) {  
6:                 this.callback = aCallback;  
7:            }  
8:            @Override  
9:            public void onResponseReceived(Request request, Response response) {  
10:                 loader.hide();  
11:                 callback.onResponseReceived(request, response);  
12:            }  
13:            @Override  
14:            public void onError(Request request, Throwable exception) {  
15:                 loader.hide();  
16:                 callback.onError(request, exception);  
17:            }  
18:       }  
19:       public LoaderRpcRequestBuilder(Loader loader) {  
20:            this.loader = loader;  
21:       }  
22:       public LoaderRpcRequestBuilder() {  
23:            this(new Loader());  
24:       }  
25:       @Override  
26:       protected RequestBuilder doCreate(String serviceEntryPoint) {  
27:            RequestBuilder rb = super.doCreate(serviceEntryPoint);  
28:            loader.show();  
29:            return rb;  
30:       }  
31:       @Override  
32:       protected void doFinish(RequestBuilder rb) {  
33:            super.doFinish(rb);  
34:            rb.setCallback(new RequestCallbackWrapper(rb.getCallback()));  
35:       }  
36:  }  

Here in above code we are creating our own custom RpcRequestBuilder by extending RpcRequestBuilder and by overriding its doCreate and doFinish() method. We are intercepting the calls that will be made. In doCreate() we are showing loader and then inside doFinish we are setting one general purpose RequestCallback wrapper which we have created to intercept response received. Where we are hiding our loader inside onResponseReceived() and onError.

Note: be sure to call super.doFinish(rb); other wise you will get SecurityException. There are some tokens which is set by doFinish method to avoid XSRF attack. (Though they say one should not just rely on this mechanism to avoid XSRF attack.)

Now that we have created custom RpcRequestBuilder we need to understand how to use it.

1:  ServiceAsync serivce = GWT.create(Service.class);  
2:  ServiceDefTarget target = (ServiceDefTarget) serivce;  
3:  target.setRpcRequestBuilder(new LoaderRpcRequestBuilder());  

When we make rpc call we get async instance before making any service call we need to set our own custom RpcRequestBuilder.





1 comment:

  1. dongtam
    game mu
    http://nhatroso.net/
    http://nhatroso.com/
    nhac san cuc manh
    tư vấn luật
    dịch vụ thành lập công ty trọn gói
    văn phòng luật
    tổng đài tư vấn pháp luật
    thành lập công ty
    http://we-cooking.com/
    chém gió
    trung tâm ngoại ngữtắc, về phần đối phó mấy người Địa tiên, Thiên tiên, Lang Lễ quả thật quá dễ dàng.

    Lang Lễ thân hình thật lớn ở trên hư không, yêu khí cuồng bạo tràn ngập, tấc tấc không gian gảy lìa, lúc này hơn mười tên Địa tiên, Thiên tiên toàn bộ bị Lang Lễ đánh chết, không ai có thể đào thoát, tới lúc này Địa tiên, Thiên tiên mới biết được chíndongtam
    game mu
    http://nhatroso.net/
    http://nhatroso.com/
    nhac san cuc manh
    tư vấn luật
    dịch vụ thành lập công ty trọn gói
    văn phòng luật
    tổng đài tư vấn pháp luật
    thành lập công ty
    http://we-cooking.com/
    chém gió
    trung tâm ngoại ngữh mình chọc phải người không nên chọc, hối hận cũng đã chậm rồi.

    Hết thảy yên lặng xuống, Diễm Ma thu hồi Thiên La Ma Võng, Nhạc Thành cũng khoanh chân ngồi trên mặt đất tiến nhập một loại ngộ đạo cảnh giới.

    - Lang Lễ, chủ nhân không phải là người bình thường, như vậy cũng có thể lĩnh ngộ.

    Diễm Ma kinh ngạc không thôi, có thể nhìn thấy đại chiến ngộ đạo, người như thế ở Tiên Ma giới cũng không có mấy người.

    - Đây chính là khó có được kỳ ngộ, chúng ta giúp chủ nhân hộ pháp.

    Lang Lễ nói, bình thường ngộ đạo cũng không biết cần bao nhiêu thời gian, chậm thì vài ngày

    ReplyDelete