aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/minitutorials/v70/CookieMonsterUI.java
blob: 342f0ada14e64b05fc3108a82126164974705da8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.v7.ui.TextField;

/**
 * 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;
    }
}