Example usage for com.google.gwt.http.client RequestBuilder setCallback

List of usage examples for com.google.gwt.http.client RequestBuilder setCallback

Introduction

In this page you can find the example usage for com.google.gwt.http.client RequestBuilder setCallback.

Prototype

public void setCallback(RequestCallback callback) 

Source Link

Document

Sets the response handler for this request.

Usage

From source file:ca.upei.ic.timetable.client.Remote.java

License:Apache License

/**
 * Calls a remote method using HTTP GET/*ww w.j  a  va  2  s  .  com*/
 * 
 * @param method
 * @param params
 * @param callback
 */
public Request get(String method, Map<String, String> params, RequestCallback callback) {
    // build the query
    StringBuffer q = new StringBuffer();
    q.append("?method=" + method);

    if (null != params) {
        for (String key : params.keySet()) {
            q.append("&" + key + "=" + params.get(key));
        }
    }

    // build the request builder
    RequestBuilder req = new RequestBuilder(RequestBuilder.GET, url_ + q);

    req.setRequestData("");
    req.setCallback(callback);
    Request request = null;
    try {
        request = req.send();
    } catch (RequestException re) {
        callback.onError(request, re);
    }
    return request;
}

From source file:ca.upei.ic.timetable.client.Remote.java

License:Apache License

/**
 * Calls a remote method using HTTP POST
 * //from   w w  w.ja  v  a  2 s  . c om
 * @param method
 * @param contentType
 * @param data
 * @param callback
 */
public Request post(String method, String contentType, String data, RequestCallback callback) {
    StringBuffer q = new StringBuffer();
    q.append("?method=" + method);

    RequestBuilder req = new RequestBuilder(RequestBuilder.POST, url_ + q);

    req.setHeader("Content-type", contentType);
    req.setRequestData(data);
    req.setCallback(callback);
    Request request = null;
    try {
        request = req.send();
    } catch (RequestException re) {
        callback.onError(request, re);
    }
    return request;
}

From source file:cc.kune.core.client.sitebar.search.MultivalueSuggestBox.java

License:GNU Affero Public License

/**
 * Retrieve Options (name-value pairs) that are suggested from the REST
 * endpoint//from www.  ja v  a  2  s  .  c  om
 * 
 * @param query
 *          - the String search term
 * @param from
 *          - the 0-based begin index int
 * @param to
 *          - the end index inclusive int
 * @param callback
 *          - the OptionQueryCallback to handle the response
 */
private void queryOptions(final String query, final int from, final int to,
        final OptionQueryCallback callback) {
    final RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
            URL.encode(mrestEndpointUrl + "?" + SearcherConstants.QUERY_PARAM + "=" + query + "&"
                    + SearcherConstants.START_PARAM + "=" + from + "&" + SearcherConstants.LIMIT_PARAM + "="
                    + PAGE_SIZE));

    // Set our headers
    builder.setHeader("Accept", "application/json; charset=utf-8");

    // Fails on chrome
    // builder.setHeader("Accept-Charset", "UTF-8");

    builder.setCallback(new RequestCallback() {

        @Override
        public void onError(final com.google.gwt.http.client.Request request, final Throwable exception) {
            callback.error(exception);
        }

        @Override
        public void onResponseReceived(final com.google.gwt.http.client.Request request,
                final Response response) {
            final JSONValue val = JSONParser.parse(response.getText());
            final JSONObject obj = val.isObject();
            final int totSize = (int) obj.get(OptionResultSet.TOTAL_SIZE).isNumber().doubleValue();
            final OptionResultSet options = new OptionResultSet(totSize);
            final JSONArray optionsArray = obj.get(OptionResultSet.OPTIONS).isArray();

            if (options.getTotalSize() > 0 && optionsArray != null) {

                for (int i = 0; i < optionsArray.size(); i++) {
                    if (optionsArray.get(i) == null) {
                        /*
                         * This happens when a JSON array has an invalid trailing comma
                         */
                        continue;
                    }

                    final JSONObject jsonOpt = optionsArray.get(i).isObject();
                    final Option option = new Option();

                    final String longName = jsonOpt.get(OptionResultSet.DISPLAY_NAME).isString().stringValue();
                    final String shortName = jsonOpt.get(OptionResultSet.VALUE).isString().stringValue();
                    final JSONValue groupTypeJsonValue = jsonOpt.get("groupType");
                    final String prefix = groupTypeJsonValue.isString() == null ? ""
                            : GroupType.PERSONAL.name().equals(groupTypeJsonValue.isString().stringValue())
                                    ? I18n.t("User") + ": "
                                    : I18n.t("Group") + ": ";
                    option.setName(prefix
                            + (!longName.equals(shortName) ? longName + " (" + shortName + ")" : shortName));
                    option.setValue(jsonOpt.get(OptionResultSet.VALUE).isString().stringValue());
                    options.addOption(option);
                }
            }
            callback.success(options);
        }
    });

    try {
        if (lastQuery != null && lastQuery.isPending()) {
            lastQuery.cancel();
        }
        lastQuery = builder.send();
    } catch (final RequestException e) {
        updateFormFeedback(FormFeedback.ERROR, "Error: " + e.getMessage());
    }

}

