Example usage for io.netty.util AsciiString toString

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

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Translates the entire byte string to a String .

Usage

From source file:com.linecorp.armeria.common.http.HttpHeadersJsonSerializer.java

License:Apache License

@Override
public void serialize(HttpHeaders headers, JsonGenerator gen, SerializerProvider serializers)
        throws IOException {

    gen.writeStartObject();/*from ww w. j  a va 2  s  .c  o  m*/

    for (AsciiString name : headers.names()) {
        gen.writeFieldName(name.toString());
        final List<String> values = headers.getAll(name);
        if (values.size() == 1) {
            gen.writeString(values.get(0));
        } else {
            gen.writeStartArray();
            for (String value : values) {
                gen.writeString(value);
            }
            gen.writeEndArray();
        }
    }

    gen.writeEndObject();
}

From source file:com.linecorp.armeria.server.auth.HttpAuthServiceTest.java

License:Apache License

private static HttpRequestBase basicGetRequest(String path, BasicToken basicToken, AsciiString header) {
    HttpGet request = new HttpGet(server.uri(path));
    request.addHeader(header.toString(), "Basic " + BASE64_ENCODER.encodeToString(
            (basicToken.username() + ':' + basicToken.password()).getBytes(StandardCharsets.US_ASCII)));
    return request;
}

From source file:com.linecorp.armeria.server.auth.HttpAuthServiceTest.java

License:Apache License

private static HttpRequestBase oauth1aGetRequest(String path, OAuth1aToken oAuth1aToken, AsciiString header) {
    HttpGet request = new HttpGet(server.uri(path));
    StringBuilder authorization = new StringBuilder("OAuth ");
    String realm = oAuth1aToken.realm();
    if (!Strings.isNullOrEmpty(realm)) {
        authorization.append("realm=\"");
        authorization.append(realm);// w ww .j  a v  a2 s. co m
        authorization.append("\",");
    }
    authorization.append("oauth_consumer_key=\"");
    authorization.append(oAuth1aToken.consumerKey());
    authorization.append("\",oauth_token=\"");
    authorization.append(oAuth1aToken.token());
    authorization.append("\",oauth_signature_method=\"");
    authorization.append(oAuth1aToken.signatureMethod());
    authorization.append("\",oauth_signature=\"");
    authorization.append(oAuth1aToken.signature());
    authorization.append("\",oauth_timestamp=\"");
    authorization.append(oAuth1aToken.timestamp());
    authorization.append("\",oauth_nonce=\"");
    authorization.append(oAuth1aToken.nonce());
    authorization.append("\",version=\"");
    authorization.append(oAuth1aToken.version());
    authorization.append('"');
    for (Entry<String, String> entry : oAuth1aToken.additionals().entrySet()) {
        authorization.append("\",");
        authorization.append(entry.getKey());
        authorization.append("=\"");
        authorization.append(entry.getValue());
        authorization.append('"');
    }
    request.addHeader(header.toString(), authorization.toString());
    return request;
}

From source file:com.linecorp.armeria.server.auth.HttpAuthServiceTest.java

License:Apache License

private static HttpRequestBase oauth2GetRequest(String path, OAuth2Token oAuth2Token, AsciiString header) {
    HttpGet request = new HttpGet(server.uri(path));
    request.addHeader(header.toString(), "Bearer " + oAuth2Token.accessToken());
    return request;
}

From source file:com.linecorp.armeria.server.http.cors.CorsService.java

License:Apache License

private void setCorsExposeHeaders(final HttpHeaders headers) {
    final Set<AsciiString> exposedHeaders = config.exposedHeaders();
    if (exposedHeaders.isEmpty()) {
        return;/*  w ww  .  j a va2s .c  o  m*/
    }

    for (AsciiString header : exposedHeaders) {
        headers.add(HttpHeaderNames.ACCESS_CONTROL_EXPOSE_HEADERS, header.toString());
    }
}

From source file:com.linecorp.armeria.server.http.cors.CorsService.java

License:Apache License

private void setCorsAllowHeaders(final HttpHeaders headers) {
    final Set<AsciiString> allowedHeaders = config.allowedRequestHeaders();
    if (allowedHeaders.isEmpty()) {
        return;//  w  w  w .  j a  v  a2  s .  c o m
    }

    for (AsciiString header : allowedHeaders) {
        headers.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, header.toString());
    }
}

From source file:com.linecorp.armeria.server.http.jetty.JettyService.java

