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:org.sigmah.client.page.login.LoginView.java

License:Open Source License

private void doLogin(final String login, final String password, final Button loginButton, final Image loader) {
    final String query = "email=" + URL.encodeComponent(login) + "&password=" + URL.encodeComponent(password);

    final RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.POST, "../Login/service");
    requestBuilder.setCallback(new RequestCallback() {
        @Override//from   www.j a  v a2  s .c  o  m
        public void onResponseReceived(Request request, Response response) {
            if (response.getText().contains("OK"))
                Window.Location.reload();
            else {
                MessageBox.alert(I18N.CONSTANTS.loginConnectErrorTitle(),
                        I18N.CONSTANTS.loginConnectErrorBadLogin(), null);
                loginButton.setEnabled(true);
                loader.getElement().getStyle().setVisibility(Visibility.HIDDEN);
            }
        }

        @Override
        public void onError(Request request, Throwable exception) {
            MessageBox.alert(I18N.CONSTANTS.loginConnectErrorTitle(), exception.getMessage(), null);
            loginButton.setEnabled(true);
            loader.getElement().getStyle().setVisibility(Visibility.HIDDEN);
        }
    });

    requestBuilder.setRequestData(query);
    requestBuilder.setHeader("Content-Type", "application/x-www-form-urlencoded");

    loginButton.setEnabled(false);
    loader.getElement().getStyle().setVisibility(Visibility.VISIBLE);
    try {
        requestBuilder.send();
    } catch (RequestException ex) {
        MessageBox.alert(I18N.CONSTANTS.loginConnectErrorTitle(), ex.getMessage(), null);
    }
}

From source file:org.sigmah.shared.servlet.ServletRequestBuilder.java

License:Open Source License

/**
 * Sends the request with its optional parameter(s) (including {@code POST} parameters).
 * /*from  w w  w  .  ja v a2 s .  c  o  m*/
 * @param callback
 *          The {@code RequestCallback}.
 * @throws ServletRequestException
 *           If an error occurs during request call.
 */
public void send(final RequestCallback callback) throws ServletRequestException {

    final RequestBuilder requestBuilder = new RequestBuilder(requestMethod, urlBuilder.toString());

    requestBuilder.setCallback(callback != null ? callback : Void);

    final StringBuilder builder = new StringBuilder();

    if (ClientUtils.isNotEmpty(requestAttributes)) {

        final Iterator<String> iterator = requestAttributes.keySet().iterator();

        while (iterator.hasNext()) {
            final String next = iterator.next();
            final String attribute = requestAttributes.get(next);

            if (attribute != null) {
                builder.append(URL.encodeQueryString(next));
                builder.append('=');
                builder.append(URL.encodeQueryString(attribute));
                if (iterator.hasNext()) {
                    builder.append('&');
                }
            }
        }
    }

    if (isPostMethod()) {
        requestBuilder.setHeader("Content-Type", "application/x-www-form-urlencoded");
        requestBuilder.setRequestData(builder.length() > 0 ? builder.toString() : null);
    }

    try {

        requestBuilder.send();

    } catch (final RequestException e) {
        throw new ServletRequestException("Servlet request '" + builder + "' execution fails.", e);
    }
}

From source file:org.silverpeas.mobile.client.common.network.RestAuthenticationDispatcher.java

License:Open Source License

@Override
public Request send(final Method method, final RequestBuilder builder) throws RequestException {
    String credentials = login + "@domain" + domainId + ":" + password;
    byte[] credentialsEncoded = Base64.encode(credentials.getBytes());
    builder.setTimeoutMillis(SpMobileRequestBuilder.TIMEOUT);
    builder.setHeader("Authorization", "Basic " + convertByteArrayToString(credentialsEncoded));

    return builder.send();
}

From source file:org.silverpeas.mobile.client.common.network.RestDispatcher.java

License:Open Source License