From source file:ccc.client.gwt.core.GWTRequestExecutor.java

License:Open Source License

/** {@inheritDoc} */
@Override//from ww w. ja va  2 s .  com
public void invokeRequest(final Request request) {

    final ResponseHandler handler = request.getCallback();

    final String url = InternalServices.globals.appURL() + request.getPath();
    final RequestBuilder builder = new RequestBuilder(getMethod(request.getMethod()), url);

    builder.setHeader("Accept", "application/json");
    builder.setHeader(HttpMethod.OVERRIDE_HEADER, request.getMethod().toString());

    if (HttpMethod.POST.equals(request.getMethod()) || HttpMethod.PUT.equals(request.getMethod())) {
        builder.setHeader("Content-Type", "application/json");
        builder.setRequestData(request.getBody());
    }

    builder.setCallback(new GWTRequestCallback(handler));

    try {
        builder.send();
        GWT.log("Sent request: " + request.getMethod() + " " + url, null);
    } catch (final RequestException e) {
        handler.onFailed(e);
    }
}

From source file:com.agnie.gwt.common.client.rpc.LoaderRpcRequestBuilder.java

License:Open Source License

@Override
protected void doFinish(RequestBuilder rb) {
    super.doFinish(rb);
    rb.setCallback(new RequestCallbackWrapper(rb.getCallback()));
}

From source file:com.ait.toolkit.cordova.client.plugins.blackberry.pushwoosh.PushWooshBlackBerry.java

License:Open Source License

public void register() {
    RequestBuilder req = new RequestBuilder(RequestBuilder.POST, getBaseUrl() + "registerDevice");
    req.setHeader("Content-type", "application/json; charset=utf-8");
    req.setRequestData(getPushWooshRegisterPayload());
    req.setCallback(new RequestCallback() {

        @Override// w  w  w.ja v  a  2  s.  co  m
        public void onResponseReceived(Request request, Response response) {
            if (response.getStatusCode() == 200) {
                registerCallback.onSuccess(response.getText());
            } else {
                registerCallback.onError(-1);
            }
        }

        @Override
        public void onError(Request request, Throwable exception) {
            registerCallback.onError(-1);
        }
    });
    try {
        req.send();
    } catch (RequestException e) {
        e.printStackTrace();
    }
}

From source file:com.ait.toolkit.cordova.client.plugins.blackberry.pushwoosh.PushWooshBlackBerry.java

License:Open Source License

