Example usage for io.netty.util AsciiString length

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

Introduction

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

Prototype

int length

To view the source code for io.netty.util AsciiString length.

Click Source Link

Document

Length in bytes for #value that we care about.

Usage

From source file:com.linecorp.armeria.internal.ArmeriaHttpUtil.java

License:Apache License

private static CharSequenceMap toLowercaseMap(Iterator<? extends CharSequence> valuesIter, int arraySizeHint) {
    final CharSequenceMap result = new CharSequenceMap(arraySizeHint);

    while (valuesIter.hasNext()) {
        final AsciiString lowerCased = AsciiString.of(valuesIter.next()).toLowerCase();
        try {/*from w  w  w .j a  v a  2  s .co  m*/
            int index = lowerCased.forEachByte(FIND_COMMA);
            if (index != -1) {
                int start = 0;
                do {
                    result.add(lowerCased.subSequence(start, index, false).trim(), EMPTY_STRING);
                    start = index + 1;
                } while (start < lowerCased.length() && (index = lowerCased.forEachByte(start,
                        lowerCased.length() - start, FIND_COMMA)) != -1);
                result.add(lowerCased.subSequence(start, lowerCased.length(), false).trim(), EMPTY_STRING);
            } else {
                result.add(lowerCased.trim(), EMPTY_STRING);
            }
        } catch (Exception e) {
            // This is not expect to happen because FIND_COMMA never throws but must be caught
            // because of the ByteProcessor interface.
            throw new IllegalStateException(e);
        }
    }
    return result;
}

From source file:com.linecorp.armeria.server.http.tomcat.TomcatService.java

License:Apache License

private static void convertHeaders(HttpHeaders headers, MimeHeaders cHeaders) {
    if (headers.isEmpty()) {
        return;//from   w w w. j av a2 s . c  o m
    }

    for (Entry<AsciiString, String> e : headers) {
        final AsciiString k = e.getKey();
        final String v = e.getValue();

        if (k.isEmpty() || k.byteAt(0) == ':') {
            continue;
        }

        final MessageBytes cValue = cHeaders.addValue(k.array(), k.arrayOffset(), k.length());
        final byte[] valueBytes = v.getBytes(StandardCharsets.US_ASCII);
        cValue.setBytes(valueBytes, 0, valueBytes.length);
    }
}

From source file:com.linecorp.armeria.server.http.tomcat.TomcatServiceInvocationHandler.java

License:Apache License

private static void convertHeaders(HttpHeaders headers, MimeHeaders cHeaders) {
    if (headers.isEmpty()) {
        return;//  w  ww . j  av  a  2  s  .  c  o m
    }

    for (Iterator<Entry<CharSequence, CharSequence>> i = headers.iteratorCharSequence(); i.hasNext();) {
        final Entry<CharSequence, CharSequence> e = i.next();
        final CharSequence k = e.getKey();
        final CharSequence v = e.getValue();

        final MessageBytes cValue;
        if (k instanceof AsciiString) {
            final AsciiString ak = (AsciiString) k;
            cValue = cHeaders.addValue(ak.array(), ak.arrayOffset(), ak.length());
        } else {
            cValue = cHeaders.addValue(k.toString());
        }

        if (v instanceof AsciiString) {
            final AsciiString av = (AsciiString) v;
            cValue.setBytes(av.array(), av.arrayOffset(), av.length());
        } else {
            final byte[] valueBytes = v.toString().getBytes(StandardCharsets.US_ASCII);
            cValue.setBytes(valueBytes, 0, valueBytes.length);
        }
    }
}

From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.CustomReqRepBenchmark.java

License:Apache License

private static boolean asciiEquals(final AsciiString s, final ByteBuf data, final int size) {
    final int ix = data.readerIndex();
    if (size != s.length()) {
        return false;
    }/*from   w w w.  j a va 2s  .  c  o  m*/
    for (int i = 0; i < size; i++) {
        char c = (char) data.getByte(ix + i);
        if (c != s.charAt(i)) {
            return false;
        }
    }
    return true;
}

From source file:jmeter.plugins.http2.sampler.NettyHttp2Client.java

License:Apache License

