Example usage for io.netty.handler.codec.http DefaultCookie DefaultCookie

List of usage examples for io.netty.handler.codec.http DefaultCookie DefaultCookie

Introduction

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

Prototype

public DefaultCookie(String name, String value) 

Source Link

Document

Creates a new cookie with the specified name and value.

Usage

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

License:Apache License

void createSession(HttpResponse response, boolean admin) {
    String sessionId = new BigInteger(130, secureRandom).toString(32);
    updateSessionExpiration(sessionId, admin);
    Cookie cookie = new DefaultCookie("GLOWROOT_SESSION_ID", sessionId);
    cookie.setHttpOnly(true);/*from w  w w.  j  a  va  2  s  . co m*/
    cookie.setPath("/");
    response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
    purgeExpiredSessions();
}

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

License:Apache License

void deleteSessionCookie(HttpResponse response) {
    Cookie cookie = new DefaultCookie("GLOWROOT_SESSION_ID", "");
    cookie.setHttpOnly(true);//from   w  w w  .  j  a  va 2  s  . c o  m
    cookie.setMaxAge(0);
    cookie.setPath("/");
    response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
}

From source file:org.iotivity.cloud.base.HttpClient.java

License:Open Source License

public void connect(String strUrl) throws URISyntaxException, InterruptedException, SSLException {
    URI uri = new URI(strUrl);

    String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
    String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();

    int port = uri.getPort();

    if (port == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = 80;// w  w w.j ava 2  s  .c om
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }

    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        return;
    }

    final boolean ssl = "https".equalsIgnoreCase(scheme);
    final SslContext sslCtx;

    if (ssl) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }

    EventLoopGroup group = new NioEventLoopGroup();

    try {
        Bootstrap b = new Bootstrap();
        b.group(group);
        b.channel(NioSocketChannel.class);
        b.handler(new HttpClientInitializer(sslCtx));

        Channel ch = b.connect(host, port).sync().channel();

        HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
                uri.getRawPath());
        request.headers().set(HttpHeaders.Names.HOST, host);
        request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
        request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

        request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder
                .encode(new DefaultCookie("my-cookie", "foo"), new DefaultCookie("another-cookie", "bar")));

        ch.writeAndFlush(request);

        ch.closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:org.nosceon.titanite.Cookie.java

License:Apache License

public Cookie(String name, String value) {
    this.c = new DefaultCookie(name, value);
}

From source file:org.ratpackframework.http.internal.DefaultResponse.java

License:Apache License

public Cookie cookie(String name, String value) {
    Cookie cookie = new DefaultCookie(name, value);
    getCookies().add(cookie);
    return cookie;
}

From source file:org.ratpackframework.session.internal.ExchangeSessionManager.java

License:Apache License

private void setCookie(String value, int expiryMins) {
    DefaultCookie cookie = new DefaultCookie(COOKIE_NAME, value);

    String cookieDomain = sessionManager.getCookieDomain();
    if (cookieDomain != null) {
        cookie.setDomain(cookieDomain);//from   w ww  . j a v a 2 s.  co m
    }

    String cookiePath = sessionManager.getCookiePath();
    if (cookiePath != null) {
        cookie.setPath(cookiePath);
    }

    if (expiryMins > 0) {
        cookie.setMaxAge(expiryMins * 60);
    }

    exchange.getResponse().getCookies().add(cookie);
}

From source file:org.ratpackframework.session.internal.RequestSessionManager.java

License:Apache License

private void setCookie(String value, int expiryMins) {
    DefaultCookie cookie = new DefaultCookie(COOKIE_NAME, value);

    String cookieDomain = sessionManager.getCookieDomain();
    if (cookieDomain != null) {
        cookie.setDomain(cookieDomain);//from  ww  w. j a  v  a2s  .  co  m
    }

    String cookiePath = sessionManager.getCookiePath();
    if (cookiePath != null) {
        cookie.setPath(cookiePath);
    }

    if (expiryMins > 0) {
        cookie.setMaxAge(expiryMins * 60);
    }

    context.getResponse().getCookies().add(cookie);
}

From source file:org.waarp.gateway.ftp.adminssl.HttpSslHandler.java

License:Open Source License

