]> source.dussan.org Git - vaadin-framework.git/commitdiff
Added cookie read/write minitutorial #9644
authorJohn Ahlroos <john@vaadin.com>
Wed, 23 Jan 2013 12:01:46 +0000 (14:01 +0200)
committerJohn Ahlroos <john@vaadin.com>
Wed, 23 Jan 2013 12:01:46 +0000 (14:01 +0200)
Change-Id: I80d7646eca9be9b8fd98ad6c933245dab724df8a

uitest/src/com/vaadin/tests/minitutorials/v70/CookieMonsterUI.java [new file with mode: 0644]

diff --git a/uitest/src/com/vaadin/tests/minitutorials/v70/CookieMonsterUI.java b/uitest/src/com/vaadin/tests/minitutorials/v70/CookieMonsterUI.java
new file mode 100644 (file)
index 0000000..361567b
--- /dev/null
@@ -0,0 +1,92 @@
+package com.vaadin.tests.minitutorials.v70;
+
+import javax.servlet.http.Cookie;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.server.VaadinService;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Notification;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.UI;
+import com.vaadin.ui.VerticalLayout;
+
+/**
+ * Tutorial example for
+ * https://vaadin.com/wiki/-/wiki/Main/Setting%20and%20reading%20Cookies
+ */
+public class CookieMonsterUI extends UI {
+
+    private static final String NAME_COOKIE = "name";
+
+    @Override
+    protected void init(VaadinRequest request) {
+        final VerticalLayout layout = new VerticalLayout();
+        layout.setMargin(true);
+        setContent(layout);
+
+        final TextField nameField = new TextField();
+        layout.addComponent(nameField);
+
+        // Read previously stored cookie value
+        Cookie nameCookie = getCookieByName(NAME_COOKIE);
+        if (getCookieByName(NAME_COOKIE) != null) {
+            nameField.setValue(nameCookie.getValue());
+        }
+
+        Button button = new Button("Store name in cookie");
+        button.addClickListener(new Button.ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+
+                String name = nameField.getValue();
+
+                // See if name cookie is already set
+                Cookie nameCookie = getCookieByName(NAME_COOKIE);
+
+                if (nameCookie != null) {
+                    String oldName = nameCookie.getValue();
+
+                    nameCookie.setValue(name);
+
+                    Notification.show("Updated name in cookie from " + oldName
+                            + " to " + name);
+
+                } else {
+                    // Create a new cookie
+                    nameCookie = new Cookie(NAME_COOKIE, name);
+                    nameCookie
+                            .setComment("Cookie for storing the name of the user");
+
+                    Notification.show("Stored name " + name + " in cookie");
+                }
+
+                // Make cookie expire in 2 minutes
+                nameCookie.setMaxAge(120);
+
+                // Set the cookie path.
+                nameCookie.setPath(VaadinService.getCurrentRequest()
+                        .getContextPath());
+
+                // Save cookie
+                VaadinService.getCurrentResponse().addCookie(nameCookie);
+            }
+        });
+        layout.addComponent(button);
+
+    }
+
+    private Cookie getCookieByName(String name) {
+        // Fetch all cookies from the request
+        Cookie[] cookies = VaadinService.getCurrentRequest().getCookies();
+
+        // Iterate to find a cookie by its name
+        for (Cookie cookie : cookies) {
+            if (name.equals(cookie.getName())) {
+                return cookie;
+            }
+        }
+
+        return null;
+    }
+}
\ No newline at end of file