Fixes issue 1715 and 660. Deprecated the HTTPRequest class and updated the JSON and SimpleXML examples to use the RequestBuilder class.
Review by: scottb, ecc
Patch by: mmendez
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1517 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/samples/json/src/com/google/gwt/sample/json/JSON.gwt.xml b/samples/json/src/com/google/gwt/sample/json/JSON.gwt.xml
index ec350e8..a0b8262 100644
--- a/samples/json/src/com/google/gwt/sample/json/JSON.gwt.xml
+++ b/samples/json/src/com/google/gwt/sample/json/JSON.gwt.xml
@@ -14,6 +14,7 @@
<module>
<inherits name='com.google.gwt.user.User'/>
+ <inherits name='com.google.gwt.http.HTTP'/>
<inherits name='com.google.gwt.json.JSON'/>
<entry-point class='com.google.gwt.sample.json.client.JSON'/>
</module>
diff --git a/samples/json/src/com/google/gwt/sample/json/client/JSON.java b/samples/json/src/com/google/gwt/sample/json/client/JSON.java
index fb688f5..6a77746 100644
--- a/samples/json/src/com/google/gwt/sample/json/client/JSON.java
+++ b/samples/json/src/com/google/gwt/sample/json/client/JSON.java
@@ -15,14 +15,17 @@
*/
package com.google.gwt.sample.json.client;
+import com.google.gwt.http.client.Request;
+import com.google.gwt.http.client.RequestBuilder;
+import com.google.gwt.http.client.RequestCallback;
+import com.google.gwt.http.client.RequestException;
+import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
-import com.google.gwt.user.client.HTTPRequest;
-import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
@@ -49,15 +52,21 @@
* object.
*
*/
- private class JSONResponseTextHandler implements ResponseTextHandler {
- public void onCompletion(String responseText) {
+ private class JSONResponseTextHandler implements RequestCallback {
+ public void onError(Request request, Throwable exception) {
+ displayRequestError(exception.toString());
+ resetSearchButtonCaption();
+ }
+
+ public void onResponseReceived(Request request, Response response) {
+ String responseText = response.getText();
try {
JSONValue jsonValue = JSONParser.parse(responseText);
displayJSONObject(jsonValue);
} catch (JSONException e) {
- displayError(responseText);
+ displayParseError(responseText);
}
- searchButton.setText(SEARCH_BUTTON_DEFAULT_TEXT);
+ resetSearchButtonCaption();
}
}
@@ -90,6 +99,12 @@
*/
private static final String SEARCH_BUTTON_WAITING_TEXT = "Waiting for JSON Response...";
+ /*
+ * RequestBuilder used to issue HTTP GET requests.
+ */
+ private final RequestBuilder requestBuilder = new RequestBuilder(
+ RequestBuilder.GET, DEFAULT_SEARCH_URL);
+
private Tree jsonTree = new Tree();
private Button searchButton = new Button();
@@ -132,11 +147,11 @@
}
}
- private void displayError(String responseText) {
+ private void displayError(String errorType, String errorMessage) {
jsonTree.removeItems();
jsonTree.setVisible(true);
- TreeItem treeItem = jsonTree.addItem("Failed to parse JSON response");
- treeItem.addItem(responseText);
+ TreeItem treeItem = jsonTree.addItem(errorType);
+ treeItem.addItem(errorMessage);
treeItem.setStyleName("JSON-JSONResponseObject");
treeItem.setState(true);
}
@@ -153,15 +168,28 @@
treeItem.setState(true);
}
+ private void displayParseError(String responseText) {
+ displayError("Failed to parse JSON response", responseText);
+ }
+
+ private void displayRequestError(String message) {
+ displayError("Request failed.", message);
+ }
+
+ private void displaySendError(String message) {
+ displayError("Failed to send the request.", message);
+ }
+
/*
* Fetch the requested URL.
*/
private void doFetchURL() {
searchButton.setText(SEARCH_BUTTON_WAITING_TEXT);
- if (!HTTPRequest.asyncGet(DEFAULT_SEARCH_URL, new JSONResponseTextHandler())) {
- // Reset the caption.
- //
- searchButton.setText(SEARCH_BUTTON_DEFAULT_TEXT);
+ try {
+ requestBuilder.sendRequest(null, new JSONResponseTextHandler());
+ } catch (RequestException ex) {
+ displaySendError(ex.toString());
+ resetSearchButtonCaption();
}
}
@@ -204,4 +232,8 @@
searchButtonSlot.add(searchButton);
treeViewSlot.add(jsonTree);
}
+
+ private void resetSearchButtonCaption() {
+ searchButton.setText(SEARCH_BUTTON_DEFAULT_TEXT);
+ }
}
diff --git a/samples/simplexml/src/com/google/gwt/sample/simplexml/SimpleXML.gwt.xml b/samples/simplexml/src/com/google/gwt/sample/simplexml/SimpleXML.gwt.xml
index 27540fc..2f0ddfc 100644
--- a/samples/simplexml/src/com/google/gwt/sample/simplexml/SimpleXML.gwt.xml
+++ b/samples/simplexml/src/com/google/gwt/sample/simplexml/SimpleXML.gwt.xml
@@ -14,6 +14,7 @@
<module>
<inherits name="com.google.gwt.user.User"/>
+ <inherits name ="com.google.gwt.http.HTTP"/>
<inherits name ="com.google.gwt.xml.XML"/>
<entry-point class="com.google.gwt.sample.simplexml.client.SimpleXML"/>
</module>
diff --git a/samples/simplexml/src/com/google/gwt/sample/simplexml/client/SimpleXML.java b/samples/simplexml/src/com/google/gwt/sample/simplexml/client/SimpleXML.java
index d4664bb..e1b4a25 100644
--- a/samples/simplexml/src/com/google/gwt/sample/simplexml/client/SimpleXML.java
+++ b/samples/simplexml/src/com/google/gwt/sample/simplexml/client/SimpleXML.java
@@ -16,8 +16,12 @@
package com.google.gwt.sample.simplexml.client;
import com.google.gwt.core.client.EntryPoint;
-import com.google.gwt.user.client.HTTPRequest;
-import com.google.gwt.user.client.ResponseTextHandler;
+import com.google.gwt.http.client.Request;
+import com.google.gwt.http.client.RequestBuilder;
+import com.google.gwt.http.client.RequestCallback;
+import com.google.gwt.http.client.RequestException;
+import com.google.gwt.http.client.Response;
+import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
@@ -42,171 +46,180 @@
private static final String NOTES_STYLE = "notes";
public void onModuleLoad() {
- HTTPRequest.asyncGet("customerRecord.xml", new ResponseTextHandler() {
- public void onCompletion(String responseText) {
- // In the real world, this text would come as a RPC response. This
- // technique is great for testing and samples though!
- renderXML(responseText);
- }
+ RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET,
+ "customerRecord.xml");
- private FlexTable createOrderTable(FlowPanel xmlParsed, String label) {
- HTML orderTableLabel = new HTML("<h2>" + label + "</h2>");
- xmlParsed.add(orderTableLabel);
- FlexTable orderTable = new FlexTable();
- orderTable.setStyleName(USER_TABLE_STYLE);
- orderTable.setBorderWidth(3);
- orderTable.getRowFormatter().setStyleName(0, USER_TABLE_LABEL_STYLE);
- orderTable.setText(0, 0, "Order ID");
- orderTable.setText(0, 1, "Item");
- orderTable.setText(0, 2, "Ordered On");
- orderTable.setText(0, 3, "Street");
- orderTable.setText(0, 4, "City");
- orderTable.setText(0, 5, "State");
- orderTable.setText(0, 6, "Zip");
- xmlParsed.add(orderTable);
- return orderTable;
- }
-
- /**
- * Creates the xml representation of xmlText. xmlText is assumed to have
- * been validated for structure on the server.
- *
- * @param xmlText xml text
- * @param xmlParsed panel to display customer record
- */
- private void customerPane(String xmlText, FlowPanel xmlParsed) {
- Document customerDom = XMLParser.parse(xmlText);
- Element customerElement = customerDom.getDocumentElement();
- // Must do this if you ever use a raw node list that you expect to be
- // all elements.
- XMLParser.removeWhitespace(customerElement);
-
- // Customer Name
- String nameValue = getElementTextValue(customerElement, "name");
- String title = "<h1>" + nameValue + "</h1>";
- HTML titleHTML = new HTML(title);
- xmlParsed.add(titleHTML);
-
- // Customer Notes
- String notesValue = getElementTextValue(customerElement, "notes");
- Label notesText = new Label();
- notesText.setStyleName(NOTES_STYLE);
- notesText.setText(notesValue);
- xmlParsed.add(notesText);
-
- // Pending orders UI setup
- FlexTable pendingTable = createOrderTable(xmlParsed, "Pending Orders");
- FlexTable completedTable = createOrderTable(xmlParsed, "Completed");
- completedTable.setText(0, 7, "Shipped by");
-
- // Fill Orders Table
- NodeList orders = customerElement.getElementsByTagName("order");
- int pendingRowPos = 0;
- int completedRowPos = 0;
- for (int i = 0; i < orders.getLength(); i++) {
- Element order = (Element) orders.item(i);
- HTMLTable table;
- int rowPos;
- if (order.getAttribute("status").equals("pending")) {
- table = pendingTable;
- rowPos = ++pendingRowPos;
- } else {
- table = completedTable;
- rowPos = ++completedRowPos;
- }
- int columnPos = 0;
- fillInOrderTableRow(customerElement, order, table, rowPos, columnPos);
- }
- }
-
- private void fillInOrderTableRow(Element customerElement, Element order,
- HTMLTable table, int rowPos, int columnPos) {
- // Order ID
- String orderId = order.getAttribute("id");
- table.setText(rowPos, columnPos++, orderId);
-
- // Item
- Element item = (Element) order.getElementsByTagName("item").item(0);
- String itemUPC = item.getAttribute("upc");
- String itemName = item.getFirstChild().getNodeValue();
- Label itemLabel = new Label(itemUPC);
- itemLabel.setTitle(itemName);
- table.setWidget(rowPos, columnPos++, itemLabel);
-
- // Ordered On
- String orderedOnValue = getElementTextValue(customerElement,
- "orderedOn");
- table.setText(rowPos, columnPos++, orderedOnValue);
-
- // Address
- Element address = (Element) order.getElementsByTagName("address").item(
- 0);
- XMLParser.removeWhitespace(address);
- NodeList lst = address.getChildNodes();
- for (int j = 0; j < lst.getLength(); j++) {
- Element next = (Element) lst.item(j);
- String addressPartText = next.getFirstChild().getNodeValue();
- table.setText(rowPos, columnPos++, addressPartText);
+ try {
+ requestBuilder.sendRequest(null, new RequestCallback() {
+ public void onError(Request request, Throwable exception) {
+ requestFailed(exception);
}
- // Shipped By (optional attribute)
- NodeList shippedByList = order.getElementsByTagName("shippingInfo");
- if (shippedByList.getLength() == 1) {
- Element shippedBy = (Element) shippedByList.item(0);
- // Depending upon the shipper, different attributes might be
- // available, so XML carries the display info
- FlexTable shippedByTable = new FlexTable();
- shippedByTable.getRowFormatter().setStyleName(0,
- USER_TABLE_LABEL_STYLE);
- shippedByTable.setBorderWidth(1);
- NodeList shippedByParts = shippedBy.getChildNodes();
- for (int j = 0; j < shippedByParts.getLength(); j++) {
- Node next = shippedByParts.item(j);
- Element elem = (Element) next;
- shippedByTable.setText(0, j, elem.getAttribute("title"));
- shippedByTable.setText(1, j, elem.getFirstChild().getNodeValue());
- }
- table.setWidget(rowPos, columnPos++, shippedByTable);
+ public void onResponseReceived(Request request, Response response) {
+ renderXML(response.getText());
}
- }
+ });
+ } catch (RequestException ex) {
+ requestFailed(ex);
+ }
+ }
- /**
- * Utility method to return the values of elements of the form <myTag>tag
- * value</myTag>
- */
- private String getElementTextValue(Element parent, String elementTag) {
- // If the xml is not coming from a known good source, this method would
- // have to include safety checks.
- return parent.getElementsByTagName(elementTag).item(0).getFirstChild()
- .getNodeValue();
- }
+ private FlexTable createOrderTable(FlowPanel xmlParsed, String label) {
+ HTML orderTableLabel = new HTML("<h2>" + label + "</h2>");
+ xmlParsed.add(orderTableLabel);
+ FlexTable orderTable = new FlexTable();
+ orderTable.setStyleName(USER_TABLE_STYLE);
+ orderTable.setBorderWidth(3);
+ orderTable.getRowFormatter().setStyleName(0, USER_TABLE_LABEL_STYLE);
+ orderTable.setText(0, 0, "Order ID");
+ orderTable.setText(0, 1, "Item");
+ orderTable.setText(0, 2, "Ordered On");
+ orderTable.setText(0, 3, "Street");
+ orderTable.setText(0, 4, "City");
+ orderTable.setText(0, 5, "State");
+ orderTable.setText(0, 6, "Zip");
+ xmlParsed.add(orderTable);
+ return orderTable;
+ }
- private void renderXML(String xmlText) {
-
- final TabPanel tab = new TabPanel();
- final FlowPanel xmlSource = new FlowPanel();
- final FlowPanel xmlParsed = new FlowPanel();
- tab.add(xmlParsed, "Customer Pane");
- tab.add(xmlSource, "XML Source");
- tab.selectTab(0);
- RootPanel.get().add(tab);
- xmlPane(xmlText, xmlSource);
- customerPane(xmlText, xmlParsed);
- }
+ /**
+ * Creates the xml representation of xmlText. xmlText is assumed to have been
+ * validated for structure on the server.
+ *
+ * @param xmlText xml text
+ * @param xmlParsed panel to display customer record
+ */
+ private void customerPane(String xmlText, FlowPanel xmlParsed) {
+ Document customerDom = XMLParser.parse(xmlText);
+ Element customerElement = customerDom.getDocumentElement();
+ // Must do this if you ever use a raw node list that you expect to be
+ // all elements.
+ XMLParser.removeWhitespace(customerElement);
- /**
- * Show the raw XML.
- *
- * @param xmlText
- * @param xmlSource
- */
- private void xmlPane(String xmlText, final FlowPanel xmlSource) {
- xmlText = xmlText.replaceAll("<", "<");
- xmlText = xmlText.replaceAll(">", ">");
- Label xml = new HTML("<pre>" + xmlText + "</pre>", false);
- xml.setStyleName(XML_LABEL_STYLE);
- xmlSource.add(xml);
+ // Customer Name
+ String nameValue = getElementTextValue(customerElement, "name");
+ String title = "<h1>" + nameValue + "</h1>";
+ HTML titleHTML = new HTML(title);
+ xmlParsed.add(titleHTML);
+
+ // Customer Notes
+ String notesValue = getElementTextValue(customerElement, "notes");
+ Label notesText = new Label();
+ notesText.setStyleName(NOTES_STYLE);
+ notesText.setText(notesValue);
+ xmlParsed.add(notesText);
+
+ // Pending orders UI setup
+ FlexTable pendingTable = createOrderTable(xmlParsed, "Pending Orders");
+ FlexTable completedTable = createOrderTable(xmlParsed, "Completed");
+ completedTable.setText(0, 7, "Shipped by");
+
+ // Fill Orders Table
+ NodeList orders = customerElement.getElementsByTagName("order");
+ int pendingRowPos = 0;
+ int completedRowPos = 0;
+ for (int i = 0; i < orders.getLength(); i++) {
+ Element order = (Element) orders.item(i);
+ HTMLTable table;
+ int rowPos;
+ if (order.getAttribute("status").equals("pending")) {
+ table = pendingTable;
+ rowPos = ++pendingRowPos;
+ } else {
+ table = completedTable;
+ rowPos = ++completedRowPos;
}
- });
+ int columnPos = 0;
+ fillInOrderTableRow(customerElement, order, table, rowPos, columnPos);
+ }
+ }
+
+ private void fillInOrderTableRow(Element customerElement, Element order,
+ HTMLTable table, int rowPos, int columnPos) {
+ // Order ID
+ String orderId = order.getAttribute("id");
+ table.setText(rowPos, columnPos++, orderId);
+
+ // Item
+ Element item = (Element) order.getElementsByTagName("item").item(0);
+ String itemUPC = item.getAttribute("upc");
+ String itemName = item.getFirstChild().getNodeValue();
+ Label itemLabel = new Label(itemUPC);
+ itemLabel.setTitle(itemName);
+ table.setWidget(rowPos, columnPos++, itemLabel);
+
+ // Ordered On
+ String orderedOnValue = getElementTextValue(customerElement, "orderedOn");
+ table.setText(rowPos, columnPos++, orderedOnValue);
+
+ // Address
+ Element address = (Element) order.getElementsByTagName("address").item(0);
+ XMLParser.removeWhitespace(address);
+ NodeList lst = address.getChildNodes();
+ for (int j = 0; j < lst.getLength(); j++) {
+ Element next = (Element) lst.item(j);
+ String addressPartText = next.getFirstChild().getNodeValue();
+ table.setText(rowPos, columnPos++, addressPartText);
+ }
+
+ // Shipped By (optional attribute)
+ NodeList shippedByList = order.getElementsByTagName("shippingInfo");
+ if (shippedByList.getLength() == 1) {
+ Element shippedBy = (Element) shippedByList.item(0);
+ // Depending upon the shipper, different attributes might be
+ // available, so XML carries the display info
+ FlexTable shippedByTable = new FlexTable();
+ shippedByTable.getRowFormatter().setStyleName(0, USER_TABLE_LABEL_STYLE);
+ shippedByTable.setBorderWidth(1);
+ NodeList shippedByParts = shippedBy.getChildNodes();
+ for (int j = 0; j < shippedByParts.getLength(); j++) {
+ Node next = shippedByParts.item(j);
+ Element elem = (Element) next;
+ shippedByTable.setText(0, j, elem.getAttribute("title"));
+ shippedByTable.setText(1, j, elem.getFirstChild().getNodeValue());
+ }
+ table.setWidget(rowPos, columnPos++, shippedByTable);
+ }
+ }
+
+ /**
+ * Utility method to return the values of elements of the form <myTag>tag
+ * value</myTag>
+ */
+ private String getElementTextValue(Element parent, String elementTag) {
+ // If the xml is not coming from a known good source, this method would
+ // have to include safety checks.
+ return parent.getElementsByTagName(elementTag).item(0).getFirstChild().getNodeValue();
+ }
+
+ private void renderXML(String xmlText) {
+ final TabPanel tab = new TabPanel();
+ final FlowPanel xmlSource = new FlowPanel();
+ final FlowPanel xmlParsed = new FlowPanel();
+ tab.add(xmlParsed, "Customer Pane");
+ tab.add(xmlSource, "XML Source");
+ tab.selectTab(0);
+ RootPanel.get().add(tab);
+ xmlPane(xmlText, xmlSource);
+ customerPane(xmlText, xmlParsed);
+ }
+
+ private void requestFailed(Throwable exception) {
+ Window.alert("Failed to send the request. The error message was: "
+ + exception.getMessage());
+ }
+
+ /**
+ * Show the raw XML.
+ *
+ * @param xmlText
+ * @param xmlSource
+ */
+ private void xmlPane(String xmlText, final FlowPanel xmlSource) {
+ xmlText = xmlText.replaceAll("<", "<");
+ xmlText = xmlText.replaceAll(">", ">");
+ Label xml = new HTML("<pre>" + xmlText + "</pre>", false);
+ xml.setStyleName(XML_LABEL_STYLE);
+ xmlSource.add(xml);
}
}
diff --git a/user/src/com/google/gwt/user/client/HTTPRequest.java b/user/src/com/google/gwt/user/client/HTTPRequest.java
index 893390c..0ff0e44 100644
--- a/user/src/com/google/gwt/user/client/HTTPRequest.java
+++ b/user/src/com/google/gwt/user/client/HTTPRequest.java
@@ -21,7 +21,11 @@
/**
* This class allows you to make asynchronous HTTP requests to the originating
* server.
+ *
+ * @deprecated As of GWT 1.5, replaced by
+ * {@link com.google.gwt.http.client.RequestBuilder RequestBuilder}.
*/
+@Deprecated
public class HTTPRequest {
private static final HTTPRequestImpl httpRequest = GWT.create(HTTPRequestImpl.class);