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

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

Introduction

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

Prototype

public Request sendRequest(String requestData, RequestCallback callback) throws RequestException 

Source Link

Document

Sends an HTTP request based on the current builder configuration with the specified data and callback.

Usage

From source file:org.kaaproject.kaa.server.admin.client.KaaAdmin.java

License:Apache License

public static void signOut() {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
            GWT.getModuleBaseURL() + "j_spring_security_logout");
    try {//from  w w  w  . j av a 2  s .c  om
        builder.sendRequest(null, new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
                redirectToModule("..");
            }

            @Override
            public void onError(Request request, Throwable exception) {
                redirectToModule("..");
            }
        });
    } catch (RequestException e) {
    }
}

From source file:org.kaaproject.kaa.server.admin.client.Login.java

License:Apache License

private void login(final String userName, String password) {
    String postData = preparePostData("j_username=" + userName, "j_password=" + password);

    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
            GWT.getModuleBaseURL() + "j_spring_security_check?" + postData);
    try {//w w w  .  jav a2s . co  m
        builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                Utils.handleException(exception, view);
            }

            public void onResponseReceived(Request request, Response response) {
                if (response.getStatusCode() == 0) {
                    Utils.handleNetworkConnectionError();
                } else {
                    String error = response.getHeader("Error");
                    String errorType = response.getHeader("ErrorType");
                    if (!isEmpty(errorType) && "TempCredentials".equals(errorType)) {
                        //change password
                        ChangePasswordDialog.Listener listener = new ChangePasswordDialog.Listener() {
                            @Override
                            public void onChangePassword() {
                                view.clearMessages();
                            }

                            @Override
                            public void onCancel() {
                            }
                        };
                        ChangePasswordDialog.showChangePasswordDialog(listener, userName,
                                Utils.messages.tempCredentials());
                    } else if (!isEmpty(error)) {
                        view.setErrorMessage(error);
                    } else {
                        view.clearMessages();
                        redirectToModule("kaaAdmin");
                    }
                }
            }
        });
    } catch (RequestException e) {
        Utils.handleException(e, view);
    }
}

From source file:org.kuali.student.common.ui.client.security.SpringSecurityLoginDialogHandler.java

License:Educational Community License

private void createLoginPanel() {
    lightbox = new KSLightBox();
    VerticalPanel panel = new VerticalPanel();

    FlexTable table = new FlexTable();

    errorLabel = new Label();
    errorLabel.setText(TIMEOUT_MSG);
    errorLabel.setStyleName("KSError");

    username = new TextBox();
    username.setName("j_username");

    password = new PasswordTextBox();
    password.setName("j_password");

    table.setText(0, 0, "Username");
    table.setWidget(0, 1, username);/*w w  w . j  av  a  2  s  .  c o m*/

    table.setText(1, 0, "Password");
    table.setWidget(1, 1, password);

    table.setWidget(2, 0, (new Button("Login", new ClickHandler() {
        public void onClick(ClickEvent event) {

            //JIRA FIX : KSENROLL-8731 - Replaced StringBuffer with StringBuilder
            StringBuilder postData = new StringBuilder();
            postData.append(URL.encode("j_username")).append("=").append(username.getText());
            postData.append("&").append(URL.encode("j_password")).append("=").append(password.getText());

            RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
                    context.getApplicationContextUrl() + "/j_spring_security_check");
            builder.setHeader("Content-type", "application/x-www-form-urlencoded");

            try {
                builder.sendRequest(postData.toString(), new RequestCallback() {

                    @Override
                    public void onError(Request req, Throwable caught) {
                        lightbox.hide();
                        KSErrorDialog.show(caught);
                    }

                    @Override
                    public void onResponseReceived(Request req, Response res) {
                        if (res.getStatusCode() == Response.SC_OK
                                && !res.getText().contains("Bad credentials")) {
                            lightbox.hide();
                        } else {
                            errorLabel.setText("Your login attempt was not successful, try again.");
                        }
                    }
                });
            } catch (RequestException e) {
                KSErrorDialog.show(e);
            }
        }
    })));

    panel.add(errorLabel);
    panel.add(table);

    panel.setStyleName("KSLoginPanel");
    lightbox.setWidget(panel);
}

From source file:org.mindinformatics.gwt.framework.component.profiles.src.testing.JsonProfileManager.java

License:Apache License

