Example usage for com.google.gwt.gears.client.httprequest HttpRequest getReadyState

List of usage examples for com.google.gwt.gears.client.httprequest HttpRequest getReadyState

Introduction

In this page you can find the example usage for com.google.gwt.gears.client.httprequest HttpRequest getReadyState.

Prototype

public native int getReadyState();

Source Link

Document

Returns the state of the request.

Usage

From source file:org.gss_project.gss.web.client.FileUploadGearsDialog.java

License:Open Source License

/**
 * Perform the HTTP request to upload the specified file.
 *//*from  w  w  w.ja va 2s.  c om*/
protected void doSend(final List<File> filesRemaining) {
    final GSS app = GSS.get();
    HttpRequest request = factory.createHttpRequest();
    requests.add(request);
    String method = "PUT";

    String path;
    final String filename = getFilename(filesRemaining.get(0).getName());
    path = folder.getUri();
    if (!path.endsWith("/"))
        path = path + "/";
    path = path + encode(filename);

    String token = app.getToken();
    String resource = path.substring(app.getApiPath().length() - 1, path.length());
    String date = RestCommand.getDate();
    String sig = RestCommand.calculateSig(method, date, resource, RestCommand.base64decode(token));
    request.open(method, path);
    request.setRequestHeader("X-GSS-Date", date);
    request.setRequestHeader("Authorization", app.getCurrentUserResource().getUsername() + " " + sig);
    request.setRequestHeader("Accept", "application/json; charset=utf-8");
    request.setCallback(new RequestCallback() {
        @Override
        public void onResponseReceived(HttpRequest req) {
            int state = req.getReadyState();
            if (state != 4)
                return;
            switch (req.getStatus()) {
            case 201: // Created falls through to updated.
            case 204:
                filesRemaining.remove(0);
                if (filesRemaining.isEmpty()) {
                    finish();
                    break;
                }
                doSend(filesRemaining);
                break;
            case 403:
                SessionExpiredDialog dlg = new SessionExpiredDialog();
                dlg.center();
                break;
            case 405:
                app.displayError("You don't have permission to " + "upload file " + filename);
                break;
            case 409:
                app.displayError("A folder with the name " + filename + " already exists at this level");
                break;
            case 413:
                app.displayError("There is not enough free space " + "available for uploading " + filename);
                break;
            default:
                app.displayError("Error uploading file " + filename + ": " + req.getStatus());
            }
        }
    });
    request.getUpload().setProgressHandler(new ProgressHandler() {
        @Override
        public void onProgress(ProgressEvent event) {
            double pcnt = (double) event.getLoaded() / event.getTotal();
            progressBars.get(0).setProgress((int) Math.floor(pcnt * 100));
            if (pcnt * 100 == 100)
                progressBars.remove(0);
        }
    });
    request.send(filesRemaining.get(0).getBlob());
}