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:n3phele.client.presenter.CommandActivity.java

License:Open Source License

protected void getRepos() {

    String url = repoListUrl;//from w  w  w .  j a va2 s .  c  o  m
    // Send request to server and catch any errors.
    RequestBuilder builder = AuthenticatedRequestFactory.newRequest(RequestBuilder.GET, url);
    try {
        builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                GWT.log("Couldn't retrieve JSON " + exception.getMessage());
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    Collection<Repository> r = Repository.asCollection(response.getText());
                    updateRepository(r.getElements());
                } else {
                    GWT.log("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                }
            }
        });
    } catch (RequestException e) {
        GWT.log("Couldn't retrieve JSON " + e.getMessage());
    }
}

From source file:n3phele.client.presenter.CommandActivity.java

License:Open Source License

public void run(Command data, String name, Map<String, String> paramMap, List<FileSpecification> inputFiles,
        List<FileSpecification> outputFiles, boolean sendEmail, String account, long profileId) {
    ExecuteCommandRequest request = new ExecuteCommandRequest();
    if (name == null || name.trim().length() == 0) {
        name = data.getName();/*from w  w w.  j  a v a  2 s .co m*/
    } else {
        name = name.trim();
    }
    request.setName(name);
    request.setAccount(account);
    request.setParameters(paramMap);
    request.setInputFiles(inputFiles);
    request.setOutputFiles(outputFiles);
    request.setNotify(sendEmail);
    request.setUser(AuthenticatedRequestFactory.getDefaultUsername());
    request.setCommand(data.getUri());
    request.setProfileId(profileId);
    String url = cacheManager.ServiceAddress + "activity";
    // Send request to server and catch any errors.
    RequestBuilder builder = AuthenticatedRequestFactory.newRequest(RequestBuilder.POST, url);
    builder.setHeader("Content-type", "application/json");

    try {
        Request msg = builder.sendRequest(request.toString(), new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                GWT.log("Couldn't retrieve JSON " + exception.getMessage());
                Window.alert("Couldn't retrieve JSON " + exception.getMessage());
            }

            public void onResponseReceived(Request request, Response response) {
                if (201 == response.getStatusCode()) {
                    GWT.log(response.getText() + " " + response.getHeadersAsString());
                    //cacheManager.refreshProgressList(); // deal with "eventual consistency" of queries
                    goToPrevious();
                } else {
                    Window.alert("Error code: " + response.getStatusCode() + " Status Text:"
                            + response.getStatusText() + "\n" + response.getText());
                    GWT.log("Couldn't submit command (" + response.getStatusText() + " " + response.getText()
                            + ")");

                }
            }
        });
    } catch (RequestException e) {
        Window.alert("Request exception " + e.getMessage() + "\n" + e.toString());
    }

}

From source file:n3phele.client.presenter.CommandActivity.java

License:Open Source License

/**
 * @param view//from ww w.j a  va2  s .  c o m
 */
public void fetchFiles(final FileNodeBrowser view, String repoURI, String prefix) {
    String url = repoURI + "/list";
    if (!isNullOrBlank(prefix)) {
        url += "?prefix=" + URL.encodeQueryString(prefix);
    }
    // Send request to server and catch any errors.
    RequestBuilder builder = AuthenticatedRequestFactory.newRequest(RequestBuilder.GET, url);
    try {
        builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                GWT.log("Couldn't retrieve JSON " + exception.getMessage());
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    GWT.log(response.getText());
                    RepoListResponse result = RepoListResponse.parseJSON(response.getText());
                    List<FileNode> crumbs = result.getCrumbs();
                    List<FileNode> namesWithPlaceholders = result.getFiles();
                    if (crumbs != null && CommandActivity.this.placeholderMap != null) {
                        String lastPath = "";
                        if (crumbs.size() > 1) {
                            FileNode lastCrumb = crumbs.get(crumbs.size() - 1);
                            lastPath = getCanonicalName(lastCrumb) + "/";
                        }
                        GWT.log("lastPath=" + lastPath);
                        if (CommandActivity.this.placeholderMap.containsKey(lastPath)) {
                            java.util.Collection<FileNode> placeholders = CommandActivity.this.placeholderMap
                                    .get(lastPath).values();
                            namesWithPlaceholders = new ArrayList<FileNode>(
                                    placeholders.size() + result.getFiles().size());
                            GWT.log("adding placeholder");
                            namesWithPlaceholders.addAll(placeholders);
                            namesWithPlaceholders.addAll(result.getFiles());
                        }
                    }
                    view.show(result.getCrumbs(), namesWithPlaceholders);
                } else {
                    GWT.log("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                }
            }
        });
    } catch (RequestException e) {
        GWT.log("Couldn't retrieve JSON " + e.getMessage());
    }

}

