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

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

Introduction

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

Prototype

protected RequestBuilder(String httpMethod, String url) 

Source Link

Document

Creates a builder using the parameters values for configuration.

Usage

From source file:fi.jyu.student.jatahama.onlineinquirytool.client.OnlineInquiryTool.java

License:Open Source License

/**
 * Check servlet existence//from  ww  w.j a va  2s .c  om
 */
private void checkServlet() throws RequestException {
    RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, loadsaveServletUrl);
    rb.setTimeoutMillis(10000);
    rb.setRequestData("Hello!");
    rb.setCallback(this);
    rb.send();
}

From source file:fr.aliasource.webmail.client.reader.invitation.GoingEventDataRequest.java

License:GNU General Public License

public void requestGoing(String extId, final String going) {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode(url));
    builder.setHeader("Content-Type", "application/x-www-form-urlencoded");

    String requestData = "token=" + URL.encodeComponent(token);
    requestData += "&extId=" + URL.encodeComponent(extId);
    requestData += "&going=" + URL.encodeComponent(going);
    try {/*from  ww w.jav  a  2 s.  c  o  m*/
        builder.sendRequest(requestData, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                GWT.log("srv error", exception);
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    // Process the response in response.getText()
                    String resp = response.getText();
                    GWT.log("text:\n" + resp, null);
                    ctrl.goingReceived(going);
                } else {
                    GWT.log("error: " + response.getStatusCode() + " " + response.getStatusText(), null);
                }
            }
        });
    } catch (RequestException e) {
        // Couldn't connect to server
    }
}

From source file:fr.aliasource.webmail.client.reader.invitation.InvitationInfoDataProvider.java

License:GNU General Public License

public void requestInvitation(MessageId messageId, String folder) {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode(url));
    builder.setHeader("Content-Type", "application/x-www-form-urlencoded");

    String requestData = "token=" + URL.encodeComponent(token);
    requestData += "&messageId=" + URL.encodeComponent(String.valueOf(messageId.getMessageId()));
    requestData += "&folder=" + URL.encodeComponent(folder);
    try {/*from  w w w .ja va 2  s .  c om*/
        builder.sendRequest(requestData, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                GWT.log("srv error", exception);
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    // Process the response in response.getText()
                    String resp = response.getText();
                    Document doc = XMLParser.parse(resp);

                    InvitationInfo invitationInfo = parseXml(doc);
                    ctrl.invitationReceived(invitationInfo);
                } else {
                    GWT.log("error: " + response.getStatusCode() + " " + response.getStatusText(), null);
                }
            }

        });
    } catch (RequestException e) {
        GWT.log("Couldn't connect to server", e);
    }
}

From source file:fr.fg.client.ajax.Action.java

License:Open Source License

private void doRequest() {
    // Encode les paramtres de la requte
    StringBuffer requestData = new StringBuffer();
    boolean first = true;
    for (String key : params.keySet()) {
        if (first)
            first = false;/*from  w  w  w.  j  a  va2s. c  om*/
        else
            requestData.append("&");

        requestData.append(key);
        requestData.append("=");
        requestData.append(URL.encodeComponent(params.get(key)));
    }

    // Excute la requte
    RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.POST,
            Config.getServerUrl() + uri + ".do");
    requestBuilder.setHeader("Content-type", "application/x-www-form-urlencoded");

    try {
        this.startTime = System.currentTimeMillis();
        request = requestBuilder.sendRequest(requestData.toString(), this);
    } catch (RequestException e) {
        retry(String.valueOf(e.getMessage()));
    }
}

From source file:fr.gael.dhus.gwt.client.module.LoginModule.java

License:Open Source License