@Override
public Request send(final Method method, final RequestBuilder builder) throws RequestException {
    builder.setTimeoutMillis(SpMobileRequestBuilder.TIMEOUT);
    builder.setHeader("Authorization", "Bearer " + SpMobil.getUser().getToken());

    if (AuthentificationManager.getInstance().getHeader(AuthentificationManager.XSTKN) != null) {
        builder.setHeader(AuthentificationManager.XSTKN,
                AuthentificationManager.getInstance().getHeader(AuthentificationManager.XSTKN));
    }//from  w w w.  ja va 2s.  c o m
    if (AuthentificationManager.getInstance().getHeader(AuthentificationManager.XSilverpeasSession) != null) {
        builder.setHeader(AuthentificationManager.XSilverpeasSession,
                AuthentificationManager.getInstance().getHeader(AuthentificationManager.XSilverpeasSession));
        builder.setHeader("Cookie", "JSESSIONID="
                + AuthentificationManager.getInstance().getHeader(AuthentificationManager.XSilverpeasSession));
    }

    return builder.send();
}

From source file:org.silverpeas.mobile.client.common.network.SpMobileRpcRequestBuilder.java

License:Open Source License

@Override
protected RequestBuilder doCreate(String serviceEntryPoint) {
    RequestBuilder builder = super.doCreate(serviceEntryPoint);
    builder.setTimeoutMillis(this.timeout);

    if (AuthentificationManager.getInstance().getHeader(AuthentificationManager.XSTKN) != null) {
        builder.setHeader(AuthentificationManager.XSTKN,
                AuthentificationManager.getInstance().getHeader(AuthentificationManager.XSTKN));
    }//from  ww  w  .  j  a v a  2  s.  co  m
    if (AuthentificationManager.getInstance().getHeader(AuthentificationManager.XSilverpeasSession) != null) {
        builder.setHeader(AuthentificationManager.XSilverpeasSession,
                AuthentificationManager.getInstance().getHeader(AuthentificationManager.XSilverpeasSession));
        builder.setHeader("Cookie", "JSESSIONID="
                + AuthentificationManager.getInstance().getHeader(AuthentificationManager.XSilverpeasSession));
    }

    /*if (SpMobil.getUserToken() != null) {
      builder.setHeader("X-Silverpeas-Session", SpMobil.getUserToken());
      builder.setHeader("X-STKN", AuthentificationManager.getInstance().getHeaders().get("X-STKN"));
    }*/

    return builder;
}

From source file:org.sonatype.gwt.client.request.DefaultRESTRequestBuilder.java

License:Open Source License

protected RequestBuilder build(String method, Resource resource, Variant variant) {
    RequestBuilder result = new FullRequestBuilder(method, getUrl(resource.getPath()));

    if (variant != null) {
        result.setHeader("Accept", variant.getMediaType());

        result.setHeader("Content-Type", variant.getMediaType());
    }/*from  ww w .j a  va  2s  .c o  m*/

    for (Map.Entry<String, String> header : resource.getHeaders().entrySet()) {
        result.setHeader(header.getKey(), header.getValue());
    }

    return result;
}

From source file:org.spiffyui.client.rest.RESTility.java

License:Apache License

/**
 * <p>//from   w  w w .  j  ava  2 s  . c  o m
 * Make an HTTP call and get the results as a JSON object.  This method handles
 * cases like login, error parsing, and configuration requests.
 *  </p>
 * 
 * @param options the options for the REST request
 */
public static void callREST(RESTOptions options) {
    if (hasPotentialXss(options.getDataString())) {
        options.getCallback().onError(new RESTException(RESTException.XSS_ERROR, "", STRINGS.noServerContact(),
                new HashMap<String, String>(), -1, options.getURL()));
        return;
    }

    RESTILITY.m_restCalls.put(options.getCallback(), new RESTCallStruct(options.getURL(),
            options.getDataString(), options.getMethod(), options.shouldReplay(), options.getEtag()));
    RequestBuilder builder = new RESTRequestBuilder(options.getMethod().getMethod(), options.getURL());
    /*
     Set our headers
     */
    builder.setHeader("Accept", "application/json");
    builder.setHeader("Accept-Charset", "UTF-8");
    if (options.getHeaders() != null) {
        for (String k : options.getHeaders().keySet()) {
            builder.setHeader(k, options.getHeaders().get(k));
        }
    }

    if (RESTILITY.m_bestLocale != null) {
        /*
         * The REST end points use the Accept-Language header to determine
         * the locale to use for the contents of the REST request.  Normally
         * the browser will fill this in with the browser locale and that
         * doesn't always match the preferred locale from the Identity Vault
         * so we need to set this value with the correct preferred locale.
         */
        builder.setHeader("Accept-Language", RESTILITY.m_bestLocale);
    }

    if (getUserToken() != null && getTokenServerUrl() != null) {
        builder.setHeader("Authorization", getFullAuthToken());
        builder.setHeader("TS-URL", getTokenServerUrl());
    }

    if (options.getEtag() != null) {
        builder.setHeader("If-Match", options.getEtag());
    }

    if (options.getDataString() != null) {
        /*
         Set our request data
         */

        builder.setRequestData(options.getDataString());

        //b/c jaxb/jersey chokes when there is no data when content-type is json
        builder.setHeader("Content-Type", options.getContentType());
    }

    builder.setCallback(RESTILITY.new RESTRequestCallback(options.getCallback()));

    try {
        /*
         If we are in the process of logging in then all other
         requests will just return with a 401 until the login
         is finished.  We want to delay those requests until
         the login is complete when we will replay all of them.
         */
        if (options.isLoginRequest() || !g_inLoginProcess) {
            builder.send();
        }
    } catch (RequestException e) {
        MessageUtil.showFatalError(e.getMessage());
    }

}

