Example usage for javax.servlet.http Cookie setMaxAge

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

Introduction

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

Prototype

public void setMaxAge(int expiry) 

Source Link

Document

Sets the maximum age in seconds for this Cookie.

Usage

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

/**
 * Removes all cookies for the domain/*from   w w  w. j a v  a  2 s .  c o  m*/
 *
 * @param request Request to get the Cookies to drop
 * @param response Response to expire the Cookies on
 */
public static int dropAllCookies(HttpServletRequest request, HttpServletResponse response) {
    int count = 0;
    Cookie[] cookies = request.getCookies();

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

    for (Cookie cookie : cookies) {
        cookie.setMaxAge(0);
        cookie.setPath(cookie.getPath());

        addCookie(cookie, response);
        count++;
    }

    return count;
}

From source file:com.identityconcepts.shibboleth.WSFedLoginHandler.java

/**
 * set cookie for pass-through/*from  www  . jav a 2  s  . co  m*/
 * cookieDomain can be configured in the handler config
 *
 * @param  path   path to which the client should return the cookie
 */
public static Cookie createCookie(String path) {
    Cookie cookie = new Cookie(COOKIE_NAME, "1");
    cookie.setMaxAge(60 * 60 * 24 * 365);
    cookie.setPath(path);
    cookie.setSecure(true);
    // use cookieDomain if set
    if (!((cookieDomain == null) || (cookieDomain == ""))) {
        cookie.setDomain(cookieDomain);
    }
    return cookie;
}

From source file:com.kingcore.framework.util.CookieUtils.java

/**
 *  ? domain clearCookie/* w  ww  . j  a  v  a 2s  . c  o m*/
 * clear a cookie from client side.
 * @param name the name of cookie will be cleared.
 * @param response HttpServletResponse Object.
 */
public static void clearCookie(String name, HttpServletResponse response) {
    Cookie cookie = new Cookie(name, null);
    cookie.setMaxAge(0);
    cookie.setPath("/");
    response.addCookie(cookie);
}

From source file:com.kingcore.framework.util.CookieUtils.java

/**
 *  domain clearCookie/*from  w  w w  . ja  va 2s  . c om*/
 *    The form of the domain name is specified by RFC 2109. A domain name begins with a dot (.foo.com) 
 *       and means that the cookie is visible to servers in a specified Domain Name System (DNS) zone 
 *       (for example, www.foo.com, but not a.b.foo.com). By default, cookies are only returned to 
 *       the server that sent them.
 * @param name ?Cookie??
 * @param response ?
 * @param domain Cookie??
 */
public static void clearCookie(String name, HttpServletResponse response, String domain) {
    Cookie cookie = new Cookie(name, null);
    cookie.setMaxAge(0);
    cookie.setPath("/");
    cookie.setDomain(domain);
    response.addCookie(cookie);
}

From source file:com.kingcore.framework.util.CookieUtils.java

/**
 *  domain,path ? clearCookie//from   w  ww  .  j  a  v  a 2s.  c  o m
 *    The form of the domain name is specified by RFC 2109. A domain name begins with a dot (.foo.com) 
 *       and means that the cookie is visible to servers in a specified Domain Name System (DNS) zone 
 *       (for example, www.foo.com, but not a.b.foo.com). By default, cookies are only returned to 
 *       the server that sent them.
 * @param name ?Cookie??
 * @param response ?
 * @param domain Cookie??
 * @param path Cookie?
 */
public static void clearCookie(String name, HttpServletResponse response, String domain, String path) {
    Cookie cookie = new Cookie(name, null);
    cookie.setMaxAge(0);
    cookie.setPath(path);
    cookie.setDomain(domain);
    response.addCookie(cookie);
}

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

/**
 * cookie/*from  w  w  w  .  j  a  v a  2  s  . c o  m*/
 * 
 * @param response
 * @param name
 * @param value
 * @param domain
 * @param expire
 */
