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.microsoft.applicationinsights.web.internal.cookies.Cookie.java

/**
 * Gets the cookie from the given http request.
 * @param eClass The required cookie type.
 * @param request THe http request to get the cookies from.
 * @param cookieName The cookie name./* w w w .  j  a  va  2  s .com*/
 * @param <E> The required cookie type.
 * @return Cookie from the required type.
 */
public static <E> E getCookie(Class<E> eClass, HttpServletRequest request, String cookieName) {
    javax.servlet.http.Cookie[] cookies = request.getCookies();

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

    javax.servlet.http.Cookie httpCookie = null;
    for (javax.servlet.http.Cookie cookie : cookies) {
        if (cookie.getName().equals(cookieName)) {
            httpCookie = cookie;
        }
    }

    if (httpCookie == null) {

        // Http cookie hasn't been found.
        return null;
    }

    E instance = null;
    try {
        instance = eClass.getConstructor(javax.servlet.http.Cookie.class).newInstance(httpCookie);
    } catch (Exception e) {
        InternalLogger.INSTANCE.error("Failed to create %s cookie with error: %s", cookieName, e.getMessage());
    }

    return instance;
}

From source file:com.hortonworks.example.util.Util.java

public static Cookie getCookie(HttpServletRequest request, String name) {
    for (Cookie cookie : request.getCookies()) {
        if (cookie.getName().equalsIgnoreCase(name)) {
            return cookie;
        }/*from ww  w . jav a  2  s  . c o m*/
    }
    return null;
}

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

/**
 * Gets the first cookie of the given name from the request
 * //from w w  w.j a va2  s. c om
 * @param name
 * @param req
 * @return
 */
public static final Cookie getCookie(String name, HttpServletRequest req) {
    if (req.getCookies() != null) {
        for (Cookie cookie : req.getCookies()) {
            if (cookie.getName().equals(name)) {
                return cookie;
            }
        }
    }
    return null;
}

From source file:io.soabase.web.filters.LanguageFilter.java

private static List<Cookie> getCookies(HttpServletRequest httpRequest) {
    if (httpRequest.getCookies() != null) {
        return Arrays.asList(httpRequest.getCookies());
    }/*from w  ww.  java  2s .c  o  m*/
    return ImmutableList.of();
}

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

public static boolean foundCurrent(HttpServletRequest request) {
    boolean f = false;
    try {//from w  ww .j a v  a  2s.  c o m
        Cookie[] cookies = request.getCookies();
        for (int i = 0; !f && cookies != null && i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (cookie.getName().equals(Constants.APP_SITE_CURRENTID_COOKIE_NAME)) {
                f = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return f;
}

From source file:com.usefullc.platform.common.utils.CookieUtils.java

/**
 * cookie /*w w w .j  a v a2 s .  c  o  m*/
 * 
 * @param request
 * @param name
 * @param domain
 * @return
 */
public static Cookie getCookie(HttpServletRequest request, String name, String domain) {
    Cookie cookies[] = request.getCookies();
    if (cookies == null) {
        return null;
    }
    for (Cookie cookie : cookies) {
        String cookieName = cookie.getName();
        String cookieDomain = cookie.getDomain();
        if (!cookieName.equals(name)) {
            continue;
        }
        if (StringUtils.isNotEmpty(domain) && !cookieDomain.equals(domain)) {
            continue;
        }
        return cookie;
    }
    return null;
}

From source file:com.utest.webservice.util.SessionUtil.java

public static String extractSession(MessageContext context, boolean insertToResponze) {
    HttpServletRequest req = context.getHttpServletRequest();
    javax.servlet.http.Cookie[] available = req.getCookies();
    String sessionId = null;//from   www .ja  va 2  s.c  om
    if (available != null) {
        for (javax.servlet.http.Cookie ck : available) {
            if (SESSION_NAME.equalsIgnoreCase(ck.getName())) {
                sessionId = ck.getValue();
            }
        }
    }
    if (sessionId == null) {
        HttpSession session = req.getSession();
        sessionId = session.getId();
        if (insertToResponze) {
            javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie(SESSION_NAME, sessionId);
            context.getHttpServletResponse().addCookie(cookie);
        }
    }
    return sessionId;
}

From source file:com.gisgraphy.webapp.util.RequestUtil.java

/**
 * Convenience method to get a cookie by name
 * /*from  w  w  w  .jav a  2  s. c o 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(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)) {
            // cookies with no value do me no good!
            if (!thisCookie.getValue().equals("")) {
                returnCookie = thisCookie;

                break;
            }
        }
    }

    return returnCookie;
}

From source file:com.jsmartframework.web.util.WebUtils.java

public static String getCookie(HttpServletRequest request, String name) {
    if (name == null) {
        return null;
    }//from w  w  w  . j a  v  a 2 s .  co  m
    Cookie[] cookies = request.getCookies();
    if (cookies == null || cookies.length == 0) {
        return null;
    }
    for (Cookie cookie : cookies) {
        if (name.equalsIgnoreCase(cookie.getName())) {
            return cookie.getValue();
        }
    }
    return null;
}

From source file:dk.dma.msinm.common.util.WebUtils.java

/**
 * Returns the cookie with the given name or null if not found
 * @param request the request//ww w  .j  a  v  a 2s . com
 * @param name the name
 * @return the cookie with the given name or null if not found
 */
public static Cookie getCookie(HttpServletRequest request, String name) {
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie c : request.getCookies()) {
            if (c.getName().equals(name)) {
                return c;
            }
        }
    }
    return null;
}