public SampleResult request() {
    SampleResult sampleResult = new SampleResult();

    final SslContext sslCtx = getSslContext();
    if (sslCtx == null) {
        sampleResult.setSuccessful(false);
        return sampleResult;
    }//w  w  w.j  a v  a  2 s. c o m

    // Configure the client.
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    Http2ClientInitializer initializer = new Http2ClientInitializer(sslCtx, Integer.MAX_VALUE);
    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 sampling
    sampleResult.sampleStart();

    // Start the client.
    Channel channel = b.connect().syncUninterruptibly().channel();

    // Wait for the HTTP/2 upgrade to occur.
    Http2SettingsHandler http2SettingsHandler = initializer.settingsHandler();
    try {
        http2SettingsHandler.awaitSettings(5, TimeUnit.SECONDS);
    } catch (Exception exception) {
        sampleResult.setSuccessful(false);
        return sampleResult;
    }

    HttpResponseHandler responseHandler = initializer.responseHandler();
    final int streamId = 3;
    final URI hostName = URI.create("https://" + host + ':' + port);

    // Set attributes to SampleResult
    try {
        sampleResult.setURL(new URL(hostName.toString()));
    } catch (MalformedURLException exception) {
        sampleResult.setSuccessful(false);
        return sampleResult;
    }

    FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, path);
    request.headers().addObject(HttpHeaderNames.HOST, hostName);

    // Add request headers set by HeaderManager
    if (headerManager != null) {
        CollectionProperty headers = headerManager.getHeaders();
        if (headers != null) {
            PropertyIterator i = headers.iterator();
            while (i.hasNext()) {
                org.apache.jmeter.protocol.http.control.Header header = (org.apache.jmeter.protocol.http.control.Header) i
                        .next().getObjectValue();
                request.headers().add(header.getName(), header.getValue());
            }
        }
    }

    channel.writeAndFlush(request);
    responseHandler.put(streamId, channel.newPromise());

    final SortedMap<Integer, FullHttpResponse> responseMap;
    try {
        responseMap = responseHandler.awaitResponses(5, TimeUnit.SECONDS);

        // Currently pick up only one response of a stream
        final FullHttpResponse response = responseMap.get(streamId);
        final AsciiString responseCode = response.status().codeAsText();
        final AsciiString reasonPhrase = response.status().reasonPhrase();
        sampleResult.setResponseCode(new StringBuilder(responseCode.length()).append(responseCode).toString());
        sampleResult
                .setResponseMessage(new StringBuilder(reasonPhrase.length()).append(reasonPhrase).toString());
        sampleResult.setResponseHeaders(getResponseHeaders(response));
    } catch (Exception exception) {
        sampleResult.setSuccessful(false);
        return sampleResult;
    }

    // Wait until the connection is closed.
    channel.close().syncUninterruptibly();

    // End sampling
    sampleResult.sampleEnd();
    sampleResult.setSuccessful(true);

    return sampleResult;
}

From source file:org.cloudfoundry.reactor.util.MultipartHttpClientRequest.java

License:Apache License

public Mono<Void> done() {
    AsciiString boundary = generateMultipartBoundary();
    AsciiString delimiter = getDelimiter(boundary);
    AsciiString closeDelimiter = getCloseDelimiter(boundary);

    List<PartHttpClientRequest> parts = this.partConsumers.stream().map(partConsumer -> {
        PartHttpClientRequest part = new PartHttpClientRequest(this.objectMapper);
        partConsumer.accept(part);/*w w  w  .j  a  v  a 2  s .  co  m*/
        return part;
    }).collect(Collectors.toList());

    Long contentLength = parts.stream().mapToLong(part -> delimiter.length() + CRLF.length() + part.getLength())
            .sum() + closeDelimiter.length();

    NettyOutbound intermediateRequest = this.request.chunkedTransfer(false)
            .header(CONTENT_TYPE, BOUNDARY_PREAMBLE.concat(boundary))
            .header(CONTENT_LENGTH, String.valueOf(contentLength));

    for (PartHttpClientRequest part : parts) {
        intermediateRequest = intermediateRequest.sendObject(Unpooled.wrappedBuffer(delimiter.toByteArray()));
        intermediateRequest = intermediateRequest.sendObject(Unpooled.wrappedBuffer(CRLF.toByteArray()));
        intermediateRequest = intermediateRequest.sendObject(part.renderedHeaders);
        intermediateRequest = part.sendPayload(intermediateRequest);
    }

    intermediateRequest = intermediateRequest.sendObject(Unpooled.wrappedBuffer(closeDelimiter.toByteArray()));

    return intermediateRequest.then();
}

From source file:org.cloudfoundry.reactor.util.MultipartHttpOutbound.java

License:Apache License

private static ByteBuf getCloseDelimiter(ByteBufAllocator allocator, AsciiString boundary) {
    AsciiString s = DOUBLE_DASH.concat(boundary).concat(DOUBLE_DASH);
    return allocator.directBuffer(s.length()).writeBytes(s.toByteArray());
}

From source file:org.cloudfoundry.reactor.util.MultipartHttpOutbound.java

License:Apache License

private static ByteBuf getDelimiter(ByteBufAllocator allocator, AsciiString boundary) {
    AsciiString s = DOUBLE_DASH.concat(boundary).concat(CRLF);
    return allocator.directBuffer(s.length()).writeBytes(s.toByteArray());
}

From source file:org.cloudfoundry.reactor.util.MultipartHttpOutbound.java

License:Apache License

private static ByteBuf getHeaders(ByteBufAllocator allocator, HttpHeaders headers) {
    AsciiString s = AsciiString.EMPTY_STRING;

    for (Map.Entry<String, String> entry : headers) {
        s = s.concat(new AsciiString(entry.getKey())).concat(HEADER_DELIMITER).concat(entry.getValue())
                .concat(CRLF);//from   ww  w.  j  ava2s  . com
    }

    return allocator.directBuffer(s.length()).writeBytes(s.toByteArray());
}