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:alpha.portal.webapp.util.RequestUtil.java

/**
 * Convenience method to get a cookie by name.
 * //from   www . ja v  a  2  s .  co m
 * @param request
 *            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 (final Cookie thisCookie : cookies) {
        if (thisCookie.getName().equals(name) && !"".equals(thisCookie.getValue())) {
            returnCookie = thisCookie;
            break;
        }
    }

    return returnCookie;
}

From source file:com.leixl.easyframework.web.CookieUtils.java

/**
 * cookie/*ww w  .j a v  a  2s  .c o m*/
 * 
 * @param request
 *            HttpServletRequest
 * @param name
 *            cookie name
 * @return if exist return cookie, else return null.
 */
public static Cookie getCookie(HttpServletRequest request, String name) {
    Assert.notNull(request);
    Cookie[] cookies = request.getCookies();
    if (cookies != null && cookies.length > 0) {
        for (Cookie c : cookies) {
            if (c.getName().equals(name)) {
                return c;
            }
        }
    }
    return null;
}

From source file:io.lavagna.web.helper.UserSession.java

private static Cookie getCookie(HttpServletRequest request, String name) {
    if (request.getCookies() != null) {
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equals(name)) {
                return cookie;
            }//w w w. j  a v a 2  s  .  c om
        }
    }
    return null;
}

From source file:architecture.ee.web.util.CookieUtils.java

public static Cookie getCookie(HttpServletRequest request, String name) {
    if (null == request || null == name)
        return null;
    Cookie cookies[];/*from   www .j a  va2s  .  c  o m*/
    cookies = request.getCookies();
    if (cookies == null || name == null || name.length() == 0)
        return null;
    try {
        Cookie cookie = null;
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i] == null || cookies[i].getName() == null || !cookies[i].getName().equals(name))
                continue;
            cookie = cookies[i];
            if (request.getServerName() != null && request.getServerName().equals(cookie.getDomain()))
                break;
        }

        return cookie;
    } catch (NullPointerException e) {
        log.debug("NPE retrieving cookies from request, returning null", e);
    }
    return null;
}

From source file:com.flexive.war.filter.FxRequestUtils.java

/**
 * Get the cookie with the given name. Returns null if no such cookie is defined.
 *
 * @param request the current request//from   w  w w .  j  a  v a 2s.c o m
 * @param name    the name of the cookie (case-sensitive)
 * @return the cookie with the given name, or null if no such cookie is defined.
 */
public static Cookie getCookie(HttpServletRequest request, String name) {
    final Cookie[] cookies = request.getCookies();
    if (cookies == null || name == null) {
        return null;
    }
    for (Cookie cookie : cookies) {
        if (name.equals(cookie.getName())) {
            return cookie;
        }
    }
    return null;
}

From source file:arena.utils.ServletUtils.java

public static String getCookieValue(HttpServletRequest req, String cookieName, String defaultValue) {
    Cookie[] cookies = req.getCookies();

    if (cookies != null) {
        for (int n = 0; n < cookies.length; n++) {
            if (cookies[n].getName().equals(cookieName)) {
                return cookies[n].getValue();
            }/*from  w ww .j  av a 2  s. c o  m*/
        }
    }

    return defaultValue;
}

From source file:com.netsteadfast.greenstep.base.sys.UserCurrentCookie.java

public static Map<String, String> getCurrentData(HttpServletRequest request) {
    Map<String, String> dataMap = new HashMap<String, String>();
    try {/* w ww  .  j  a v a 2 s .c  om*/
        Cookie[] cookies = request.getCookies();
        for (int i = 0; cookies != null && dataMap.size() == 0 && i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (cookie.getName().equals(Constants.APP_SITE_CURRENTID_COOKIE_NAME)) {
                if (StringUtils.isBlank(cookie.getValue())) {
                    return dataMap;
                }
                String decVal = SimpleUtils.deHex(cookie.getValue());
                decVal = EncryptorUtils.decrypt(Constants.getEncryptorKey1(), Constants.getEncryptorKey2(),
                        decVal);
                String tmp[] = decVal.split(Constants.ID_DELIMITER);
                if (tmp != null && tmp.length == 4) {
                    dataMap.put("currentId", tmp[0]);
                    dataMap.put("sessionId", tmp[1]);
                    dataMap.put("account", tmp[2]);
                    dataMap.put("lang", tmp[3]);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dataMap;
}

From source file:com.threewks.thundr.http.Cookies.java

/**
 * Gets all cookies of the given name from the request
 * //from  w  ww .  ja v  a  2 s .  c o  m
 * @param name
 * @param req
 * @return
 */
public static final List<Cookie> getCookies(String name, HttpServletRequest req) {
    List<Cookie> results = new ArrayList<Cookie>();
    if (req.getCookies() != null) {
        for (Cookie cookie : req.getCookies()) {
            if (cookie.getName().equals(name)) {
                results.add(cookie);
            }
        }
    }
    return results;
}

From source file:cn.vlabs.duckling.vwb.VWBFilter.java

private static String getLocaleString(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("Portal.Locale")) {
                return cookie.getValue();
            }/*from w w w . ja  va  2  s . c  o  m*/
        }
    }
    return null;
}

From source file:io.syndesis.rest.v1.handler.credential.CredentialHandler.java

static void removeCredentialCookies(final HttpServletRequest request,
        final HttpServletResponse response) {
    Arrays.stream(request.getCookies())
            .filter(c -> c.getName().startsWith(CredentialFlowState.CREDENTIAL_PREFIX)).forEach(c -> {
                final Cookie removal = new Cookie(c.getName(), "");
                removal.setPath("/");
                removal.setMaxAge(0);/* ww w .ja va 2 s  .  c  o  m*/
                removal.setHttpOnly(true);
                removal.setSecure(true);

                response.addCookie(removal);
            });
}