Example usage for io.netty.handler.codec.http.cookie Cookie setPath

List of usage examples for io.netty.handler.codec.http.cookie Cookie setPath

Introduction

In this page you can find the example usage for io.netty.handler.codec.http.cookie Cookie setPath.

Prototype

void setPath(String path);

Source Link

Document

Sets the path of this Cookie .

Usage

From source file:cn.wantedonline.puppy.httpserver.httptools.CookieHelper.java

License:Apache License

public static void addCookie(String key, String value, int maxAge, HttpResponse response) {
    Cookie cookie = new DefaultCookie(key, value);
    cookie.setDomain(domain);/*from   w  w  w  .ja  va  2 s . co  m*/
    cookie.setPath("/");
    cookie.setMaxAge(maxAge);
    response.addCookie(cookie);
}

From source file:cn.wantedonline.puppy.httpserver.httptools.CookieHelper.java

License:Apache License

public static void addSessionCookie(String sessionId, int maxAge, HttpResponse response) {
    Cookie cookie = new DefaultCookie(HttpServerConfig.SESSIONID_PARAMERTER, sessionId);
    cookie.setDomain(domain);/*  w w w .j  av a2 s  .c o m*/
    cookie.setPath("/");
    cookie.setMaxAge(maxAge);
    response.addCookie(cookie);
}

From source file:com.bay1ts.bay.core.Response.java

License:Apache License

/**
 * Adds cookie to the response. Can be invoked multiple times to insert more than one cookie.
 *
 * @param path     path of the cookie/*from w  w  w  . j a v a 2 s  . c  o  m*/
 * @param name     name of the cookie
 * @param value    value of the cookie
 * @param maxAge   max age of the cookie in seconds (negative for the not persistent cookie, zero - deletes the cookie)
 * @param secured  if true : cookie will be secured
 * @param httpOnly if true: cookie will be marked as http only
 */
public void cookie(String path, String name, String value, int maxAge, boolean secured, boolean httpOnly) {
    Cookie cookie = new DefaultCookie(name, value);
    cookie.setPath(path);
    cookie.setMaxAge(maxAge);
    cookie.setSecure(secured);
    cookie.setHttpOnly(httpOnly);
    response.headers().set(HttpHeaderNames.COOKIE, ServerCookieEncoder.STRICT.encode(cookie));
}

From source file:io.vertx.ext.web.client.SessionAwareWebClientTest.java

License:Open Source License

public void testReceiveAndSendCookie(TestContext context, String pathToCall, String cookiePath) {
    prepareServer(context, req -> {/*from  w  w  w.j  a  v  a  2 s  . c  o  m*/
        req.response().setChunked(true);
        Cookie c = new DefaultCookie("test", "toast");
        c.setPath(cookiePath);
        req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(c));
        c = getCookieValue(req, "test");
        if (c != null) {
            req.response().write("OK");
        } else {
            req.response().write("ERR");
        }
    });

    HttpRequest<Buffer> req = client.get(PORT, "localhost", pathToCall);

    {
        Async async = context.async();
        req.send(ar -> {
            context.assertTrue(ar.succeeded());
            async.complete();
        });
        async.await();
    }

    Async async = context.async();
    req.send(ar -> {
        context.assertTrue(ar.succeeded());
        HttpResponse<Buffer> res = ar.result();
        context.assertEquals(200, res.statusCode());
        context.assertEquals("OK", res.bodyAsString());
        async.complete();
    });
}

From source file:io.vertx.ext.web.client.SessionAwareWebClientTest.java

License:Open Source License

@Test
public void testSharedWebClient(TestContext context) {
    AtomicInteger cnt = new AtomicInteger(0);
    prepareServer(context, req -> {//from   w  w  w . ja v  a2 s . c om
        req.response().setChunked(true);
        Cookie c = getCookieValue(req, "test");
        if (c != null) {
            req.response().write("OK");
        } else {
            c = new DefaultCookie("test", "" + cnt.getAndIncrement());
            c.setPath("/");
            req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(c));
            req.response().write("ERR");
        }
    });

    int numClients = 4;

    WebClientSession[] clients = IntStream.range(0, numClients)
            .mapToObj(val -> val == 0 ? client : buildClient(plainWebClient, CookieStore.build()))
            .toArray(WebClientSession[]::new);

    Async async = context.async(clients.length);
    for (WebClientSession client : clients) {
        HttpRequest<Buffer> req = client.get(PORT, "localhost", "/index.html");

        Async waiter = context.async();
        req.send(ar -> {
            context.assertTrue(ar.succeeded());
            waiter.complete();
        });
        waiter.await();

        req.send(ar -> {
            context.assertTrue(ar.succeeded());
            HttpResponse<Buffer> res = ar.result();
            context.assertEquals(200, res.statusCode());
            context.assertEquals("OK", res.bodyAsString());
            async.countDown();
        });
    }

    async.await();

    cnt.set(0);
    for (WebClientSession client : clients) {
        Iterable<Cookie> cookies = client.cookieStore().get(false, "localhost", "/");

        int i = 0;
        for (Cookie c : cookies) {
            context.assertEquals("" + cnt.getAndIncrement(), c.value());
            i++;
        }

        context.assertEquals(i, 1);
    }
}

From source file:io.vertx.ext.web.client.SessionAwareWebClientTest.java

License:Open Source License

