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

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

Introduction

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

Prototype

public void setHeader(String header, String value) 

Source Link

Document

Sets a request header with the given name and value.

Usage

From source file:br.com.pegasus.solutions.smartgwt.lib.client.view.impl.advanced.bar.infra.ExporterData.java

License:Apache License

private static void processRequest(String params) {
    try {//from   w  w  w . j ava  2  s .c om
        RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.POST,
                GWT.getModuleBaseURL() + "ExportServlet");
        requestBuilder.setHeader("Content-type", "application/x-www-form-urlencoded");

        requestBuilder.sendRequest(params, new RequestCallback() {
            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    ExporterData.callExportServlet
                            .fireEvent(new ClickEvent(ExporterData.callExportServlet.getJsObj()));
                } else {
                    MessageUtil
                            .showError("An Error occurred response status code: " + response.getStatusCode());
                }
            }

            public void onError(Request request, Throwable exception) {
                MessageUtil.showError(exception.getMessage());
            }
        });
    } catch (RequestException e) {
        MessageUtil.showError(e.getMessage());
    }
}

From source file:br.com.pegasus.solutions.smartgwt.lib.client.view.impl.util.HttpRequestUtil.java

License:Apache License

/**
 * do request/*w w  w . j  a v  a 2s  .c o m*/
 * 
 * @param servletName {@link String}
 * @param params {@link String}
 * @param iRequestBuilderSucessAction {@link IRequestBuilderFailedAction}
 * @param iFailedAction {@link IRequestBuilderFailedAction}
 * @throws RequestException
 * @return void
 */
private static void doRequest(final IRequestBuilderSucessAction iRequestBuilderSucessAction,
        final IRequestBuilderFailedAction iFailedAction, String url, String params,
        RequestBuilder.Method method) throws RequestException {

    RequestBuilder requestBuilder = new RequestBuilder(method, url);
    requestBuilder.setHeader("Content-type", "application/x-www-form-urlencoded");
    requestBuilder.sendRequest(params, new RequestCallback() {
        public void onResponseReceived(Request request, Response response) {
            if (200 == response.getStatusCode() && iRequestBuilderSucessAction != null) {
                iRequestBuilderSucessAction.executeAction(request, response);
            }
        }

        public void onError(Request request, Throwable exception) {
            if (iFailedAction != null) {
                iFailedAction.executeAction(request, exception);
            } else {
                MessageUtil.showError(exception.getMessage());
            }
        }
    });
}

From source file:bufferings.ktr.wjr.client.service.KtrWjrJsonServiceAsync.java

License:Apache License

/**
 * Sends a request to the ktrwjr servlet.
 * /*from  w w  w  .j  ava  2 s.  c o  m*/
 * @param params
 *          the parameters.
 * @param callback
 *          the callback.
 * @throws RequestException
 *           thrown when the error occured.
 */
void sendRequest(List<Pair> params, RequestCallback callback) throws RequestException {
    RequestBuilder builder = new RequestBuilder(POST, JSON_SERVLET_URL);
    builder.setHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_X_WWW_FORM_URLENCODED);
    builder.sendRequest(format(params), callback);
}

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

License:Apache License

/**
 * Calls a remote method using HTTP POST
 * /*  w w  w .  ja v a 2  s. c  o  m*/
 * @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.auth.WaveClientSimpleAuthenticator.java

License:GNU Affero Public License

/**
 * Do login./*  www.  j  ava2  s  .c om*/
 * 
 * @param userWithoutDomain
 *          the user without domain
 * @param passwd
 *          the passwd
 * @param callback
 *          the callback
 */
public void doLogin(final String userWithoutDomain, final String passwd, final AsyncCallback<Void> callback) {
    final RequestBuilder request = new RequestBuilder(RequestBuilder.POST, "/auth/signin");
    final StringBuffer params = new StringBuffer();
    params.append("address=");
    params.append(URL.encodeQueryString(userWithoutDomain));
    params.append("&password=");
    params.append(URL.encodeQueryString(passwd));
    params.append("&signIn=");
    params.append(URL.encodeQueryString("Sign in"));
    try {
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.sendRequest(params.toString(), new RequestCallback() {
            @Override
            public void onError(final Request request, final Throwable exception) {
                StackErrorEvent.fire(eventBus, exception);
                callback.onFailure(exception);
            }

            @Override
            public void onResponseReceived(final Request request, final Response response) {
                callback.onSuccess(null);
            }
        });
    } catch (final RequestException e) {
        StackErrorEvent.fire(eventBus, e);
    }
}

From source file:cc.kune.core.client.auth.WaveClientSimpleAuthenticator.java

License:GNU Affero Public License

/**
 * Do logout./* www . java2s.  c  o  m*/
 * 
 * @param callback
 *          the callback
 */
public void doLogout(final AsyncCallback<Void> callback) {
    // Original: <a href=\"/auth/signout?r=/\">"
    final RequestBuilder request = new RequestBuilder(RequestBuilder.GET, "/auth/signout");
    try {
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        final StringBuffer params = new StringBuffer();
        request.sendRequest(params.toString(), new RequestCallback() {
            @Override
            public void onError(final Request request, final Throwable exception) {
                StackErrorEvent.fire(eventBus, exception);
                callback.onFailure(exception);
            }

            @Override
            public void onResponseReceived(final Request request, final Response response) {
                callback.onSuccess(null);
            }
        });
    } catch (final RequestException e) {
        StackErrorEvent.fire(eventBus, e);
    }
}

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//w  ww  . j a  va 2 s . c  o  m
 * 
 * @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 w  w w  .jav  a 2  s  .c o  m
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:colt.json.gwt.client.JsonClient.java

License:Apache License

public void invoke(final String _url, final String _serviceName, String requestData, final IAsyncJSON result) {
    try {/*from  w ww .  j  a v a  2  s  .  c om*/
        RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.POST,
                URL.encode(_url + _serviceName));
        requestBuilder.setHeader("content-type", "application/x-www-form-urlencoded");

        Request request = requestBuilder.sendRequest(requestData, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                result.error(exception);
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    String text = response.getText();
                    JSONValue parser = JSONParser.parse(text);
                    JSONObject jobj = parser.isObject();
                    result.done(jobj);
                } else {
                    result.error(new RuntimeException(_url + _serviceName + " :("));
                }
            }
        });
    } catch (RequestException e) {
        Window.alert(e.getMessage());
        result.error(e);
    }
}

From source file:com.ait.tooling.nativetools.client.rpc.JSONCommandRequest.java

License:Open Source License

protected void doPrepareBuilderInit(final RequestBuilder builder) {
    builder.setHeader(ACCEPT_HEADER, CONTENT_TYPE_APPLICATION_JSON);

    builder.setHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_APPLICATION_JSON);

    builder.setHeader(X_STRICT_JSON_FORMAT_HEADER, "true");

    builder.setHeader(X_CLIENT_UUID_HEADER, Client.get().getClientUUID());
}