001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 * $Id$
013 */
014
015package org.nuxeo.theme.jsf.negotiation;
016
017import java.util.Map;
018
019import javax.faces.context.ExternalContext;
020import javax.servlet.http.Cookie;
021import javax.servlet.http.HttpServletResponse;
022
023public final class CookieManager {
024
025    public static String getCookie(final String name, final ExternalContext context) {
026        if (context == null) {
027            return null;
028        }
029        final Map<String, Object> cookies = context.getRequestCookieMap();
030        final Cookie cookie = (Cookie) cookies.get(name);
031        if (cookie == null) {
032            return null;
033        }
034        return cookie.getValue();
035    }
036
037    public static void setCookie(String name, String value, final ExternalContext context) {
038        if (context == null) {
039            return;
040        }
041        HttpServletResponse response = (HttpServletResponse) context.getResponse();
042        final Cookie cookie = new Cookie(name, value);
043        response.addCookie(cookie);
044    }
045
046    public static void expireCookie(String name, final ExternalContext context) {
047        if (context == null) {
048            return;
049        }
050        HttpServletResponse response = (HttpServletResponse) context.getResponse();
051        final Cookie cookie = new Cookie(name, "");
052        cookie.setMaxAge(0);
053        response.addCookie(cookie);
054    }
055
056}