First off, I love thata GWT 2.0 includes native support for JSON-P
requests!

However, I can't quite dump my own JSONP implementation in favor of
the new GWT counterpart. Reason: my JSON-P web service requires OAuth
signatures.

Details:
Unfortunately, neither the (HTTP) RequestBuilder nor the
JsonpRequestBuilder allow you to change the URL much in a way that
allows me to sign my requests for OAuth in a friendly way. Because of
this, I have abstracted my own implementation of a Request object so I
can hand the Request off to a UrlSigner before converting it to a
RequestBuilder or JsonpRequestBuilder.

Unfortunately, the JsonpRequestBuilder does not add the "callback"
parameter until invoking the send method, at which point it is too
late to re-sign the request. Also, there is no way provided to set a
known callback value, or even query the request to determine what it
is. This means my URL cannot be properly signed via OAuth since I do
not know the value of the callback parameter when I sign the URL.



Perhaps my previous implementation will provide some inspiration as to
how a similar level of functionality could be included in the GWT-
provided JsonpRequest[Builder]:



public class JSONPRestService extends AbstractRestService {

    private static int cbIndex = 0;
    private Map<String, Callback> callbacks = new HashMap<String,
Callback>();

    @Override
    public void get(String uri, Callback cb, JavaScriptObject params)
{
        String callback = reserveCallback();
        this.callbacks.put(callback, cb);

        JSONObject jParams = params == null ? new JSONObject()
                : new JSONObject(params);
        jParams.put("callback", new JSONString(callback));

        // Abstracted Request model that can be manipulated before
signing...
        RequestProxy rp = super.buildRequest(Method.GET, uri, jParams
                .getJavaScriptObject());

        // Url Signing process, which includes the callback parameter
        OAuthParams op = new OAuthParams();
        op.signatureLocation = OAuthSignatureLocation.QUERYSTRING;
        OAuth.signRequest(rp, op);

        ScriptElement tag = Document.get().createScriptElement();
        tag.setLang("JavaScript");
        tag.setSrc(rp.url);

        createCallback(this, callback, tag);
        Document.get().getBody().appendChild(tag);
    }

    private native void createCallback(JSONPRestService svc, String
cbName,
            Element tag) /*-{
        $wnd[cbName] = function(results){
 
[email protected]::proceed
(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;Lcom/
google/gwt/dom/client/Element;)(cbName, results, tag);
        };
    }-*/;

    @SuppressWarnings("unused")
    private void proceed(String cbName, JavaScriptObject results,
            final Element tag) {
        Callback cb = this.callbacks.remove(cbName);
        cb.execute(results);
        DeferredCommand.addCommand(new Command() {
            public void execute() {
                Document.get().getBody().removeChild(tag);
            }
        });

    }

    private String reserveCallback() {
        return "__" + GWT.getModuleName().replaceAll("\\.", "_") +
"__"
                + JSONPRestService.cbIndex++;
    }
}

-- 
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to