Java Code Examples for com.linecorp.armeria.common.HttpRequest#headers()

The following examples show how to use com.linecorp.armeria.common.HttpRequest#headers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: THttpService.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Nullable
private SerializationFormat determineSerializationFormat(HttpRequest req) {
    final HttpHeaders headers = req.headers();
    final MediaType contentType = headers.contentType();

    final SerializationFormat serializationFormat;
    if (contentType != null) {
        serializationFormat = findSerializationFormat(contentType);
        if (serializationFormat == null) {
            // Browser clients often send a non-Thrift content type.
            // Choose the default serialization format for some vague media types.
            if (!("text".equals(contentType.type()) &&
                  "plain".equals(contentType.subtype())) &&
                !("application".equals(contentType.type()) &&
                  "octet-stream".equals(contentType.subtype()))) {
                return null;
            }
        } else {
            return serializationFormat;
        }
    }

    return defaultSerializationFormat();
}
 
Example 2
Source File: DefaultClientRequestContext.java    From armeria with Apache License 2.0 6 votes vote down vote up
private void autoFillSchemeAndAuthority() {
    final HttpRequest req = request();
    if (req == null) {
        return;
    }

    final RequestHeaders headers = req.headers();
    final String authority = endpoint != null ? endpoint.authority() : "UNKNOWN";
    if (headers.scheme() == null || !authority.equals(headers.authority())) {
        final RequestHeadersBuilder headersBuilder =
                headers.toBuilder()
                       .removeAndThen(HttpHeaderNames.HOST)
                       .authority(authority)
                       .scheme(sessionProtocol());
        unsafeUpdateRequest(req.withHeaders(headersBuilder));
    }
}
 
Example 3
Source File: Armeria084ClientInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(final EnhancedInstance objInst, final Method method, final Object[] allArguments,
    final Class<?>[] argumentsTypes, final MethodInterceptResult result) throws Throwable {

    final UserClient userClient = (UserClient) objInst;
    final URI uri = userClient.uri();
    final HttpMethod httpMethod = (HttpMethod) allArguments[1];
    final String path = (String) allArguments[2];
    final Object req = allArguments[5];

    if (!(req instanceof HttpRequest)) {
        return;
    }

    final HttpRequest httpReq = (HttpRequest) req;

    final ContextCarrier contextCarrier = new ContextCarrier();
    final String remotePeer = uri.getHost() + ":" + uri.getPort();

    final AbstractSpan exitSpan = ContextManager.createExitSpan(path, contextCarrier, remotePeer);

    exitSpan.setComponent(ComponentsDefine.ARMERIA);
    exitSpan.setLayer(SpanLayer.HTTP);
    Tags.HTTP.METHOD.set(exitSpan, httpMethod.name());

    HttpHeaders headers = httpReq.headers();
    for (CarrierItem item = contextCarrier.items(); item.hasNext(); ) {
        item = item.next();
        headers.add(AsciiString.of(item.getHeadKey()), item.getHeadValue());
    }
}
 
Example 4
Source File: ArmeriaClientHttpRequestTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
public void writeWithPublisher() {
    final ArmeriaClientHttpRequest request = request();
    final Flux<DataBuffer> body = Flux.just("a", "b", "c", "d", "e")
                                      .map(String::getBytes)
                                      .map(DataBufferFactoryWrapper.DEFAULT.delegate()::wrap);

    assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
    request.getHeaders().add(HttpHeaderNames.USER_AGENT.toString(), "spring/armeria");
    request.getCookies().add("a", new HttpCookie("a", "1"));

    request.writeWith(body)
           .then(Mono.defer(request::setComplete))
           .subscribe();

    // Wait until calling HttpClient#execute.
    await().until(() -> request.future().isDone());

    // Consume from Armeria HttpRequest.
    final HttpRequest httpRequest = request.request();
    assertThat(httpRequest).isNotNull();

    // Check the headers.
    final RequestHeaders headers = httpRequest.headers();
    assertThat(headers.method()).isEqualTo(com.linecorp.armeria.common.HttpMethod.GET);
    assertThat(headers.path()).isEqualTo(TEST_PATH_AND_QUERY);
    assertThat(headers.get(HttpHeaderNames.ACCEPT)).isEqualTo("*/*");
    assertThat(headers.get(HttpHeaderNames.USER_AGENT)).isEqualTo("spring/armeria");
    assertThat(headers.get(HttpHeaderNames.COOKIE)).isEqualTo("a=1");

    assertThat(httpRequest.whenComplete().isDone()).isFalse();

    // Armeria HttpRequest produces http body only.
    final Flux<String> requestBody =
            Flux.from(httpRequest).cast(HttpData.class).map(HttpData::toStringUtf8);
    StepVerifier.create(requestBody, 1)
                .expectNext("a").thenRequest(1)
                .expectNext("b").thenRequest(1)
                .expectNext("c").thenRequest(1)
                .expectNext("d").thenRequest(1)
                .expectNext("e").thenRequest(1)
                .expectComplete()
                .verify();

    await().until(() -> httpRequest.whenComplete().isDone());
}
 
Example 5
Source File: ArmeriaClientHttpRequestTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
public void writeAndFlushWithMultiplePublisher() {
    final ArmeriaClientHttpRequest request = request();
    final Flux<Flux<DataBuffer>> body = Flux.just(
            Flux.just("a", "b", "c", "d", "e").map(String::getBytes)
                .map(DataBufferFactoryWrapper.DEFAULT.delegate()::wrap),
            Flux.just("1", "2", "3", "4", "5").map(String::getBytes)
                .map(DataBufferFactoryWrapper.DEFAULT.delegate()::wrap)
    );

    request.writeAndFlushWith(body)
           .then(Mono.defer(request::setComplete))
           .subscribe();

    // Wait until calling HttpClient#execute.
    await().until(() -> request.future().isDone());

    // Consume from Armeria HttpRequest.
    final HttpRequest httpRequest = request.request();
    assertThat(httpRequest).isNotNull();

    // Check the headers.
    final RequestHeaders headers = httpRequest.headers();
    assertThat(headers.method()).isEqualTo(com.linecorp.armeria.common.HttpMethod.GET);
    assertThat(headers.path()).isEqualTo(TEST_PATH_AND_QUERY);
    assertThat(headers.get(HttpHeaderNames.ACCEPT)).isEqualTo("*/*");

    // Armeria HttpRequest produces http body only.
    final Flux<String> requestBody =
            Flux.from(httpRequest).cast(HttpData.class).map(HttpData::toStringUtf8);
    StepVerifier.create(requestBody, 1)
                .expectNext("a").thenRequest(1)
                .expectNext("b").thenRequest(1)
                .expectNext("c").thenRequest(1)
                .expectNext("d").thenRequest(1)
                .expectNext("e").thenRequest(1)
                .expectNext("1").thenRequest(1)
                .expectNext("2").thenRequest(1)
                .expectNext("3").thenRequest(1)
                .expectNext("4").thenRequest(1)
                .expectNext("5").thenRequest(1)
                .expectComplete()
                .verify();

    await().until(() -> httpRequest.whenComplete().isDone());
}