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 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);//  w  ww.  jav  a  2  s. 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.acc.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   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.companyname.services.OnLoginSuccessHandler.java

private Cookie createCookie(HttpServletRequest request, String name, String value) {
    logger.info("create a new token with name: " + name);
    Cookie cookie = new Cookie(name, value);
    cookie.setDomain(getCookieDomain());
    cookie.setPath(getCookiePath(request));
    cookie.setMaxAge(getCookieExpireTimeLength());
    return cookie;
}

From source file:com.qut.middleware.esoe.sso.plugins.post.handler.impl.PostLogicImpl.java

private void sendPostResponseDocument(SSOProcessorData data) throws PostBindingException {
    String remoteAddress = data.getHttpRequest().getRemoteAddr();
    try {/*from   w ww.ja  v a2s . c o m*/
        HttpServletResponse response = data.getHttpResponse();
        PrintWriter writer = response.getWriter();

        response.setContentType("text/html");

        /* Set cookie to allow javascript enabled browsers to auto submit, ensures navigation with the back button is not broken
         * because auto submit is active only when this cookie exists, and the submit javascript removes it */
        Cookie autoSubmit = new Cookie("esoeAutoSubmit", "enabled");
        autoSubmit.setMaxAge(172800); //set expiry to be 48 hours just to make sure we still work with badly configured clocks skewed from GMT
        autoSubmit.setPath("/");
        response.addCookie(autoSubmit);

        this.logger.debug("[SSO for {}] Cookie added. About to check for response document.", remoteAddress); //$NON-NLS-1$

        if (data.getResponseDocument() == null) {
            this.logger.error(
                    "[SSO for {}] No response document was generated. Unable to respond to HTTP-POST binding request.", //$NON-NLS-1$
                    remoteAddress);
            throw new PostBindingException(
                    "No response document was generated. Unable to respond to HTTP-POST binding request.");
        }

        // TODO relaystate
        String responseRelayState = "";// = data.getRelayState();
        if (responseRelayState == null)
            responseRelayState = new String("");

        /* Encode SAML Response in base64 */
        byte[] samlResponseEncoded = Base64.encodeBase64(data.getResponseDocument()); //$NON-NLS-1$
        Object[] responseArgs = new Object[] { data.getResponseEndpoint(), new String(samlResponseEncoded),
                responseRelayState };
        String htmlOutput = this.samlMessageFormat.format(responseArgs);

        this.logger.debug(
                "[SSO for {}] Writing HTML document, response for HTTP-POST request. Length: {} bytes",
                remoteAddress, htmlOutput.length());

        this.logger.trace("[SSO for {}] Writing HTML document. Content:\n{}", remoteAddress, htmlOutput);

        writer.print(htmlOutput);
        writer.flush();
    } catch (IOException e) {
        this.logger.error(
                "[SSO for {}] I/O exception occurred trying to write the HTTP response. Unable to respond with HTTP-POST binding. Error was: {}",
                remoteAddress, e.getMessage());
        throw new PostBindingException(
                "I/O exception occurred trying to write the HTTP response. Unable to respond with HTTP-POST binding.",
                e);
    }
}

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

    final Cookie expectedCookie = new Cookie("guid", "cookie_monster");
    expectedCookie.setPath("/");
    expectedCookie.setSecure(false);/* w w w.  ja v  a 2  s. c  om*/
    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");

}

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

@Test
public void testClientSideCookieDefaultPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(false);// client side

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

    final Cookie expectedCookie = new Cookie(JSESSIONID, "cookie_monster");
    expectedCookie.setPath("/");
    expectedCookie.setSecure(false);//from ww  w.  j  a  va2  s .  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:de.hybris.platform.ytelcoacceleratorstorefront.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(request, response, "cookie_monster");

    final Cookie expectedCookie = new Cookie("guid", "cookie_monster");
    expectedCookie.setPath("/");
    expectedCookie.setSecure(false);// w  ww . ja v a  2 s . com
    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");

}

From source file:de.hybris.platform.ytelcoacceleratorstorefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testClientSideCookieDefaultPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(false);//client side

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

    final Cookie expectedCookie = new Cookie(JSESSIONID, "cookie_monster");
    expectedCookie.setPath("/");
    expectedCookie.setSecure(false);//from w ww  .  jav  a2s  . 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:am.ik.categolj2.app.authentication.AuthenticationHelper.java

void saveAccessTokenInCookie(OAuth2AccessToken accessToken, HttpServletResponse response)
        throws UnsupportedEncodingException {
    Cookie accessTokenValueCookie = new Cookie(Categolj2Cookies.ACCESS_TOKEN_VALUE_COOKIE,
            URLEncoder.encode(accessToken.getValue(), "UTF-8"));
    accessTokenValueCookie.setMaxAge(accessToken.getExpiresIn());
    Cookie accessTokenExpireCookie = new Cookie(Categolj2Cookies.ACCESS_TOKEN_EXPIRATION_COOKIE,
            URLEncoder.encode(String.valueOf(accessToken.getExpiration().getTime()), "UTF-8"));
    accessTokenExpireCookie.setMaxAge(accessToken.getExpiresIn());

    response.addCookie(accessTokenValueCookie);
    response.addCookie(accessTokenExpireCookie);

    OAuth2RefreshToken refreshToken = accessToken.getRefreshToken();
    if (refreshToken != null) {
        Cookie refreshTokenCookie = new Cookie(Categolj2Cookies.REFRESH_TOKEN_VALUE_COOKIE,
                URLEncoder.encode(refreshToken.getValue(), "UTF-8"));
        refreshTokenCookie.setMaxAge(getRefreshTokenMaxAge(accessToken));
        response.addCookie(refreshTokenCookie);
    }//w w w  .j a v a 2s  .co  m
}

From source file:org.bibsonomy.webapp.util.CookieLogic.java

/** Adds a cookie to the response. Sets default values for path and maxAge. 
 * /*from  w ww  .j a  v  a 2  s . co m*/
 * @param key - The key identifying this cookie.
 * @param value - The value of the cookie.
 */
private void addCookie(final String key, final String value) {
    log.debug("Adding cookie " + key + ": " + value);
    final Cookie cookie = new Cookie(key, value);
    cookie.setPath(cookiePath);
    cookie.setMaxAge(cookieAge);
    responseLogic.addCookie(cookie);
}