package com.microservices.wiki.refactoring.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class CookieUtil {

    /**
     * 返回一个可用的 cookie
     * @param request
     * @return
     */
    public static String getToken(String name, HttpServletRequest request) {
        String token = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for(Cookie ck : cookies){
                if (ck.getName().equals(name)) {
                    token =  ck.getValue();
                }
            }
        }
        if(token == null){
            token =  request.getHeader("Authorization");
        }
        if (token == null) {
            token = request.getParameter("token");
        }
        return token;
    }

}
