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:org.craftercms.core.util.HttpServletUtils.java

public static Cookie getCookie(String name, HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (ArrayUtils.isNotEmpty(cookies)) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(name)) {
                return cookie;
            }//from   w  ww  .  j  ava 2  s  .c  om
        }
    }

    return null;
}

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

public static Object getRequestCookieValue(HttpServletRequest request, String key) {
    Object value = request.getSession().getAttribute(key);
    if (value != null) {
        return value;
    }//w w w.  ja  va  2s .  co  m
    return getCookieValue(request.getCookies(), key);
}

From source file:org.apache.cxf.fediz.service.idp.util.WebUtils.java

public static Cookie readCookie(final RequestContext context, final String cookieName) {
    HttpServletRequest httpServletRequest = getHttpServletRequest(context);
    Cookie[] cookies = httpServletRequest.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals(cookieName)) {
                return cookies[i];
            }/*w  w w. j  a v a2 s  .  com*/
        }
    }
    return null;
}

From source file:com.ax.utils.CookieUtils.java

/**
 * Returns the specified cookie, or <tt>null</tt> if the cookie does not
 * exist. Note: because of the way that cookies are implemented it's
 * possible for multiple cookies with the same name to exist (but with
 * different domain values). This method will return the first cookie that
 * has a name match.//ww w.jav a2s .  c o  m
 * 
 * @param request
 *            the servlet request.
 * @param name
 *            the name of the cookie.
 * @return the Cookie object if it exists, otherwise <tt>null</tt>.
 */
public static Cookie getCookie(HttpServletRequest request, String name) {
    Cookie cookies[] = request.getCookies();
    // Return null if there are no cookies or the name is invalid.
    if (cookies == null || name == null || name.length() == 0) {
        return null;
    }
    // Otherwise, we do a linear scan for the cookie.
    Cookie cookie = null;
    for (int i = 0; i < cookies.length; i++) {
        // If the current cookie name matches the one we're looking for,
        // we've
        // found a matching cookie.
        cookie = cookies[i];
        // LOG.info("ServerName = " + request.getServerName() +
        // " , Domain = " + cookies[i].getDomain() + " , name = " + name +
        // " , " + cookies[i].getValue());
        // LOG.info("name = " + cookie.getName() + " , value = " +
        // cookie.getValue() + " , path = " + cookie.getPath());
        // if (cookie.getName().equals(name) &&
        // "/".equals(cookie.getPath())) {
        if (cookie.getName().equals(name)) {
            return cookie;
            // The best matching cookie will be the one that has the correct
            // domain name. If we've found the cookie with the correct
            // domain
            // name,
            // return it. Otherwise, we'll keep looking for a better match.
            /*
             * if (request.getServerName().equals(cookies[i].getDomain())) {
             * return cookies[i]; }
             */
        }
    }
    return null;
}

From source file:com.sniper.springmvc.utils.CookieUtils.java

/**
 * Cookie// w ww .j  a  va 2s  . c  o m
 * 
 * @param request
 *            
 * @param response
 *            ?
 * @param name
 *            ??
 * @param isRemove
 *            ?
 * @return 
 */
public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name,
        boolean isRemove) {
    String value = null;
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(name)) {
                try {
                    value = URLDecoder.decode(cookie.getValue(), "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (isRemove) {
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }
    }
    return value;
}

From source file:nl.strohalm.cyclos.utils.RequestHelper.java

/**
 * Finds a cookie with the given name, returning null when it's not found
 *//*from  w ww  . java  2s.  c om*/
public static Cookie getCookie(final ServletRequest servletRequest, final String name) {
    final HttpServletRequest request = (HttpServletRequest) servletRequest;
    final Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (final Cookie cookie : cookies) {
            if (cookie.getName().equals(name)) {
                return cookie;
            }
        }
    }
    return null;
}

From source file:scratch.cucumber.example.security.servlet.XAuthTokenHttpServletRequestBinder.java

private static String findToken(HttpServletRequest request) {

    final String headerToken = request.getHeader(X_AUTH_TOKEN);

    if (headerToken != null) {
        return headerToken;
    }/*from w  ww. j  a  va 2s.co  m*/

    final Cookie[] cookies = request.getCookies();

    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (X_AUTH_TOKEN.equals(cookie.getName())) {
                return cookie.getValue();
            }
        }
    }

    return null;
}

From source file:com.erudika.scoold.utils.HttpUtils.java

/**
 * Reads a cookie.// ww  w  . j a  va 2  s  .  c o m
 * @param name the name
 * @param req HTTP request
 * @return the cookie value
 */
public static String getCookieValue(HttpServletRequest req, String name) {
    if (StringUtils.isBlank(name) || req == null) {
        return null;
    }
    Cookie[] cookies = req.getCookies();
    if (cookies == null) {
        return null;
    }
    //Otherwise, we have to do a linear scan for the cookie.
    for (Cookie cookie : cookies) {
        if (cookie.getName().equals(name)) {
            return cookie.getValue();
        }
    }
    return null;
}

From source file:org.apache.archiva.redback.integration.util.AutoLoginCookies.java

private static Cookie getCookie(HttpServletRequest request, String name) {
    Cookie[] cookies = request.getCookies();

    Cookie cookie = null;/*w w w.j  a v  a2  s .  c o  m*/
    if (cookies != null && !StringUtils.isEmpty(name)) {
        for (int i = 0; i < cookies.length && cookie == null; i++) {
            if (StringUtils.equals(name, cookies[i].getName())) {
                cookie = cookies[i];
            }
        }
    }

    return cookie;
}

From source file:edu.utah.further.i2b2.hook.further.web.ServletUtil.java

/**
 * Convenience method to get a cookie by name
 *
 * @param request/* w  w w.java 2 s  . co m*/
 *            the current request
 * @param name
 *            the name of the cookie to find
 * @return the cookie (if found), null if not found
 */
public static Cookie getCookie(final HttpServletRequest request, final String name) {
    final Cookie[] cookies = request.getCookies();
    Cookie returnCookie = null;

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

    for (int i = 0; i < cookies.length; i++) {
        final Cookie thisCookie = cookies[i];

        if (thisCookie.getName().equals(name)) {
            // cookies with no value do me no good!
            if (!thisCookie.getValue().equals("")) {
                returnCookie = thisCookie;

                break;
            }
        }
    }

    return returnCookie;
}