public static void addCookie(HttpServletResponse response, String name, String value, String domain,
        Integer expire) {
    Cookie cookie = new Cookie(name, value);
    if (StringUtils.isNotEmpty(domain)) {
        cookie.setDomain(domain);
    }
    if (expire != null) {
        cookie.setMaxAge(expire);
    }
    cookie.setPath("/");
    response.addCookie(cookie);
}

From source file:org.b3log.symphony.util.Sessions.java

/**
 * Logins the specified user from the specified request.
 *
 * <p>//from  w w  w  . j  a va 2s.c o m
 * If no session of the specified request, do nothing.
 * </p>
 *
 * @param request the specified request
 * @param response the specified response
 * @param user the specified user, for example,      <pre>
 * {
 *     "oId": "",
 *     "userPassword": ""
 * }
 * </pre>
 */
public static void login(final HttpServletRequest request, final HttpServletResponse response,
        final JSONObject user) {
    final HttpSession session = request.getSession(false);

    if (null == session) {
        LOGGER.warn("The session is null");

        return;
    }

    session.setAttribute(User.USER, user);
    session.setAttribute(Common.CSRF_TOKEN, RandomStringUtils.randomAlphanumeric(12));

    try {
        final JSONObject cookieJSONObject = new JSONObject();

        cookieJSONObject.put(Keys.OBJECT_ID, user.optString(Keys.OBJECT_ID));
        cookieJSONObject.put(Common.TOKEN, user.optString(User.USER_PASSWORD));

        final Cookie cookie = new Cookie("b3log-latke", cookieJSONObject.toString());

        cookie.setPath("/");
        cookie.setMaxAge(COOKIE_EXPIRY);
        cookie.setHttpOnly(true); // HTTP Only

        response.addCookie(cookie);
    } catch (final Exception e) {
        LOGGER.log(Level.WARN, "Can not write cookie", e);
    }
}

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

private static void removeCookie(HttpServletResponse response, HttpServletRequest httpRequest,
        String cookieName, String domain, String path) {
    Cookie cookie = createCookie(cookieName, "", domain, path, httpRequest);
    cookie.setMaxAge(0);
    response.addCookie(cookie);//from w  w w . j  a  va 2  s  .  c om
}

From source file:com.adobe.acs.commons.util.CookieUtil.java

/**
 * <p>//from w w w .  j  ava 2  s.  co  m
 * Extend the cookie life.
 * <p></p>
 * This can be used when a cookie should be valid for X minutes from the last point of activity.
 * <p></p>
 * This method will leave expired or deleted cookies alone.
 * </p>
 *
 * @param request    Request to get the Cookie from
 * @param response   Response to write the extended Cookie to
 * @param cookieName Name of Cookie to extend the life of
 * @param expiry     New Cookie expiry
 */
public static boolean extendCookieLife(final HttpServletRequest request, final HttpServletResponse response,
        final String cookieName, final String cookiePath, final int expiry) {
    final Cookie cookie = getCookie(request, cookieName);
    if (cookie == null) {
        return false;
    }

    if (cookie.getMaxAge() <= 0) {
        return false;
    }

    final Cookie responseCookie = (Cookie) cookie.clone();
    responseCookie.setMaxAge(expiry);
    responseCookie.setPath(cookiePath);

    addCookie(responseCookie, response);

    return true;
}

From source file:com.liusoft.dlog4j.util.RequestUtils.java

/**
 * COOKIE//from   ww  w  .  j  a va  2 s .  c  o m
 * 
 * @param name
 * @param value
 * @param maxAge
 */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, int maxAge) {
    Cookie cookie = new Cookie(name, value);
    cookie.setMaxAge(maxAge);
    String serverName = request.getServerName();
    String domain = getDomainOfServerName(serverName);
    if (domain != null && domain.indexOf('.') != -1) {
        cookie.setDomain('.' + domain);
    }
    cookie.setPath("/");
    response.addCookie(cookie);
}