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