Search in sources :

Example 6 with RequestBuilder

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);
    }
}
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)

Example 7 with RequestBuilder

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) {
    }
}
Also used : RequestBuilder(com.google.gwt.http.client.RequestBuilder) JSONArray(com.google.gwt.json.client.JSONArray) RequestException(com.google.gwt.http.client.RequestException) JSONValue(com.google.gwt.json.client.JSONValue) RequestCallback(com.google.gwt.http.client.RequestCallback) RequestCallback(com.google.gwt.http.client.RequestCallback)

Example 8 with RequestBuilder

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

Example 9 with RequestBuilder

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);
}
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) ErrorDialog(com.google.gerrit.client.ErrorDialog) NativeString(com.google.gerrit.client.rpc.NativeString) RequestException(com.google.gwt.http.client.RequestException)

Example 10 with RequestBuilder

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);
    }
}
Also used : GWT(com.google.gwt.core.client.GWT) PUT(com.google.gwt.http.client.RequestBuilder.PUT) POST(com.google.gwt.http.client.RequestBuilder.POST) GET(com.google.gwt.http.client.RequestBuilder.GET) RequestBuilder(com.google.gwt.http.client.RequestBuilder) RequestException(com.google.gwt.http.client.RequestException)

Aggregations

RequestBuilder (com.google.gwt.http.client.RequestBuilder)11 RequestException (com.google.gwt.http.client.RequestException)8 RequestCallback (com.google.gwt.http.client.RequestCallback)6 Request (com.google.gwt.http.client.Request)5 Response (com.google.gwt.http.client.Response)5 GWT (com.google.gwt.core.client.GWT)2 GET (com.google.gwt.http.client.RequestBuilder.GET)2 POST (com.google.gwt.http.client.RequestBuilder.POST)2 PUT (com.google.gwt.http.client.RequestBuilder.PUT)2 JSONValue (com.google.gwt.json.client.JSONValue)2 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 JSONArray (com.google.gwt.json.client.JSONArray)1 JSONObject (com.google.gwt.json.client.JSONObject)1 JSONString (com.google.gwt.json.client.JSONString)1 UiHandler (com.google.gwt.uibinder.client.UiHandler)1 RpcRequestBuilder (com.google.gwt.user.client.rpc.RpcRequestBuilder)1 DisclosurePanel (com.google.gwt.user.client.ui.DisclosurePanel)1