Example usage for io.netty.handler.codec.http.cookie ClientCookieEncoder LAX

List of usage examples for io.netty.handler.codec.http.cookie ClientCookieEncoder LAX

Introduction

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

Prototype

ClientCookieEncoder LAX

To view the source code for io.netty.handler.codec.http.cookie ClientCookieEncoder LAX.

Click Source Link

Document

Lax instance that doesn't validate name and value, and (for methods that accept multiple cookies) keeps cookies in the order in which they were given.

Usage

From source file:io.gatling.http.client.RequestBuilder.java

License:Apache License

public Request build() {

    Uri fullUri = UriEncoder.uriEncoder(fixUrlEncoding).encode(uri, queryParams);

    if (!headers.contains(ACCEPT)) {
        headers.set(ACCEPT, ACCEPT_ALL_HEADER_VALUE);
    }/*from  w ww  . j  av a2 s .c om*/

    if (realm instanceof BasicRealm) {
        headers.add(AUTHORIZATION, ((BasicRealm) realm).getAuthorizationHeader());
    }

    String originalAcceptEncoding = headers.get(ACCEPT_ENCODING);
    if (originalAcceptEncoding != null) {
        // we don't support Brotly ATM
        headers.set(ACCEPT_ENCODING, filterOutBrotliFromAcceptEncoding(originalAcceptEncoding));
    }

    if (isNonEmpty(cookies)) {
        headers.set(COOKIE, ClientCookieEncoder.LAX.encode(cookies));
    }

    if (!headers.contains(ORIGIN)) {
        headers.set(ORIGIN, originHeader(uri));
    }

    if (!headers.contains(HOST)) {
        headers.set(HOST, virtualHost != null ? virtualHost : hostHeader(uri));
    }

    RequestBody<?> body = null;
    if (bodyBuilder != null) {
        Charset charset = defaultCharset;
        String contentType = headers.get(CONTENT_TYPE);
        if (contentType != null) {
            Charset contentTypeCharset = extractContentTypeCharsetAttribute(contentType);
            if (contentTypeCharset != null) {
                charset = contentTypeCharset;
            } else {
                // set Content-Type header missing charset attribute
                contentType = contentType + "; charset=" + charset.name();
            }
        }
        body = bodyBuilder.build(contentType, charset);
        String bodyContentType = body.getContentType();
        if (bodyContentType != null) {
            headers.set(CONTENT_TYPE, bodyContentType);
        }
    }

    return new Request(method, fullUri, headers, cookies != null ? cookies : Collections.emptyList(), body,
            requestTimeout, virtualHost, localAddress, realm, proxyServer, signatureCalculator, nameResolver,
            http2Enabled, alpnRequired, http2PriorKnowledge);
}

From source file:nettyTest.http.HttpHelloWorldClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    URI uri = new URI(URL);
    String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
    String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
    int port = uri.getPort();

    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        System.err.println("Only HTTP(S) is supported.");
        return;//  w w  w  .  j a  va2s. c  o m
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000)
                .handler(new HttpHelloWorldClientInitializer());

        // Make the connection attempt.
        Channel ch = b.connect(host, port).sync().channel();

        // Prepare the HTTP request.
        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);

        // Set some example cookies.
        request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX
                .encode(new DefaultCookie("my-cookie", "foo"), new DefaultCookie("another-cookie", "bar")));

        // Send the HTTP request.
        ch.writeAndFlush(request);

        // Wait for the server to close the connection.
        ch.closeFuture().sync();
    } finally {
        // Shut down executor threads to exit.
        group.shutdownGracefully();
    }
}

From source file:nettyTest.httpSsl.oneWay.HttpsHelloWorldClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    URI uri = new URI(URL);
    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;/*www .j  av a 2s .  c o  m*/
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }

    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        System.err.println("Only HTTP(S) is supported.");
        return;
    }

    // Configure SSL context if necessary.
    final boolean ssl = "https".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        File certificate = new File(
                HttpsHelloWorldServer.class.getClassLoader().getResource("nettyca/google.crt").getFile());
        //TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        //TrustManagerFactory instance = SimpleTrustManagerFactory.getInstance(SimpleTrustManagerFactory.getDefaultAlgorithm());
        TrustManagerFactory instance = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        //TrustManagerFactory instance = FingerprintTrustManagerFactory.getInstance(FingerprintTrustManagerFactory.getDefaultAlgorithm());
        KeyStore keyStore = null;
        instance.init(keyStore);
        TrustManager[] tms = instance.getTrustManagers();
        //instance.init(null);
        sslCtx = SslContextBuilder.forClient().trustManager(instance).build();
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class)
                .handler(new HttpsHelloWorldClientInitializer(sslCtx, host, port));

        // Make the connection attempt.
        Channel ch = b.connect(host, port).sync().channel();

        // Prepare the HTTP request.
        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);

        // Set some example cookies.
        request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX
                .encode(new DefaultCookie("my-cookie", "foo"), new DefaultCookie("another-cookie", "bar")));

        // Send the HTTP request.
        ch.writeAndFlush(request);

        // Wait for the server to close the connection.
        ch.closeFuture().sync();
    } finally {
        // Shut down executor threads to exit.
        group.shutdownGracefully();
    }
}

From source file:org.asynchttpclient.CookieStoreTest.java

License:Open Source License

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);// w ww . j a  v  a2  s  . com
    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"));
}

From source file:zipkin2.autoconfigure.ui.ZipkinUiAutoConfigurationTest.java

License:Apache License

private AggregatedHttpMessage serveIndex(Cookie... cookies) {
    HttpHeaders headers = HttpHeaders.of(HttpMethod.GET, "/");
    String encodedCookies = ClientCookieEncoder.LAX.encode(cookies);
    if (encodedCookies != null) {
        headers.set(HttpHeaderNames.COOKIE, encodedCookies);
    }//from   www . j ava 2s  .  com
    HttpRequest req = HttpRequest.of(headers);
    try {
        return context.getBean(ZipkinUiAutoConfiguration.class).indexSwitchingService()
                .serve(ServiceRequestContext.of(req), req).aggregate().get();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:zipkin2.server.internal.ui.ZipkinUiConfigurationTest.java

License:Apache License

private AggregatedHttpMessage serveIndex(Cookie... cookies) {
    HttpHeaders headers = HttpHeaders.of(HttpMethod.GET, "/");
    String encodedCookies = ClientCookieEncoder.LAX.encode(cookies);
    if (encodedCookies != null) {
        headers.set(HttpHeaderNames.COOKIE, encodedCookies);
    }/* w w  w .  j  av a 2s  .co m*/
    HttpRequest req = HttpRequest.of(headers);
    try {
        return context.getBean(ZipkinUiConfiguration.class).indexSwitchingService()
                .serve(ServiceRequestContext.of(req), req).aggregate().get();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}