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

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

Introduction

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

Prototype

public native void setRequestHeader(String name, String value);

Source Link

Document

Adds the header to the set of HTTP headers to be sent.

Usage

From source file:com.google.gwt.gears.sample.uploaddemo.client.UploadDemo.java

License:Apache License

/**
 * This is the entry point method./*from   ww w  . j  a  va 2 s. c  o  m*/
 */
public void onModuleLoad() {
    root = RootPanel.get();

    final Button browse = new Button("Browse");

    final Button upload = new Button("Upload");
    upload.setEnabled(false);

    final TextBox selected = new TextBox();
    selected.setEnabled(false);

    final SimplePanel progressInner = new SimplePanel();
    progressInner.addStyleName("progressInner");

    final SimplePanel progressGauge = new SimplePanel();
    progressGauge.addStyleName("progressGauge");
    progressGauge.add(progressInner);

    final HTML result = new HTML();

    browse.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            Desktop desktop = factory.createDesktop();
            desktop.openFiles(new OpenFilesHandler() {

                public void onOpenFiles(OpenFilesEvent event) {
                    File[] files = event.getFiles();
                    selected.setText(files[0].getName());
                    selectedFile = files[0].getBlob();
                    upload.setEnabled(true);
                }
            }, true);
        }
    });

    upload.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            HttpRequest request = factory.createHttpRequest();
            request.open("POST", GWT.getModuleBaseURL() + "upload");
            request.setRequestHeader("X-Filename", selected.getText());

            request.setCallback(new RequestCallback() {
                public void onResponseReceived(HttpRequest request) {
                    String msg = request.getStatus() + " " + request.getResponseText();
                    if (request.getStatus() != 200) {
                        result.setHTML("<p style=\"color:red\">" + msg + "</p>");
                    } else {
                        result.setHTML("<p style=\"color:green\">" + msg + "</p>");
                    }
                }
            });

            request.getUpload().setProgressHandler(new ProgressHandler() {
                public void onProgress(ProgressEvent event) {
                    double pcnt = ((double) event.getLoaded() / event.getTotal());
                    progressInner.setWidth((int) Math.floor(pcnt * 100) + "%");
                }
            });
            request.send(selectedFile);
        }
    });

    HorizontalPanel inputPanel = new HorizontalPanel();
    inputPanel.add(selected);
    inputPanel.add(browse);
    inputPanel.add(upload);

    root.add(inputPanel);
    root.add(new HTML("Progress:"));
    root.add(progressGauge);
    root.add(result);
}

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  ww  w  .java 2  s. c  o m
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());
}

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

License:Open Source License

/**
 * Perform the HTTP request to upload the specified file.
 */// w w  w.  ja  v a  2 s.c  o m
@Override
protected void doSend(final List<File> filesRemaining) {
    final GSS app = GSS.get();
    HttpRequest request = factory.createHttpRequest();
    requests.add(request);
    String method = "POST";

    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) {
            // XXX: No error checking, since IE throws an Internal Error
            // when accessing req.getStatus().
            filesRemaining.remove(0);
            if (filesRemaining.isEmpty()) {
                finish();
            }
            doSend(filesRemaining);
        }
    });
    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());
}