public void unregister(final PushWooshBlackBerryRegisterCallback cb) {
    RequestBuilder req = new RequestBuilder(RequestBuilder.POST, getBaseUrl() + "unregisterDevice");
    req.setHeader("Content-type", "application/json; charset=utf-8");
    req.setRequestData(getPushWooshUnregisterPayload());
    req.setCallback(new RequestCallback() {

        @Override/*from   www. j a  v a 2 s.  c o m*/
        public void onResponseReceived(Request request, Response response) {
            if (response.getStatusCode() == 200) {
                cb.onSuccess(response.getText());
            } else {
                cb.onError(-1);
            }
        }

        @Override
        public void onError(Request request, Throwable exception) {
            cb.onError(-1);
        }
    });
}

From source file:com.arcbees.analytics.client.ClientAnalytics.java

License:Apache License

public void fallback(JsArrayMixed arguments) {
    if ("send".equals(arguments.getString(0))) {
        JSONObject jsonOptions = new JSONObject(arguments.getObject(arguments.length() - 1));
        StringBuilder url = new StringBuilder();
        url.append(fallbackPath).append("?");
        url.append(ProtocolTranslator.getFieldName("hitType")).append("=")
                .append(URL.encodeQueryString(arguments.getString(1)));

        for (String key : jsonOptions.keySet()) {
            if (!"hitCallback".equals(key)) {
                JSONValue jsonValue = jsonOptions.get(key);
                String strValue = "";
                if (jsonValue.isBoolean() != null) {
                    strValue = jsonValue.isBoolean().booleanValue() + "";
                } else if (jsonValue.isNumber() != null) {
                    strValue = jsonValue.isNumber().doubleValue() + "";
                } else if (jsonValue.isString() != null) {
                    strValue = jsonValue.isString().stringValue();
                }/*from www  .j a  va  2 s .  c  om*/
                url.append("&").append(ProtocolTranslator.getFieldName(key)).append("=")
                        .append(URL.encodeQueryString(strValue));
            }
        }
        try {
            RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url.toString());
            requestBuilder.setCallback(new RequestCallback() {

                @Override
                public void onResponseReceived(Request request, Response response) {
                    // TODO call hitcallback if needed.
                }

                @Override
                public void onError(Request request, Throwable exception) {
                    // TODO Auto-generated method stub
                }
            });
            requestBuilder.send();
        } catch (RequestException e) {
        }
    }
}

From source file:com.cgxlib.xq.client.plugins.deferred.PromiseReqBuilder.java

License:Apache License

public PromiseReqBuilder(RequestBuilder builder) {
    builder.setCallback(this);
    try {/*from  w w  w . j a  va2s . c o m*/
        builder.send();
    } catch (RequestException e) {
        onError(null, e);
    }
}

From source file:com.facebook.tsdb.tsdash.client.service.HTTPService.java

License:Apache License

private <T> void get(final AsyncCallback<T> callback, final String url, String params,
        final JSONDecoder<T> decoder) {
    RequestBuilder req = new RequestBuilder(RequestBuilder.GET, url + "?" + params);
    req.setTimeoutMillis(TIMEOUT);
    req.setCallback(new RequestCallback() {
        @Override//from   w w  w  .j  a  va2  s  .  c o  m
        public void onResponseReceived(Request request, Response response) {
            try {
                T result = decoder.tryDecodeFromService(response.getText());
                callback.onSuccess(result);
            } catch (JSONParseException e) {
                GWT.log("Error parsing data from '" + url + "'", e);
                callback.onFailure(e);
            } catch (ServiceException e) {
                GWT.log("Error in remote service", e);
                callback.onFailure(e);
            }
        }

        @Override
        public void onError(Request request, Throwable e) {
            GWT.log("Error sending GET request to '" + url + "'", e);
            callback.onFailure(e);
        }
    });
    try {
        req.send();
    } catch (RequestException e) {
        GWT.log("Request exception for '" + url + "'", e);
        callback.onFailure(e);
    }
}