@Override public MultiValueMap<String, ResponseCookie> getCookies() { MultiValueMap<String, ResponseCookie> result = new LinkedMultiValueMap<>(); this.response.cookies().values().stream().flatMap(Collection::stream) .forEach(cookie -> result.add(cookie.name(), ResponseCookie.from(cookie.name(), cookie.value()) .domain(cookie.domain()) .path(cookie.path()) .maxAge(cookie.maxAge()) .secure(cookie.isSecure()) .httpOnly(cookie.isHttpOnly()) .build())); return CollectionUtils.unmodifiableMultiValueMap(result); }
@Override public Response cookie(@NonNull com.blade.mvc.http.Cookie cookie) { Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(cookie.name(), cookie.value()); if (cookie.domain() != null) { nettyCookie.setDomain(cookie.domain()); } if (cookie.maxAge() > 0) { nettyCookie.setMaxAge(cookie.maxAge()); } nettyCookie.setPath(cookie.path()); nettyCookie.setHttpOnly(cookie.httpOnly()); nettyCookie.setSecure(cookie.secure()); this.cookies.add(nettyCookie); return this; }
@Override public Response cookie(@NonNull String name, @NonNull String value, int maxAge, boolean secured) { Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, value); nettyCookie.setPath("/"); nettyCookie.setMaxAge(maxAge); nettyCookie.setSecure(secured); this.cookies.add(nettyCookie); return this; }
@Override public Response cookie(@NonNull String name, @NonNull String value, int maxAge) { Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, value); nettyCookie.setPath("/"); nettyCookie.setMaxAge(maxAge); this.cookies.add(nettyCookie); return this; }
/** * Batch encodes cookies into Set-Cookie header values. * * @param cookies a bunch of cookies * @return the corresponding bunch of Set-Cookie headers */ public List<String> encode(Cookie... cookies) { if (checkNotNull(cookies, "cookies").length == 0) { return Collections.emptyList(); } List<String> encoded = new ArrayList<String>(cookies.length); Map<String, Integer> nameToIndex = strict && cookies.length > 1 ? new HashMap<String, Integer>() : null; boolean hasDupdName = false; for (int i = 0; i < cookies.length; i++) { Cookie c = cookies[i]; encoded.add(encode(c)); if (nameToIndex != null) { hasDupdName |= nameToIndex.put(c.name(), i) != null; } } return hasDupdName ? dedup(encoded, nameToIndex) : encoded; }
private void returnMultipleCookiesEvenIfTheyHaveSameName() { CookieStore store = new ThreadSafeCookieStore(); store.add(Uri.create("http://foo.com"), ClientCookieDecoder.LAX.decode("JSESSIONID=FOO; Domain=.foo.com")); store.add(Uri.create("http://sub.foo.com"), ClientCookieDecoder.LAX.decode("JSESSIONID=BAR; Domain=sub.foo.com")); Uri uri1 = Uri.create("http://sub.foo.com"); List<Cookie> cookies1 = store.get(uri1); assertTrue(cookies1.size() == 2); assertTrue(cookies1.stream().filter(c -> c.value().equals("FOO") || c.value().equals("BAR")).count() == 2); String result = ClientCookieEncoder.LAX.encode(cookies1.get(0), cookies1.get(1)); assertTrue(result.equals("JSESSIONID=FOO; JSESSIONID=BAR")); }
CookieBasedSsoHandler(String cookieName, String cookieValue) { requireNonNull(cookieName, "cookieName"); requireNonNull(cookieValue, "cookieValue"); final Cookie cookie = new DefaultCookie(cookieName, cookieValue); cookie.setDomain(spHostname); cookie.setPath("/"); cookie.setHttpOnly(true); setCookie = ServerCookieEncoder.STRICT.encode(cookie); }
private boolean cookieHeaderAlreadyExists(HttpResponse response, Cookie cookieValue) { List<String> setCookieHeaders = response.getHeader(SET_COOKIE.toString()); for (String setCookieHeader : setCookieHeaders) { String existingCookieName = ClientCookieDecoder.LAX.decode(setCookieHeader).name(); String existingCookieValue = ClientCookieDecoder.LAX.decode(setCookieHeader).value(); if (existingCookieName.equalsIgnoreCase(cookieValue.getName().getValue()) && existingCookieValue.equalsIgnoreCase(cookieValue.getValue().getValue())) { return true; } } return false; }
private void add(String requestDomain, String requestPath, Cookie cookie) { AbstractMap.SimpleEntry<String, Boolean> pair = cookieDomain(cookie.domain(), requestDomain); String keyDomain = pair.getKey(); boolean hostOnly = pair.getValue(); String keyPath = cookiePath(cookie.path(), requestPath); CookieKey key = new CookieKey(cookie.name().toLowerCase(), keyDomain, keyPath); if (hasCookieExpired(cookie, 0)) cookieJar.remove(key); else cookieJar.put(key, new StoredCookie(cookie, hostOnly, cookie.maxAge() != Cookie.UNDEFINED_MAX_AGE)); }
@Override public int compareTo(Cookie c) { int v = name().compareTo(c.name()); if (v != 0) { return v; } if (path() == null) { if (c.path() != null) { return -1; } } else if (c.path() == null) { return 1; } else { v = path().compareTo(c.path()); if (v != 0) { return v; } } if (domain() == null) { if (c.domain() != null) { return -1; } } else if (c.domain() == null) { return 1; } else { v = domain().compareToIgnoreCase(c.domain()); return v; } return 0; }
private void shouldServeCookiesBasedOnTheUriScheme() { CookieStore store = new ThreadSafeCookieStore(); store.add(Uri.create("https://foo.org/moodle/"), ClientCookieDecoder.LAX.decode("cookie1=VALUE1; Path=/")); store.add(Uri.create("https://foo.org:443/moodle/login"), ClientCookieDecoder.LAX.decode("cookie1=VALUE2; Path=/")); store.add(Uri.create("https://foo.org:443/moodle/login"), ClientCookieDecoder.LAX.decode("cookie1=VALUE3; Path=/; Secure")); Uri uri = Uri.create("https://foo.org/moodle/login"); assertTrue(store.getAll().size() == 1); assertTrue(store.get(uri).get(0).value().equals("VALUE3")); assertTrue(store.get(uri).get(0).isSecure()); }
@Override public Response removeCookie(@NonNull String name) { Optional<Cookie> cookieOpt = this.cookies.stream().filter(cookie -> cookie.name().equals(name)).findFirst(); cookieOpt.ifPresent(cookie -> { cookie.setValue(""); cookie.setMaxAge(-1); }); Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, ""); nettyCookie.setMaxAge(-1); this.cookies.add(nettyCookie); return this; }
static MultiMap removeCookieHeaders(MultiMap headers) { // We don't want to remove the JSESSION cookie. String cookieHeader = headers.get(COOKIE); if (cookieHeader != null) { headers.remove(COOKIE); Set<Cookie> nettyCookies = ServerCookieDecoder.STRICT.decode(cookieHeader); for (Cookie cookie: nettyCookies) { if (cookie.name().equals("JSESSIONID")) { headers.add(COOKIE, ServerCookieEncoder.STRICT.encode(cookie)); break; } } } return headers; } }
@Test public void should_set_and_recognize_cookie_with_path() throws IOException { runWithConfiguration("cookie.json"); Cookie decodeCookie = getCookie("/cookie-with-path"); assertThat(decodeCookie.name(), is("login")); assertThat(decodeCookie.value(), is("true")); assertThat(decodeCookie.path(), is("/")); }
@Test public void should_set_and_recognize_cookie_with_domain() throws IOException { runWithConfiguration("cookie.json"); Cookie decodeCookie = getCookie("/cookie-with-domain"); assertThat(decodeCookie.name(), is("login")); assertThat(decodeCookie.value(), is("true")); assertThat(decodeCookie.domain(), is("github.com")); }
@Test public void should_set_and_recognize_cookie_with_max_age() throws IOException { runWithConfiguration("cookie.json"); Cookie decodeCookie = getCookie("/cookie-with-max-age"); assertThat(decodeCookie.name(), is("login")); assertThat(decodeCookie.value(), is("true")); assertThat(decodeCookie.maxAge(), is(3600L)); }
@Test public void should_set_and_recognize_cookie_with_secure() throws IOException { runWithConfiguration("cookie.json"); Cookie decodeCookie = getCookie("/cookie-with-secure"); assertThat(decodeCookie.name(), is("login")); assertThat(decodeCookie.value(), is("true")); assertThat(decodeCookie.isSecure(), is(true)); }
@Test public void should_set_and_recognize_cookie_with_http_only() throws IOException { runWithConfiguration("cookie.json"); Cookie decodeCookie = getCookie("/cookie-with-http-only"); assertThat(decodeCookie.name(), is("login")); assertThat(decodeCookie.value(), is("true")); assertThat(decodeCookie.isHttpOnly(), is(true)); }