From source file:org.t3as.snomedct.gwt.client.gwt.AnalyseHandler.java

License:Open Source License

private void sendTextToServer() {
    statusLabel.setText("");
    conceptList.clear();//from w w w  . ja v  a  2  s  .  c  o m

    // don't do anything if we have no text
    final String text = mainTextArea.getText();
    if (text.length() < 1) {
        statusLabel.setText(messages.pleaseEnterTextLabel());
        return;
    }

    // disable interaction while we wait for the response
    glassPanel.setPositionAndShow();

    // build up the AnalysisRequest JSON object
    // start with any options
    final JSONArray options = new JSONArray();
    setSemanticTypesOption(types, options);
    // defaults
    options.set(options.size(), new JSONString("word_sense_disambiguation"));
    options.set(options.size(), new JSONString("composite_phrases 8"));
    options.set(options.size(), new JSONString("no_derivational_variants"));
    options.set(options.size(), new JSONString("strict_model"));
    options.set(options.size(), new JSONString("ignore_word_order"));
    options.set(options.size(), new JSONString("allow_large_n"));
    options.set(options.size(), new JSONString("restrict_to_sources SNOMEDCT_US"));

    final JSONObject analysisRequest = new JSONObject();
    analysisRequest.put("text", new JSONString(text));
    analysisRequest.put("options", options);

    // send the input to the server
    final RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, webserviceUrl);
    builder.setHeader("Content-Type", MediaType.APPLICATION_JSON);
    builder.setRequestData(analysisRequest.toString());

    // create the async callback
    builder.setCallback(new SnomedRequestCallback(conceptList, statusLabel, glassPanel, typeCodeToDescription));

    // send the request
    try {
        builder.send();
    } catch (final RequestException e) {
        statusLabel.setText(messages.problemPerformingAnalysisLabel());
        GWT.log("There was a problem performing the analysis: " + e.getMessage(), e);
        glassPanel.hide();
    }
}

From source file:org.talend.mdm.webapp.base.client.ServiceEnhancer.java

License:Open Source License

public static void customizeService(ServiceDefTarget service) {

    service.setRpcRequestBuilder(new RpcRequestBuilder() {

        @Override/*from w w  w  .ja v  a2 s . c o  m*/
        protected void doFinish(RequestBuilder rb) {
            super.doFinish(rb);
            Log.info(UrlUtil.getLanguage());
            rb.setHeader(LANGUAGE_HEADER, UrlUtil.getLanguage());
        }
    });
}

From source file:org.turbogwt.net.http.client.ServerConnectionImpl.java

License:Apache License

@Override
public void sendRequest(int timeout, @Nullable String user, @Nullable String password,
        @Nullable Headers headers, RequestBuilder.Method method, String url, String data,
        RequestCallback callback) throws RequestException {
    final RequestBuilder requestBuilder = new RequestBuilder(method, url);
    if (timeout > 0)
        requestBuilder.setTimeoutMillis(timeout);
    if (user != null)
        requestBuilder.setUser(user);/*from   w w  w  . j  a va 2  s .  com*/
    if (password != null)
        requestBuilder.setPassword(password);
    if (user != null && password != null)
        requestBuilder.setIncludeCredentials(true);
    if (headers != null) {
        for (Header header : headers) {
            requestBuilder.setHeader(header.getName(), header.getValue());
        }
    }
    requestBuilder.sendRequest(data, callback);
}