Allow GWT plugins to invoke the REST API

The package 'com.google.gerrit' in 'gerrit-plugin-gwtui' is renamed
to 'com.google.gerrit.plugin' since Eclipse doesn't like to have the
RestApi and Natives classes once again in the same package as in
'gerrit-gwtui'.

Change-Id: I37e28dfc52bce90204cf067b543ef91abead8e27
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin 2013-11-08 09:06:27 +01:00 committed by Shawn Pearce
parent fa3435ba3f
commit 1a31722152
12 changed files with 366 additions and 15 deletions

View File

@ -135,6 +135,14 @@ public class ActionContext extends JavaScriptObject {
}
static final void post(RestApi api, JavaScriptObject in, JavaScriptObject cb) {
if (NativeString.is(in)) {
post(api, ((NativeString) in).asString(), cb);
} else {
api.post(in, wrap(cb));
}
}
static final void post(RestApi api, String in, JavaScriptObject cb) {
api.post(in, wrap(cb));
}
@ -143,6 +151,14 @@ public class ActionContext extends JavaScriptObject {
}
static final void put(RestApi api, JavaScriptObject in, JavaScriptObject cb) {
if (NativeString.is(in)) {
put(api, ((NativeString) in).asString(), cb);
} else {
api.put(in, wrap(cb));
}
}
static final void put(RestApi api, String in, JavaScriptObject cb) {
api.put(in, wrap(cb));
}

View File

