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.internal.http.ArmeriaHttpUtil.java

License:Apache License

public static void toArmeria(io.netty.handler.codec.http.HttpHeaders inHeaders, HttpHeaders out) {
    final Iterator<Entry<CharSequence, CharSequence>> i = inHeaders.iteratorCharSequence();
    while (i.hasNext()) {
        final Entry<CharSequence, CharSequence> entry = i.next();
        final AsciiString aName = AsciiString.of(entry.getKey()).toLowerCase();
        if (!HTTP_TO_HTTP2_HEADER_BLACKLIST.contains(aName)) {
            // https://tools.ietf.org/html/rfc7540#section-8.1.2.2 makes a special exception for TE
            if (aName.contentEqualsIgnoreCase(HttpHeaderNames.TE)
                    && !AsciiString.contentEqualsIgnoreCase(entry.getValue(), HttpHeaderValues.TRAILERS)) {
                continue;
            }/*from   www. ja v  a  2  s . c  o m*/

            out.add(aName, entry.getValue().toString());
        }
    }
}

From source file:com.linecorp.armeria.internal.tracing.AsciiStringKeyFactory.java

License:Apache License

@Override
public AsciiString create(String name) {
    return AsciiString.of(name);
}

From source file:com.linecorp.armeria.server.AnnotatedHttpServiceMethod.java

License:Apache License

@Nullable
private static Object httpHeaderValue(Parameter entry, HttpRequest req) {
    final String name = entry.name();
    assert name != null;

    final Class<?> wrapperType = entry.wrapperType();
    if (wrapperType != null
            && (List.class.isAssignableFrom(wrapperType) || Set.class.isAssignableFrom(wrapperType))) {
        assert entry.type() == String.class : entry.type().getName();
        try {/*from   www.  j  av a 2 s .  c  o  m*/
            @SuppressWarnings("unchecked")
            final Collection<String> value = (Collection<String>) wrapperType.newInstance();

            // We do not call convertParameter() here because the element type is String.
            final List<String> headerValues = req.headers().getAll(AsciiString.of(name));
            if (headerValues != null && !headerValues.isEmpty()) {
                value.addAll(headerValues);
            } else {
                final String defaultValue = entryDefaultValue(entry);
                if (defaultValue != null) {
                    value.add(defaultValue);
                }
            }
            return value;
        } catch (Throwable cause) {
            throw new IllegalArgumentException("Cannot get the value of an HTTP header: " + name, cause);
        }
    }

    final String value = req.headers().get(AsciiString.of(name));
    return convertParameter(value != null ? value : entryDefaultValue(entry), entry);
}

From source file:com.linecorp.armeria.server.AnnotatedHttpServiceRequestConverterTest.java

License:Apache License

@Test
public void testDefaultRequestConverter_bean1() throws Exception {
    final HttpClient client = HttpClient.of(rule.uri("/"));
    final ObjectMapper mapper = new ObjectMapper();

    AggregatedHttpMessage response;/*from  w w  w. ja  va2s.c o  m*/

    // test for RequestBean1
    RequestBean1 expectedRequestBean = new RequestBean1();
    expectedRequestBean.userName = "john";
    expectedRequestBean.age = 25;
    expectedRequestBean.gender = MALE;
    expectedRequestBean.permissions = Arrays.asList("permission1", "permission2");
    expectedRequestBean.clientName = "TestClient";
    expectedRequestBean.seqNum = 1234L;
    expectedRequestBean.manager = true;

    String expectedResponseContent = mapper.writeValueAsString(expectedRequestBean);

    // Normal Request: POST + Form Data
    HttpData formData = HttpData.ofAscii("age=25&manager=true&gender=male");
    HttpHeaders reqHeaders = HttpHeaders.of(HttpMethod.POST, "/2/default/bean1/john/1234")
            .set(AsciiString.of("x-user-permission"), "permission1,permission2")
            .set(AsciiString.of("x-client-name"), "TestClient").contentType(MediaType.FORM_DATA);

    response = client.execute(AggregatedHttpMessage.of(reqHeaders, formData)).aggregate().join();
    assertThat(response.headers().status()).isEqualTo(HttpStatus.OK);
    assertThat(response.content().toStringUtf8()).isEqualTo(expectedResponseContent);

    // Normal Request: GET + Query String
    reqHeaders = HttpHeaders.of(HttpMethod.GET, "/2/default/bean1/john/1234?age=25&manager=true&gender=MALE")
            .set(AsciiString.of("x-user-permission"), "permission1,permission2")
            .set(AsciiString.of("x-client-name"), "TestClient");

    response = client.execute(AggregatedHttpMessage.of(reqHeaders)).aggregate().join();
    assertThat(response.headers().status()).isEqualTo(HttpStatus.OK);
    assertThat(response.content().toStringUtf8()).isEqualTo(expectedResponseContent);

    // Bad Request: age=badParam
    reqHeaders = HttpHeaders
            .of(HttpMethod.GET, "/2/default/bean1/john/1234?age=badParam&manager=true&gender=male")
            .set(AsciiString.of("x-user-permission"), "permission1,permission2")
            .set(AsciiString.of("x-client-name"), "TestClient");

    response = client.execute(AggregatedHttpMessage.of(reqHeaders)).aggregate().join();
    assertThat(response.headers().status()).isEqualTo(HttpStatus.BAD_REQUEST);

    // Bad Request: seqNum=badParam
    reqHeaders = HttpHeaders
            .of(HttpMethod.GET, "/2/default/bean1/john/badParam?age=25&manager=true&gender=MALE")
            .set(AsciiString.of("x-user-permission"), "permission1,permission2")
            .set(AsciiString.of("x-client-name"), "TestClient");

    response = client.execute(AggregatedHttpMessage.of(reqHeaders)).aggregate().join();
    assertThat(response.headers().status()).isEqualTo(HttpStatus.BAD_REQUEST);

    // Bad Request: gender=badParam
    reqHeaders = HttpHeaders
            .of(HttpMethod.GET, "/2/default/bean1/john/1234?age=25&manager=true&gender=badParam")
            .set(AsciiString.of("x-user-permission"), "permission1,permission2")
            .set(AsciiString.of("x-client-name"), "TestClient");

    response = client.execute(AggregatedHttpMessage.of(reqHeaders)).aggregate().join();
    assertThat(response.headers().status()).isEqualTo(HttpStatus.BAD_REQUEST);
}