@Override
public void retrieveUserProfiles() {
    String url = GWT.getModuleBaseURL() + "profile/" + _application.getUserManager().getUser().getUserName()
            + "/all?format=json";
    if (!_application.isHostedMode())
        url = ApplicationUtils.getUrlBase(GWT.getModuleBaseURL()) + "profile/"
                + _application.getUserManager().getUser().getUserName() + "/all?format=json";

    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
    builder.setHeader("Content-type", "application/json");

    try {//w  ww .jav  a 2s  . co m
        Request request = builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                _application.getInitializer().addException("Couldn't retrieve User Profiles JSON");
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    ArrayList<MProfile> userProfiles = new ArrayList<MProfile>();
                    JsArray profiles = (JsArray) parseJson(response.getText());
                    for (int j = 0; j < profiles.length(); j++) {
                        JsoProfile jsoProfile = (JsoProfile) profiles.get(j);

                        MProfile profile = new MProfile();
                        profile.setUuid(jsoProfile.getUuid());
                        profile.setName(jsoProfile.getName());
                        profile.setDescription(jsoProfile.getDescription());
                        profile.setLastSavedOn(jsoProfile.getCreatedOn());

                        JsArray<JavaScriptObject> creators = jsoProfile.getCreatedBy();
                        if (getObjectType(creators.get(0)).equals(IPerson.TYPE)) {
                            AgentsFactory factory = new AgentsFactory();
                            JsoAgent jperson = (JsoAgent) creators.get(0);
                            profile.setLastSavedBy((IPerson) factory.createAgent(jperson));
                        }

                        JsArray<JsoProfileEntry> plugins = jsoProfile.getStatusPlugins();
                        for (int i = 0; i < plugins.length(); i++) {
                            JsoProfileEntry entry = plugins.get(i);
                            profile.getPlugins().put(entry.getName(), entry.getStatus());
                        }

                        JsArray<JsoProfileEntry> features = jsoProfile.getStatusFeatures();
                        for (int i = 0; i < features.length(); i++) {
                            JsoProfileEntry entry = features.get(i);
                            profile.getFeatures().put(entry.getName(), entry.getStatus());
                        }

                        userProfiles.add(profile);
                    }

                    setProfiles(userProfiles);
                    stageCompleted();
                } else {
                    _application.getInitializer().addException(
                            "Couldn't retrieve User Profiles JSON (" + response.getStatusText() + ")");
                }
            }
        });
    } catch (RequestException e) {
        _application.getInitializer().addException("Couldn't retrieve User Profiles JSON");
    }
}

From source file:org.mindinformatics.gwt.framework.component.profiles.src.testing.JsonProfileManager.java

License:Apache License

@Override
public void retrieveUserCurrentProfile() {

    String url = GWT.getModuleBaseURL() + "profile/" + _application.getUserManager().getUser().getUserName()
            + "/info?format=json";
    if (!_application.isHostedMode())
        url = ApplicationUtils.getUrlBase(GWT.getModuleBaseURL()) + "profile/"
                + _application.getUserManager().getUser().getUserName() + "/info?format=json";

    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
    builder.setHeader("Content-type", "application/json");

    try {//from  www . ja v a 2s  . c  o m
        Request request = builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                _application.getInitializer().addException("Couldn't retrieve User Profile JSON");
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    JsoProfile jsoProfile = (JsoProfile) ((JsArray) parseJson(response.getText())).get(0);

                    MProfile profile = new MProfile();
                    profile.setUuid(jsoProfile.getUuid());
                    profile.setName(jsoProfile.getName());
                    profile.setDescription(jsoProfile.getDescription());
                    profile.setLastSavedOn(jsoProfile.getCreatedOn());

                    AgentsFactory factory = new AgentsFactory();
                    JsArray<JavaScriptObject> creators = jsoProfile.getCreatedBy();
                    if (getObjectType(creators.get(0)).equals(IPerson.TYPE)) {
                        JsoAgent person = (JsoAgent) creators.get(0);
                        profile.setLastSavedBy((IPerson) factory.createAgent(person));
                    }

                    JsArray<JsoProfileEntry> plugins = jsoProfile.getStatusPlugins();
                    for (int i = 0; i < plugins.length(); i++) {
                        JsoProfileEntry entry = plugins.get(i);
                        profile.getPlugins().put(entry.getName(), entry.getStatus());
                    }

                    JsArray<JsoProfileEntry> features = jsoProfile.getStatusFeatures();
                    for (int i = 0; i < features.length(); i++) {
                        JsoProfileEntry entry = features.get(i);
                        profile.getFeatures().put(entry.getName(), entry.getStatus());
                    }

                    setCurrentProfile(profile);
                    stageCompleted();
                } else {
                    _application.getInitializer().addException(
                            "Couldn't retrieve User Profile JSON (" + response.getStatusText() + ")");
                }
            }
        });
    } catch (RequestException e) {
        _application.getInitializer().addException("Couldn't retrieve User Profile JSON");
    }
}

