Example usage for javax.servlet.http HttpServletRequest getCookies

List of usage examples for javax.servlet.http HttpServletRequest getCookies

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getCookies.

Prototype

public Cookie[] getCookies();

Source Link

Document

Returns an array containing all of the Cookie objects the client sent with this request.

Usage

From source file:com.iterzp.momo.utils.WebUtils.java

/**
 * ?cookie//from w w  w .  jav a2  s . co m
 * 
 * @param request
 *            HttpServletRequest
 * @param name
 *            cookie??
 * @return ?null
 */
public static String getCookie(HttpServletRequest request, String name) {
    Assert.notNull(request);
    Assert.hasText(name);
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        try {
            name = URLEncoder.encode(name, "UTF-8");
            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    return URLDecoder.decode(cookie.getValue(), "UTF-8");
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.activecq.api.utils.CookieUtil.java

/**
 * Get the named cookie from the HTTP Request
 *
 * @param request Request to get the Cookie from
 * @param cookieName name of Cookie to get
 * @return the named Cookie, null if the named Cookie cannot be found
 */// w  w w .j a  v a 2 s  .c  o m
public static Cookie getCookie(HttpServletRequest request, String cookieName) {
    cookieName = StringUtils.trimToNull(cookieName);
    if (cookieName == null) {
        return null;
    }

    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return null;
    }

    if (cookies.length > 0) {
        for (Cookie cookie : cookies) {
            if (StringUtils.equals(cookieName, cookie.getName())) {
                return cookie;
            }
        }
    }

    return null;
}

From source file:com.kingcore.framework.util.CookieUtils.java

/**
 * Returns true if a cookie with the specified name is
 * present in the request.//from w ww .  j  a  v  a  2 s.  c om
 * ???name cookie cooki??
 * @param name the name of the cookie will be checked.
 * @param req response Object.
 * @return
 */
public static boolean isCookieSet(String name, HttpServletRequest req) {
    try {
        //           check input information.
        if (name == null) {
            return false;
        }

        Cookie[] cookies = req.getCookies();
        if (cookies == null) {// cookie
            //out.print("none any cookie");
            return false;
        }
        for (int i = 0; i < cookies.length; i++) {
            if (name.equals(cookies[i].getName())) {
                return true;
            }
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        log.debug("debug", e);
        /// e.pri ntStackTrace();
    }
    return false;
}

From source file:org.dd4t.core.util.HttpUtils.java

public static String getSessionPreviewToken(HttpServletRequest request) {
    if (request == null) {
        return null;
    }/*from  w w w.j  a  v  a2s  . c  o m*/

    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (TridionUtils.PREVIEW_SESSION_TOKEN.equals(cookie.getName())) {
                return cookie.getValue();
            }
        }
    }

    return null;
}

From source file:org.apache.roller.weblogger.ui.rendering.mobile.MobileDeviceRepository.java

public static DeviceType getRequestType(HttpServletRequest request) {
    DeviceType type = DeviceType.standard;

    String deviceTypeParam = request.getParameter(USER_AGENT_PARAMETER);
    if (deviceTypeParam != null) {
        return deviceTypeParam.trim().equals("standard") ? DeviceType.standard : DeviceType.mobile;
    }//from w ww.  j  a  va2 s .c om

    String cookie = getCookieValue(request.getCookies(), USER_REQUEST_TYPE, null);
    if (cookie != null) {
        return cookie.equals("standard") ? DeviceType.standard : DeviceType.mobile;
    }

    if (isMobileDevice(request)) {
        type = DeviceType.mobile;
    }
    return type;
}

From source file:edu.stanford.epad.epadws.xnat.XNATSessionOperations.java

public static String getJSessionIDFromRequest(HttpServletRequest servletRequest) {
    String jSessionID = null;/*from  w ww.ja va2  s  .c  o m*/

    Cookie[] cookies = servletRequest.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if ("JSESSIONID".equalsIgnoreCase(cookie.getName())) {
                jSessionID = cookie.getValue();
                break;
            }
        }
    }
    if (jSessionID == null)
        log.warning("No JSESESSIONID cookie present in request " + servletRequest.getRequestURL());

    return jSessionID;
}

From source file:com.kingcore.framework.util.CookieUtils.java