@ -59,16 +59,24 @@ public class ApiGlue {
_api: function(u) {return @com.google.gerrit.client.rpc.RestApi::new(Ljava/lang/String;)(u)},
get: function(u,b){@com.google.gerrit.client.api.ActionContext::get(Lcom/google/gerrit/client/rpc/RestApi;Lcom/google/gwt/core/client/JavaScriptObject;)(this._api(u),b)},
post: function(u,i,b){@com.google.gerrit.client.api.ActionContext::post(Lcom/google/gerrit/client/rpc/RestApi;Lcom/google/gwt/core/client/JavaScriptObject;Lcom/google/gwt/core/client/JavaScriptObject;)(this._api(u),i,b)},
post: function(u,i,b){
if (typeof i=='string')
@com.google.gerrit.client.api.ActionContext::post(Lcom/google/gerrit/client/rpc/RestApi;Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)(this._api(u),i,b);
else
@com.google.gerrit.client.api.ActionContext::post(Lcom/google/gerrit/client/rpc/RestApi;Lcom/google/gwt/core/client/JavaScriptObject;Lcom/google/gwt/core/client/JavaScriptObject;)(this._api(u),i,b);
},
put: function(u,i,b){
if(b){
@com.google.gerrit.client.api.ActionContext::put(Lcom/google/gerrit/client/rpc/RestApi;Lcom/google/gwt/core/client/JavaScriptObject;Lcom/google/gwt/core/client/JavaScriptObject;)(this._api(u),i,b)
if(typeof i=='string')
@com.google.gerrit.client.api.ActionContext::put(Lcom/google/gerrit/client/rpc/RestApi;Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)(this._api(u),i,b);
else
@com.google.gerrit.client.api.ActionContext::put(Lcom/google/gerrit/client/rpc/RestApi;Lcom/google/gwt/core/client/JavaScriptObject;Lcom/google/gwt/core/client/JavaScriptObject;)(this._api(u),i,b);
}else{
b=i;
@com.google.gerrit.client.api.ActionContext::put(Lcom/google/gerrit/client/rpc/RestApi;Lcom/google/gwt/core/client/JavaScriptObject;)(this._api(u),b)
@com.google.gerrit.client.api.ActionContext::put(Lcom/google/gerrit/client/rpc/RestApi;Lcom/google/gwt/core/client/JavaScriptObject;)(this._api(u),i)
}
},
'delete': function(u,b){@com.google.gerrit.client.api.ActionContext::delete(Lcom/google/gerrit/client/rpc/RestApi;Lcom/google/gwt/core/client/JavaScriptObject;)(this._api(u),b)},
JsonString: @com.google.gerrit.client.rpc.NativeString::TYPE,
};
Plugin.prototype = {

View File

@ -19,10 +19,15 @@ import com.google.gwt.user.client.rpc.AsyncCallback;
/** Wraps a String that was returned from a JSON API. */
public final class NativeString extends JavaScriptObject {
private static final JavaScriptObject TYPE = init();
public static final JavaScriptObject TYPE = init();
private static final native JavaScriptObject init()
/*-{ return function(s){this.s=s} }-*/;
private static final native JavaScriptObject init() /*-{
var T = function(s){this.s=s};
T.prototype = {
get: function(){return this.s},
};
return T;
}-*/;
static final NativeString wrap(String s) {
return wrap0(TYPE, s);
@ -32,7 +37,6 @@ public final class NativeString extends JavaScriptObject {
/*-{ return new T(s) }-*/;
public final native String asString() /*-{ return this.s; }-*/;
private final native void set(String v) /*-{ this.s = v; }-*/;
public static final AsyncCallback<NativeString>
unwrap(final AsyncCallback<String> cb) {

View File

@ -336,6 +336,11 @@ public class RestApi {
send(PUT, cb);
}
public <T extends JavaScriptObject> void put(String content,
AsyncCallback<T> cb) {
sendRaw(PUT, content, cb);
}
public <T extends JavaScriptObject> void put(
JavaScriptObject content,
AsyncCallback<T> cb) {

View File

@ -14,10 +14,10 @@
limitations under the License.
-->
<module>
<define-linker name="gerrit_plugin" class="com.google.gerrit.linker.GerritPluginLinker"/>
<define-linker name="gerrit_plugin" class="com.google.gerrit.plugin.linker.GerritPluginLinker"/>
<add-linker name="gerrit_plugin"/>
<generate-with class="com.google.gerrit.rebind.PluginGenerator">
<when-type-assignable class="com.google.gerrit.client.Plugin"/>
<generate-with class="com.google.gerrit.plugin.rebind.PluginGenerator">
<when-type-assignable class="com.google.gerrit.plugin.client.Plugin"/>
</generate-with>
<source path="client"/>
<source path="plugin/client"/>
</module>

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.client;
package com.google.gerrit.plugin.client;
import com.google.gwt.core.client.EntryPoint;

View File

@ -0,0 +1,54 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.plugin.client.rpc;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.rpc.AsyncCallback;
/** Wraps a String that was returned from a JSON API. */
public class NativeString extends JavaScriptObject {
private static final JavaScriptObject TYPE = init();
private static final native JavaScriptObject init()
/*-{ return $wnd.Gerrit.JsonString } }-*/;
public final native String asString()
/*-{ return this.get(); }-*/;
public static final
AsyncCallback<NativeString> unwrap(final AsyncCallback<String> cb) {
return new AsyncCallback<NativeString>() {
@Override
public void onSuccess(NativeString result) {
cb.onSuccess(result != null ? result.asString() : null);
}
@Override
public void onFailure(Throwable caught) {
cb.onFailure(caught);
}
};
}
public static final boolean is(JavaScriptObject o) {
return is(TYPE, o);
}
private static final native boolean is(JavaScriptObject T, JavaScriptObject o)
/*-{ return o instanceof T }-*/;
protected NativeString() {
}
}

View File

@ -0,0 +1,71 @@
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.plugin.client.rpc;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.json.client.JSONObject;
import java.util.AbstractList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class Natives {
/**
* Get the names of defined properties on the object. The returned set
* iterates in the native iteration order, which may match the source order.
*/
public static Set<String> keys(JavaScriptObject obj) {
if (obj != null) {
return new JSONObject(obj).keySet();
}
return Collections.emptySet();
}
public static <T extends JavaScriptObject> List<T> asList(
final JsArray<T> arr) {
if (arr == null) {
return null;
}
return new AbstractList<T>() {
@Override
public T set(int index, T element) {
T old = arr.get(index);
arr.set(index, element);
return old;
}
@Override
public T get(int index) {
return arr.get(index);
}
@Override
public int size() {
return arr.length();
}
};
}
public static <T extends JavaScriptObject> JsArray<T> arrayOf(T element) {
JsArray<T> arr = JavaScriptObject.createArray().cast();
arr.push(element);
return arr;
}
private Natives() {
}
}

View File

@ -0,0 +1,22 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.plugin.client.rpc;
import com.google.gwt.core.client.JavaScriptObject;
public class NoContent extends JavaScriptObject {
protected NoContent() {
}
}

View File

@ -0,0 +1,171 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.plugin.client.rpc;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.rpc.AsyncCallback;
public class RestApi {
private final StringBuilder path;
private boolean hasQueryParams;
public RestApi(String name) {
path = new StringBuilder();
path.append(name);
}
public RestApi view(String name) {
return idRaw(name);
}
public RestApi view(String pluginName, String name) {
return idRaw(pluginName + "~" + name);
}
public RestApi id(String id) {
return idRaw(URL.encodeQueryString(id));
}
public RestApi id(int id) {
return idRaw(Integer.toString(id));
}
public RestApi idRaw(String name) {
if (hasQueryParams) {
throw new IllegalStateException();
}
if (path.charAt(path.length() - 1) != '/') {
path.append('/');
}
path.append(name);
return this;
}
public RestApi addParameter(String name, String value) {
return addParameterRaw(name, URL.encodeQueryString(value));
}
public RestApi addParameter(String name, String... value) {
for (String val : value) {
addParameter(name, val);
}
return this;
}
public RestApi addParameterTrue(String name) {
return addParameterRaw(name, null);
}
public RestApi addParameter(String name, boolean value) {
return addParameterRaw(name, value ? "t" : "f");
}
public RestApi addParameter(String name, int value) {
return addParameterRaw(name, String.valueOf(value));
}
public RestApi addParameter(String name, Enum<?> value) {
return addParameterRaw(name, value.name());
}
public RestApi addParameterRaw(String name, String value) {
if (hasQueryParams) {
path.append("&");
} else {
path.append("?");
hasQueryParams = true;
}
path.append(name);
if (value != null) {
path.append("=").append(value);
}
return this;
}
public String path() {
return path.toString();
}
public <T extends JavaScriptObject>
void get(AsyncCallback<T> cb) {
get(path(), wrap(cb));
}
public void getString(AsyncCallback<String> cb) {
get(NativeString.unwrap(cb));
}
private native static void get(String p, JavaScriptObject r)
/*-{ $wnd.Gerrit.get(p, r) }-*/;
public <T extends JavaScriptObject>
void put(AsyncCallback<T> cb) {
put(path(), wrap(cb));
}
private native static void put(String p, JavaScriptObject r)
/*-{ $wnd.Gerrit.put(p, r) }-*/;
public <T extends JavaScriptObject>
void put(String content, AsyncCallback<T> cb) {
put(path(), content, wrap(cb));
}
private native static
void put(String p, String c, JavaScriptObject r)
/*-{ $wnd.Gerrit.put(p, c, r) }-*/;
public <T extends JavaScriptObject>
void put(JavaScriptObject content, AsyncCallback<T> cb) {
put(path(), content, wrap(cb));
}
private native static
void put(String p, JavaScriptObject c, JavaScriptObject r)
/*-{ $wnd.Gerrit.put(p, c, r) }-*/;
public <T extends JavaScriptObject>
void post(String content, AsyncCallback<T> cb) {
post(path(), content, wrap(cb));
}
private native static
void post(String p, String c, JavaScriptObject r)
/*-{ $wnd.Gerrit.post(p, c, r) }-*/;
public <T extends JavaScriptObject>
void post(JavaScriptObject content, AsyncCallback<T> cb) {
post(path(), content, wrap(cb));
}
private native static
void post(String p, JavaScriptObject c, JavaScriptObject r)
/*-{ $wnd.Gerrit.post(p, c, r) }-*/;
public void delete(AsyncCallback<NoContent> cb) {
delete(path(), wrap(cb));
}
private native static void delete(String p, JavaScriptObject r)
/*-{ '$wnd.Gerrit.delete'(p, r) }-*/;
private native static <T extends JavaScriptObject>
JavaScriptObject wrap(AsyncCallback<T> b) /*-{
return function(r) {
b.@com.google.gwt.user.client.rpc.AsyncCallback::onSuccess(Ljava/lang/Object;)(r)
}
}-*/;
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.linker;
package com.google.gerrit.plugin.linker;
import com.google.gwt.core.ext.LinkerContext;
import com.google.gwt.core.linker.CrossSiteIframeLinker;

View File

@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.rebind;
package com.google.gerrit.plugin.rebind;
import java.io.PrintWriter;