From source file:com.linecorp.armeria.server.AnnotatedHttpServiceRequestConverterTest.java

License:Apache License

@Test
public void testDefaultRequestConverter_bean2() throws Exception {
    final HttpClient client = HttpClient.of(rule.uri("/"));
    final ObjectMapper mapper = new ObjectMapper();

    AggregatedHttpMessage response;/*from  w ww.  j av a2 s.co m*/

    // test for RequestBean2
    RequestBean2 expectedRequestBean = new RequestBean2(98765L, "abcd-efgh");
    expectedRequestBean.userName = "john";
    expectedRequestBean.age = 25;
    expectedRequestBean.gender = MALE;
    expectedRequestBean.permissions = Arrays.asList("permission1", "permission2");
    expectedRequestBean.clientName = "TestClient";

    String expectedResponseContent = mapper.writeValueAsString(expectedRequestBean);

    // Normal Request: POST + Form Data
    HttpData formData = HttpData.ofAscii("age=25&gender=male");
    HttpHeaders reqHeaders = HttpHeaders.of(HttpMethod.POST, "/2/default/bean2/john/98765")
            .set(AsciiString.of("x-user-permission"), "permission1,permission2")
            .set(AsciiString.of("x-client-name"), "TestClient").set(AsciiString.of("uid"), "abcd-efgh")
            .contentType(MediaType.FORM_DATA);

    response = client.execute(AggregatedHttpMessage.of(reqHeaders, formData)).aggregate().join();
    assertThat(response.headers().status()).isEqualTo(HttpStatus.OK);
    assertThat(response.content().toStringUtf8()).isEqualTo(expectedResponseContent);

    // Normal Request: GET + Query String
    reqHeaders = HttpHeaders.of(HttpMethod.GET, "/2/default/bean2/john?age=25&gender=MALE&serialNo=98765")
            .set(AsciiString.of("x-user-permission"), "permission1,permission2")
            .set(AsciiString.of("x-client-name"), "TestClient").set(AsciiString.of("uid"), "abcd-efgh");

    response = client.execute(AggregatedHttpMessage.of(reqHeaders)).aggregate().join();
    assertThat(response.headers().status()).isEqualTo(HttpStatus.OK);
    assertThat(response.content().toStringUtf8()).isEqualTo(expectedResponseContent);
}

From source file:com.linecorp.armeria.server.AnnotatedHttpServiceRequestConverterTest.java

License:Apache License

