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.adobe.acs.commons.util.CookieUtil.java

/**
 * Gets Cookies from the Request whose names match the provides Regex
 *
 * @param request Request to get the Cookie from
 * @param regex   Regex to match against Cookie names
 * @return Cookies which match the Regex
 *//*w  ww  .j  a v a2  s  .com*/
public static List<Cookie> getCookies(final HttpServletRequest request, final String regex) {
    final ArrayList<Cookie> foundCookies = new ArrayList<Cookie>();
    if (StringUtils.isBlank(regex)) {
        return foundCookies;
    }

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

    final Pattern p = Pattern.compile(regex);
    for (final Cookie cookie : cookies) {
        final Matcher m = p.matcher(cookie.getName());
        if (m.matches()) {
            foundCookies.add(cookie);
        }
    }

    return foundCookies;
}

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

/**
 * Gets Cookies from the Request whose's names match the provides Regex
 *
 * @param request Request to get the Cookie from
 * @param regex Regex to match against Cookie names
 * @return Cookies which match the Regex
 *//*from   www. java  2  s.c om*/
public static List<Cookie> getCookies(HttpServletRequest request, String regex) {
    ArrayList<Cookie> foundCookies = new ArrayList<Cookie>();
    regex = StringUtils.trimToNull(regex);
    if (regex == null) {
        return foundCookies;
    }

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

    for (Cookie cookie : cookies) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(cookie.getName());
        if (m.matches()) {
            foundCookies.add(cookie);
        }
    }

    return foundCookies;
}

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

/**
 *  {@link Cookie}.//  w w  w.j  a  v a2 s.co m
 * 
 * <p>
 * ?? {@link HttpServletRequest#getCookies()}, {@link Cookie#getName()} <code>cookieName</code>  {@link Cookie}
 * </p>
 * 
 * @param request
 *            the request
 * @param cookieName
 *            the cookie name
 * @return  {@link HttpServletRequest#getCookies()} null,null;<br>
 *          <code>cookieName</code> ? {@link Cookie},null
 * @see javax.servlet.http.HttpServletRequest#getCookies()
 * @see javax.servlet.http.Cookie#getName()
 */
public static Cookie getCookie(HttpServletRequest request, String cookieName) {
    Cookie[] cookies = request.getCookies();

    if (isNullOrEmpty(cookies)) {
        LOGGER.info("request's cookies is null or empty!!");
        return null;
    }

    for (Cookie cookie : cookies) {
        if (cookie.getName().equals(cookieName)) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("[getCookie],cookieName:[{}],cookie info:[{}]", cookieName,
                        JsonUtil.format(cookie, 0, 0));
            }
            return cookie;
        }
    }
    LOGGER.info("can't find the cookie:[{}]", cookieName);
    return null;
}

From source file:com.xidu.framework.common.util.CookieUtils.java

/**
 * set the name/value entry to the cookie
 * /*from   w  w  w. java2 s.c om*/
 * @Date : 2011-3-23
 * @param req
 *            - HttpServletRequest's instance
 * @param res
 *            - HttpServletResponse's instance
 * @param domain
 *            - the domain of sites
 * @param name
 *            - cookie's entry name
 * @param value
 *            - cookie's entry value
 * @param expiry
 *            - cookie's expired time
 */
public static void setCookie(HttpServletRequest req, HttpServletResponse res, String domain, String name,
        String value, int expiry) {
    value = EnDecoderUtils.encode(value);
    if (StringUtils.isBlank(domain) || StringUtils.isBlank(name) || StringUtils.isBlank(value)) {
        return;
    }
    Cookie cookie = null;
    Cookie[] cookies = req.getCookies();
    boolean isNew = true;
    if (null != cookies) {
        for (int i = 0; i < cookies.length; i++) {
            cookie = cookies[i];
            if (name.equals(cookie.getName())) {
                isNew = false;
                setCookie(res, name, value, "/", domain, expiry);
            }
        }
    }
    if (isNew) {
        setCookie(res, name, value, "/", domain, expiry);
    }
}

