@Override public final void visit(final Cookie cookie) { cookie.setPath(path); } }
@Override public Cookie path(String path) { nettyCookie.setPath(path); 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; }
@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; }
@Override public Response cookie(@NonNull String path, @NonNull String name, @NonNull String value, int maxAge, boolean secured) { Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, value); nettyCookie.setMaxAge(maxAge); nettyCookie.setSecure(secured); nettyCookie.setPath(path); this.cookies.add(nettyCookie); return this; }
@Override public Response cookie(@NonNull String path, @NonNull String name, @NonNull String value, int maxAge, boolean secured) { Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, value); nettyCookie.setMaxAge(maxAge); nettyCookie.setSecure(secured); nettyCookie.setPath(path); 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, 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; }
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); }
/** * 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>")); }
@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 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; }
@Test public void testAddOrReplaceCookies() { RequestBuilder requestBuilder = new RequestBuilder(); Cookie cookie = new DefaultCookie("name", "value"); cookie.setDomain("google.com"); cookie.setPath("/"); cookie.setMaxAge(1000); cookie.setSecure(true); cookie.setHttpOnly(true); requestBuilder.addOrReplaceCookie(cookie); assertEquals(requestBuilder.cookies.size(), 1, "cookies size should be 1 after adding one cookie"); assertEquals(requestBuilder.cookies.get(0), cookie, "cookie does not match"); Cookie cookie2 = new DefaultCookie("name", "value"); cookie2.setDomain("google2.com"); cookie2.setPath("/path"); cookie2.setMaxAge(1001); cookie2.setSecure(false); cookie2.setHttpOnly(false); requestBuilder.addOrReplaceCookie(cookie2); assertEquals(requestBuilder.cookies.size(), 1, "cookies size should remain 1 as we just replaced a cookie with same name"); assertEquals(requestBuilder.cookies.get(0), cookie2, "cookie does not match"); Cookie cookie3 = new DefaultCookie("name2", "value"); cookie3.setDomain("google.com"); cookie3.setPath("/"); cookie3.setMaxAge(1000); cookie3.setSecure(true); cookie3.setHttpOnly(true); requestBuilder.addOrReplaceCookie(cookie3); assertEquals(requestBuilder.cookies.size(), 2, "cookie size must be 2 after adding 1 more cookie i.e. cookie3"); }
@Override public Cookie setPath(final String path) { nettyCookie.setPath(path); this.changed = true; return this; }
@Override protected void applyCookies() { for (String name : getCookies().keySet()) { for (ResponseCookie httpCookie : getCookies().get(name)) { Cookie cookie = new DefaultCookie(name, httpCookie.getValue()); if (!httpCookie.getMaxAge().isNegative()) { cookie.setMaxAge(httpCookie.getMaxAge().getSeconds()); } if (httpCookie.getDomain() != null) { cookie.setDomain(httpCookie.getDomain()); } if (httpCookie.getPath() != null) { cookie.setPath(httpCookie.getPath()); } cookie.setSecure(httpCookie.isSecure()); cookie.setHttpOnly(httpCookie.isHttpOnly()); this.response.addCookie(cookie); } } }
@Override protected void applyCookies() { for (String name : getCookies().keySet()) { for (ResponseCookie httpCookie : getCookies().get(name)) { Cookie cookie = new DefaultCookie(name, httpCookie.getValue()); if (!httpCookie.getMaxAge().isNegative()) { cookie.setMaxAge(httpCookie.getMaxAge().getSeconds()); } if (httpCookie.getDomain() != null) { cookie.setDomain(httpCookie.getDomain()); } if (httpCookie.getPath() != null) { cookie.setPath(httpCookie.getPath()); } cookie.setSecure(httpCookie.isSecure()); cookie.setHttpOnly(httpCookie.isHttpOnly()); this.response.addCookie(cookie); } } }
@Test public void getWithCookies() throws Throwable { withClient().run(client -> withServer(server).run(server -> { final Cookie coo = new DefaultCookie("foo", "value"); coo.setDomain("/"); coo.setPath("/"); server.enqueueEcho(); client.prepareGet(getTargetUrl()) .addCookie(coo) .execute(new AsyncCompletionHandlerAdapter() { @Override public Response onCompleted(Response response) { assertEquals(response.getStatusCode(), 200); List<Cookie> cookies = response.getCookies(); assertEquals(cookies.size(), 1); assertEquals(cookies.get(0).toString(), "foo=value"); return response; } }).get(TIMEOUT, SECONDS); })); }
@Override public Cookie setPath(final String path) { nettyCookie.setPath(path); this.changed = true; 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; }
@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; }