vaadin - 使用@Push读取和写入cookie

标签 vaadin vaadin7

在我的 vaadin 应用程序中,我需要使用 @Push,但自从我添加它后,我无法读取和写入 cookie,因为 VaadinService.getSurrentResponse() 返回null 因为 Push。我使用此类管理 cookie:

import javax.servlet.http.Cookie;

import com.vaadin.server.VaadinResponse;
import com.vaadin.server.VaadinService;

public class CookieManager {
    private VaadinResponse response;

    public CookieManager(VaadinResponse response){
        this.response = response;
    }

    public Cookie getCookieByName(final String name) {
        // Fetch all cookies from the request
        Cookie[] cookies = VaadinService.getCurrentRequest().getCookies();

        // Iterate to find cookie by its name
        for (Cookie cookie : cookies) {
            if (name.equals(cookie.getName())) {
                return cookie;
            }
        }
        return null;
    }

    public Cookie createCookie(final String name, final String value, final int maxAge) {
        // Create a new cookie
        final Cookie cookie = new Cookie(name, value);

        cookie.setMaxAge(maxAge);

        // Set the cookie path.
        cookie.setPath(VaadinService.getCurrentRequest().getContextPath());

        // Save cookie
        addCookie(cookie);          

        return cookie;
    }

    private void addCookie(Cookie cookie){
        response.addCookie(cookie);
    }

    public Cookie updateCookieValue(final String name, final String value) {
        // Create a new cookie
        Cookie cookie = getCookieByName(name);

        cookie.setValue(value);

        // Save cookie
        addCookie(cookie);

        return cookie;
    }

    public void destroyCookieByName(final String name) {
        Cookie cookie = getCookieByName(name);

        if (cookie != null) {
            cookie.setValue(null);
            // By setting the cookie maxAge to 0 it will deleted immediately
            cookie.setMaxAge(0);
            cookie.setPath(VaadinService.getCurrentRequest().getContextPath());
            addCookie(cookie);
        }
    }
}

当我想创建 cookie 时(例如在用户登录时),由于 VaadinResponse 为 null,我收到 nullPointerException。

所以我尝试在构造函数中禁用 Push 并在 addCookie() 方法末尾重新启用它,但它禁用了我所有应用程序的推送,即使我只是重新启用它在 addCookie 方法之后。

我在 vaadin 的 trac ( http://dev.vaadin.com/ticket/11808 ) 上看到一张票,说这个问题不会被修复,有人建议从服务器创建一个常规的 AJAX 查询来创建 cookie,但我真的不知道该怎么做。

我如何管理我的cookie?我需要创建并获取cookie,所以javascript无法帮助我,因为我无法在vaadin中获取javascript的返回,所以我无法获取cookie。

最佳答案

这是我的解决方案,如何在使用 @Push 时存储 cookie。 首先我们创建容器来存储客户端 UI 的所有实例。 ( 这个容器本身就有很大的潜力)

public class UISession {

private List<WebAppUI> uis = new ArrayList<WebAppUI>();

public void addUI(WebAppUI webAppUI) {
    uis.add(webAppUI);
}

public List<WebAppUI> getUIs() {
    return uis;
}

public static UISession getInstance() {
    try {
        UI.getCurrent().getSession().lock();
        return (UISession) UI.getCurrent().getSession().getAttribute("userUiSession");
    } finally {
        UI.getCurrent().getSession().unlock();
    }
}

在 UI.init() 中,我们向 session 添加新实例(例如,当用户打开新选项卡时)

@Override
protected void init(VaadinRequest vaadinRequest) {
    /** Set singleton uisesison for each browser*/
    if(UISession.getInstance()==null){
        UI.getCurrent().getSession().setAttribute("userUiSession",new UISession());
    }
    UISession.getInstance().addUI(this);
    System.out.println("UI count fo current browser "+UISession.getInstance().getUIs().size());
    ... 
}

这是我的辅助 cookie 类:

class MyCookie{
private String value;
private String name;
private Date expired;
private String path="/";

public MyCookie(String name, String value) {
    this.name=name;
    this.value=value;
}
public void setMaxAge(int minute) {
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MINUTE, minute);
    expired=c.getTime();

}
public String getStringToCreateCookie(){
    return "document.cookie=\""+getName()+"="+getValue()+"; expires="+expired.toString()+"; path="+path+"\"";
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getValue() {
    return value;
}
public void setValue(String value) {
    this.value = value;
}
public Date getExpired() {
    return expired;
}
public void setExpired(Date expired) {
    this.expired = expired;
}
public String getPath() {
    return path;
}
public void setPath(String path) {
    this.path = path;
}

}

最后,当我们需要添加新的 cookie 时,我们只需要找到处于事件状态的 Ui 并调用 js 函数

public static void addCookie(String name, String value, int age){
    MyCookie myCookie = new MyCookie(name, value);
    myCookie.setMaxAge(age);
    for(WebAppUI ui : UISession.getInstance().getUIs()){
        if(ui.isAttached()){
            ui.getPage().getJavaScript().execute(myCookie.getStringToCreateCookie());
            return;
        }
    }
}

就我而言,我可以访问存储 cookie(当用户发出请求时)。我只是在添加新 cookie 时遇到问题,所以这是我的工作解决方案。

关于vaadin - 使用@Push读取和写入cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28086179/

相关文章:

vaadin - 如何在 DateField 上定义禁用日期?

Vaadin:自动刷新网格(类似ajax)

authentication - Vaadin 7 - 在 VaadinSession 中设置和获取属性

spring-boot - Vaadin 使用 SpringSecurity 身份验证

java - HttpServletRequest getLocale 返回操作系统语言环境而不是浏览器语言环境

java - Vaadin 和 WebSecurity 中的 permitAll 问题 - 不工作

java - 限制 Vaadin 表中选定行的数量

css - vaadin 组件 setstylename 无效

java - 如何重置 Spring 托管 Bean

java - 为什么我的 View 没有加载