License:Apache License

private static MetaData.Request toRequestMetadata(ServiceRequestContext ctx, AggregatedHttpMessage aReq) {
    // Construct the HttpURI
    final StringBuilder uriBuf = new StringBuilder();
    final HttpHeaders aHeaders = aReq.headers();

    uriBuf.append(ctx.sessionProtocol().isTls() ? "https" : "http");
    uriBuf.append("://");
    uriBuf.append(aHeaders.authority());
    uriBuf.append(aHeaders.path());//w  w w.ja va 2s . co  m

    final HttpURI uri = new HttpURI(uriBuf.toString());
    final String encoded = PATH_SPLITTER.splitToList(ctx.mappedPath()).stream()
            .map(UrlEscapers.urlPathSegmentEscaper()::escape).collect(Collectors.joining("/"));
    uri.setPath(encoded);

    // Convert HttpHeaders to HttpFields
    final HttpFields jHeaders = new HttpFields(aHeaders.size());
    aHeaders.forEach(e -> {
        final AsciiString key = e.getKey();
        if (!key.isEmpty() && key.byteAt(0) != ':') {
            jHeaders.add(key.toString(), e.getValue());
        }
    });

    return new MetaData.Request(aHeaders.method().name(), uri, HttpVersion.HTTP_1_1, jHeaders,
            aReq.content().length());
}

From source file:com.linecorp.armeria.server.jetty.JettyService.java

License:Apache License

private static MetaData.Request toRequestMetadata(ServiceRequestContext ctx, AggregatedHttpMessage aReq) {
    // Construct the HttpURI
    final StringBuilder uriBuf = new StringBuilder();
    final HttpHeaders aHeaders = aReq.headers();

    uriBuf.append(ctx.sessionProtocol().isTls() ? "https" : "http");
    uriBuf.append("://");
    uriBuf.append(aHeaders.authority());
    uriBuf.append(aHeaders.path());/*from  www .ja  v  a 2 s  . c  o  m*/

    final HttpURI uri = new HttpURI(uriBuf.toString());
    uri.setPath(ctx.mappedPath());

    // Convert HttpHeaders to HttpFields
    final HttpFields jHeaders = new HttpFields(aHeaders.size());
    aHeaders.forEach(e -> {
        final AsciiString key = e.getKey();
        if (!key.isEmpty() && key.byteAt(0) != ':') {
            jHeaders.add(key.toString(), e.getValue());
        }
    });

    return new MetaData.Request(aHeaders.method().name(), uri, HttpVersion.HTTP_1_1, jHeaders,
            aReq.content().length());
}

From source file:netty.http2.ConcurrentHttp2Client.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//www  . jav a2 s. co  m
        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();
    } else {
        sslCtx = null;
    }

    EventLoopGroup workerGroup = new NioEventLoopGroup();
    ConcurrentHttp2ClientInitializer initializer = new ConcurrentHttp2ClientInitializer();

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

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

        HttpScheme scheme = SSL ? HttpScheme.HTTPS : HttpScheme.HTTP;
        AsciiString hostName = new AsciiString(HOST + ':' + PORT);
        System.err.println("Sending request(s)...");
        if (URL != null) {
            // Create a simple GET request.
            for (int i = 0; i < 1; i++) {
                StreamRequest request = new StreamRequestBuilder(new URI(URL)).setMethod("GET")
                        //.setMethod("POST")
                        .setHeader(HttpHeaderNames.HOST.toString(), hostName.toString())
                        //.build(EntityStreams.emptyStream());
                        .build(EntityStreams
                                .newEntityStream(new ByteStringWriter(ByteString.copy(new byte[0 * 1024]))));
                channel.writeAndFlush(request);
                System.err.println("Sent request #" + i);
            }
        }
        System.err.println("Finished HTTP/2 request(s)");
        long start = System.currentTimeMillis();

        // Wait until the connection is closed.
        channel.closeFuture().sync();

        long end = System.currentTimeMillis();
        System.err.println("Server Idled for: " + (end - start) + " milliseconds");
    } finally {
        workerGroup.shutdownGracefully();
    }
}

From source file:org.fomky.ratpack.core.session.HeaderBasedSessionId.java

License:Apache License

private AsciiString assignId() {
    AsciiString id = sessionIdGenerator.generateSessionId();
    setCookie(id.toString(), cookieConfig.getExpires());
    return id;
}