From source file:n3phele.client.presenter.CommandActivity.java

License:Open Source License

/**
 * @param view/*ww w .  j  a  v a2s .  c  om*/
 * @param repoURI
 * @param filename
 */
public void checkExists(final FileNodeBrowser view, String repoURI, final String filename) {
    String url = repoURI + "/validate";
    if (filename != null) {
        url += "?filename=" + URL.encodeQueryString(filename);
    }
    // Send request to server and catch any errors.
    RequestBuilder builder = AuthenticatedRequestFactory.newRequest(RequestBuilder.GET, url);
    try {
        builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                GWT.log("Couldn't retrieve JSON " + exception.getMessage());
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    GWT.log(response.getText());
                    boolean result = ValidationResponse.parseJSON(response.getText()).getExists();
                    if (result)
                        view.enableRun(filename);
                } else {
                    GWT.log("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                }
            }
        });
    } catch (RequestException e) {
        GWT.log("Couldn't retrieve JSON " + e.getMessage());
    }

}

From source file:n3phele.client.presenter.CommandListActivity.java

License:Open Source License

public void fetch(final int start, String search, boolean preferred) {
    lastStart = start;// ww w .java 2s .  c  om
    String url = URL.encode(
            collectionUrl + "&start=" + start + "&end=" + (start + PAGESIZE) + "&preferred=" + preferred);
    if (!isBlankOrNull(search))
        url += "&search=" + URL.encodeQueryString(search);
    // Send request to server and catch any errors.
    RequestBuilder builder = AuthenticatedRequestFactory.newRequest(RequestBuilder.GET, url);
    try {
        builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                Window.alert("Request error " + exception.getMessage());
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    Collection<Command> c = Command.asCollection(response.getText());
                    updateData(c.getUri(), c.getElements(), start, c.getTotal());
                } else {
                    Window.alert("Response error " + response.getStatusText());
                }
            }
        });
    } catch (RequestException exception) {
        Window.alert("Request exception " + exception.getMessage());
    }
}

From source file:n3phele.client.presenter.CreateServiceActivity.java

License:Open Source License

public void getAccount() {
    RequestBuilder builder = AuthenticatedRequestFactory.newRequest(RequestBuilder.GET, accountUri);
    try {/*  w  w w.  ja  v  a2  s . c o  m*/
        Request request = builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                // displayError("Couldn't retrieve JSON "+exception.getMessage());
            }

            public void onResponseReceived(Request request, Response response) {
                GWT.log("Got reply");
                if (200 == response.getStatusCode()) {
                    Account account = Account.asAccount(response.getText());
                    updateAccount(account);
                } else {

                }
            }

        });
    } catch (RequestException e) {
    }
}

From source file:n3phele.client.presenter.CreateServiceActivity.java

License:Open Source License

private void updateAccountDetails(String url, String name, String description, String cloud, String cloudId,
        final String password) {

    // Send request to server and catch any errors.
    if (url == null || url.trim().length() == 0 || url.equals("null")) {
        url = cacheManager.ServiceAddress + "account";
    }// ww  w.  ja v a2 s.c  o  m
    RequestBuilder builder = AuthenticatedRequestFactory.newRequest(RequestBuilder.POST, url);
    builder.setHeader("Content-type", "application/x-www-form-urlencoded");
    StringBuilder args = new StringBuilder();
    args.append("name=");
    args.append(URL.encodeQueryString(name));
    if (description != null && description.length() != 0) {
        args.append("&description=");
        args.append(URL.encodeQueryString(description));
    }
    args.append("&cloud=");
    args.append(URL.encodeQueryString(cloud));
    if (password != null && password.length() > 0) {
        args.append("&accountId=");
        args.append(URL.encodeQueryString(cloudId));
        args.append("&secret=");
        args.append(URL.encodeQueryString(password));
    }
    try {
        @SuppressWarnings("unused")
        Request request = builder.sendRequest(args.toString(), new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                // displayError("Couldn't retrieve JSON "+exception.getMessage());
            }

            public void onResponseReceived(Request request, Response response) {
                GWT.log("Got reply");
                if (200 == response.getStatusCode()) {
                    goToPrevious();
                } else if (201 == response.getStatusCode()) {
                    goToPrevious();
                } else {
                    Window.alert("Account update error " + response.getStatusCode() + response.getStatusText());
                }
            }

        });
    } catch (RequestException e) {
        //displayError("Couldn't retrieve JSON "+e.getMessage());
    }
}