private static void init() {
    final SecurityServiceAsync securityService = SecurityServiceAsync.Util.getInstance();

    usernameInput = TextBox.wrap(RootPanel.get("login_username").getElement());
    passwordInput = PasswordTextBox.wrap(RootPanel.get("login_password").getElement());

    final RootPanel login_button = RootPanel.get("login_button");
    final RootPanel logout_button = RootPanel.get("logout_button");

    login_button.addDomHandler(new ClickHandler() {
        @Override/*from  www. ja  v a2 s.c o m*/
        public void onClick(ClickEvent event) {
            try {
                String url = GWT.getHostPageBaseURL() + "/login";
                StringBuilder data = new StringBuilder();
                data.append("login_username=").append(URL.encodeQueryString(usernameInput.getValue()));
                data.append("&");
                data.append("login_password=").append(URL.encodeQueryString(passwordInput.getValue()));
                RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, URL.encode(url));
                rb.setHeader("Content-Type", "application/x-www-form-urlencoded");
                rb.sendRequest(data.toString(), new RequestCallback() {
                    @Override
                    public void onResponseReceived(Request request, Response response) {
                        if (response.getStatusCode() != 200) {
                            loginError(response.getText());
                        } else {
                            securityService.getCurrentUser(new AccessDeniedRedirectionCallback<UserData>() {

                                @Override
                                public void onSuccess(UserData result) {
                                    if (result == null) {
                                        loginError(
                                                "There was an error with your login/password combination. Please try again.");
                                        return;
                                    }
                                    loginRefresh();
                                }

                                @Override
                                public void _onFailure(Throwable caught) {
                                    Window.alert(caught.getMessage());
                                    loginRefresh();
                                }
                            });
                        }
                    }

                    @Override
                    public void onError(Request request, Throwable exception) {
                        Window.alert(exception.getMessage());
                        loginRefresh();
                    }
                });
            } catch (Exception e) {
                Window.alert(e.getMessage());
                loginRefresh();
            }
        }
    }, ClickEvent.getType());

    usernameInput.addKeyDownHandler(new KeyDownHandler() {
        @Override
        public void onKeyDown(KeyDownEvent event) {
            if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                passwordInput.setFocus(true);
            }
        }
    });
    passwordInput.addKeyDownHandler(new KeyDownHandler() {
        @Override
        public void onKeyDown(KeyDownEvent event) {
            if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                NativeEvent evt = Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false);
                DomEvent.fireNativeEvent(evt, login_button);
            }
        }
    });

    logout_button.addDomHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            try {
                String url = GWT.getHostPageBaseURL() + "/logout";
                RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, URL.encode(url));
                rb.setHeader("Content-Type", "application/x-www-form-urlencoded");
                rb.sendRequest(null, new RequestCallback() {

                    @Override
                    public void onResponseReceived(Request request, Response response) {
                        loginRefresh();
                    }

                    @Override
                    public void onError(Request request, Throwable exception) {
                        Window.alert("Error while loging out user.\n" + exception.getMessage());
                        loginRefresh();
                    }
                });
            } catch (RequestException e) {
                Window.alert("Error while loging out user:\n" + e.getMessage());
            }
        }
    }, ClickEvent.getType());

    loginRefresh();
}

From source file:fr.gael.dhus.gwt.client.page.SearchViewPage.java

License:Open Source License