private void checkAuthent(ChannelHandlerContext ctx) {
    newSession = true;//www  . ja v  a 2  s. c o m
    if (request.method() == HttpMethod.GET) {
        String logon = Logon();
        responseContent.append(logon);
        clearSession();
        writeResponse(ctx);
        return;
    } else if (request.method() == HttpMethod.POST) {
        getParams();
        if (params == null) {
            String logon = Logon();
            responseContent.append(logon);
            clearSession();
            writeResponse(ctx);
            return;
        }
    }
    boolean getMenu = false;
    if (params.containsKey("Logon")) {
        String name = null, password = null;
        List<String> values = null;
        if (!params.isEmpty()) {
            // get values
            if (params.containsKey("name")) {
                values = params.get("name");
                if (values != null) {
                    name = values.get(0);
                    if (name == null || name.length() == 0) {
                        getMenu = true;
                    }
                }
            } else {
                getMenu = true;
            }
            // search the nb param
            if ((!getMenu) && params.containsKey("passwd")) {
                values = params.get("passwd");
                if (values != null) {
                    password = values.get(0);
                    if (password == null || password.length() == 0) {
                        getMenu = true;
                    } else {
                        getMenu = false;
                    }
                } else {
                    getMenu = true;
                }
            } else {
                getMenu = true;
            }
        } else {
            getMenu = true;
        }
        if (!getMenu) {
            logger.debug("Name=" + name + " vs "
                    + name.equals(FileBasedConfiguration.fileBasedConfiguration.ADMINNAME) + " Passwd="
                    + password + " vs "
                    + FileBasedConfiguration.fileBasedConfiguration.checkPassword(password));
            if (name.equals(FileBasedConfiguration.fileBasedConfiguration.ADMINNAME)
                    && FileBasedConfiguration.fileBasedConfiguration.checkPassword(password)) {
                authentHttp.specialNoSessionAuth(FileBasedConfiguration.fileBasedConfiguration.HOST_ID);
            } else {
                getMenu = true;
            }
            if (!authentHttp.isIdentified()) {
                logger.debug("Still not authenticated: {}", authentHttp);
                getMenu = true;
            }
            // load DbSession
            if (this.dbSession == null) {
                try {
                    if (DbConstant.admin.isActive) {
                        this.dbSession = new DbSession(DbConstant.admin, false);
                        DbAdmin.nbHttpSession++;
                        this.isPrivateDbSession = true;
                    }
                } catch (WaarpDatabaseNoConnectionException e1) {
                    // Cannot connect so use default connection
                    logger.warn("Use default database connection");
                    this.dbSession = DbConstant.admin.session;
                }
            }
        }
    } else {
        getMenu = true;
    }
    if (getMenu) {
        String logon = Logon();
        responseContent.append(logon);
        clearSession();
        writeResponse(ctx);
    } else {
        String index = index();
        responseContent.append(index);
        clearSession();
        admin = new DefaultCookie(FTPSESSION,
                FileBasedConfiguration.fileBasedConfiguration.HOST_ID + Long.toHexString(random.nextLong()));
        sessions.put(admin.value(), this.authentHttp);
        if (this.isPrivateDbSession) {
            dbSessions.put(admin.value(), dbSession);
        }
        logger.debug("CreateSession: " + uriRequest + ":{}", admin);
        writeResponse(ctx);
    }
}

From source file:org.wisdom.engine.wrapper.cookies.CookieHelper.java

License:Apache License

/**
 * Converts the Wisdom's cookie to a Netty's cookie.
 *
 * @param cookie the Wisdom's cookie//from   w ww.ja v  a2  s. c  o m
 * @return the Netty's cookie with the same metadata and content than the input cookie.
 */
public static io.netty.handler.codec.http.Cookie convertWisdomCookieToNettyCookie(Cookie cookie) {
    io.netty.handler.codec.http.Cookie nettyCookie = new DefaultCookie(cookie.name(), cookie.value());
    nettyCookie.setMaxAge(cookie.maxAge());
    if (cookie.comment() != null) {
        nettyCookie.setComment(cookie.comment());
    }
    if (cookie.domain() != null) {
        nettyCookie.setDomain(cookie.domain());
    }
    if (cookie.isSecure()) {
        nettyCookie.setSecure(true);
    }
    if (cookie.path() != null) {
        nettyCookie.setPath(cookie.path());
    }
    if (cookie.isHttpOnly()) {
        nettyCookie.setHttpOnly(true);
    }
    return nettyCookie;
}

From source file:ratpack.test.http.internal.DefaultTestHttpClient.java

License:Apache License

public List<Cookie> getCookies() {
    List<Cookie> clonedList = new ArrayList<>();
    if (cookies != null) {
        cookies.stream().forEach(c -> clonedList.add(new DefaultCookie(c.name(), c.value())));
    }/* w  w w .  j av a 2 s .c  o  m*/
    return clonedList;
}