From source file:org.mindinformatics.gwt.framework.component.profiles.src.testing.JsonProfileManager.java

License:Apache License

@Override
public MProfile saveUserProfile(MProfile newProfile, final IUpdateProfileCallback callback) {
    String url = GWT.getModuleBaseURL() + "profile/" + _application.getUserManager().getUser().getUserName()
            + "/info?format=json";
    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
    builder.setHeader("Content-type", "application/json");

    HashMap<String, String> pluginsStatus = callback.getPluginsStatus();
    HashMap<String, String> featuresStatus = callback.getFeaturesStatus();

    StringBuffer postData = new StringBuffer();
    postData.append("[");
    postData.append("  {");
    postData.append("    \"uuid\": \"");
    postData.append(newProfile.getUuid());
    postData.append("\",");
    postData.append("    \"name\": \"");
    postData.append(newProfile.getName());
    postData.append("\",");
    postData.append("    \"description\": \"");
    postData.append(newProfile.getDescription());
    postData.append("\",");
    postData.append("    \"createdon\": \"");
    DateTimeFormat fmt = DateTimeFormat.getFormat("MM/dd/yyyy HH:mm:ss Z");
    postData.append(fmt.format(new Date()));
    postData.append("\",");
    postData.append("    \"createdby\": [");
    postData.append("      {");
    postData.append("        \"@id\": \"");
    postData.append(_application.getAgentManager().getUserPerson().getUri());
    postData.append("\",");
    postData.append("        \"@type\": \"");
    postData.append("foafx:Person");
    postData.append("\",");
    postData.append("        \"foafx:name\": \"");
    postData.append(_application.getAgentManager().getUserPerson().getName());
    postData.append("\"");
    postData.append("      }");
    postData.append("     ],");

    postData.append("    \"statusplugins\": [");
    for (String key : pluginsStatus.keySet()) {
        postData.append("      {");
        postData.append("        \"uuid\": \"");
        postData.append(key);/*from w  ww.j  av  a 2  s. c o m*/
        postData.append("\",");
        postData.append("        \"status\": \"");
        postData.append(pluginsStatus.get(key));
        postData.append("\"");
        postData.append("      },");
    }
    postData.append("     ],");

    postData.append("    \"statusfeatures\": [");
    for (String key : featuresStatus.keySet()) {
        postData.append("      {");
        postData.append("        \"uuid\": \"");
        postData.append(key);
        postData.append("\",");
        postData.append("        \"status\": \"");
        postData.append(featuresStatus.get(key));
        postData.append("\"");
        postData.append("      },");
    }
    postData.append("     ]");

    postData.append("  }");
    postData.append("]");

    try {
        Request request = builder.sendRequest(postData.toString(), new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                _application.getInitializer().addException("Couldn't retrieve User Profile JSON");
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {

                    JsoProfile jsoProfile = (JsoProfile) ((JsArray) parseJson(response.getText())).get(0);

                    MProfile profile = new MProfile();
                    profile.setUuid(jsoProfile.getUuid());
                    profile.setName(jsoProfile.getName());
                    profile.setDescription(jsoProfile.getDescription());
                    profile.setLastSavedOn(jsoProfile.getCreatedOn());

                    AgentsFactory factory = new AgentsFactory();
                    JsArray<JavaScriptObject> creators = jsoProfile.getCreatedBy();
                    if (getObjectType(creators.get(0)).equals(IPerson.TYPE)) {
                        JsoAgent person = (JsoAgent) creators.get(0);
                        profile.setLastSavedBy((IPerson) factory.createAgent(person));
                    }

                    JsArray<JsoProfileEntry> plugins = jsoProfile.getStatusPlugins();
                    for (int i = 0; i < plugins.length(); i++) {
                        JsoProfileEntry entry = plugins.get(i);
                        profile.getPlugins().put(entry.getName(), entry.getStatus());
                    }

                    JsArray<JsoProfileEntry> features = jsoProfile.getStatusFeatures();
                    for (int i = 0; i < features.length(); i++) {
                        JsoProfileEntry entry = features.get(i);
                        profile.getFeatures().put(entry.getName(), entry.getStatus());
                    }

                    setCurrentProfile(profile);
                    callback.updateCurrentProfile();
                } else {
                    _application.getInitializer().addException(
                            "Couldn't retrieve User Profile JSON (" + response.getStatusText() + ")");
                }
            }
        });
    } catch (RequestException e) {
        _application.getInitializer().addException("Couldn't retrieve User Profile JSON");
    }
    return newProfile;
}

