Refine search
/** * 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; }
final String name = checkNotNull(cookie, "cookie").name(); final String value = cookie.value() != null ? cookie.value() : ""; validateCookie(name, value);
/** * Batch encodes cookies into Set-Cookie header values. * * @param cookies a bunch of cookies * @return the corresponding bunch of Set-Cookie headers */ @Deprecated public static List<String> encode(Iterable<Cookie> cookies) { return io.netty.handler.codec.http.cookie.ServerCookieEncoder.LAX.encode(cookies); }
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; } }
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); }
tokenCookie.setPath("/"); long nowSeconds = TimeUnit.MICROSECONDS.toSeconds(Utils.getSystemNowMicrosUtc()); long maxAge = expirationTime - nowSeconds; tokenCookie.setMaxAge(maxAge > 0 ? maxAge : 0); tokenCookie.setHttpOnly(true); String tokenCookieString = ServerCookieEncoder.LAX.encode(tokenCookie); response.headers().add(Operation.SET_COOKIE_HEADER, tokenCookieString);
@Test public void testRequestIsPrepared(TestContext context) { prepareServer(context, req -> { req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(new DefaultCookie("test", "toast"))); }); Async async = context.async(); Cookie c = new DefaultCookie("test", "localhost"); c.setPath("/"); client.cookieStore().remove(c); r.send(ar -> {
public void handle(Message msg, Session sess) throws IOException { Message res = new Message(); res.setId(msg.getId()); res.setHeader("location", "/?cmd=login"); Cookie cookie = new DefaultCookie(Protocol.TOKEN, ""); cookie.setMaxAge(0); res.setHeader("Set-Cookie", ServerCookieEncoder.STRICT.encode(cookie)); res.setStatus(302); sess.write(res); } };
/** * Encodes the specified cookie name-value pair into a Set-Cookie header value. * * @param name the cookie name * @param value the cookie value * @return a single Set-Cookie header value */ @Deprecated public static String encode(String name, String value) { return io.netty.handler.codec.http.cookie.ServerCookieEncoder.LAX.encode(name, value); }
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; } }
/** * Invoked when the SAML authentication process is finished and a user is authenticated. You can get * information about the authenticated user from the {@link Response}, especially his or her login name. * In this example, an email address is used as a login name. The login name is transferred to a web * browser via {@code Set-Cookie} header. */ @Override public HttpResponse loginSucceeded(ServiceRequestContext ctx, AggregatedHttpMessage req, MessageContext<Response> message, @Nullable String sessionIndex, @Nullable String relayState) { final String username = getNameId(message.getMessage(), SamlNameIdFormat.EMAIL).map(NameIDType::getValue) .orElse(null); if (username == null) { return HttpResponse.of(HttpStatus.UNAUTHORIZED, MediaType.HTML_UTF_8, "<html><body>Username is not found.</body></html>"); } logger.info("{} user '{}' has been logged in.", ctx, username); final Cookie cookie = new DefaultCookie("username", username); cookie.setHttpOnly(true); cookie.setDomain("localhost"); cookie.setMaxAge(60); cookie.setPath("/"); return HttpResponse.of( HttpHeaders.of(HttpStatus.OK) .contentType(MediaType.HTML_UTF_8) .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie)), HttpData.ofUtf8("<html><body onLoad=\"window.location.href='/welcome'\"></body></html>")); }
final String name = checkNotNull(cookie, "cookie").name(); final String value = cookie.value() != null ? cookie.value() : ""; validateCookie(name, value);
/** * 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(Collection<? extends Cookie> cookies) { if (checkNotNull(cookies, "cookies").isEmpty()) { return Collections.emptyList(); } List<String> encoded = new ArrayList<String>(cookies.size()); Map<String, Integer> nameToIndex = strict && cookies.size() > 1 ? new HashMap<String, Integer>() : null; int i = 0; boolean hasDupdName = false; for (Cookie c : cookies) { encoded.add(encode(c)); if (nameToIndex != null) { hasDupdName |= nameToIndex.put(c.name(), i++) != null; } } return hasDupdName ? dedup(encoded, nameToIndex) : encoded; }
/** * Encodes the specified cookie into a Set-Cookie header value. * * @param cookie the cookie * @return a single Set-Cookie header value */ @Deprecated public static String encode(Cookie cookie) { return io.netty.handler.codec.http.cookie.ServerCookieEncoder.LAX.encode(cookie); }
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; } }
final String name = checkNotNull(cookie, "cookie").name(); final String value = cookie.value() != null ? cookie.value() : ""; validateCookie(name, value);
/** * 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(Iterable<? extends Cookie> cookies) { Iterator<? extends Cookie> cookiesIt = checkNotNull(cookies, "cookies").iterator(); if (!cookiesIt.hasNext()) { return Collections.emptyList(); } List<String> encoded = new ArrayList<String>(); Cookie firstCookie = cookiesIt.next(); Map<String, Integer> nameToIndex = strict && cookiesIt.hasNext() ? new HashMap<String, Integer>() : null; int i = 0; encoded.add(encode(firstCookie)); boolean hasDupdName = nameToIndex != null && nameToIndex.put(firstCookie.name(), i++) != null; while (cookiesIt.hasNext()) { Cookie c = cookiesIt.next(); encoded.add(encode(c)); if (nameToIndex != null) { hasDupdName |= nameToIndex.put(c.name(), i++) != null; } } return hasDupdName ? dedup(encoded, nameToIndex) : encoded; } }