views:

79

answers:

2

I'm creating a facelets template for all my company's internal applications. Its appearance is based on the skin which the user selects (like gmail themes).

It makes sense to store the user's preferred skin in a cookie.

My "user-preferences" WAR can see this cookie. However, my other applications are unable to find the cookie. They are on the same domain/subdomain as the user-preferences WAR.

Is there some reason for this?

Here is my bean which is used to create/find the preferred skin. This same file is used in all the projects:

// BackingBeanBase is just a class with convenience methods. Doesn't 
// really affect anything here.
public class UserSkinBean extends BackingBeanBase {

    private final static String SKIN_COOKIE_NAME = "preferredSkin";

    private final static String DEFAULT_SKIN_NAME = "classic";


    /**
     * Get the name of the user's preferred skin. If this value wasn't set previously, 
     * it will return a default value.
     * 
     * @return
     */
    public String getSkinName() {

        Cookie skinNameCookie = findSkinCookie();

        if (skinNameCookie == null) {
            skinNameCookie = initializeSkinNameCookie(DEFAULT_SKIN_NAME);
            addCookie(skinNameCookie);
        }

        return skinNameCookie.getValue();

    }


    /**
     * Set the skin to the given name. Must be the name of a valid richFaces skin.
     *     
     * @param skinName
     */
    public void setSkinName(String skinName) {

        if (skinName == null) {
            skinName = DEFAULT_SKIN_NAME;
        }

        Cookie skinNameCookie = findSkinCookie();

        if (skinNameCookie == null) {
            skinNameCookie = initializeSkinNameCookie(skinName);
        }
        else {
            skinNameCookie.setValue(skinName);    
        }

        addCookie(skinNameCookie);
    }

    private void addCookie(Cookie skinNameCookie) {
        ((HttpServletResponse)getFacesContext().getExternalContext().getResponse()).addCookie(skinNameCookie);
    }

    private Cookie initializeSkinNameCookie(String skinName) {

        Cookie ret = new Cookie(SKIN_COOKIE_NAME, skinName);
        ret.setComment("The purpose of this cookie is to hold the name of the user's preferred richFaces skin.");

        //set the max age to one year.
        ret.setMaxAge(60 * 60 * 24 * 365);
        ret.setPath("/");
        return ret;
    }


    private Cookie findSkinCookie() {
        Cookie[] cookies = ((HttpServletRequest)getFacesContext().getExternalContext().getRequest()).getCookies();

        Cookie ret = null;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(SKIN_COOKIE_NAME)) {
                ret = cookie;
                break;
            }
        }

        return ret;
    }
}

Can anyone see what I'm doing wrong?

Update: I've narrowed it down a bit...it works fine in FF, but IE still doesn't like it (of course).

Thanks, Zack

A: 

I think you need to assign domain/subdomain to the cookies.

Like, (Note that the domain should start with a dot)

ret.setDomain(".test.com");
ret.setDomain(".test.co.uk");

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Cookies.html

adatapost
That didn't do it. I'm on the same domain. I also tried setting the path to "/", and that didn't do it.
Zack
A: 

I found a solution.

I just used javascript on the client-side to create the cookies.

This worked fine.

Zack