001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    
019    import java.util.Collections;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import javax.servlet.http.Cookie;
024    import javax.servlet.http.HttpServletRequest;
025    
026    /**
027     * @author     Brian Wing Shun Chan
028     * @author     Shuyang Zhou
029     * @deprecated {@link com.liferay.portal.kernel.util.CookieKeys}
030     */
031    public class CookieUtil {
032    
033            public static String get(HttpServletRequest request, String name) {
034                    return get(request, name, true);
035            }
036    
037            public static String get(
038                    HttpServletRequest request, String name, boolean toUpperCase) {
039    
040                    Map<String, Cookie> cookieMap = _getCookieMap(request);
041    
042                    if (toUpperCase) {
043                            name = name.toUpperCase();
044                    }
045    
046                    Cookie cookie = cookieMap.get(name);
047    
048                    if (cookie == null) {
049                            return null;
050                    }
051                    else {
052                            return cookie.getValue();
053                    }
054            }
055    
056            private static Map<String, Cookie> _getCookieMap(
057                    HttpServletRequest request) {
058    
059                    Map<String, Cookie> cookieMap =
060                            (Map<String, Cookie>)request.getAttribute(
061                                    CookieUtil.class.getName());
062    
063                    if (cookieMap != null) {
064                            return cookieMap;
065                    }
066    
067                    Cookie[] cookies = request.getCookies();
068    
069                    if (cookies == null) {
070                            cookieMap = Collections.emptyMap();
071                    }
072                    else {
073                            cookieMap = new HashMap<String, Cookie>(cookies.length * 4 / 3);
074    
075                            for (Cookie cookie : cookies) {
076                                    String cookieName = GetterUtil.getString(cookie.getName());
077    
078                                    cookieName = cookieName.toUpperCase();
079    
080                                    cookieMap.put(cookieName, cookie);
081                            }
082                    }
083    
084                    request.setAttribute(CookieUtil.class.getName(), cookieMap);
085    
086                    return cookieMap;
087            }
088    
089    }