use of com.google.gwt.http.client.RequestBuilder in project libgdx by libgdx.
the class GwtNet method sendHttpRequest.
@Override
public void sendHttpRequest(final HttpRequest httpRequest, final HttpResponseListener httpResultListener) {
if (httpRequest.getUrl() == null) {
httpResultListener.failed(new GdxRuntimeException("can't process a HTTP request without URL set"));
return;
}
final String method = httpRequest.getMethod();
final String value = httpRequest.getContent();
final boolean valueInBody = method.equalsIgnoreCase(HttpMethods.POST) || method.equals(HttpMethods.PUT);
RequestBuilder builder;
String url = httpRequest.getUrl();
if (method.equalsIgnoreCase(HttpMethods.GET)) {
if (value != null) {
url += "?" + value;
}
builder = new RequestBuilder(RequestBuilder.GET, url);
} else if (method.equalsIgnoreCase(HttpMethods.POST)) {
builder = new RequestBuilder(RequestBuilder.POST, url);
} else if (method.equalsIgnoreCase(HttpMethods.DELETE)) {
if (value != null) {
url += "?" + value;
}
builder = new RequestBuilder(RequestBuilder.DELETE, url);
} else if (method.equalsIgnoreCase(HttpMethods.PUT)) {
builder = new RequestBuilder(RequestBuilder.PUT, url);
} else {
throw new GdxRuntimeException("Unsupported HTTP Method");
}
Map<String, String> content = httpRequest.getHeaders();
Set<String> keySet = content.keySet();
for (String name : keySet) {
builder.setHeader(name, content.get(name));
}
builder.setTimeoutMillis(httpRequest.getTimeOut());
builder.setIncludeCredentials(httpRequest.getIncludeCredentials());
try {
Request request = builder.sendRequest(valueInBody ? value : null, new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
httpResultListener.handleHttpResponse(new HttpClientResponse(response));
requests.remove(httpRequest);
listeners.remove(httpRequest);
}
@Override
public void onError(Request request, Throwable exception) {
httpResultListener.failed(exception);
requests.remove(httpRequest);
listeners.remove(httpRequest);
}
});
requests.put(httpRequest, request);
listeners.put(httpRequest, httpResultListener);
} catch (Throwable e) {
httpResultListener.failed(e);
}
}
use of com.google.gwt.http.client.RequestBuilder in project opentsdb by OpenTSDB.
the class RemoteOracle method requestSuggestions.
@Override
public void requestSuggestions(final Request request, final Callback callback) {
if (current != null) {
pending_req = request;
pending_cb = callback;
return;
}
current = callback;
{
final String this_query = request.getQuery();
// range of results covering the new request.
if ((last_query != null && last_query.compareTo(this_query) <= 0 && this_query.compareTo(last_suggestion) < 0) || queries_seen.check(this_query)) {
current = null;
cache.requestSuggestions(request, callback);
return;
}
last_query = this_query;
}
final RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, SUGGEST_URL + type + "&q=" + last_query);
try {
builder.sendRequest(null, new RequestCallback() {
public void onError(final com.google.gwt.http.client.Request r, final Throwable e) {
// Something bad happened, drop the current request.
current = null;
if (pending_req != null) {
// But if we have another waiting...
// ... try it now.
requestSuggestions(pending_req, pending_cb);
}
}
// Need to use fully-qualified names as this class inherits already
// from a pair of inner classes called Request / Response :-/
public void onResponseReceived(final com.google.gwt.http.client.Request r, final com.google.gwt.http.client.Response response) {
if (response.getStatusCode() == com.google.gwt.http.client.Response.SC_OK) {
final JSONValue json = JSONParser.parse(response.getText());
// In case this request returned nothing, we pretend the last
// suggestion ended with the largest character possible, so we
// won't send more requests to the server if the user keeps
// adding extra characters.
last_suggestion = last_query + "\377";
if (json != null && json.isArray() != null) {
final JSONArray results = json.isArray();
final int n = Math.min(request.getLimit(), results.size());
for (int i = 0; i < n; i++) {
final JSONValue suggestion = results.get(i);
if (suggestion == null || suggestion.isString() == null) {
continue;
}
final String suggestionstr = suggestion.isString().stringValue();
last_suggestion = suggestionstr;
cache.add(suggestionstr);
}
// Is this response still relevant to what the requester wants?
if (requester.getText().startsWith(last_query)) {
cache.requestSuggestions(request, callback);
pending_req = null;
pending_cb = null;
}
}
}
// Regardless of what happened above, this is done.
current = null;
if (pending_req != null) {
final Request req = pending_req;
final Callback cb = pending_cb;
pending_req = null;
pending_cb = null;
requestSuggestions(req, cb);
}
}
});
} catch (RequestException ignore) {
}
}
use of com.google.gwt.http.client.RequestBuilder in project opennms by OpenNMS.
the class DefaultChartService method sendRequest.
private void sendRequest(String url, RequestCallback callback) {
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
builder.setHeader("Accept", "application/json");
builder.setUser("ipv6Rest");
builder.setPassword("ipv6Rest");
try {
builder.sendRequest(null, callback);
} catch (RequestException e) {
e.printStackTrace();
}
}
use of com.google.gwt.http.client.RequestBuilder in project gerrit by GerritCodeReview.
the class NewAgreementScreen method showCLA.
private void showCLA(AgreementInfo cla) {
current = cla;
String url = cla.url();
if (url != null && url.length() > 0) {
agreementGroup.setVisible(true);
agreementHtml.setText(Gerrit.C.rpcStatusWorking());
if (!url.startsWith("http:") && !url.startsWith("https:")) {
url = GWT.getHostPageBaseURL() + url;
}
final RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, url);
rb.setCallback(new RequestCallback() {
@Override
public void onError(Request request, Throwable exception) {
new ErrorDialog(exception).center();
}
@Override
public void onResponseReceived(Request request, Response response) {
final String ct = response.getHeader("Content-Type");
if (response.getStatusCode() == 200 && ct != null && (ct.equals("text/html") || ct.startsWith("text/html;"))) {
agreementHtml.setHTML(response.getText());
} else {
new ErrorDialog(response.getStatusText()).center();
}
}
});
try {
rb.send();
} catch (RequestException e) {
new ErrorDialog(e).show();
}
} else {
agreementGroup.setVisible(false);
}
finalGroup.setVisible(cla.autoVerifyGroup() != null);
yesIAgreeBox.setText("");
submit.setEnabled(false);
}
use of com.google.gwt.http.client.RequestBuilder in project gerrit by GerritCodeReview.
the class RestApi method sendText.
private <T extends JavaScriptObject> void sendText(Method method, String body, HttpCallback<T> cb) {
HttpImpl<T> httpCallback = new HttpImpl<>(background, cb);
try {
if (!background) {
RpcStatus.INSTANCE.onRpcStart();
}
RequestBuilder req = request(method);
req.setHeader("Content-Type", TEXT_UTF8);
req.sendRequest(body, httpCallback);
} catch (RequestException e) {
httpCallback.onError(null, e);
}
}
Aggregations