diff --git a/.gitignore b/.gitignore index c794569..2005788 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /test-classes/ /gwt-unitCache/ /war/fotostream/ +/war/img/ /war/WEB-INF/deploy/ .gwt-log \ No newline at end of file diff --git a/src/net/molez/mandlm/fotostream/client/FotoStream.java b/src/net/molez/mandlm/fotostream/client/FotoStream.java index 536e659..ca9dc34 100644 --- a/src/net/molez/mandlm/fotostream/client/FotoStream.java +++ b/src/net/molez/mandlm/fotostream/client/FotoStream.java @@ -1,51 +1,20 @@ package net.molez.mandlm.fotostream.client; -import net.molez.mandlm.fotostream.shared.FieldVerifier; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; -import com.google.gwt.event.dom.client.ClickEvent; -import com.google.gwt.event.dom.client.ClickHandler; -import com.google.gwt.event.dom.client.KeyCodes; -import com.google.gwt.event.dom.client.KeyUpEvent; -import com.google.gwt.event.dom.client.KeyUpHandler; import com.google.gwt.event.dom.client.LoadEvent; import com.google.gwt.event.dom.client.LoadHandler; +import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.rpc.AsyncCallback; -import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.DialogBox; -import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.Image; -import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; -import com.google.gwt.user.client.ui.TextBox; -import com.google.gwt.user.client.ui.VerticalPanel; -/** - * Entry point classes define onModuleLoad(). - */ -public class FotoStream implements EntryPoint { - /** - * The message displayed to the user when the server cannot be reached or - * returns an error. - */ - private static final String SERVER_ERROR = "An error occurred while " - + "attempting to contact the server. Please check your network " + "connection and try again."; - - /** - * Create a remote service proxy to talk to the server-side Greeting service. - */ - private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class); +public class FotoStream implements EntryPoint +{ private final CurrentImageURLServiceAsync imageURLService = GWT.create(CurrentImageURLService.class); - /** - * This is the entry point method. - */ public void onModuleLoad() { - final Button sendButton = new Button("Send"); - final TextBox nameField = new TextBox(); - nameField.setText("GWT User"); - final Label errorLabel = new Label(); - final Image image = new Image(); image.setVisible(false); image.addLoadHandler(new LoadHandler() @@ -58,125 +27,37 @@ public class FotoStream implements EntryPoint { } }); - imageURLService.getCurrentImageURL(new AsyncCallback() + Timer imageReloadTimer = new Timer() { @Override - public void onFailure(Throwable caught) + public void run() { - DialogBox errorMsg = new DialogBox(); - - errorMsg.setTitle("Error loading current immage"); - errorMsg.setHTML(caught.getMessage()); - errorMsg.show(); - } - - @Override - public void onSuccess(String result) - { - image.setUrl(result); - } - }); - - // We can add style names to widgets - sendButton.addStyleName("sendButton"); - - // Add the nameField and sendButton to the RootPanel - // Use RootPanel.get() to get the entire body element - RootPanel.get("nameFieldContainer").add(nameField); - RootPanel.get("sendButtonContainer").add(sendButton); - RootPanel.get("errorLabelContainer").add(errorLabel); - RootPanel.get("imageContainer").add(image); - - // Focus the cursor on the name field when the app loads - nameField.setFocus(true); - nameField.selectAll(); - - // Create the popup dialog box - final DialogBox dialogBox = new DialogBox(); - dialogBox.setText("Remote Procedure Call"); - dialogBox.setAnimationEnabled(true); - final Button closeButton = new Button("Close"); - // We can set the id of a widget by accessing its Element - closeButton.getElement().setId("closeButton"); - final Label textToServerLabel = new Label(); - final HTML serverResponseLabel = new HTML(); - VerticalPanel dialogVPanel = new VerticalPanel(); - dialogVPanel.addStyleName("dialogVPanel"); - dialogVPanel.add(new HTML("Sending name to the server:")); - dialogVPanel.add(textToServerLabel); - dialogVPanel.add(new HTML("
Server replies:")); - dialogVPanel.add(serverResponseLabel); - dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT); - dialogVPanel.add(closeButton); - dialogBox.setWidget(dialogVPanel); - - // Add a handler to close the DialogBox - closeButton.addClickHandler(new ClickHandler() { - public void onClick(ClickEvent event) { - dialogBox.hide(); - sendButton.setEnabled(true); - sendButton.setFocus(true); - } - }); - - // Create a handler for the sendButton and nameField - class MyHandler implements ClickHandler, KeyUpHandler { - /** - * Fired when the user clicks on the sendButton. - */ - public void onClick(ClickEvent event) { - sendNameToServer(); - } - - /** - * Fired when the user types in the nameField. - */ - public void onKeyUp(KeyUpEvent event) { - if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { - sendNameToServer(); - } - } - - /** - * Send the name from the nameField to the server and wait for a response. - */ - private void sendNameToServer() { - // First, we validate the input. - errorLabel.setText(""); - String textToServer = nameField.getText(); - if (!FieldVerifier.isValidName(textToServer)) { - errorLabel.setText("Please enter at least four characters"); - return; - } - - // Then, we send the input to the server. - sendButton.setEnabled(false); - textToServerLabel.setText(textToServer); - serverResponseLabel.setText(""); - greetingService.greetServer(textToServer, new AsyncCallback() { - public void onFailure(Throwable caught) { - // Show the RPC error message to the user - dialogBox.setText("Remote Procedure Call - Failure"); - serverResponseLabel.addStyleName("serverResponseLabelError"); - serverResponseLabel.setHTML(SERVER_ERROR); - dialogBox.center(); - closeButton.setFocus(true); + imageURLService.getCurrentImageURL(new AsyncCallback() + { + @Override + public void onFailure(Throwable caught) + { + DialogBox errorMsg = new DialogBox(); + + errorMsg.setTitle("Error loading image"); + errorMsg.setHTML(caught.getMessage()); + errorMsg.show(); } - public void onSuccess(String result) { - dialogBox.setText("Remote Procedure Call"); - serverResponseLabel.removeStyleName("serverResponseLabelError"); - serverResponseLabel.setHTML(result); - dialogBox.center(); - closeButton.setFocus(true); - } + @Override + public void onSuccess(String result) + { + if (result != image.getUrl()) + { + image.setUrl(result); + } + } }); - } - } - - // Add a handler to send the name to the server - MyHandler handler = new MyHandler(); - sendButton.addClickHandler(handler); - nameField.addKeyUpHandler(handler); + } + }; + + imageReloadTimer.scheduleRepeating(2000); + + RootPanel.get("imageContainer").add(image); } } diff --git a/src/net/molez/mandlm/fotostream/client/GreetingService.java b/src/net/molez/mandlm/fotostream/client/GreetingService.java deleted file mode 100644 index efce9e8..0000000 --- a/src/net/molez/mandlm/fotostream/client/GreetingService.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.molez.mandlm.fotostream.client; - -import com.google.gwt.user.client.rpc.RemoteService; -import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; - -/** - * The client-side stub for the RPC service. - */ -@RemoteServiceRelativePath("greet") -public interface GreetingService extends RemoteService { - String greetServer(String name) throws IllegalArgumentException; -} diff --git a/src/net/molez/mandlm/fotostream/client/GreetingServiceAsync.java b/src/net/molez/mandlm/fotostream/client/GreetingServiceAsync.java deleted file mode 100644 index 357a190..0000000 --- a/src/net/molez/mandlm/fotostream/client/GreetingServiceAsync.java +++ /dev/null @@ -1,10 +0,0 @@ -package net.molez.mandlm.fotostream.client; - -import com.google.gwt.user.client.rpc.AsyncCallback; - -/** - * The async counterpart of GreetingService. - */ -public interface GreetingServiceAsync { - void greetServer(String input, AsyncCallback callback) throws IllegalArgumentException; -} diff --git a/src/net/molez/mandlm/fotostream/server/CurrentImageURLServiceImpl.java b/src/net/molez/mandlm/fotostream/server/CurrentImageURLServiceImpl.java index 857e816..37207e8 100644 --- a/src/net/molez/mandlm/fotostream/server/CurrentImageURLServiceImpl.java +++ b/src/net/molez/mandlm/fotostream/server/CurrentImageURLServiceImpl.java @@ -1,7 +1,6 @@ package net.molez.mandlm.fotostream.server; -import java.util.ArrayList; -import java.util.List; +import java.io.File; import java.util.Random; import com.google.gwt.user.server.rpc.RemoteServiceServlet; @@ -13,11 +12,10 @@ public class CurrentImageURLServiceImpl extends RemoteServiceServlet implements { public String getCurrentImageURL() { - List fileList = new ArrayList(); + File imageFolder = new File("img/"); - fileList.add("img/dummy_1.jpg"); - fileList.add("img/dummy_2.jpg"); + File[] fileList = imageFolder.listFiles(); - return fileList.get(new Random().nextInt(fileList.size())); + return fileList[new Random().nextInt(fileList.length)].getPath(); } } diff --git a/src/net/molez/mandlm/fotostream/server/GreetingServiceImpl.java b/src/net/molez/mandlm/fotostream/server/GreetingServiceImpl.java deleted file mode 100644 index a06279d..0000000 --- a/src/net/molez/mandlm/fotostream/server/GreetingServiceImpl.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.molez.mandlm.fotostream.server; - -import net.molez.mandlm.fotostream.client.GreetingService; -import net.molez.mandlm.fotostream.shared.FieldVerifier; -import com.google.gwt.user.server.rpc.RemoteServiceServlet; - -/** - * The server-side implementation of the RPC service. - */ -@SuppressWarnings("serial") -public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService { - - public String greetServer(String input) throws IllegalArgumentException { - // Verify that the input is valid. - if (!FieldVerifier.isValidName(input)) { - // If the input is not valid, throw an IllegalArgumentException back to - // the client. - throw new IllegalArgumentException("Name must be at least 4 characters long"); - } - - String serverInfo = getServletContext().getServerInfo(); - String userAgent = getThreadLocalRequest().getHeader("User-Agent"); - - // Escape data from the client to avoid cross-site script vulnerabilities. - input = escapeHtml(input); - userAgent = escapeHtml(userAgent); - - return "Hello, " + input + "!

I am running " + serverInfo + ".

It looks like you are using:
" - + userAgent; - } - - /** - * Escape an html string. Escaping data received from the client helps to - * prevent cross-site script vulnerabilities. - * - * @param html the html string to escape - * @return the escaped string - */ - private String escapeHtml(String html) { - if (html == null) { - return null; - } - return html.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">"); - } -} diff --git a/src/net/molez/mandlm/fotostream/shared/FieldVerifier.java b/src/net/molez/mandlm/fotostream/shared/FieldVerifier.java deleted file mode 100644 index 2a84d17..0000000 --- a/src/net/molez/mandlm/fotostream/shared/FieldVerifier.java +++ /dev/null @@ -1,42 +0,0 @@ -package net.molez.mandlm.fotostream.shared; - -/** - *

- * FieldVerifier validates that the name the user enters is valid. - *

- *

- * This class is in the shared package because we use it in both - * the client code and on the server. On the client, we verify that the name is - * valid before sending an RPC request so the user doesn't have to wait for a - * network round trip to get feedback. On the server, we verify that the name is - * correct to ensure that the input is correct regardless of where the RPC - * originates. - *

- *

- * When creating a class that is used on both the client and the server, be sure - * that all code is translatable and does not use native JavaScript. Code that - * is not translatable (such as code that interacts with a database or the file - * system) cannot be compiled into client-side JavaScript. Code that uses native - * JavaScript (such as Widgets) cannot be run on the server. - *

- */ -public class FieldVerifier { - - /** - * Verifies that the specified name is valid for our service. - * - * In this example, we only require that the name is at least four - * characters. In your application, you can use more complex checks to ensure - * that usernames, passwords, email addresses, URLs, and other fields have the - * proper syntax. - * - * @param name the name to validate - * @return true if valid, false if invalid - */ - public static boolean isValidName(String name) { - if (name == null) { - return false; - } - return name.length() > 3; - } -} diff --git a/war/FotoStream.css b/war/FotoStream.css index 7aca7ac..c128d57 100644 --- a/war/FotoStream.css +++ b/war/FotoStream.css @@ -1,34 +1,8 @@ /** Add css rules here for your application. */ -/** Example rules used by the template application (remove for your app) */ -h1 { - font-size: 2em; - font-weight: bold; - color: #777777; - margin: 40px 0px 70px; - text-align: center; +body { + background-color: black; } -.sendButton { - display: block; - font-size: 16pt; -} -/** Most GWT widgets already have a style name defined */ -.gwt-DialogBox { - width: 400px; -} - -.dialogVPanel { - margin: 5px; -} - -.serverResponseLabelError { - color: red; -} - -/** Set ids using widget.getElement().setId("idOfElement") */ -#closeButton { - margin: 15px 6px 6px; -} diff --git a/war/FotoStream.html b/war/FotoStream.html index 2b610af..0b9d2cd 100644 --- a/war/FotoStream.html +++ b/war/FotoStream.html @@ -16,7 +16,7 @@ - Web Application Starter Project + FotoStream @@ -32,10 +32,6 @@ - - - - -

Web Application Starter Project

- - - - - - - - - - - - - - - -
Please enter your name:
+
diff --git a/war/WEB-INF/web.xml b/war/WEB-INF/web.xml index 2cd57fe..003a6da 100644 --- a/war/WEB-INF/web.xml +++ b/war/WEB-INF/web.xml @@ -6,15 +6,6 @@ xmlns="http://java.sun.com/xml/ns/javaee"> - - greetServlet - net.molez.mandlm.fotostream.server.GreetingServiceImpl - - - - greetServlet - /fotostream/greet - currentImageURLServlet diff --git a/war/img/dummy_1.jpg b/war/img/dummy_1.jpg deleted file mode 100644 index 1963596..0000000 Binary files a/war/img/dummy_1.jpg and /dev/null differ diff --git a/war/img/dummy_2.jpg b/war/img/dummy_2.jpg deleted file mode 100644 index 7eb25f7..0000000 Binary files a/war/img/dummy_2.jpg and /dev/null differ