Search in sources :

Example 1 with Request

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());
    }
}
Also used : RequestBuilder(com.google.gwt.http.client.RequestBuilder) Request(com.google.gwt.http.client.Request) DisclosurePanel(com.google.gwt.user.client.ui.DisclosurePanel) JSONString(com.google.gwt.json.client.JSONString) RequestException(com.google.gwt.http.client.RequestException) EntryPoint(com.google.gwt.core.client.EntryPoint) Response(com.google.gwt.http.client.Response) JSONValue(com.google.gwt.json.client.JSONValue) RequestCallback(com.google.gwt.http.client.RequestCallback) JSONObject(com.google.gwt.json.client.JSONObject) InlineLabel(com.google.gwt.user.client.ui.InlineLabel) JSONString(com.google.gwt.json.client.JSONString)

Example 2 with Request

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);
    }
}
Also used : Request(com.google.gwt.http.client.Request)

Example 3 with Request

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");
}
Also used : Response(com.google.gwt.http.client.Response) RequestBuilder(com.google.gwt.http.client.RequestBuilder) RequestCallback(com.google.gwt.http.client.RequestCallback) Request(com.google.gwt.http.client.Request) RequestException(com.google.gwt.http.client.RequestException) UiHandler(com.google.gwt.uibinder.client.UiHandler)

Example 4 with Request

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);
    }
}
Also used : Response(com.google.gwt.http.client.Response) RequestBuilder(com.google.gwt.http.client.RequestBuilder) RequestCallback(com.google.gwt.http.client.RequestCallback) Request(com.google.gwt.http.client.Request) RequestException(com.google.gwt.http.client.RequestException)

Example 5 with Request

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);
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Response(com.google.gwt.http.client.Response) RequestBuilder(com.google.gwt.http.client.RequestBuilder) RequestCallback(com.google.gwt.http.client.RequestCallback) Request(com.google.gwt.http.client.Request)

Aggregations

Request (com.google.gwt.http.client.Request)8 RequestCallback (com.google.gwt.http.client.RequestCallback)7 Response (com.google.gwt.http.client.Response)7 RequestBuilder (com.google.gwt.http.client.RequestBuilder)5 RequestException (com.google.gwt.http.client.RequestException)4 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)1 ErrorDialog (com.google.gerrit.client.ErrorDialog)1 NativeString (com.google.gerrit.client.rpc.NativeString)1 EntryPoint (com.google.gwt.core.client.EntryPoint)1 JSONObject (com.google.gwt.json.client.JSONObject)1 JSONString (com.google.gwt.json.client.JSONString)1 JSONValue (com.google.gwt.json.client.JSONValue)1 UiHandler (com.google.gwt.uibinder.client.UiHandler)1 DisclosurePanel (com.google.gwt.user.client.ui.DisclosurePanel)1 DockLayoutPanel (com.google.gwt.user.client.ui.DockLayoutPanel)1 FlowPanel (com.google.gwt.user.client.ui.FlowPanel)1 Image (com.google.gwt.user.client.ui.Image)1 InlineLabel (com.google.gwt.user.client.ui.InlineLabel)1 AnnotatedTimeLine (com.google.gwt.visualization.client.visualizations.AnnotatedTimeLine)1