Example usage for javax.servlet.http Cookie getMaxAge

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

Introduction

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

Prototype

public int getMaxAge() 

Source Link

Document

Gets the maximum age in seconds of this Cookie.

Usage

From source file:org.jasig.cas.web.support.CookieRetrievingCookieGeneratorTests.java

public void testCookieAddWithRememberMe() {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(RememberMeCredentials.REQUEST_PARAMETER_REMEMBER_ME, "true");
    final MockHttpServletResponse response = new MockHttpServletResponse();

    this.g.addCookie(request, response, "test");

    final Cookie c = response.getCookie("test");
    assertEquals(100, c.getMaxAge());
    assertEquals("test", c.getValue());
}

From source file:docs.security.RememberMeSecurityConfigurationTests.java

@Test
public void authenticateWhenSpringSessionRememberMeEnabledThenCookieMaxAgeAndSessionExpirationSet()
        throws Exception {
    // @formatter:off
    MvcResult result = this.mockMvc.perform(formLogin()).andReturn();
    // @formatter:on

    Cookie cookie = result.getResponse().getCookie("SESSION");
    assertThat(cookie.getMaxAge()).isEqualTo(Integer.MAX_VALUE);
    T session = this.sessions.getSession(cookie.getValue());
    assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo((int) TimeUnit.DAYS.toSeconds(30));

}

From source file:com.enonic.cms.business.resolver.ForceResolverValueServiceImpl.java

private String getForcedResolverValueFromCookie(HttpServletRequest request, String forcedValueKey) {

    Cookie cookie = CookieUtil.getCookie(request, forcedValueKey);

    return cookie == null || cookie.getMaxAge() == 0 ? null : decodeCookieValue(cookie.getValue());
}

From source file:com.haulmont.cuba.web.sys.AppCookies.java

public int getCookieMaxAge(String name) {
    Cookie cookie = getCookieIfEnabled(name);
    return cookie == null ? 0 : cookie.getMaxAge();
}

From source file:Cookies.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html");
    req.getSession();//from   w  w w .  j  a va 2  s .  c o  m
    PrintWriter out = resp.getWriter();
    Cookie cookies[] = req.getCookies();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet Cookie Information</title>");
    out.println("</head>");
    out.println("<body>");

    if ((cookies == null) || (cookies.length == 0)) {
        out.println("<center><h1>No Cookies found</h1>");
    } else {

        out.println("<center><h1>Cookies found</h1>");
        out.println("<table border>");
        out.println("<tr><th>Name</th><th>Value</th>" + "<th>Comment</th><th>Max Age</th></tr>");

        for (int i = 0; i < cookies.length; i++) {
            Cookie c = cookies[i];
            out.println("<tr><td>" + c.getName() + "</td><td>" + c.getValue() + "</td><td>" + c.getComment()
                    + "</td><td>" + c.getMaxAge() + "</td></tr>");
        }

        out.println("</table></center>");
    }
    out.println("</body>");
    out.println("</html>");
    out.flush();
}

From source file:org.codemucker.testserver.capturing.CapturedCookie.java

public CapturedCookie(Cookie c) {
    domain = c.getDomain();//from  www  .ja va 2  s  .  com
    path = c.getPath();
    name = c.getName();
    value = c.getValue();
    secure = c.getSecure();
    maxAge = c.getMaxAge();
    version = c.getVersion();
}

