001    /**
002     * Copyright (c) 2000-2013 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 As of 6.2.0, replaced by {@link
030     *             com.liferay.portal.kernel.util.CookieKeys}
031     */
032    public class CookieUtil {
033    
034            public static String get(HttpServletRequest request, String name) {
035                    return get(request, name, true);
036            }
037    
038            public static String get(
039                    HttpServletRequest request, String name, boolean toUpperCase) {
040    
041                    Map<String, Cookie> cookieMap = _getCookieMap(request);
042    
043                    if (toUpperCase) {
044                            name = name.toUpperCase();
045                    }
046    
047                    Cookie cookie = cookieMap.get(name);
048    
049                    if (cookie == null) {
050                            return null;
051                    }
052                    else {
053                            return cookie.getValue();
054                    }
055            }
056    
057            private static Map<String, Cookie> _getCookieMap(
058                    HttpServletRequest request) {
059    
060                    Map<String, Cookie> cookieMap =
061                            (Map<String, Cookie>)request.getAttribute(
062                                    CookieUtil.class.getName());
063    
064                    if (cookieMap != null) {
065                            return cookieMap;
066                    }
067    
068                    Cookie[] cookies = request.getCookies();
069    
070                    if (cookies == null) {
071                            cookieMap = Collections.emptyMap();
072                    }
073                    else {
074                            cookieMap = new HashMap<String, Cookie>(cookies.length * 4 / 3);
075    
076                            for (Cookie cookie : cookies) {
077                                    String cookieName = GetterUtil.getString(cookie.getName());
078    
079                                    cookieName = cookieName.toUpperCase();
080    
081                                    cookieMap.put(cookieName, cookie);
082                            }
083                    }
084    
085                    request.setAttribute(CookieUtil.class.getName(), cookieMap);
086    
087                    return cookieMap;
088            }
089    
090    }