diff options
author | John Ahlroos <john@vaadin.com> | 2013-01-23 14:01:46 +0200 |
---|---|---|
committer | John Ahlroos <john@vaadin.com> | 2013-01-23 14:01:46 +0200 |
commit | 640c7244d64f015ccd18b7564e7190746ae8f999 (patch) | |
tree | 2005aef661d715b0b63474c4beaa30edbe24fa75 | |
parent | c62a76f02d0587aa9bf6c1a95d2c308dbea0093b (diff) | |
download | vaadin-framework-640c7244d64f015ccd18b7564e7190746ae8f999.tar.gz vaadin-framework-640c7244d64f015ccd18b7564e7190746ae8f999.zip |
Added cookie read/write minitutorial #9644
Change-Id: I80d7646eca9be9b8fd98ad6c933245dab724df8a
-rw-r--r-- | uitest/src/com/vaadin/tests/minitutorials/v70/CookieMonsterUI.java | 92 |
1 files changed, 92 insertions, 0 deletions
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 index 0000000000..361567b088 --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/v70/CookieMonsterUI.java @@ -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 |