Example usage for javax.servlet.http Cookie Cookie

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

Introduction

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

Prototype

public Cookie(String name, String value) 

Source Link

Document

Constructs a cookie with the specified name and value.

Usage

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);/*from  w  ww .j a v  a 2s .  com*/
    cookie.setMaxAge(age);
    this.response.addCookie(cookie);
}

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);/* w w  w  .  j av  a  2s .  co  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:org.jasig.portlet.test.mvc.tests.CookieTestController.java

/**
* Generates a new cookie with random name and value.
* 
* @param response//  w w  w.j a va  2 s  . co m
*/
@ActionMapping(value = "randomCookieAction")
protected void createRandomCookie(ActionRequest request, ActionResponse response) {
    final String name = RandomStringUtils.randomAlphabetic(8);
    final String value = RandomStringUtils.randomAlphanumeric(8);
    Cookie cookie = new Cookie(name, value);
    cookie.setComment("Random Cookie Test comment");
    cookie.setMaxAge(-1);
    cookie.setSecure(request.isSecure());
    response.addProperty(cookie);

}

From source file:com.sse.abtester.VariantSelectionFilterTest.java

/**
 * Test updates vm if cookie./*from  w ww . ja  v a2  s .  c  om*/
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws ServletException the servlet exception
 */
@Test
public void testUpdatesVMIfCookie() throws IOException, ServletException {
    VariantBean testvariant = mock(VariantBean.class);
    IVariationStrategy forward = new UrlRewrite();

    when(testvariant.getKey()).thenReturn(TESTKEY);
    when(testvariant.getVariationStrategy()).thenReturn(forward);

    when(VM.enrollRequest(mockReq)).thenReturn(testvariant);

    mockReq.setCookies(new Cookie(VSKEY, "" + TESTKEY)); // set the test cookie

    vsf.doFilter(mockReq, mockRes, fc);

    verify(VM, times(0)).enrollRequest(mockReq);
    verify(VM).updateVariant("" + TESTKEY);
    verify(VM).publishVariationResponse(mockRes);
}

From source file:AIR.Common.Web.Session.MultiValueCookie.java

public Cookie getUnderlyingWebCookie() {

    serializeCookieValue();//from   w w  w. java  2s .c  om
    if (_cookie == null) {
        _cookie = new Cookie(this._name, this._encodedValue);
    } else
        _cookie.setValue(_encodedValue);

    if (_maxAge != null)
        _cookie.setMaxAge(_maxAge);

    if (StringUtils.isNotEmpty(_path))
        _cookie.setPath(_path);

    if (StringUtils.isNotEmpty(_comment))
        _cookie.setComment(_comment);

    if (StringUtils.isNotEmpty(_domain))
        _cookie.setDomain(_domain);

    if (_isSecure != null)
        _cookie.setSecure(_isSecure);

    if (_version != null)
        _cookie.setVersion(_version);

    return _cookie;
}

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);//from  ww  w  . jav  a 2s  . 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,
            "JSESSIONID=cookie_monster; Domain=\"what a domain\"; Path=/; HttpOnly");

}

From source file:org.craftercms.security.processors.impl.AddSecurityCookiesProcessorTest.java

@Test
public void testAddCookiesLoggedOut() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);
    RequestSecurityProcessor flushResponseProcessor = new RequestSecurityProcessor() {

        @Override/*from www . j a  v  a  2s .co  m*/
        public void processRequest(RequestContext context, RequestSecurityProcessorChain processorChain)
                throws Exception {
            context.getResponse().getOutputStream().flush();
        }

    };

    Cookie ticketCookie = new Cookie(SecurityUtils.TICKET_COOKIE_NAME, new ObjectId().toString());
    Cookie profileLastModifiedCookie = new Cookie(SecurityUtils.PROFILE_LAST_MODIFIED_COOKIE_NAME,
            String.valueOf(System.currentTimeMillis()));

    request.setCookies(ticketCookie, profileLastModifiedCookie);

    RequestSecurityProcessorChain chain = new RequestSecurityProcessorChainImpl(
            Arrays.asList(processor, flushResponseProcessor).iterator());

    processor.processRequest(context, chain);

    ticketCookie = response.getCookie(SecurityUtils.TICKET_COOKIE_NAME);

    assertNotNull(ticketCookie);
    assertEquals(null, ticketCookie.getValue());
    assertEquals(0, ticketCookie.getMaxAge());

    profileLastModifiedCookie = response.getCookie(SecurityUtils.PROFILE_LAST_MODIFIED_COOKIE_NAME);

    assertNotNull(profileLastModifiedCookie);
    assertEquals(null, profileLastModifiedCookie.getValue());
    assertEquals(0, profileLastModifiedCookie.getMaxAge());
}

From source file:org.sharetask.controller.UserController.java

@RequestMapping(value = "/login", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void performLogin(@RequestBody final UserPassword login, final HttpServletRequest request,
        final HttpServletResponse response) {
    final UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
            login.getUsername(), login.getPassword());
    try {/*w w w . j a  va2s  .  c  o  m*/
        final Authentication auth = authenticationManager.authenticate(token);
        SecurityContextHolder.getContext().setAuthentication(auth);
        repository.saveContext(SecurityContextHolder.getContext(), request, response);
        rememberMeServices.loginSuccess(request, response, auth);
        // language cookie
        final UserInfoDTO user = userService.read(SecurityUtil.getCurrentSignedInUsername());
        final Cookie locale = new Cookie(RequestUltil.LOCALE, user.getLanguage());
        locale.setMaxAge(-1);
        locale.setPath("/");
        response.addCookie(locale);
        response.setStatus(HttpStatus.OK.value());
    } catch (final BadCredentialsException ex) {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
    }
}

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;
}