Example usage for io.netty.util AsciiString AsciiString

List of usage examples for io.netty.util AsciiString AsciiString

Introduction

In this page you can find the example usage for io.netty.util AsciiString AsciiString.

Prototype

public AsciiString(CharSequence value) 

Source Link

Document

Create a copy of value into this instance assuming ASCII encoding.

Usage

From source file:org.wso2.esb.integration.common.utils.clients.Http2Client.java

License:Open Source License

public Http2Response doPost(String url, String data, Map<String, String> headers) {

    initChannel();//from   w ww.  j  a va2  s.co  m
    HttpScheme scheme = SSL ? HttpScheme.HTTPS : HttpScheme.HTTP;
    AsciiString hostName = new AsciiString(HOST + ':' + PORT);

    FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, url,
            Unpooled.copiedBuffer(data.getBytes()));

    if (!headers.isEmpty()) {
        for (Map.Entry h : headers.entrySet()) {
            request.headers().add((CharSequence) h.getKey(), h.getValue());
        }
    }
    request.headers().add(HttpHeaderNames.HOST, hostName);
    request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name());
    request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
    request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE);
    io.netty.channel.ChannelPromise p;
    int s = StreamId;
    responseHandler.put(StreamId, channel.writeAndFlush(request), p = channel.newPromise());
    StreamId += 2;
    Http2Response response;
    try {
        while (!p.isSuccess()) {
            log.info("Waiting for response");
            Thread.sleep(20);
        }
        response = responseHandler.getResponse(s);
    } catch (InterruptedException e) {
        response = null;
        log.error(e.getStackTrace());
    }
    return response;
}

From source file:push2.apns.demo2.Http2Client.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;

    SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK;
    sslCtx = SslContextBuilder.forClient().sslProvider(provider)
            /* NOTE: the cipher filter may not include all ciphers required by the HTTP/2 specification.
             * Please refer to the HTTP/2 specification for cipher requirements. */
            .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
            .trustManager(InsecureTrustManagerFactory.INSTANCE)
            .applicationProtocolConfig(new ApplicationProtocolConfig(Protocol.ALPN,
                    // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers.
                    SelectorFailureBehavior.NO_ADVERTISE,
                    // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers.
                    SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolNames.HTTP_2,
                    ApplicationProtocolNames.HTTP_1_1))
            .build();//from ww  w. j  av  a  2  s  . c  o m

    EventLoopGroup workerGroup = new NioEventLoopGroup();
    Http2ClientInitializer initializer = new Http2ClientInitializer(sslCtx, Integer.MAX_VALUE);

    try {
        // Configure the client.
        Bootstrap b = new Bootstrap();
        b.group(workerGroup);
        b.channel(NioSocketChannel.class);
        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.remoteAddress(HOST, PORT);
        b.handler(initializer);

        // Start the client.
        Channel channel = b.connect().channel();
        System.out.println("Connected to [" + HOST + ':' + PORT + ']');

        // Wait for the HTTP/2 upgrade to occur.
        Http2SettingsHandler http2SettingsHandler = initializer.settingsHandler();
        //http2SettingsHandler.awaitSettings(5, TimeUnit.SECONDS);

        HttpResponseHandler responseHandler = initializer.responseHandler();
        int streamId = 3;

        AsciiString hostName = new AsciiString(HOST + ':' + PORT);
        System.err.println("Sending request(s)...");

        // Create a simple POST request with a body.
        FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST,
                "https://" + HOST + "/3/device/" + DEVICE_TOKEN,
                wrappedBuffer(payload.getBytes(CharsetUtil.UTF_8)));
        request.headers().add(HttpHeaderNames.HOST, hostName);
        request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), HttpScheme.HTTPS.name());
        request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
        request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE);
        request.headers().add("authorization", "bearer " + JWT.getToken(TEAM_ID, KEY_ID, ACCESS_SECRET));
        request.headers().add("apns-topic", "com.hujiang.hjm.normandy");

        channel.write(request);
        // responseHandler.put(streamId, channel.write(request), channel.newPromise());

        channel.flush();
        //responseHandler.awaitResponses(5, TimeUnit.SECONDS);
        System.out.println("Finished HTTP/2 request(s)");

        // Wait until the connection is closed.
        channel.close().syncUninterruptibly();
    } finally {
        workerGroup.shutdownGracefully();
    }

    while (Thread.activeCount() > 1) {
        Thread.yield();
    }
}

From source file:ratpack.handling.internal.UuidBasedRequestIdGenerator.java

License:Apache License

@Override
public RequestId generate(Request request) {
    Random random = ThreadLocalRandom.current();
    return RequestId.of(new AsciiString(new UUID(random.nextLong(), random.nextLong()).toString()));
}

From source file:ratpack.session.store.internal.AsciiStringByteBufRedisCodec.java

License:Apache License

@Override
public AsciiString decodeKey(ByteBuffer bytes) {
    return new AsciiString(bytes);
}