@Test
public void testRequestIsPrepared(TestContext context) {
    prepareServer(context, req -> {//  ww  w.j a v  a 2 s . c o m
        req.response().headers().add("set-cookie",
                ServerCookieEncoder.STRICT.encode(new DefaultCookie("test", "toast")));
    });

    Consumer<HttpRequest<Buffer>> check = r -> {
        Async async = context.async();
        Cookie c = new DefaultCookie("test", "localhost");
        c.setPath("/");
        client.cookieStore().remove(c);
        r.send(ar -> {
            async.complete();
            validate(context, client.cookieStore().get(false, "localhost", "/"), new String[] { "test" },
                    new String[] { "toast" });
        });
        async.await();
    };

    check.accept(client.delete("/"));
    check.accept(client.delete("localhost", "/"));
    check.accept(client.delete(PORT, "localhost", "/"));
    check.accept(client.deleteAbs("http://localhost/"));
    check.accept(client.get("/"));
    check.accept(client.get("localhost", "/"));
    check.accept(client.get(PORT, "localhost", "/"));
    check.accept(client.getAbs("http://localhost/"));
    check.accept(client.head("/"));
    check.accept(client.head("localhost", "/"));
    check.accept(client.head(PORT, "localhost", "/"));
    check.accept(client.headAbs("http://localhost/"));
    check.accept(client.patch("/"));
    check.accept(client.patch("localhost", "/"));
    check.accept(client.patch(PORT, "localhost", "/"));
    check.accept(client.patchAbs("http://localhost/"));
    check.accept(client.post("/"));
    check.accept(client.post("localhost", "/"));
    check.accept(client.post(PORT, "localhost", "/"));
    check.accept(client.postAbs("http://localhost/"));
    check.accept(client.put("/"));
    check.accept(client.put("localhost", "/"));
    check.accept(client.put(PORT, "localhost", "/"));
    check.accept(client.putAbs("http://localhost/"));
    check.accept(client.request(HttpMethod.GET, new RequestOptions()));
    check.accept(client.request(HttpMethod.GET, "/"));
    check.accept(client.request(HttpMethod.GET, "localhost", "/"));
    check.accept(client.request(HttpMethod.GET, PORT, "localhost", "/"));
    check.accept(client.requestAbs(HttpMethod.GET, "http://localhost/"));
}

From source file:io.vertx.ext.web.client.SessionAwareWebClientTest.java

License:Open Source License

@Test
public void testCookieStore(TestContext context) {
    CookieStore store = CookieStore.build();
    Cookie c;

    c = new DefaultCookie("a", "1");
    store.put(c);//from w ww.j av a 2s .co m

    c = new DefaultCookie("b", "2");
    c.setDomain("vertx.io");
    store.put(c);

    c = new DefaultCookie("c", "3");
    c.setDomain("www.vertx.io");
    c.setPath("/web-client");
    store.put(c);

    c = new DefaultCookie("d", "4");
    c.setPath("/web-client");
    store.put(c);

    c = new DefaultCookie("e", "5");
    c.setDomain("vertx.io");
    c.setSecure(true);
    store.put(c);

    c = new DefaultCookie("b", "20");
    c.setDomain("www.vertx.io");
    store.put(c);

    c = new DefaultCookie("b", "200");
    c.setDomain("www.vertx.io");
    c.setPath("/web-client");
    store.put(c);

    validate(context, store.get(false, "www.vertx.io", "/"), new String[] { "a", "b" },
            new String[] { "1", "20" });
    validate(context, store.get(false, "a.www.vertx.io", "/"), new String[] { "a", "b" },
            new String[] { "1", "20" });
    validate(context, store.get(false, "test.vertx.io", "/"), new String[] { "a", "b" },
            new String[] { "1", "2" });
    validate(context, store.get(false, "www.vertx.io", "/web-client"), new String[] { "a", "b", "c", "d" },
            new String[] { "1", "200", "3", "4" });
    validate(context, store.get(true, "test.vertx.io", "/"), new String[] { "a", "b", "e" },
            new String[] { "1", "2", "5" });
}

From source file:nikoladasm.aspark.ResponseImpl.java

License:Open Source License

@Override
public void cookie(String path, String name, String value, int maxAge, boolean secured, boolean httpOnly) {
    Cookie cookie = new DefaultCookie(name, value);
    cookie.setPath(path);
    cookie.setMaxAge(maxAge);/* w w w.j  a  v  a 2 s.c  o  m*/
    cookie.setSecure(secured);
    cookie.setHttpOnly(httpOnly);
    cookies.put(name, cookie);
}

From source file:org.glowroot.ui.HttpSessionManager.java

License:Apache License

void deleteSessionCookie(CommonResponse response) throws Exception {
    Cookie cookie = new DefaultCookie(configRepository.getWebConfig().sessionCookieName(), "");
    cookie.setHttpOnly(true);/* w  ww .j a  va  2  s  .co m*/
    cookie.setMaxAge(0);
    cookie.setPath("/");
    response.setHeader(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(cookie));
}

From source file:org.glowroot.ui.HttpSessionManager.java

License:Apache License

private CommonResponse createSession(String username, Set<String> roles, boolean ldap) throws Exception {
    String sessionId = new BigInteger(130, secureRandom).toString(32);
    ImmutableSession session = ImmutableSession.builder().caseAmbiguousUsername(username).ldap(ldap)
            .roles(roles).lastRequest(clock.currentTimeMillis()).build();
    sessionMap.put(sessionId, session);// w ww  . j a  v a  2s  .  c o  m

    String layoutJson = layoutService.getLayoutJson(session.createAuthentication(central, configRepository));
    CommonResponse response = new CommonResponse(OK, MediaType.JSON_UTF_8, layoutJson);
    Cookie cookie = new DefaultCookie(configRepository.getWebConfig().sessionCookieName(), sessionId);
    cookie.setHttpOnly(true);
    cookie.setPath("/");
    response.setHeader(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(cookie));
    purgeExpiredSessions();

    auditSuccessfulLogin(username);
    return response;
}