/**
 * Returns the value of the Cookie with the specified name,
 * or null of not found.//from   w ww .  j a v  a2s.  c o m
 *   ?cookie??name cookie 
 * @param name cookie's name.
 * @param req response Object.
 * @param needEncode ??Cookie?true.
 * @return
 * @throws DecoderException 
 */
public static String getCookieValue(String name, HttpServletRequest req, boolean needDecode)
        throws DecoderException {
    //       check input information.
    if (name == null) {
        return null;
    }

    Cookie[] cookies = req.getCookies();
    if (cookies == null) {// cookie
        //out.print("none any cookie");
        return null;
    }
    String value = null;
    for (int i = 0; i < cookies.length; i++) {
        if (name.equals(cookies[i].getName())) {
            try {
                value = cookies[i].getValue();
                if (needDecode) {
                    value = Base64.decode(value, "utf-8");
                }

            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                log.error("debug", e);
                //e.pri ntStackTrace();

            } //new String( Hex.decode( cookies[i].getValue() ) );
            break;
        }
    }
    return value;
}

From source file:com.feilong.servlet.http.CookieUtil.java

/**
 * {@link Cookie} key value? map({@link TreeMap}).
 *
 * @param request//from   ww w  .  j  a  v a 2  s .com
 *            the request
 * @return  {@link HttpServletRequest#getCookies()}, {@link Collections#emptyMap()};<br>
 *         ?, loop cookies,? {@link Cookie#getName()}? {@link Cookie#getValue()} ?map 
 * @see HttpServletRequest#getCookies()
 * @see javax.servlet.http.Cookie#getName()
 * @see javax.servlet.http.Cookie#getValue()
 * @see javax.servlet.jsp.el.ImplicitObjectELResolver.ImplicitObjects#createCookieMap(javax.servlet.jsp.PageContext)
 */
public static Map<String, String> getCookieMap(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (isNullOrEmpty(cookies)) {
        return emptyMap();
    }

    Map<String, String> map = new TreeMap<>();
    for (Cookie cookie : cookies) {
        map.put(cookie.getName(), cookie.getValue());
    }
    return map;
}

From source file:de.arago.portlet.util.UserContainer.java

private static User getUserFromCookies(HttpServletRequest request) throws Exception {
    // https://www.everit.biz/web/guest/everit-blog/-/blogs/getting-current-liferay-user-in-a-standalone-webapp
    final Cookie[] cookies = request.getCookies() == null ? new Cookie[0] : request.getCookies();
    String userId = null;// w ww.j a  v  a 2 s .  c  o  m
    String password = null;
    String companyId = null;

    for (Cookie c : cookies) {
        if ("COMPANY_ID".equals(c.getName())) {
            companyId = c.getValue();
        } else if ("ID".equals(c.getName())) {
            userId = hexStringToStringByAscii(c.getValue());
        } else if ("PASSWORD".equals(c.getName())) {
            password = hexStringToStringByAscii(c.getValue());
        }
    }

    if (userId != null && password != null && companyId != null) {
        final KeyValuePair kvp = UserLocalServiceUtil.decryptUserId(Long.parseLong(companyId), userId,
                password);
        return getUser(kvp.getKey());
    }

    return null;
}

From source file:com.pureinfo.tgirls.utils.servlet.CookieUtils.java

public static User getLoginUser(HttpServletRequest _request, HttpServletResponse _response) {
    User loginUser = (User) _request.getSession().getAttribute(ArkHelper.ATTR_LOGIN_USER);
    if (loginUser != null) {

        logger.debug("get user from session." + loginUser.getTaobaoID());

        return loginUser;
    }/*from  ww  w .  ja  va 2 s.  c om*/

    logger.debug("user not in session.");

    String taobaoId = getCookieValue(_request.getCookies(), TAOBAO_ID);
    if (StringUtils.isEmpty(taobaoId)) {
        return null;
    }

    try {
        IUserMgr mgr = (IUserMgr) ArkContentHelper.getContentMgrOf(User.class);
        loginUser = mgr.getUserByTaobaoId(taobaoId);
        _request.getSession().setAttribute(ArkHelper.ATTR_LOGIN_USER, loginUser);
        return loginUser;
    } catch (PureException e) {
        // TODO Auto-generated catch block
        e.printStackTrace(System.err);
    }

    return null;
}