@Test
public void testDefaultRequestConverter_bean3() throws Exception {
    final HttpClient client = HttpClient.of(rule.uri("/"));
    final ObjectMapper mapper = new ObjectMapper();

    AggregatedHttpMessage response;/*ww  w . j a  v  a  2 s  .  c o  m*/

    // test for RequestBean3
    RequestBean3 expectedRequestBean = new RequestBean3(3349);
    expectedRequestBean.userName = "john";
    expectedRequestBean.age = 25;
    expectedRequestBean.gender = MALE;
    expectedRequestBean.permissions = Arrays.asList("permission1", "permission2");
    expectedRequestBean.clientName = "TestClient";

    String expectedResponseContent = mapper.writeValueAsString(expectedRequestBean);

    // Normal Request: POST + Form Data
    HttpData formData = HttpData.ofAscii("age=25&gender=male");
    HttpHeaders reqHeaders = HttpHeaders.of(HttpMethod.POST, "/2/default/bean3/john/3349")
            .set(AsciiString.of("x-user-permission"), "permission1,permission2")
            .set(AsciiString.of("x-client-name"), "TestClient").contentType(MediaType.FORM_DATA);

    response = client.execute(AggregatedHttpMessage.of(reqHeaders, formData)).aggregate().join();
    assertThat(response.headers().status()).isEqualTo(HttpStatus.OK);
    assertThat(response.content().toStringUtf8()).isEqualTo(expectedResponseContent);

    // Normal Request: GET + Query String
    reqHeaders = HttpHeaders.of(HttpMethod.GET, "/2/default/bean3/john?age=25&gender=MALE&departmentNo=3349")
            .set(AsciiString.of("x-user-permission"), "permission1,permission2")
            .set(AsciiString.of("x-client-name"), "TestClient");

    response = client.execute(AggregatedHttpMessage.of(reqHeaders)).aggregate().join();
    assertThat(response.headers().status()).isEqualTo(HttpStatus.OK);
    assertThat(response.content().toStringUtf8()).isEqualTo(expectedResponseContent);
}

From source file:com.linecorp.armeria.server.cors.HttpServerCorsTest.java

License:Apache License

@Test
public void testCorsPreflight() throws Exception {
    HttpClient client = HttpClient.of(clientFactory, server.uri("/"));
    AggregatedHttpMessage response = client.execute(HttpHeaders.of(HttpMethod.OPTIONS, "/cors")
            .set(HttpHeaderNames.ACCEPT, "utf-8").set(HttpHeaderNames.ORIGIN, "http://example.com")
            .set(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD, "POST")).aggregate().get();

    assertEquals(HttpStatus.OK, response.status());
    assertEquals("http://example.com", response.headers().get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN));
    assertEquals("Hello CORS", response.headers().get(AsciiString.of("x-preflight-cors")));
}

From source file:com.linecorp.armeria.server.grpc.ArmeriaGrpcServerStreamTest.java

License:Apache License

@Test
public void closeBeforeClientHalfCloseShouldSucceed() throws Exception {
    stream.close(Status.OK, new Metadata());

    verify(responseWriter).write(new DefaultHttpHeaders(true, 0, true).status(HttpStatus.OK)
            .set(HttpHeaderNames.CONTENT_TYPE, GrpcUtil.CONTENT_TYPE_GRPC)
            .set(AsciiString.of("grpc-status"), "0"));
    verify(responseWriter).close();/* ww w .ja  v a  2s  . c om*/
    verifyNoMoreInteractions(responseWriter);
    verifyZeroInteractions(serverListener);

    stream.transportState().complete();
    verify(serverListener).closed(Status.OK);
    verifyNoMoreInteractions(serverListener);
}

From source file:com.linecorp.armeria.server.grpc.ArmeriaGrpcServerStreamTest.java

License:Apache License

@Test
public void closeWithErrorBeforeClientHalfCloseShouldSucceed() throws Exception {
    // Error is sent on wire and ends the stream
    stream.close(Status.CANCELLED, new Metadata());

    verify(responseWriter).write(new DefaultHttpHeaders(true, 0, true).status(HttpStatus.OK)
            .set(HttpHeaderNames.CONTENT_TYPE, GrpcUtil.CONTENT_TYPE_GRPC)
            .set(AsciiString.of("grpc-status"), "1"));
    verify(responseWriter).close();/*from   w w w.j a v a2  s .  c om*/
    verifyNoMoreInteractions(responseWriter);
    verifyZeroInteractions(serverListener);

    // Sending complete. Listener gets closed()
    stream.transportState().complete();
    verify(serverListener).closed(Status.OK);
    verifyNoMoreInteractions(serverListener);
}

From source file:com.linecorp.armeria.server.grpc.ArmeriaGrpcServerStreamTest.java

License:Apache License

@Test
public void closeAfterClientHalfCloseShouldSucceed() throws Exception {
    // Client half-closes. Listener gets halfClosed()
    stream.transportState().inboundDataReceived(ReadableBuffers.empty(), true);

    verify(serverListener).halfClosed();

    // Server closes. Status sent
    stream.close(Status.OK, new Metadata());
    verifyZeroInteractions(serverListener);

    verify(responseWriter).write(new DefaultHttpHeaders(true, 0, true).status(HttpStatus.OK)
            .set(HttpHeaderNames.CONTENT_TYPE, GrpcUtil.CONTENT_TYPE_GRPC)
            .set(AsciiString.of("grpc-status"), "0"));
    verify(responseWriter).close();/* www.j  a v  a  2s. c  o  m*/
    verifyNoMoreInteractions(responseWriter);

    // Sending and receiving complete. Listener gets closed()
    stream.transportState().complete();
    verify(serverListener).closed(Status.OK);
    verifyNoMoreInteractions(serverListener);
}