From source file:org.mindinformatics.gwt.framework.component.profiles.src.testing.JsonProfileManager.java

License:Apache License

@Override
public void saveCurrentProfile(MProfile currentProfile, final IUpdateProfileCallback callback) {
    String url = GWT.getModuleBaseURL() + "profile/" + _application.getUserManager().getUser().getUserName()
            + "/save?format=json";
    if (!_application.isHostedMode())
        url = ApplicationUtils.getUrlBase(GWT.getModuleBaseURL()) + "profile/"
                + _application.getUserManager().getUser().getUserName() + "/save?format=json";

    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
    builder.setHeader("Content-type", "application/json");

    HashMap<String, String> pluginsStatus = callback.getPluginsStatus();
    HashMap<String, String> featuresStatus = callback.getFeaturesStatus();

    StringBuffer postData = new StringBuffer();
    postData.append("[");
    postData.append("  {");
    postData.append("    \"uuid\": \"");
    postData.append(currentProfile.getUuid());
    postData.append("\",");
    postData.append("    \"name\": \"");
    postData.append(currentProfile.getName());
    postData.append("\",");
    postData.append("    \"description\": \"");
    postData.append(currentProfile.getDescription());
    postData.append("\",");
    postData.append("    \"createdOn\": \"");
    DateTimeFormat fmt = DateTimeFormat.getFormat("MM/dd/yyyy HH:mm:ss Z");
    postData.append(fmt.format(new Date()));
    postData.append("\",");
    postData.append("    \"createdBy\": [");
    postData.append("      {");
    postData.append("        \"@id\": \"");
    //Window.alert(""+currentProfile.getLastSavedBy());
    postData.append(currentProfile.getLastSavedBy().getUri());
    postData.append("\",");
    postData.append("        \"@type\": \"");
    postData.append("foafx:Person");
    postData.append("\",");
    postData.append("        \"foafx:name\": \"");
    postData.append(currentProfile.getLastSavedBy().getName());
    postData.append("\"");
    postData.append("      }");
    postData.append("     ],");
    postData.append("    \"plugins\": [");

    for (String key : pluginsStatus.keySet()) {
        postData.append("      {");
        postData.append("        \"uuid\": \"");
        postData.append(key);//from w w  w  .  j  a  va  2s  .  c om
        postData.append("\",");
        postData.append("        \"status\": \"");
        postData.append(pluginsStatus.get(key));
        postData.append("\"");
        postData.append("      },");
    }
    postData.append("     ],");

    postData.append("    \"statusfeatures\": [");
    for (String key : featuresStatus.keySet()) {
        postData.append("      {");
        postData.append("        \"uuid\": \"");
        postData.append(key);
        postData.append("\",");
        postData.append("        \"status\": \"");
        postData.append(featuresStatus.get(key));
        postData.append("\"");
        postData.append("      },");
    }
    postData.append("     ]");

    postData.append("  }");
    postData.append("]");

    try {
        Request request = builder.sendRequest(postData.toString(), new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                _application.getInitializer().addException("Couldn't retrieve User Profile JSON");
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {

                    /*
                    JsoProfile jsoProfile = (JsoProfile)((JsArray) parseJson(response.getText())).get(0);
                            
                    MProfile profile = new MProfile();
                    profile.setUuid(jsoProfile.getUuid());
                    profile.setName(jsoProfile.getName());
                    profile.setDescription(jsoProfile.getDescription());
                    profile.setLastSavedOn(jsoProfile.getCreatedOn());
                            
                    AgentsFactory factory = new AgentsFactory();
                    JsArray<JavaScriptObject> creators = jsoProfile.getCreatedBy();
                    if(getObjectType(creators.get(0)).equals(IPerson.TYPE)) {
                       JsoAgent person = (JsoAgent) creators.get(0);
                       profile.setLastSavedBy((IPerson) factory.createAgent(person));
                    }
                            
                    JsArray<JsoProfileEntry> plugins = jsoProfile.getStatusPlugins();
                    for(int i=0; i<plugins.length(); i++) {
                       JsoProfileEntry entry = plugins.get(i);
                       profile.getPlugins().put(entry.getName(), entry.getStatus());
                    }
                            
                    setCurrentProfile(profile);
                    */

                    callback.updateCurrentProfile();
                } else {
                    _application.getInitializer().addException(
                            "Couldn't retrieve User Profile JSON (" + response.getStatusText() + ")");
                }
            }
        });
    } catch (RequestException e) {
        _application.getInitializer().addException("Couldn't retrieve User Profile JSON");
        callback.updateCurrentProfile();
    }
}

