use of com.google.gwt.http.client.Request in project opentsdb by OpenTSDB.
the class QueryUi method asyncGetJson.
private void asyncGetJson(final String url, final GotJsonCallback callback) {
final RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
try {
builder.sendRequest(null, new RequestCallback() {
public void onError(final Request request, final Throwable e) {
displayError("Failed to get " + url + ": " + e.getMessage());
// Since we don't call the callback we've been given, reset this
// bit of state as we're not going to retry anything right now.
pending_requests = 0;
}
public void onResponseReceived(final Request request, final Response response) {
final int code = response.getStatusCode();
if (code == Response.SC_OK) {
clearError();
callback.got(JSONParser.parse(response.getText()));
return;
} else if (code >= Response.SC_BAD_REQUEST) {
// 400+ => Oops.
// Since we don't call the callback we've been given, reset this
// bit of state as we're not going to retry anything right now.
pending_requests = 0;
String err = response.getText();
// an error message.
if (!err.isEmpty() && err.charAt(0) == '{') {
final JSONValue json = JSONParser.parse(err);
final JSONObject result = json == null ? null : json.isObject();
final JSONValue jerr = result == null ? null : result.get("err");
final JSONString serr = jerr == null ? null : jerr.isString();
err = serr.stringValue();
// If the error message has multiple lines (which is common if
// it contains a stack trace), show only the first line and
// hide the rest in a panel users can expand.
final int newline = err.indexOf('\n', 1);
final String msg = "Request failed: " + response.getStatusText();
if (newline < 0) {
displayError(msg + ": " + err);
} else {
displayError(msg);
final DisclosurePanel dp = new DisclosurePanel(err.substring(0, newline));
// Attach the widget.
RootPanel.get("queryuimain").add(dp);
final InlineLabel content = new InlineLabel(err.substring(newline, err.length()));
// For readable stack traces.
content.addStyleName("fwf");
dp.setContent(content);
current_error.getElement().appendChild(dp.getElement());
}
} else {
displayError("Request failed while getting " + url + ": " + response.getStatusText());
// Since we don't call the callback we've been given, reset this
// bit of state as we're not going to retry anything right now.
pending_requests = 0;
}
graphstatus.setText("");
}
}
});
} catch (RequestException e) {
displayError("Failed to get " + url + ": " + e.getMessage());
}
}
use of com.google.gwt.http.client.Request in project libgdx by libgdx.
the class GwtNet method cancelHttpRequest.
@Override
public void cancelHttpRequest(HttpRequest httpRequest) {
HttpResponseListener httpResponseListener = listeners.get(httpRequest);
Request request = requests.get(httpRequest);
if (httpResponseListener != null && request != null) {
request.cancel();
httpResponseListener.cancelled();
requests.remove(httpRequest);
listeners.remove(httpRequest);
}
}
use of com.google.gwt.http.client.Request in project opennms by OpenNMS.
the class Navigation method linkTopOpenNMSClicked.
@UiHandler("m_link")
public void linkTopOpenNMSClicked(ClickEvent event) {
StringBuffer postData = new StringBuffer();
// note param pairs are separated by a '&'
// and each key-value pair is separated by a '='
postData.append(URL.encode("j_username")).append("=").append(URL.encode("ipv6"));
postData.append("&");
postData.append(URL.encode("j_password")).append("=").append(URL.encode("ipv6"));
postData.append("&");
postData.append(URL.encode("Login")).append("=").append(URL.encode("login"));
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode("/opennms/j_spring_security_check"));
builder.setHeader("Content-type", "application/x-www-form-urlencoded");
try {
builder.sendRequest(postData.toString(), new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
if (response.getStatusCode() == 200) {
Window.open("/opennms/index.jsp", "_target", null);
} else {
Window.alert("Failed to login");
}
}
@Override
public void onError(Request request, Throwable exception) {
Window.alert("Problem Logging in");
}
});
} catch (RequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Window.alert("Cliking link to OpenNMS");
}
use of com.google.gwt.http.client.Request in project gerrit by GerritCodeReview.
the class Gerrit method getDocIndex.
private static void getDocIndex(final AsyncCallback<DocInfo> cb) {
RequestBuilder req = new RequestBuilder(RequestBuilder.HEAD, GWT.getHostPageBaseURL() + INDEX);
req.setCallback(new RequestCallback() {
@Override
public void onResponseReceived(Request req, Response resp) {
switch(resp.getStatusCode()) {
case Response.SC_OK:
case Response.SC_MOVED_PERMANENTLY:
case Response.SC_MOVED_TEMPORARILY:
cb.onSuccess(DocInfo.create());
break;
default:
cb.onSuccess(null);
break;
}
}
@Override
public void onError(Request request, Throwable e) {
cb.onFailure(e);
}
});
try {
req.send();
} catch (RequestException e) {
cb.onFailure(e);
}
}
use of com.google.gwt.http.client.Request 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);
}
}
Aggregations