package com.linkwerk.gwt.client; import com.google.gwt.core.client.EntryPoint; 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.JSONValue; import com.google.gwt.user.client.HTTPRequest; import com.google.gwt.user.client.ResponseTextHandler; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; import com.google.gwt.xml.client.DOMException; import com.google.gwt.xml.client.Document; import com.google.gwt.xml.client.Node; import com.google.gwt.xml.client.XMLParser; /** * Entry point classes define onModuleLoad(). */ public class Glueckskeks implements EntryPoint { final static String DEFAULT_MESSAGE = "keine Gluecks-Nachricht verfügbar"; final static String READY_MESSAGE = "Ready."; final static String ERROR_MESSAGE = "Error."; final static String SEARCH_MESSAGE = "Status: Searching..."; final static String GETTING_MESSAGE = "Getting "; final Label statusLabel = new Label("Status:"); private class MyResponseTextHandler implements ResponseTextHandler { Label label; public MyResponseTextHandler(Label label) { this.label = label; } public void onCompletion(String responseText) { statusLabel.setText(statusLabel.getText() + GETTING_MESSAGE + "Text Message..."); label.setText(responseText.replaceAll("\"", "")); statusLabel.setText(statusLabel.getText() + READY_MESSAGE); } } private class MyResponseJsonHandler implements ResponseTextHandler { Label label; public MyResponseJsonHandler(Label label) { this.label = label; } public void onCompletion(String responseText) { statusLabel.setText(statusLabel.getText() + GETTING_MESSAGE + "JSON Message..."); try { JSONObject jsonObj = (JSONObject)JSONParser.parse(responseText); JSONValue messageVal = jsonObj.get("message"); JSONValue authorVal = jsonObj.get("author"); if(messageVal != null && authorVal != null && messageVal.isString() != null && authorVal.isString() != null) { String message = messageVal.isString().stringValue(); String author = authorVal.isString().stringValue(); String messageComplete = message + " (" + author + ")"; label.setText(messageComplete.replaceAll("\"", "")); statusLabel.setText(statusLabel.getText() + READY_MESSAGE); } else { throw new JSONException(); } } catch(JSONException e) { label.setText(DEFAULT_MESSAGE); statusLabel.setText(statusLabel.getText() + ERROR_MESSAGE); } } } private class MyResponseXMLHandler implements ResponseTextHandler { Label label; public MyResponseXMLHandler(Label label) { this.label = label; } public void onCompletion(String responseText) { statusLabel.setText(statusLabel.getText() + GETTING_MESSAGE + "XML Message..."); try { Document doc = XMLParser.parse(responseText); Node node = doc.getElementsByTagName("message").item(0); String message = node.getChildNodes().item(0).getNodeValue(); label.setText(message); statusLabel.setText(statusLabel.getText() + READY_MESSAGE); } catch(DOMException de) { label.setText(DEFAULT_MESSAGE); statusLabel.setText(statusLabel.getText() + ERROR_MESSAGE); } } } /** * This is the entry point method. */ public void onModuleLoad() { final Button button = new Button("Neuer Glückskeks"); final Button buttonJson = new Button("Neuer Glückskeks (JSON)"); final Button buttonXML = new Button("Neuer Glückskeks (XML)"); final Label messageLabel = new Label(""); button.addClickListener(new ClickListener() { public void onClick(Widget sender) { HTTPRequest.asyncGet("/code/WS/getFortuneCookieWS/python/", new MyResponseTextHandler(messageLabel)); statusLabel.setText(SEARCH_MESSAGE); } }); buttonJson.addClickListener(new ClickListener() { public void onClick(Widget sender) { // asyncPost als Alternative HTTPRequest.asyncGet("/code/WS/getFortuneCookieWS/?mimeType=application/json", new MyResponseJsonHandler(messageLabel)); statusLabel.setText(SEARCH_MESSAGE); } }); buttonXML.addClickListener(new ClickListener() { public void onClick(Widget sender) { // asyncPost als Alternative HTTPRequest.asyncGet("/code/WS/getFortuneCookieWS/?mimeType=text/xml", new MyResponseXMLHandler(messageLabel)); statusLabel.setText(SEARCH_MESSAGE); } }); RootPanel.get("fortuneStatus").add(statusLabel); RootPanel.get("fortuneButton").add(button); RootPanel.get("fortuneButtonJson").add(buttonJson); RootPanel.get("fortuneButtonXML").add(buttonXML); RootPanel.get("fortuneMessage").add(messageLabel); } }