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.shirokumacafe.archetype.common.utilities.Servlets.java

/**
 * cookie/*w w w. j  a  v a2s. com*/
 */
public static void setCookie(String name, String value, HttpServletResponse response) {
    Cookie cookie = new Cookie(name, value);
    cookie.setMaxAge((int) ONE_YEAR_SECONDS);
    cookie.setPath("/");
    response.addCookie(cookie);
}

From source file:org.commonfarm.security.util.CookieUtils.java

/**
 * Sets a cookie/*from  ww  w.  j  a  va  2s. c  o m*/
 *
 * This will also put the cookie in a list of cookies to send with this request's response
 * (so that in case of a redirect occurring down the chain, the first filter
 * will always try to set this cookie again)
 */
public static Cookie setCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, int maxAge, String path) {
    log.debug("CookieUtils.setCookie " + name + ":" + value);
    Cookie cookie = new Cookie(name, value);
    cookie.setMaxAge(maxAge);
    cookie.setPath(path);
    response.addCookie(cookie);

    return cookie;
}

From source file:net.shopxx.util.CookieUtils.java

/**
 * cookie/*w w  w.  j a va2  s.  c o m*/
 * 
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 * @param name
 *            cookie??
 * @param path
 *            
 * @param domain
 *            
 */
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String path, String domain) {
    Assert.notNull(request);
    Assert.notNull(response);
    Assert.hasText(name);
    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);
}

From source file:alpha.portal.webapp.util.RequestUtil.java

/**
 * Convenience method to set a cookie.// www. j  a  va 2s  .  co m
 * 
 * @param response
 *            the current response
 * @param name
 *            the name of the cookie
 * @param value
 *            the value of the cookie
 * @param path
 *            the path to set it on
 */
public static void setCookie(final HttpServletResponse response, final String name, final String value,
        final String path) {
    if (RequestUtil.log.isDebugEnabled()) {
        RequestUtil.log.debug("Setting cookie '" + name + "' on path '" + path + "'");
    }

    final Cookie cookie = new Cookie(name, value);
    cookie.setSecure(false);
    cookie.setPath(path);
    cookie.setMaxAge(3600 * 24 * 30); // 30 days

    response.addCookie(cookie);
}

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

/**
 * Logouts a user with the specified request.
 *
 * @param request the specified request//from w w  w .j a v  a2 s  . c  o  m
 * @param response the specified response
 * @return {@code true} if succeed, otherwise returns {@code false}
 */
public static boolean logout(final HttpServletRequest request, final HttpServletResponse response) {
    final HttpSession session = request.getSession(false);

    if (null != session) {
        final Cookie cookie = new Cookie("b3log-latke", null);

        cookie.setMaxAge(0);
        cookie.setPath("/");

        response.addCookie(cookie);

        session.invalidate();

        return true;
    }

    return false;
}

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

public static void setCurrentId(HttpServletResponse response, String currentId, String sessionId,
        String account, String language) {
    try {//www  .jav  a2 s . co m
        String value = currentId + Constants.ID_DELIMITER + sessionId + Constants.ID_DELIMITER + account
                + Constants.ID_DELIMITER + language;
        String encValue = EncryptorUtils.encrypt(Constants.getEncryptorKey1(), Constants.getEncryptorKey2(),
                value);
        encValue = SimpleUtils.toHex(encValue);
        Cookie cookie = new Cookie(Constants.APP_SITE_CURRENTID_COOKIE_NAME, encValue);
        cookie.setPath("/");
        cookie.setValue(encValue);
        cookie.setMaxAge(60 * 60 * 24); // 1-day
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

public static void addCookie(final RequestContext context, final String cookieName, final String cookieValue) {
    HttpServletResponse httpServletResponse = getHttpServletResponse(context);
    Cookie cookie = new Cookie(cookieName, cookieValue);
    cookie.setSecure(true);//from ww  w.  j av a2 s. co m
    cookie.setMaxAge(-1);
    httpServletResponse.addCookie(cookie);
}

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

public static void removeCookie(final RequestContext context, final String cookieName) {
    HttpServletResponse httpServletResponse = getHttpServletResponse(context);
    Cookie cookie = readCookie(context, cookieName);
    if (cookie != null) {
        cookie.setMaxAge(0);
        cookie.setValue("");
        httpServletResponse.addCookie(cookie);
    }/*from w ww . ja  v  a 2s. co  m*/
}

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);
                removal.setHttpOnly(true);
                removal.setSecure(true);

                response.addCookie(removal);
            });//  ww  w. ja v a 2 s.c om
}

From source file:alpha.portal.webapp.util.RequestUtil.java

/**
 * Convenience method for deleting a cookie by name.
 * /*from   ww  w .jav a  2s .c  o  m*/
 * @param response
 *            the current web response
 * @param cookie
 *            the cookie to delete
 * @param path
 *            the path on which the cookie was set (i.e. /appfuse)
 */
public static void deleteCookie(final HttpServletResponse response, final Cookie cookie, final String path) {
    if (cookie != null) {
        // Delete the cookie by setting its maximum age to zero
        cookie.setMaxAge(0);
        cookie.setPath(path);
        response.addCookie(cookie);
    }
}