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.iterzp.momo.utils.WebUtils.java

/**
 * cookie//from  w w  w  .j a v a 2 s  .  c  o  m
 * 
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 * @param name
 *            cookie??
 * @param value
 *            cookie
 * @param maxAge
 *            (??: )
 * @param path
 *            
 * @param domain
 *            
 * @param secure
 *            ??
 */
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, Integer maxAge, String path, String domain, Boolean secure) {
    Assert.notNull(request);
    Assert.notNull(response);
    Assert.hasText(name);
    try {
        name = URLEncoder.encode(name, "UTF-8");
        value = URLEncoder.encode(value, "UTF-8");
        Cookie cookie = new Cookie(name, value);
        if (maxAge != null) {
            cookie.setMaxAge(maxAge);
        }
        if (StringUtils.isNotEmpty(path)) {
            cookie.setPath(path);
        }
        if (StringUtils.isNotEmpty(domain)) {
            cookie.setDomain(domain);
        }
        if (secure != null) {
            cookie.setSecure(secure);
        }
        response.addCookie(cookie);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:ai.susi.server.AbstractAPIHandler.java

/**
 * Delete the login cookie if present/* w w w.j  a va 2s. co m*/
 * @param response
 */
protected static void deleteLoginCookie(HttpServletResponse response) {
    Cookie deleteCookie = new Cookie("login", null);
    deleteCookie.setPath("/");
    deleteCookie.setMaxAge(0);
    response.addCookie(deleteCookie);
}

From source file:com.sniper.springmvc.utils.CookieUtils.java

/**
 *  Cookie//from w  w w .j  a v  a2  s. c  om
 * 
 * @param name
 *            ??
 * @param value
 *            
 * @param maxAge
 *            ??
 */
public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
    Cookie cookie = new Cookie(name, null);
    if (StringUtils.isNotBlank(SpringContextHolder.getApplicationContext().getApplicationName())) {
        cookie.setPath(SpringContextHolder.getApplicationContext().getApplicationName());
    } else {
        cookie.setPath("/");
    }
    cookie.setMaxAge(maxAge);
    try {
        cookie.setValue(URLEncoder.encode(value, "utf-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    response.addCookie(cookie);
}

From source file:org.apache.hadoop.yarn.server.webproxy.WebAppProxyServlet.java

private static Cookie makeCheckCookie(ApplicationId id, boolean isSet) {
    Cookie c = new Cookie(getCheckCookieName(id), String.valueOf(isSet));
    c.setPath(ProxyUriUtils.getPath(id));
    c.setMaxAge(60 * 60 * 2); //2 hours in seconds
    return c;/*  w ww.ja v  a  2  s  .c o  m*/
}

From source file:org.beanfuse.utils.web.CookieUtils.java

/**
 * Convenience method to set a cookie <br>
 * value?<br>/*w w w .  j av  a2s  .  c  o  m*/
 * 
 * @param response
 * @param name
 * @param value
 * @param path
 */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, String path, int age) {
    LOG.debug("add cookie[name:{},value={},path={}]", new String[] { name, value, path });
    Cookie cookie = null;
    try {
        cookie = new Cookie(name, new URLCodec().encode(value, "utf-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    cookie.setSecure(false);
    cookie.setPath(path);
    cookie.setMaxAge(age);
    response.addCookie(cookie);
}

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

/**
 * <p>/* w  w  w  .  j  a va 2  s.c  o 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(HttpServletRequest request, HttpServletResponse response,
        String cookieName, int expiry) {
    Cookie cookie = getCookie(request, cookieName);
    if (cookie == null) {
        return false;
    }

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

    cookie.setMaxAge(expiry);
    cookie.setPath(cookie.getPath());

    addCookie(cookie, response);

    return true;
}

From source file:com.ax.utils.CookieUtils.java

/**
 * Deletes the specified cookie./*from w  w  w  .ja  v a 2 s .co m*/
 * 
 * @param request
 *            the servlet request.
 * @param response
 *            the servlet response.
 * @param cookie
 *            the cookie object to be deleted.
 * @param path
 *            the path.
 */
public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, Cookie cookie,
        String path) {
    if (cookie != null) {
        // Invalidate the cookie
        if (StringUtils.isEmpty(path)) {
            path = "/";
        }
        cookie.setPath(path);
        cookie.setValue("");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}

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

public static void clearCookie(HttpServletRequest request, HttpServletResponse response, String path,
        String domain) {/*  ww w. jav a  2  s  . c o  m*/
    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.sniper.springmvc.utils.CookieUtils.java

/**
 * Cookie//from w  w w.ja  va  2 s  .co  m
 * 
 * @param request
 *            
 * @param response
 *            ?
 * @param name
 *            ??
 * @param isRemove
 *            ?
 * @return 
 */
public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name,
        boolean isRemove) {
    String value = null;
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(name)) {
                try {
                    value = URLDecoder.decode(cookie.getValue(), "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (isRemove) {
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }
    }
    return value;
}

From source file:de.eod.jliki.users.utils.UserDBHelper.java

/**
 * Logs out the user.<br/>// ww w .  java2  s  .com
 * @param userLogin the login object
 */
public static void logoutUser(final LoginBean userLogin) {
    final SessionFactory sf = DBSetup.getDbManager().getSessionFactory();
    final Session session = sf.openSession();
    final Transaction trx = session.beginTransaction();
    final Query query = session.createQuery("select user from User as user where user.name = :username");
    query.setString("username", userLogin.getUserName());

    final Iterator<?> it = query.iterate();
    if (query.iterate().hasNext()) {
        final User dbUser = (User) it.next();
        dbUser.setCookieid("");
        session.update(dbUser);
    }

    trx.commit();
    session.close();

    userLogin.setLoggedIn(false);
    userLogin.setPassword(null);
    userLogin.setRememberMe(false);
    userLogin.setUserName(null);
    userLogin.clearPermissions();

    if (userLogin.isRememberMe()) {
        final HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();
        final Cookie cookie = new Cookie("login", "");
        cookie.setMaxAge(0);
        httpServletResponse.addCookie(cookie);
    }

    userLogin.setRememberMe(false);
}