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:de.hybris.platform.ytelcoacceleratorstorefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testServerSideCookieDynamicPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(true);//server side
    cookieGenerator.setUseDefaultPath(false);

    BDDMockito.given(request.getContextPath()).willReturn("/some_path");

    cookieGenerator.addCookie(request, response, "cookie_monster");

    final Cookie expectedCookie = new Cookie(JSESSIONID, "cookie_monster");
    expectedCookie.setPath("/some_path");
    expectedCookie.setSecure(false);//from  w w w .  j  a va 2 s  . c  o m
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

    Mockito.verify(response).addCookie(Mockito.argThat(new CookieArgumentMatcher(expectedCookie)));
    Mockito.verify(response).addHeader(EnhancedCookieGenerator.HEADER_COOKIE,
            "JSESSIONID=cookie_monster; Domain=\"what a domain\"; Path=/; HttpOnly");

}

From source file:edu.usu.sdl.openstorefront.web.action.LoginAction.java

@HandlesEvent("Logout")
public Resolution logout() {
    Subject currentUser = SecurityUtils.getSubject();
    String userLoggedIn = SecurityUtil.getCurrentUserName();
    currentUser.logout();//  ww w.ja v a 2s.  c o  m
    getContext().getRequest().getSession().invalidate();
    try {
        getContext().getRequest().logout();
    } catch (ServletException ex) {
        throw new OpenStorefrontRuntimeException(ex);
    }

    //For now invalidate all cookies; in the future there may be some that should persist.
    Cookie[] cookies = getContext().getRequest().getCookies();
    if (cookies != null && cookies.length > 0) {
        for (Cookie cookie : cookies) {
            cookie.setValue("-");
            cookie.setMaxAge(0);
            getContext().getResponse().addCookie(cookie);
        }
    }

    if (OpenStorefrontConstant.ANONYMOUS_USER.equals(userLoggedIn)) {
        log.log(Level.INFO, "User was not logged when the logut was called.");
    } else {
        log.log(Level.INFO, MessageFormat.format("Logged off user: {0}", userLoggedIn));
    }

    String logoutUrl = PropertiesManager.getValue(PropertiesManager.KEY_LOGOUT_URL, "/login.jsp");
    if (StringUtils.isBlank(logoutUrl)) {
        logoutUrl = "/login.jsp";
    }
    if (logoutUrl.toLowerCase().startsWith("http")) {
        return new RedirectResolution(logoutUrl, false);
    } else {
        return new RedirectResolution(logoutUrl);
    }
}

From source file:iddb.web.security.service.CommonUserService.java

protected void invalidateUserSession(HttpServletRequest request, HttpServletResponse response) {
    context.removeSubject();//from   w w  w .  j  a  v a 2  s.  c  o m
    String sessionKey = null;
    HttpSession session = request.getSession(false);
    if (session != null) {
        session.removeAttribute(UserService.SUBJECT);
        sessionKey = (String) session.getAttribute(UserService.SESSION_KEY);
        session.removeAttribute(UserService.SESSION_KEY);
    }
    // remove cookie
    Cookie cookie = new Cookie("iddb-u", "");
    cookie.setPath(request.getContextPath() + "/");
    cookie.setMaxAge(0);
    response.addCookie(cookie);

    cookie = new Cookie("iddb-k", "");
    cookie.setPath(request.getContextPath() + "/");
    cookie.setMaxAge(0);
    response.addCookie(cookie);

    if (sessionKey != null) {
        removeSession(sessionKey);
    }
}

From source file:eu.semlibproject.annotationserver.managers.CookiesManager.java

/**
 * Generate a new cookie for the annotation server
 * /* w w w  . jav a  2s .co  m*/
 * @param accessToken   the accessToken
 * @return              the new generated cookie
 */
public Cookie generateNewASCookie(String accessToken) {

    if (accessToken != null) {
        Cookie cookie = new Cookie(SemlibConstants.COOCKIE_NAME, accessToken);
        cookie.setComment(SemlibConstants.COOCKIE_DESCRIPTION);
        cookie.setPath(SemlibConstants.COOKIE_PATH);
        cookie.setMaxAge(SemlibConstants.COOKIE_TIME);
        cookie.setVersion(1);
        cookie.setSecure(false);

        return cookie;
    }

    return null;
}

From source file:csns.web.controller.SectionController.java

@RequestMapping("/section/taken")
public String taken(@RequestParam(required = false) Quarter quarter, ModelMap models, HttpSession session,
        HttpServletResponse response) {/*  ww  w. ja va  2  s  .c  o m*/
    Cookie cookie = new Cookie("default-home", "/section/taken");
    cookie.setPath("/");
    cookie.setMaxAge(100000000);
    response.addCookie(cookie);

    return list("taken", quarter, models, session);
}

From source file:csns.web.controller.SectionController.java

@RequestMapping("/section/taught")
public String taught(@RequestParam(required = false) Quarter quarter, ModelMap models, HttpSession session,
        HttpServletResponse response) {//from w  w  w.  j a va 2 s .  c  o m
    Cookie cookie = new Cookie("default-home", "/section/taught");
    cookie.setPath("/");
    cookie.setMaxAge(100000000);
    response.addCookie(cookie);

    return list("taught", quarter, models, session);
}

From source file:MyServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>");
    out.println("A Web Page");
    out.println("</TITLE>");
    out.println("</HEAD>");
    out.println("<BODY");

    Cookie[] cookies = request.getCookies();
    boolean foundCookie = false;

    for (int i = 0; i < cookies.length; i++) {
        Cookie cookie1 = cookies[i];
        if (cookie1.getName().equals("color")) {
            out.println("bgcolor = " + cookie1.getValue());
            foundCookie = true;//  w  w  w  .  ja  v a 2  s  .  c  o  m
        }
    }

    if (!foundCookie) {
        Cookie cookie1 = new Cookie("color", "cyan");
        cookie1.setMaxAge(24 * 60 * 60);
        response.addCookie(cookie1);
    }

    out.println(">");
    out.println("<H1>Setting and Reading Cookies</H1>");
    out.println("This page will set its background color using a cookie when reloaded.");
    out.println("</BODY>");
    out.println("</HTML>");
}

From source file:csns.web.controller.SectionController.java

@RequestMapping("/section/evaluated")
public String evaluated(@RequestParam(required = false) Quarter quarter, ModelMap models, HttpSession session,
        HttpServletResponse response) {//from  ww  w . j  a v a2 s . c  o  m
    Cookie cookie = new Cookie("default-home", "/section/evaluated");
    cookie.setPath("/");
    cookie.setMaxAge(100000000);
    response.addCookie(cookie);

    return list("evaluated", quarter, models, session);
}

From source file:com.xpn.xwiki.web.XWikiServletResponse.java

public void addCookie(String cookieName, String cookieValue, int age) {
    Cookie cookie = new Cookie(cookieName, cookieValue);
    cookie.setVersion(1);// w  w  w.  ja va  2  s.  com
    cookie.setMaxAge(age);
    this.response.addCookie(cookie);
}

From source file:com.xpn.xwiki.web.XWikiServletResponse.java

/**
 * Remove a cookie.//from  ww  w .ja va2s.  c  om
 *
 * @param request The servlet request needed to find the cookie to remove
 * @param cookieName The name of the cookie that must be removed.
 */
@Override
public void removeCookie(String cookieName, XWikiRequest request) {
    Cookie cookie = request.getCookie(cookieName);
    if (cookie != null) {
        cookie.setMaxAge(0);
        cookie.setPath(cookie.getPath());
        addCookie(cookie);
    }
}