From source file:edu.ucsb.nceas.metacat.util.RequestUtil.java

/**
 * Get a cookie from a request by the cookie name
 * //from w  w w  .ja v  a 2s  .  com
 * @param request
 *            the request from which to get the cookie
 * @param cookieName
 *            the name of the cookie to look for
 */
public static Cookie getCookie(HttpServletRequest request, String cookieName) {
    Cookie sessionCookies[] = request.getCookies();

    if (sessionCookies == null) {
        return null;
    }

    for (int i = 0; i < sessionCookies.length; i++) {
        if (sessionCookies[i].getName().equals(cookieName)) {
            return sessionCookies[i];
        }
    }

    return null;
}

From source file:com.lushapp.common.web.utils.WebUtils.java

/**
 * ?Cookie.//  w  w w .  ja v  a 2s  .c o  m
 * @param request
 * @param name
 * @return
 */
public static Cookie getCookie(HttpServletRequest request, String name) {
    Cookie[] cookies = request.getCookies();
    Cookie returnCookie = null;

    if (cookies == null) {
        return returnCookie;
    }

    for (Cookie thisCookie : cookies) {
        if (thisCookie.getName().equals(name)) {
            if (!thisCookie.getValue().equals("")) {
                returnCookie = thisCookie;

                break;
            }
        }
    }

    return returnCookie;
}

From source file:com.ofbizcn.securityext.login.LoginEvents.java

public static String getUsername(HttpServletRequest request) {
    String cookieUsername = null;
    Cookie[] cookies = request.getCookies();
    if (Debug.verboseOn())
        Debug.logVerbose("Cookies:" + cookies, module);
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(usernameCookieName)) {
                cookieUsername = cookie.getValue();
                break;
            }/*from w w w.j a v  a  2 s.  c  om*/
        }
    }
    return cookieUsername;
}

From source file:com.persistent.cloudninja.controller.AuthFilterUtils.java

/**
 * /*from   w w  w.j a va 2  s.co  m*/
 * @param HttpServletRequest
 *            object
 * @return cookie, if present, null otherwise.
 */
public static Cookie checkForPreExistentCookie(HttpServletRequest request, String cookieName) {
    // Check whether the browser already has a SSO cookie
    Cookie[] cookies = request.getCookies();
    Cookie currentCookie = null;

    if (cookies != null && cookies.length > 0) {
        for (Cookie c : cookies) {
            if (cookieName.equals(c.getName())) {
                currentCookie = c;
                break;
            }
        }
    }
    return currentCookie;
}

From source file:edu.stanford.epad.epadws.security.EPADSessionOperations.java

public static String getJSessionIDFromRequest(HttpServletRequest servletRequest) {
    String jSessionID = null;//  w  ww .j  a v  a 2s.  co  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 JSESSIONID cookie present in request " + servletRequest.getRequestURL());
    } else {
        int comma = jSessionID.indexOf(",");
        if (comma != -1) {
            log.warning("Multiple cookies:" + jSessionID);
            jSessionID = jSessionID.substring(0, comma);
        }
    }
    return jSessionID;
}

From source file:com.vmware.identity.samlservice.Shared.java

/**
 * Utility method to check if session cookie is present
 *
 * @param req//w w  w.  ja v a 2  s  .  c  om
 * @param sessionManager
 * @param tenant
 * @return
 */
public static boolean hasSessionCookie(HttpServletRequest req, SessionManager sessionManager, String tenant) {
    boolean retval = false;

    if (req != null) {
        String sessionId = getCookieValue(req.getCookies(), Shared.getTenantSessionCookieName(tenant), null);
        // the reason for session manager check is it possible for someone to
        // log out the user from a different process
        // (browser gets a cookie, and test tool sends a logout, or bad guy guesses session id and logs user out)
        // In that scenario, browser still has the cookie which will affect login page logic unless
        // we check server side session as well.
        if (sessionId != null && sessionManager != null) {
            Session session = sessionManager.get(sessionId);
            if (session != null && session.isValid())
                retval = true;
        }
    }

    return retval;
}