Example usage for io.netty.util AsciiString of

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

Introduction

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

Prototype

public static AsciiString of(CharSequence string) 

Source Link

Document

Returns an AsciiString containing the given character sequence.

Usage

From source file:com.linecorp.armeria.client.ClientOptionsTest.java

License:Apache License

@Test
public void testSetHttpHeader() {
    HttpHeaders httpHeader = HttpHeaders.of(AsciiString.of("x-user-defined"), "HEADER_VALUE");

    ClientOptions options = ClientOptions.of(ClientOption.HTTP_HEADERS.newValue(httpHeader));
    assertThat(options.get(ClientOption.HTTP_HEADERS), is(Optional.of(httpHeader)));

    ClientOptions options2 = ClientOptions.DEFAULT;
    assertThat(options2.get(ClientOption.HTTP_HEADERS), is(Optional.of(HttpHeaders.EMPTY_HEADERS)));

}

From source file:com.linecorp.armeria.client.endpoint.StickyEndpointSelectionStrategyTest.java

License:Apache License

private static ClientRequestContext contextWithHeader(String k, String v) {
    ClientRequestContext ctx = mock(ClientRequestContext.class);
    when(ctx.request())//from   w  w w  . ja  v a2  s  .  c  o m
            .thenReturn(HttpRequest.of(HttpHeaders.of(HttpMethod.GET, "/").set(AsciiString.of(k), v)));
    return ctx;
}

From source file:com.linecorp.armeria.client.thrift.ThriftOverHttpClientTest.java

License:Apache License

@Test(timeout = 10000)
public void testDerivedClient() throws Exception {
    final String AUTHORIZATION = "authorization";
    final String NO_TOKEN = "";
    final String TOKEN_A = "token 1234";
    final String TOKEN_B = "token 5678";

    final HeaderService.Iface client = Clients.newClient(clientFactory(), getURI(Handlers.HEADER),
            Handlers.HEADER.Iface(), clientOptions);

    assertThat(client.header(AUTHORIZATION), is(NO_TOKEN));

    final HeaderService.Iface clientA = Clients.newDerivedClient(client,
            newHttpHeaderOption(AsciiString.of(AUTHORIZATION), TOKEN_A));

    final HeaderService.Iface clientB = Clients.newDerivedClient(client,
            newHttpHeaderOption(AsciiString.of(AUTHORIZATION), TOKEN_B));

    assertThat(clientA.header(AUTHORIZATION), is(TOKEN_A));
    assertThat(clientB.header(AUTHORIZATION), is(TOKEN_B));

    // Ensure that the parent client's HTTP_HEADERS option did not change:
    assertThat(client.header(AUTHORIZATION), is(NO_TOKEN));
}

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

License:Apache License

@Test
public void testInvalidHeaderName() throws Exception {
    assertThatThrownBy(() -> HttpHeaders.of((AsciiString) null, "value1"))
            .isInstanceOf(IllegalArgumentException.class);

    assertThatThrownBy(() -> HttpHeaders.of(AsciiString.of(""), "value1"))
            .isInstanceOf(IllegalArgumentException.class);
}

From source file:com.linecorp.armeria.common.logging.DefaultRequestLogTest.java

License:Apache License

@Test
public void addChild() {
    final DefaultRequestLog child = new DefaultRequestLog(ctx);
    log.addChild(child);/*w w  w .  ja v  a  2s. c o  m*/
    child.startRequest(channel, SessionProtocol.H2C, "/example.com");
    assertThat(log.requestStartTimeMillis()).isEqualTo(child.requestStartTimeMillis());
    assertThat(log.channel()).isSameAs(channel);
    assertThat(log.sessionProtocol()).isSameAs(SessionProtocol.H2C);
    assertThat(log.host()).isEqualTo("/example.com");

    child.serializationFormat(SerializationFormat.NONE);
    assertThat(log.serializationFormat()).isSameAs(SerializationFormat.NONE);

    final HttpHeaders foo = HttpHeaders.of(AsciiString.of("foo"), "foo");
    child.requestHeaders(foo);
    assertThat(log.requestHeaders()).isSameAs(foo);

    final String requestContent = "baz";
    final String rawRequestContent = "bax";

    child.requestContent(requestContent, rawRequestContent);
    assertThat(log.requestContent()).isSameAs(requestContent);
    assertThat(log.rawRequestContent()).isSameAs(rawRequestContent);

    child.endRequest();
    assertThat(log.requestDurationNanos()).isEqualTo(child.requestDurationNanos());

    // response-side log are propagated when RequestLogBuilder.endResponseWithLastChild() is invoked
    child.startResponse();
    assertThatThrownBy(() -> log.responseStartTimeMillis())
            .isExactlyInstanceOf(RequestLogAvailabilityException.class);

    final HttpHeaders bar = HttpHeaders.of(AsciiString.of("bar"), "bar");
    child.responseHeaders(bar);
    assertThatThrownBy(() -> log.responseHeaders()).isExactlyInstanceOf(RequestLogAvailabilityException.class);

    log.endResponseWithLastChild();
    assertThat(log.responseStartTimeMillis()).isEqualTo(child.responseStartTimeMillis());
    assertThat(log.responseHeaders()).isSameAs(bar);

    final String responseContent = "baz1";
    final String rawResponseContent = "bax1";
    child.responseContent(responseContent, rawResponseContent);
    assertThat(log.responseContent()).isSameAs(responseContent);
    assertThat(log.rawResponseContent()).isSameAs(rawResponseContent);

    child.endResponse(new AnticipatedException("Oops!"));
    assertThat(log.responseDurationNanos()).isEqualTo(child.responseDurationNanos());
    assertThat(log.totalDurationNanos()).isEqualTo(child.totalDurationNanos());
}

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