From source file:org.ngrinder.infra.spring.UserHandlerMethodArgumentResolver.java

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    User currentUser = getUserContext().getCurrentUser();

    String userParam = webRequest.getParameter("ownerId");
    if (StringUtils.isNotBlank(userParam) && currentUser.getRole().hasPermission(Permission.SWITCH_TO_ANYONE)) {
        return getUserService().getOne(userParam);
    }//  w  w w . ja va  2 s  . c o  m

    // User want to do something through other User status and this
    // switchUser is other user Id
    String switchUser = null;
    Cookie[] cookies = getCookies(webRequest);
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if ("switchUser".equals(cookie.getName()) && cookie.getMaxAge() != 0) {
                switchUser = cookie.getValue();
            }
        }
    }
    // Let this can be done with parameter as well.
    switchUser = StringUtils.defaultIfBlank(webRequest.getParameter("switchUser"), switchUser);

    if (currentUser.getUserId().equals(switchUser)) {
        currentUser.setOwnerUser(null);
    } else if (StringUtils.isNotEmpty(switchUser)) {
        User ownerUser = getUserService().getOne(switchUser);
        // CurrentUser should remember whose status he used
        if (currentUser.getRole().hasPermission(Permission.SWITCH_TO_ANYONE)
                || (ownerUser.getFollowers() != null && ownerUser.getFollowers().contains(currentUser))) {
            currentUser.setOwnerUser(ownerUser);
            return ownerUser;
        }
    } else if (StringUtils.isEmpty(switchUser)) {
        currentUser.setOwnerUser(null);
    }

    return currentUser.getFactualUser();
}

From source file:com.pureinfo.tgirls.sns.servlet.TestSNSEntryServlet.java

@Override
protected void doPost(HttpServletRequest _req, HttpServletResponse _resp) throws ServletException, IOException {
    System.out.println("==================test entry=====POST==============");

    try {// w ww  .  ja va 2 s.  c om
        String userId = _req.getParameter("id");
        if (StringUtils.isEmpty(userId)) {
            userId = "1";
        }

        System.out.println("----user id----" + userId);

        IUserMgr mgr = (IUserMgr) ArkContentHelper.getContentMgrOf(User.class);
        User u = mgr.getUserByTaobaoId(userId);

        System.out.println("user:::;" + u);

        addCookie(u, _req, _resp);

        Cookie[] cookies = _req.getCookies();

        if (cookies == null) {
            System.out.println("=====cookie is null=======");
        } else {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                System.out.println("cookie[" + i + "]:[" + cookie.getName() + ":" + cookie.getValue() + "("
                        + cookie.getMaxAge() + ")]");
            }

        }

        int i = new Random().nextInt();
        Cookie c = new Cookie("topsessionid", "abc" + i);

        _resp.addCookie(c);

        System.out.println("========================");

        //            Cookie[] cs = _req.getCookies();
        //             for (int ii = 0; ii < cs.length; ii++) {
        //                 Cookie cc = cs[ii];
        //                 System.out.println("cookie[" + cc.getName() + "]:" + cc.getValue());
        //             }

        //_resp.sendRedirect(_req.getContextPath() + "/index.html");

        RequestDispatcher rd = _req.getRequestDispatcher("/index.html");
        rd.forward(_req, _resp);

        //_req.getSession().setAttribute(ArkHelper.ATTR_LOGIN_USER, u);
        // _resp.sendRedirect(_req.getContextPath());
        //            _req.getCookies()[0].
    } catch (PureException e) {
        // TODO Auto-generated catch block
        e.printStackTrace(System.err);
    }

}

From source file:org.tonguetied.web.CookieUtilsTest.java

/**
 * Test method for {@link org.tonguetied.web.CookieUtils#createCookie(HttpServletRequest, String, String)}.
 *///w w  w  .  j a  v a  2 s .  co m
@Test
public final void testCreateCookie() {
    Cookie cookie = CookieUtils.createCookie(request, "name", "value");
    assertEquals("name", cookie.getName());
    assertEquals("value", cookie.getValue());
    assertEquals("/test", cookie.getPath());
    assertEquals(-1, cookie.getMaxAge());
}

From source file:com.redhat.rhn.frontend.servlets.test.PxtSessionDelegateImplTest.java

public final void testInvalidatePxtSessionDeletesPxtCookie() {
    setUpInvalidatePxtSession();// w ww. ja va2 s .  c  om

    Constraint deletePxtCookie = new Constraint() {
        public StringBuffer describeTo(StringBuffer description) {
            return description.append("cookie ").append("max age must = 0");
        }

        public boolean eval(Object arg) {
            Cookie pxtCookie = (Cookie) arg;

            return pxtCookie.getMaxAge() == 0;
        }
    };

    mockResponse.expects(once()).method("addCookie").with(deletePxtCookie);

    pxtSessionDelegate.invalidatePxtSession(getRequest(), getResponse());
}