From source file:n3phele.client.presenter.CreateServiceActivity.java

License:Open Source License

public void getAccountList() {
    String url = cacheManager.ServiceAddress + "account/listAccounts";
    RequestBuilder builder = AuthenticatedRequestFactory.newRequest(RequestBuilder.GET, url);
    try {/*  ww w.  ja  v a2 s  . com*/
        @SuppressWarnings("unused")
        Request request = builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                // displayError("Couldn't retrieve JSON "+exception.getMessage());
            }

            public void onResponseReceived(Request request, Response response) {
                GWT.log("Got reply");
                Collection<CommandCloudAccount> accounts = CommandCloudAccount.asCollection(response.getText());
                display.setCloudAccounts(accounts.getElements());
            }

        });
    } catch (RequestException e) {
        //displayError("Couldn't retrieve JSON "+e.getMessage());
    }
}

From source file:n3phele.client.presenter.CreateServiceActivity.java

License:Open Source License

public void exec(String name, Context context) {
    List<String> params = new ArrayList<String>();
    String action = "StackService";
    if (name == null || name.trim().length() == 0) {
        name = null;/* ww w.  j a  va 2s .  c o m*/
    }
    params.add("action=" + URL.encodeQueryString(action));
    String arg = "";
    params.add("arg=" + URL.encodeQueryString(arg.trim()));
    if (name != null)
        params.add("name=" + URL.encodeQueryString(name.trim()));

    String url = cacheManager.ServiceAddress + "process/exec";
    String seperator = "?";
    for (String param : params) {
        url = url + seperator + param;
        seperator = "&";
    }
    // Send request to server and catch any errors.
    RequestBuilder builder = AuthenticatedRequestFactory.newRequest(RequestBuilder.POST, url);
    builder.setHeader("Content-type", "application/json");

    try {
        GWT.log("Context :" + context.toJSON().toString());
        Request msg = builder.sendRequest(context == null ? null : context.toJSON().toString(),
                new RequestCallback() {
                    public void onError(Request request, Throwable exception) {
                        GWT.log("Couldn't retrieve JSON " + exception.getMessage());
                        Window.alert("Couldn't retrieve JSON " + exception.getMessage());
                    }

                    public void onResponseReceived(Request request, Response response) {
                        if (201 == response.getStatusCode()) {
                            GWT.log(response.getText() + " " + response.getHeader("location"));
                            goToPrevious();
                        } else {
                            Window.alert("Error code: " + response.getStatusCode() + " Status Text:"
                                    + response.getStatusText() + "\n" + response.getText());
                            GWT.log("Couldn't submit command (" + response.getStatusText() + " "
                                    + response.getText() + ")");

                        }
                    }
                });
    } catch (RequestException e) {
        Window.alert("Request exception " + e.getMessage() + "\n" + e.toString());
    }
}

From source file:n3phele.client.presenter.ProcessActivity.java

License:Open Source License

public void getProcess() {
    // Send request to server and catch any errors.
    RequestBuilder builder = AuthenticatedRequestFactory.newRequest(RequestBuilder.GET, processUri);
    try {/*w w  w  .j  a  v  a2  s.  c o m*/
        Request request = builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                // displayError("Couldn't retrieve JSON "+exception.getMessage());
            }

            public void onResponseReceived(Request request, Response response) {
                GWT.log("Got reply");
                if (200 == response.getStatusCode()) {
                    CloudProcess process = CloudProcess.asCloudProcess(response.getText());
                    updateProcess(process);
                } else {

                }
            }

        });
    } catch (RequestException e) {
        //displayError("Couldn't retrieve JSON "+e.getMessage());
    }
}