@Test public void testCookieBuilder() { // given LocalDateTime now = LocalDateTime.now(); // when Cookie cookie = CookieBuilder.create() .name("foo") .value("bar") .path("/foobar") .domain("www.foo.com") .maxAge(1223) .expires(now) .discard(true) .secure(true) .httpOnly(true) .build(); // then assertThat(cookie, not(nullValue())); assertThat("foo", equalTo(cookie.getName())); assertThat("bar", equalTo(cookie.getValue())); assertThat("/foobar", equalTo(cookie.getPath())); assertThat("www.foo.com", equalTo(cookie.getDomain())); assertThat( Date.from(now.atZone(ZoneId.systemDefault()).toInstant()), equalTo(cookie.getExpires())); assertThat(cookie.getMaxAge(), equalTo(1223)); assertThat(cookie.isDiscard(), equalTo(true)); assertThat(cookie.isSecure(), equalTo(true)); assertThat(cookie.isHttpOnly(), equalTo(true)); }
@Override protected void _initCookieMap() { Map<String, Cookie> cookies = hse.getRequestCookies(); if (cookies.isEmpty()) { return; } for (Map.Entry<String, io.undertow.server.handlers.Cookie> entry : cookies.entrySet()) { io.undertow.server.handlers.Cookie cookie = entry.getValue(); try { _setCookie(cookie.getName(), CookieConverter.undertow2osgl(cookie)); } catch (IllegalArgumentException e) { // Ignore bad cookie } } }
private static String getCookieString(final Cookie cookie) { switch (cookie.getVersion()) { case 0: return addVersion0ResponseCookieToExchange(cookie); case 1: default: return addVersion1ResponseCookieToExchange(cookie); } }
private static String addVersion0ResponseCookieToExchange(final Cookie cookie) { final StringBuilder header = new StringBuilder(cookie.getName()); header.append("="); header.append(cookie.getValue()); if (cookie.getPath() != null) { header.append("; path="); header.append(cookie.getPath()); } if (cookie.getDomain() != null) { header.append("; domain="); header.append(cookie.getDomain()); } if (cookie.isSecure()) { header.append("; secure"); } if (cookie.isHttpOnly()) { header.append("; HttpOnly"); } if (cookie.getExpires() != null) { header.append("; Expires="); header.append(DateUtils.toOldCookieDateString(cookie.getExpires())); } else if (cookie.getMaxAge() != null) { if (cookie.getMaxAge() >= 0) { header.append("; Max-Age="); header.append(cookie.getMaxAge()); } if (cookie.getMaxAge() == 0) { Date expires = new Date(); expires.setTime(0); header.append("; Expires="); header.append(DateUtils.toOldCookieDateString(expires)); } else if (cookie.getMaxAge() > 0) { Date expires = new Date(); expires.setTime(expires.getTime() + cookie.getMaxAge() * 1000); header.append("; Expires="); header.append(DateUtils.toOldCookieDateString(expires)); } } return header.toString(); }
private static String addVersion1ResponseCookieToExchange(final Cookie cookie) { final StringBuilder header = new StringBuilder(cookie.getName()); header.append("="); header.append(cookie.getValue()); header.append("; Version=1"); if (cookie.getPath() != null) { header.append("; Path="); header.append(cookie.getPath()); } if (cookie.getDomain() != null) { header.append("; Domain="); header.append(cookie.getDomain()); } if (cookie.isDiscard()) { header.append("; Discard"); } if (cookie.isSecure()) { header.append("; Secure"); } if (cookie.isHttpOnly()) { header.append("; HttpOnly"); } if (cookie.getMaxAge() != null) { if (cookie.getMaxAge() >= 0) { header.append("; Max-Age="); header.append(cookie.getMaxAge()); } } if (cookie.getExpires() != null) { header.append("; Expires="); header.append(DateUtils.toDateString(cookie.getExpires())); } return header.toString(); }