From 0ecb7191936866095c7d79cbafaec3d51a2b8af0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Sun, 17 Mar 2013 20:01:14 +0100 Subject: [PATCH] Adding some utility classes to create Promises for GWT RequestBuilder, RPC and RFC services --- .../com/google/gwt/query/client/Function.java | 15 +++ .../gwt/query/client/plugins/Deferred.java | 108 +++++++++++++++++- 2 files changed, 117 insertions(+), 6 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java index 8c170b8c..83aae6ad 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java @@ -138,6 +138,21 @@ public abstract class Function { public T getArgument(int idx) { return getArgument(-1, idx, null); } + + /** + * Convenience alias for the getArguments(idx) method thought just to + * make gQuery code look closed to jQuery. + */ + public T arguments(int idx) { + return getArgument(idx); + } + + /** + * Convenience alias for the getArguments(argIdx, pos) method; + */ + public T arguments(int argIdx, int pos) { + return getArgument(argIdx, pos); + } /** * Safety return the argument in the position idx. diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java index fbef19d1..5c1ce194 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Deferred.java @@ -17,11 +17,22 @@ import static com.google.gwt.query.client.Promise.PENDING; import static com.google.gwt.query.client.Promise.REJECTED; import static com.google.gwt.query.client.Promise.RESOLVED; +import java.util.Set; + +import javax.validation.ConstraintViolation; + +import com.google.gwt.http.client.Request; +import com.google.gwt.http.client.RequestBuilder; +import com.google.gwt.http.client.RequestCallback; +import com.google.gwt.http.client.RequestException; +import com.google.gwt.http.client.Response; import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.Promise; import com.google.gwt.query.client.plugins.callbacks.Callbacks; import com.google.gwt.user.client.rpc.AsyncCallback; +import com.google.web.bindery.requestfactory.shared.Receiver; +import com.google.web.bindery.requestfactory.shared.ServerFailure; /** * Implementation of jQuery.Deferred for gwtquery. @@ -64,10 +75,94 @@ public class Deferred extends GQuery implements Promise.Deferred { } /** - * Utility class used to create promises for RPC CallBacks. + * Utility class used to create promises for RequestBuilder. + *
+   *        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "http://127.0.0.1:8888/whatever");
+   *        PromiseRequest gettingResponse = new PromiseRequest(builder);
+   *        
+   *        gettingResponse.fail(new Function() {
+   *          public void f() {
+   *            Throwable exception = arguments(0);
+   *          }
+   *        }).done(new Function() {
+   *          public void f() {
+   *            Response response = arguments(0);
+   *          }
+   *        });
+   * 
+ */ + public static class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCallback { + public PromiseReqBuilder(RequestBuilder builder) { + builder.setCallback(this); + try { + builder.send(); + } catch (RequestException e) { + onError(null, e); + } + } + + public void onError(Request request, Throwable exception) { + dfd.reject(exception, request); + } + + public void onResponseReceived(Request request, Response response) { + int status = response.getStatusCode(); + if (status <= 0 || status >= 400) { + String statusText = status <= 0 ? "Bad CORS" : response.getStatusText(); + onError(request, new RequestException("HTTP ERROR: " + status + " " + statusText + "\n" + response.getText())); + } else { + dfd.resolve(response, request); + } + } + } + + + /** + * Utility class used to create promises for RequestFactory services. + *
+   *    Request req1 = loginFact.api().login(null, null);
+   *    Request req2 = srvFact.api().getCurrentUser();
+   *    
+   *    Deferred.when(new PromiseRF(req1), new PromiseRF(req2)
+   *      .done(new Function() {
+   *        public void f() {
+   *          SessionProxy session = arguments(0, 0);
+   *          UserProxy user = arguments(1, 0);
+   *        }
+   *      })
+   *      .fail(new Function() {
+   *        public void f() {
+   *          ServerFailure failure = arguments(0);
+   *        }
+   *      }); 
+   * 
+ */ + public static class PromiseRF extends DeferredPromiseImpl { + public PromiseRF(com.google.web.bindery.requestfactory.shared.Request request) { + request.fire(new Receiver() { + public void onConstraintViolation(Set> violations) { + dfd.reject(new ServerFailure("ConstraintViolation"), violations); + } + + public void onFailure(ServerFailure error) { + dfd.reject(error); + } + + public void onSuccess(T response) { + dfd.resolve(response); + } + }); + } + } + + /** + * Utility class used to create promises for RPC services. *
    *        PromiseRPC gretting = new PromiseRPC();
    *        
+   *        GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
+   *        greetingService.greetServer("hi", gretting);
+   *        
    *        gretting.fail(new Function(){
    *          public void f() {
    *            Throwable error = getArgument(0);
@@ -88,20 +183,21 @@ public class Deferred extends GQuery implements Promise.Deferred {
       dfd.resolve(result);
     }
   }
+
   
   /**
    * Implementation of the Promise interface which is used internally by Deferred.
    */
   private static class DeferredPromiseImpl implements Promise {
-    protected com.google.gwt.query.client.plugins.Deferred dfd;
+    com.google.gwt.query.client.plugins.Deferred dfd;
     
-    protected DeferredPromiseImpl(com.google.gwt.query.client.plugins.Deferred o) {
-      dfd = o;
-    }
-
     protected DeferredPromiseImpl() {
       dfd = new com.google.gwt.query.client.plugins.Deferred();
     }
+
+    protected DeferredPromiseImpl(com.google.gwt.query.client.plugins.Deferred o) {
+      dfd = o;
+    }
     
     public Promise always(Function... f) {
       return done(f).fail(f);
-- 
2.39.5