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:cec.easyshop.storefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testClientSideCookieDefaultPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(false);//client side
    cookieGenerator.addCookie(response, "cookie_monster");
    final Cookie expectedCookie = new Cookie(JSESSIONID, "cookie_monster");
    expectedCookie.setPath("/");
    expectedCookie.setSecure(false);//from  w  ww  . jav  a2 s  .co m
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

    Mockito.verify(response).addCookie(Mockito.argThat(new CookieArgumentMatcher(expectedCookie)));
    assertNoHeaderAdjustments();
}

From source file:io.mapzone.controller.vm.http.LoginProvision.java

protected void registerUser(String userId, @SuppressWarnings("hiding") HttpServletResponse response) {
    // cookie token
    byte[] bytes = new byte[8];
    rand.nextBytes(bytes);//from   w  w  w  .j a  va  2  s  .co m
    String token = Base64.encodeBase64URLSafeString(bytes);

    // FIXME Leak: entries are never removed (allow just one cookie/session per user?)
    if (loggedIn.putIfAbsent(token, userId) != null) {
        throw new IllegalStateException("Token already exists: " + token);
    }

    // set cookie
    Cookie newCookie = new Cookie(COOKIE_NAME, token);
    newCookie.setHttpOnly(true);
    newCookie.setPath(COOKIE_PATH);
    newCookie.setSecure(false); // XXX
    newCookie.setMaxAge(COOKIE_MAX_AGE);
    response.addCookie(newCookie);
}

From source file:com.persistent.cloudninja.controller.LogoutFilter.java

private void removeCookie(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
        String authCookieName) {//from www  .  j  a  v a2 s.  co  m

    Cookie[] cookies = httpServletRequest.getCookies();
    Cookie currentCookie = null;

    if (cookies != null && cookies.length > 0) {
        for (Cookie c : cookies) {
            if (authCookieName.equals(c.getName())) {
                currentCookie = c;
                currentCookie.setMaxAge(0);
                currentCookie.setValue("");
                currentCookie.setPath("/");

                httpServletResponse.addCookie(currentCookie);
            }

        }
    }

}

From source file:org.apereo.portal.url.UrlCanonicalizingFilter.java

protected void setRedirectCount(HttpServletRequest request, HttpServletResponse response, int count) {
    final Cookie cookie = new Cookie(COOKIE_NAME, Integer.toString(count));
    cookie.setPath(request.getContextPath());
    cookie.setMaxAge(30);
    response.addCookie(cookie);//from  w w  w  . j a v a2s  .c  o m
}

From source file:com.salesmanager.core.util.www.SalesManagerInterceptor.java

private MerchantStore setMerchantStore(HttpServletRequest req, HttpServletResponse resp, String merchantId)
        throws Exception {

    // different merchantId
    int iMerchantId = 1;

    try {/*from w ww .j  ava 2 s  .  c o  m*/
        iMerchantId = Integer.parseInt(merchantId);
    } catch (Exception e) {
        log.error("Cannot parse merchantId to Integer " + merchantId);
    }

    // get MerchantStore
    MerchantService mservice = (MerchantService) ServiceFactory.getService(ServiceFactory.MerchantService);
    MerchantStore mStore = mservice.getMerchantStore(iMerchantId);

    if (mStore == null) {
        // forward to error page
        log.error("MerchantStore does not exist for merchantId " + merchantId);
        return null;
    }

    req.getSession().setAttribute("STORE", mStore);
    req.setAttribute("STORE", mStore);

    //get store configuration for template
    ReferenceService rservice = (ReferenceService) ServiceFactory.getService(ServiceFactory.ReferenceService);
    Map storeConfiguration = rservice.getModuleConfigurationsKeyValue(mStore.getTemplateModule(),
            mStore.getCountry());

    if (storeConfiguration != null) {
        req.getSession().setAttribute("STORECONFIGURATION", storeConfiguration);
    }

    Cookie c = new Cookie("STORE", merchantId);
    c.setMaxAge(365 * 24 * 60 * 60);
    resp.addCookie(c);

    if (!RefCache.isLoaded()) {
        RefCache.createCache();
    }

    return mStore;

}

From source file:com.tenduke.example.scribeoauth.SessionManager.java

/**
 * Creates an authenticated session.//from   ww  w .  j a  v  a 2  s . c om
 * @param request Client HTTP request.
 * @param response HTTP response.
 * @param user User information as a JSON object.
 */
public void createSession(final HttpServletRequest request, final HttpServletResponse response,
        final JSONObject user) {
    //
    final String sessionId = UUID.randomUUID().toString();
    final SessionInformation sessionInfo = new SessionInformation(sessionId, user);
    //
    final String cookieValue = MessageFormat.format(SESSION_COOKIE_PATTERN, sessionId,
            resolveUserProfileId(user));
    //
    setSessionInformation(sessionInfo);
    //
    final Cookie cookie = new Cookie(SIGNED_SESSION_COOKIE_NAME, cookieValue);
    cookie.setMaxAge(COOKIE_MAX_AGE);
    cookie.setPath("/");
    response.addCookie(cookie);
}

From source file:net.longfalcon.web.BaseController.java

protected void setCookies(User user, HttpServletResponse httpServletResponse) {
    long userId = user.getId();
    String idh = EncodingUtil.sha1Hash(user.getUserseed() + String.valueOf(userId));
    Cookie uidCookie = new Cookie("uid", String.valueOf(userId));
    uidCookie.setMaxAge(2592000);
    Cookie idhCookie = new Cookie("idh", idh);
    idhCookie.setMaxAge(2592000);// w w  w  . j ava 2s.  c om
    httpServletResponse.addCookie(uidCookie);
    httpServletResponse.addCookie(idhCookie);
}

From source file:cec.easyshop.storefront.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(response, "cookie_monster");

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

    Mockito.verify(response).addHeader(EnhancedCookieGenerator.HEADER_COOKIE,
            "JSESSIONID=cookie_monster; Version=1; Domain=\"what a domain\"; Path=/some_path; HttpOnly");
}

From source file:com.tenduke.example.webadmin.SessionManager.java

/**
 * Creates an authenticated session./*from   w ww  .j a  v a  2  s.  c  o  m*/
 * @param request Client HTTP request.
 * @param response HTTP response.
 * @param user User information as a JSON object.
 * @return Session information object as result.
 */
public SessionInformation createSession(final HttpServletRequest request, final HttpServletResponse response,
        final JSONObject user) {
    //
    final String sessionId = UUID.randomUUID().toString();
    final SessionInformation sessionInfo = new SessionInformation(sessionId, user);
    //
    final String cookieValue = MessageFormat.format(SESSION_COOKIE_PATTERN, sessionId,
            resolveUserProfileId(user));
    //
    setSessionInformation(sessionInfo);
    //
    final Cookie cookie = new Cookie(SIGNED_SESSION_COOKIE_NAME, cookieValue);
    cookie.setMaxAge(COOKIE_MAX_AGE);
    cookie.setPath("/");
    response.addCookie(cookie);
    //
    return sessionInfo;
}

From source file:com.acc.storefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testServerSideCookieDefaultPath() {
    cookieGenerator.setCookieName("guid");
    cookieGenerator.setHttpOnly(true);//server side

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

    cookieGenerator.addCookie(response, "cookie_monster");

    final Cookie expectedCookie = new Cookie("guid", "cookie_monster");
    expectedCookie.setPath("/");
    expectedCookie.setSecure(false);//from   ww w  .j  a va 2s  .  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,
            "guid=cookie_monster; Domain=\"what a domain\"; Path=/; HttpOnly");

}