Example usage for javax.servlet.http Cookie getName

List of usage examples for javax.servlet.http Cookie getName

Introduction

In this page you can find the example usage for javax.servlet.http Cookie getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the cookie.

Usage

From source file:com.o2o.util.WebUtils.java

/**
 * ?cookie/*from ww  w.  j  a  v  a  2s  . c o m*/
 * 
 * @param request
 *            HttpServletRequest
 * @param name
 *            cookie??
 * @return ?null
 */
public static String getCookie(HttpServletRequest request, String name) {
    if (null == name) {
        return null;
    }
    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.feilong.servlet.http.CookieUtil.java

/**
 *  {@link Cookie}.//w  w  w .  j a  v  a2  s  .c  o 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.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  www. ja v a 2s. c  o  m*/
        }
    }
    return cookieUsername;
}

From source file:com.o2o.util.WebUtils.java

public static void clearCookie(HttpServletRequest request, HttpServletResponse response, String path,
        String domain) {/*w w  w  .java2s  . com*/
    Assert.notNull(request);
    Assert.notNull(response);
    try {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie_old : cookies) {
            String name = URLEncoder.encode(cookie_old.getName(), "UTF-8");
            Cookie cookie = new Cookie(name, null);
            cookie.setMaxAge(0);
            if (StringUtils.isNotEmpty(path)) {
                cookie.setPath(path);
            }
            if (StringUtils.isNotEmpty(domain)) {
                cookie.setDomain(domain);
            }
            response.addCookie(cookie);
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:com.aaasec.sigserv.csspserver.SpServlet.java

private static SpSession getSession(HttpServletRequest request, HttpServletResponse response) {
    BigInteger sessionID = new BigInteger(32, new Random(System.currentTimeMillis()));
    Cookie[] cookies = request.getCookies();
    try {/*from   w ww  .j  a va  2s.  c om*/
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("SigSpSession")) {
                sessionID = new BigInteger(cookie.getValue());
            }
        }
    } catch (Exception ex) {
    }
    response.addCookie(new Cookie("SigSpSession", sessionID.toString()));
    return getSessionFromID(sessionID);
}

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

/**
 * ?Cookie./*from ww w  .ja va  2 s.  co  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:org.carewebframework.ui.FrameworkWebSupport.java

/**
 * Returns the named cookie from the specified request. When values are retrieved, they should
 * be decoded./*from w w w .  j av a  2s. c o m*/
 * 
 * @see #decodeCookieValue(String)
 * @param cookieName Name of cookie
 * @param httpRequest Request containing cookie.
 * @return A cookie, or null if not found.
 * @throws IllegalArgumentException if arguments are null
 */
public static Cookie getCookie(final String cookieName, final HttpServletRequest httpRequest) {
    Validate.notNull(cookieName, "The cookieName must not be null");
    Validate.notNull(httpRequest, "The httpRequest must not be null");
    final Cookie[] cookies = httpRequest.getCookies();

    if (cookies != null) {
        for (final Cookie cookie : httpRequest.getCookies()) {
            if (cookieName.equals(cookie.getName())) {
                return cookie;
            }
        }
    }

    return null;
}

From source file:com.anjz.util.CookieUtils.java

private static void getCookieHeaderValue(final Cookie cookie, final StringBuffer buf, final boolean httpOnly) {
    final int version = cookie.getVersion();

    // this part is the same for all cookies

    String name = cookie.getName(); // Avoid NPE on malformed cookies
    if (name == null) {
        name = "";
    }//from   w ww  .  jav  a2 s .co  m
    String value = cookie.getValue();
    if (value == null) {
        value = "";
    }

    buf.append(name);
    buf.append("=");

    maybeQuote(version, buf, value);

    // add version 1 specific information
    if (version == 1) {
        // Version=1 ... required
        buf.append("; Version=1");

        // Comment=comment
        if (cookie.getComment() != null) {
            buf.append("; Comment=");
            maybeQuote(version, buf, cookie.getComment());
        }
    }

    // add domain information, if present

    if (cookie.getDomain() != null) {
        buf.append("; Domain=");
        maybeQuote(version, buf, cookie.getDomain());
    }

    // Max-Age=secs/Discard ... or use old "Expires" format
    if (cookie.getMaxAge() >= 0) {
        if (version == 0) {
            buf.append("; Expires=");
            SimpleDateFormat dateFormat = new SimpleDateFormat(OLD_COOKIE_PATTERN, LOCALE_US);
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); //GMT?
            if (cookie.getMaxAge() == 0) {
                dateFormat.format(new Date(10000), buf, new FieldPosition(0));
            } else {
                dateFormat.format(new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000L), buf,
                        new FieldPosition(0));
            }
        } else {
            buf.append("; Max-Age=");
            buf.append(cookie.getMaxAge());
        }
    } else if (version == 1) {
        buf.append("; Discard");
    }

    // Path=path
    if (cookie.getPath() != null) {
        buf.append("; Path=");
        maybeQuote(version, buf, cookie.getPath());
    }

    // Secure
    if (cookie.getSecure()) {
        buf.append("; Secure");
    }

    // HttpOnly
    if (httpOnly) {
        buf.append("; HttpOnly");
    }
}

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

/**
 * /*w  w w .  j  a v a 2  s  .c  om*/
 * @param preExistentCookie
 *            , cloudNinjaUser
 */
public static Cookie updateExistingCookie(Cookie preExistentCookie, CloudNinjaUser cloudNinjaUser) {
    String cookieValue = createNewCookieForACSAuthenticatedUser(cloudNinjaUser, preExistentCookie.getName())
            .getValue();
    preExistentCookie.setValue(cookieValue);
    return preExistentCookie;
}

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

public static String getJSessionIDFromRequest(HttpServletRequest servletRequest) {
    String jSessionID = null;/*from  w w w  .j av a 2 s.com*/

    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;
}