License:Apache License

/**
 * Converts the specified Netty HTTP/2 into Armeria HTTP/2 headers.
 *//*www  .  j  ava 2  s . com*/
public static HttpHeaders toArmeria(Http2Headers headers) {
    final HttpHeaders converted = new DefaultHttpHeaders(false, headers.size());
    StringJoiner cookieJoiner = null;
    for (Entry<CharSequence, CharSequence> e : headers) {
        final AsciiString name = AsciiString.of(e.getKey());
        final CharSequence value = e.getValue();

        // Cookies must be concatenated into a single octet string.
        // https://tools.ietf.org/html/rfc7540#section-8.1.2.5
        if (name.equals(HttpHeaderNames.COOKIE)) {
            if (cookieJoiner == null) {
                cookieJoiner = new StringJoiner(COOKIE_SEPARATOR);
            }
            COOKIE_SPLITTER.split(value).forEach(cookieJoiner::add);
        } else {
            converted.add(name, convertHeaderValue(name, value));
        }
    }

    if (cookieJoiner != null && cookieJoiner.length() != 0) {
        converted.add(HttpHeaderNames.COOKIE, cookieJoiner.toString());
    }

    return converted;
}

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

License:Apache License

/**
 * Converts the specified Netty HTTP/1 headers into Armeria HTTP/2 headers.
 *///from w  w w. j a v  a 2  s .  c  o m
public static void toArmeria(io.netty.handler.codec.http.HttpHeaders inHeaders, HttpHeaders out) {
    final Iterator<Entry<CharSequence, CharSequence>> iter = inHeaders.iteratorCharSequence();
    // Choose 8 as a default size because it is unlikely we will see more than 4 Connection headers values,
    // but still allowing for "enough" space in the map to reduce the chance of hash code collision.
    final CharSequenceMap connectionBlacklist = toLowercaseMap(
            inHeaders.valueCharSequenceIterator(HttpHeaderNames.CONNECTION), 8);
    StringJoiner cookieJoiner = null;
    while (iter.hasNext()) {
        final Entry<CharSequence, CharSequence> entry = iter.next();
        final AsciiString aName = AsciiString.of(entry.getKey()).toLowerCase();
        if (HTTP_TO_HTTP2_HEADER_BLACKLIST.contains(aName) || connectionBlacklist.contains(aName)) {
            continue;
        }

        // https://tools.ietf.org/html/rfc7540#section-8.1.2.2 makes a special exception for TE
        if (aName.equals(HttpHeaderNames.TE)) {
            toHttp2HeadersFilterTE(entry, out);
            continue;
        }

        // Cookies must be concatenated into a single octet string.
        // https://tools.ietf.org/html/rfc7540#section-8.1.2.5
        final CharSequence value = entry.getValue();
        if (aName.equals(HttpHeaderNames.COOKIE)) {
            if (cookieJoiner == null) {
                cookieJoiner = new StringJoiner(COOKIE_SEPARATOR);
            }
            COOKIE_SPLITTER.split(value).forEach(cookieJoiner::add);
        } else {
            out.add(aName, convertHeaderValue(aName, value));
        }
    }

    if (cookieJoiner != null && cookieJoiner.length() != 0) {
        out.add(HttpHeaderNames.COOKIE, cookieJoiner.toString());
    }
}

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  www . jav a2  s .c  o  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.internal.ArmeriaHttpUtilTest.java

License:Apache License

@Test
public void stripConnectionNomineesWithCsv() {
    final io.netty.handler.codec.http.HttpHeaders in = new io.netty.handler.codec.http.DefaultHttpHeaders();
    in.add(HttpHeaderNames.CONNECTION, "foo,  bar");
    in.add("foo", "baz");
    in.add("bar", "qux");
    in.add("hello", "world");
    final HttpHeaders out = new DefaultHttpHeaders();
    toArmeria(in, out);//from w w  w. ja va2 s.  com
    assertThat(out).hasSize(1);
    assertThat(out.get(AsciiString.of("hello"))).isEqualTo("world");
}

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

License:Apache License

public static HttpHeaders toArmeria(Http2Headers headers) {
    final HttpHeaders converted = new DefaultHttpHeaders(false, headers.size());
    for (Entry<CharSequence, CharSequence> e : headers) {
        converted.add(AsciiString.of(e.getKey()), e.getValue().toString());
    }/*from  w  w  w.  j  a v a2  s. com*/
    return converted;
}