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.codename1.corsproxy.CORSProxy.java

@Override
protected void copyProxyCookie(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
        Header header) {/*from  w w w  . ja  va  2  s  .c  om*/
    List<HttpCookie> cookies = HttpCookie.parse(header.getValue());
    String path = servletRequest.getContextPath(); // path starts with / or is empty string
    path += servletRequest.getServletPath(); // servlet path starts with / or is empty string

    for (HttpCookie cookie : cookies) {
        //set cookie name prefixed w/ a proxy value so it won't collide w/ other cookies
        String proxyCookieName = getCookieNamePrefix() + cookie.getName();
        Cookie servletCookie = new Cookie(proxyCookieName, cookie.getValue());
        servletCookie.setComment(cookie.getComment());
        servletCookie.setMaxAge((int) cookie.getMaxAge());
        servletCookie.setPath(path); //set to the path of the proxy servlet
        // don't set cookie domain
        //servletCookie.setSecure(cookie.getSecure());
        servletCookie.setSecure(false);
        servletCookie.setVersion(cookie.getVersion());
        servletResponse.addCookie(servletCookie);
    }
}

From source file:am.ik.categolj2.app.authentication.AuthenticationHelper.java

void saveUserInformationInCookie(String username, String firstName, String lastName, String email,
        OAuth2AccessToken accessToken, HttpServletResponse response) throws UnsupportedEncodingException {
    try {/*from   w  ww.j a  va2  s.  c om*/
        Cookie cookie = new Cookie(Categolj2Cookies.USER_COOKIE,
                objectMapper.writeValueAsString(new UserInfo(username, firstName, lastName, email)));

        cookie.setMaxAge(getRefreshTokenMaxAge(accessToken));
        response.addCookie(cookie);
    } catch (JsonProcessingException e) {
        logger.error("JSON conversion failed!", e);
    }
}

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

@Test
public void testClientSideCookieDynamicPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(false);//client side
    cookieGenerator.setCookieSecure(true);
    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(true);//ww w. j  a v  a 2  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:com.acc.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);//  ww 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:memedb.httpd.MemeDBHandler.java

private void addCredentialedCookie(HttpServletResponse response, Credentials cred) {
    Cookie cookie = new Cookie(COOKIE_ID, cred.getToken());
    cookie.setMaxAge(24 * 60 * 60); // max time is one day... afterwhich, it needs to reauth.
    response.addCookie(cookie);/* ww w . j a va 2 s .com*/
}

From source file:com.laxser.blitz.web.var.FlashImpl.java

public void writeNewMessages() {
    if (logger.isDebugEnabled()) {
        logger.debug("writeNextMessages");
    }/*ww  w  . jav a 2 s  .co m*/
    HttpServletResponse response = invocation.getResponse();
    List<String> responseCookies = null;
    for (Map.Entry<String, String> entry : next.entrySet()) {
        if (responseCookies == null) {
            responseCookies = new ArrayList<String>(next.size());
        }
        String cookieValue;
        if (entry.getValue() == null) {
            cookieValue = "";
        } else {
            try {
                cookieValue = base64.encodeToString(entry.getValue().getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                throw new Error(e);
            }
        }
        Cookie cookie = new Cookie(cookiePrefix + entry.getKey(), cookieValue);
        cookie.setPath("/");
        cookie.setMaxAge(1);
        response.addCookie(cookie);
        responseCookies.add(cookie.getName());
        if (logger.isDebugEnabled()) {
            logger.debug("write flash cookie:" + cookie.getName() + "=" + cookie.getValue());
        }
    }
    for (Map.Entry<String, String> entry : last.entrySet()) {
        if (responseCookies == null || !responseCookies.contains(entry.getKey())) {
            Cookie c = new Cookie(entry.getKey(), null);
            c.setMaxAge(0);
            c.setPath("/");
            response.addCookie(c);
            if (logger.isDebugEnabled()) {
                logger.debug("delete flash cookie:" + c.getName() + "=" + c.getValue());
            }
        }
    }
}

From source file:com.epam.cme.storefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testClientSideCookieDynamicPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(false);// client side
    cookieGenerator.setCookieSecure(true);
    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(true);//from   w  w w.j ava2s.c  o m
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

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

}

From source file:com.sinosoft.one.mvc.web.var.FlashImpl.java

protected synchronized void readLastMessages() {
    if (lastRead) {
        return;//from   w ww .j ava  2 s .  co m
    }
    lastRead = true;
    if (logger.isDebugEnabled()) {
        logger.debug("readLastMessages");
    }
    Cookie[] cookies = invocation.getRequest().getCookies();
    for (int i = 0; cookies != null && i < cookies.length; i++) {
        if (logger.isDebugEnabled()) {
            logger.debug("cookie " + cookies[i].getName() + "=" + cookies[i].getValue() + "; age="
                    + cookies[i].getMaxAge());
        }
        if (cookies[i].getValue() == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("ignore cookie: " + cookies[i].getName());
            }
            continue;
        }
        if (cookies[i].getName().startsWith(cookiePrefix)) {
            StringTokenizer st = new StringTokenizer(cookies[i].getName(), DELIM);
            String[] splits = new String[st.countTokens()];
            for (int j = 0; j < splits.length; j++) {
                splits[j] = st.nextToken();
            }
            if (splits.length < 2) {
                if (logger.isInfoEnabled()) {
                    logger.info("ignore flash cookie: " + cookies[i].getName());
                }
                continue;
            }
            String name = splits[1];
            String cookieValue = cookies[i].getValue();
            String flashMessage;
            if (cookieValue.length() == 0) {
                flashMessage = "";
            } else {
                try {
                    flashMessage = new String(base64.decodeFromString(cookieValue), "UTF-8");
                } catch (Exception e) {
                    logger.error("failed to decode '" + cookieValue + "' as" + " a base64 string", e);
                    flashMessage = cookieValue;
                }
            }
            if (last.size() == 0) {
                last = new LinkedHashMap<String, String>();
            }
            this.last.put(name, flashMessage);
            Cookie cookie = new Cookie(cookies[i].getName(), "");
            cookie.setPath("/");
            cookie.setMaxAge(0);
            invocation.getResponse().addCookie(cookie);
            if (logger.isDebugEnabled()) {
                logger.debug("found flash message:" + name + "=" + flashMessage);
            }
        }
    }
}

From source file:com.epam.cme.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(request, response, "cookie_monster");

    final Cookie expectedCookie = new Cookie(JSESSIONID, "cookie_monster");
    expectedCookie.setPath("/some_path");
    expectedCookie.setSecure(false);/*from ww  w .java  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:de.hybris.platform.ytelcoacceleratorstorefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testClientSideCookieDynamicPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(false);//client side
    cookieGenerator.setCookieSecure(true);
    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(true);//from   ww w.ja  va 2s .c  om
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

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

}