private static void getHTML(final XMLNodeData parent, boolean loadMoreNodeClicked,
        final AccessDeniedRedirectionCallback<List<XMLNodeData>> callback) {
    String request = loadMoreNodeClicked ? parent.getLoadMoreRequest() : parent.getRequest();
    String urlToRead = displayedProduct.getOdataPath(GWT.getHostPageBaseURL()) + "/" + request;
    urlToRead = URL.encode(urlToRead);
    urlToRead = urlToRead.replaceAll("#", "%23");
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, urlToRead);

    try {/*  w  ww  . j  a  v  a 2  s. co  m*/
        builder.sendRequest(null, new RequestCallback() {

            @Override
            public void onResponseReceived(Request request, Response response) {
                Document doc = XMLParser.parse(response.getText());

                ArrayList<XMLNodeData> xmlNodes = new ArrayList<XMLNodeData>();

                NodeList list = doc.getFirstChild().getChildNodes();

                for (int listId = 0; listId < list.getLength(); listId++) {
                    if (list.item(listId).getNodeName() == "entry") {
                        NodeList entryNodes = list.item(listId).getChildNodes();
                        Integer childrenNumber = 0;
                        String name = null;
                        String value = "";
                        String path = parent.getPath();
                        for (int entryNodeId = 0; entryNodeId < entryNodes.getLength(); entryNodeId++) {
                            Node node = entryNodes.item(entryNodeId);
                            NodeList children = node.getChildNodes();
                            if (node.getNodeName() == "title") {
                                for (int childId = 0; childId < children.getLength(); childId++) {
                                    Node child = children.item(childId);
                                    if (child.getNodeName() == "#text") {
                                        name = child.getNodeValue();
                                    }
                                }
                            }
                            if (node.getNodeName() == "m:properties") {
                                NodeList properties = node.getChildNodes();
                                for (int propertyId = 0; propertyId < properties.getLength(); propertyId++) {
                                    Node property = properties.item(propertyId);
                                    NodeList ns = property.getChildNodes();
                                    if (property.getNodeName() == "d:ChildrenNumber") {
                                        for (int nId = 0; nId < ns.getLength(); nId++) {
                                            Node n = ns.item(nId);
                                            if (n.getNodeName() == "#text") {
                                                childrenNumber = new Integer(n.getNodeValue());
                                            }
                                        }
                                    }
                                    if (property.getNodeName() == "d:Value") {
                                        for (int nId = 0; nId < ns.getLength(); nId++) {
                                            Node n = ns.item(nId);
                                            if (n.getNodeName() == "#text") {
                                                value = n.getNodeValue() == null ? "" : n.getNodeValue();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        XMLNodeData xmlNode = new XMLNodeData(name, value,
                                (path.isEmpty() ? "" : path + "/") + "Nodes('" + name + "')", childrenNumber);

                        if (pathCounter.containsKey(xmlNode.getPath())) {
                            Object item = pathCounter.get(xmlNode.getPath());
                            int id;
                            if (item instanceof XMLNodeData) {
                                XMLNodeData n = (XMLNodeData) item;
                                n.setPath((path.isEmpty() ? "" : path + "/") + "Nodes('" + name + "[" + 1
                                        + "]')");
                                id = 2;
                            } else {
                                id = (Integer) pathCounter.get(xmlNode.getPath()) + 1;
                            }
                            pathCounter.put(xmlNode.getPath(), id);
                            xmlNode.setPath(
                                    (path.isEmpty() ? "" : path + "/") + "Nodes('" + name + "[" + id + "]')");
                        } else {
                            pathCounter.put(xmlNode.getPath(), xmlNode);
                        }
                        xmlNode.setDeep(parent.getDeep() + 1);
                        xmlNodes.add(xmlNode);
                    }
                }

                callback.onSuccess(xmlNodes);
            }

            @Override
            public void onError(Request request, Throwable exception) {
                Window.alert(exception.getMessage());
            }
        });
    } catch (RequestException e) {
        Window.alert(e.getMessage());
        return;
    }
}

From source file:fr.mncc.gwttoolbox.ajax.client.Json.java

License:Open Source License

private static <T extends JavaScriptObject> void sendRequest(RequestBuilder.Method httpMethod, String url,
        String data, final AsyncCallback<T> callback) {
    try {//  w  w  w .  ja v  a  2 s .c o m
        RequestBuilder requestBuilder = new RequestBuilder(httpMethod, url);
        requestBuilder.setHeader("Accept", "application/json");
        requestBuilder.setHeader("Content-Type", "application/json");
        requestBuilder.sendRequest(data, new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
                if (response.getStatusCode() != Response.SC_OK) {
                    if (callback != null)
                        callback.onFailure(new Exception("status code = " + response.getStatusCode()
                                + ", status text = " + response.getStatusText()));
                } else {
                    if (callback != null)
                        callback.onSuccess(JsonParser.<T>fromJson(response.getText()));
                }
            }

            @Override
            public void onError(Request request, Throwable exception) {
                if (callback != null)
                    callback.onFailure(exception);
            }
        });
    } catch (RequestException e) {
        GWT.log(e.toString());
    }
}

From source file:fr.mncc.gwttoolbox.rpc.client.requests.RestCall.java

License:Open Source License

private static <T extends JavaScriptObject> void sendRequest(final RequestBuilder.Method method,
        final String url, final String data, final AsyncCallback<T> callback) {
    try {//from w ww .  j ava  2  s  .c o  m
        final RequestBuilder rb = new RequestBuilder(method, URL.encode(url));
        rb.setHeader("Accept", "application/json");
        rb.sendRequest(data, new RequestCallback() {
            @Override
            public void onError(final com.google.gwt.http.client.Request request, final Throwable exception) {
                if (callback != null)
                    callback.onFailure(exception);
            }

            @Override
            public void onResponseReceived(final com.google.gwt.http.client.Request request,
                    final Response response) {
                if (response.getStatusCode() != Response.SC_OK) {
                    if (callback != null)
                        callback.onFailure(
                                new Exception("response.getStatusCode() = " + response.getStatusCode()));
                } else {
                    final JsonParser<T> parser = new JsonParser<T>();
                    if (callback != null)
                        callback.onSuccess(parser.fromJson(response.getText()));
                }
            }
        });
    } catch (final Exception exception) {
        if (callback != null)
            callback.onFailure(exception);
    }
}

From source file:fr.mncc.minus.ajax.client.Ajax.java

License:Open Source License

private static <T extends JavaScriptObject> void sendRequest(RequestBuilder.Method httpMethod, String url,
        String data, final AsyncCallback<T> callback) {
    try {/*from  w  w w .  java  2s.co m*/
        RequestBuilder requestBuilder = new RequestBuilder(httpMethod, url);
        requestBuilder.setHeader("Accept", "application/json");
        requestBuilder.setHeader("Content-Type", "application/json");
        requestBuilder.sendRequest(data, new RequestCallback() {

            @Override
            public void onResponseReceived(Request request, Response response) {
                if (response.getStatusCode() != Response.SC_OK) {
                    if (callback != null) {
                        callback.onFailure(new Exception("status code = " + response.getStatusCode()
                                + ", status text = " + response.getStatusText()));
                    }
                } else {
                    if (callback != null) {
                        callback.onSuccess(JsonParser.<T>fromJson(response.getText()));
                    }
                }
            }

            @Override
            public void onError(Request request, Throwable exception) {
                if (callback != null) {
                    callback.onFailure(exception);
                }
            }
        });
    } catch (RequestException e) {
        GWT.log(e.toString());
    }
}

From source file:fr.putnami.pwt.doc.client.page.sample.decorator.SampleDecorator.java

License:Open Source License

private void requestFile(final String fileName) {
    this.sourceCode.asWidget().setVisible(false);
    this.sourceCode.setText("");

    RequestCallback callBack = new RequestCallback() {

        @Override/* w  ww  . j  a v a2s .  c  o  m*/
        public void onResponseReceived(Request request, Response response) {
            if (fileName.endsWith("xml")) {
                SampleDecorator.this.sourceCode.setConfiguration(XmlConfiguration.XML_CONFIGURATION);
            } else if (fileName.endsWith("java")) {
                SampleDecorator.this.sourceCode.setConfiguration(JavaConfiguration.JAVA_CONFIGURATION);
            } else {
                SampleDecorator.this.displayError(new RuntimeException("Unknow file type"));
            }
            SampleDecorator.this.sourceCode.setText(response.getText());
            SampleDecorator.this.sourceCode.asWidget().setVisible(true);
        }

        @Override
        public void onError(Request request, Throwable exception) {
            SampleDecorator.this.displayError(exception);
        }
    };
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
            GWT.getModuleBaseURL() + "sample/" + fileName);
    builder.setCallback(callBack);
    try {
        builder.send();
    } catch (RequestException e) {
        callBack.onError(null, e);
    }
}