From source file:org.n52.client.ctrl.PropertiesManager.java

License:Open Source License

private void init() {
    try {//from w  ww.j  a va2  s.  c om
        // TODO refactor grabbing properties + loading mechanism
        setCurrentLanguage();
        RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET,
                "properties/client-properties.xml");
        RequestCallback callback = new RequestCallback() {

            public void onError(Request request, Throwable exception) {
                Toaster.getToasterInstance().addMessage(i18n.errorRequest());
            }

            public void onResponseReceived(Request request, Response response) {
                PropertiesManager.this.properties = XMLParser.parse(response.getText());
                Application.continueStartup();
            }
        };
        requestBuilder.sendRequest(null, callback);
    } catch (RequestException ex) {
        Toaster.getToasterInstance().addMessage(i18n.errorRequest());
    }
}

From source file:org.nsesa.editor.gwt.editor.client.Editor.java

License:EUPL

/**
 * Configures the local/*  w w w .ja v a 2 s  . c o m*/
 */
protected void configure() {
    try {
        RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, CONFIGURATION_FILE);

        requestBuilder.sendRequest("", new RequestCallback() {
            @Override
            public void onResponseReceived(Request req, Response resp) {
                final JavaScriptObject configuration;
                try {
                    configuration = JsonUtils.safeEval(resp.getText());
                    clientFactory.setConfiguration(configuration);
                    // configuration ok -- continue
                    LOG.log(Level.INFO, "Successfully read " + CONFIGURATION_FILE);
                } catch (Exception e) {
                    LOG.log(Level.WARNING,
                            "Could parse " + CONFIGURATION_FILE + ", configuration will be empty.", e);
                } finally {
                    // continue with the loading
                    onModuleLoadDeferred();
                }
            }

            @Override
            public void onError(Request res, Throwable throwable) {
                // handle errors
                LOG.log(Level.INFO, "Could not read " + CONFIGURATION_FILE + ", configuration will be empty.",
                        throwable);
                clientFactory.setConfiguration(JavaScriptObject.createObject());
                // configuration failed -- ignore
                onModuleLoadDeferred();
            }
        });
    } catch (RequestException e) {
        LOG.log(Level.SEVERE, "Could not execute GET request to retrieve " + CONFIGURATION_FILE, e);
        clientFactory.setConfiguration(JavaScriptObject.createObject());
        // configuration failed -- ignore
        onModuleLoadDeferred();
    }
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.annotea.AnnoteaClient.java

License:Apache License

public void getAnnotationList(String annotates, final boolean forceDecorate) {
    if (annotates.contains("?")) {
        annotates = annotates.substring(0, annotates.indexOf('?'));
    }/*from   w w  w. jav a  2s  .co m*/
    String url = controller.getAnnoteaServerUrl() + "?w3c_annotates=" + annotates;
    RequestBuilder getRequest = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
    try {
        getRequest.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
            }

            public void onResponseReceived(Request request, Response response) {
                responseManager.processAnnotationListResponse(response.getText());
                if (forceDecorate) {
                    // Force all the annotations to be redecorated
                    controller.updateAnnotations(true);
                }
            }
        });
    } catch (RequestException e) {
        GWT.log("Error while requesting annotations: " + url, e);
        Log.debug("Error while requesting annotations: " + url, e);
        Window.alert(e.toString());
    }
}